incubator-ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgog...@apache.org
Subject svn commit: r1176757 - in /incubator/ambari/trunk: ./ client/src/main/java/org/apache/ambari/common/rest/entities/ controller/src/main/java/org/apache/ambari/components/impl/ controller/src/main/java/org/apache/ambari/controller/ controller/src/main/ja...
Date Wed, 28 Sep 2011 07:17:42 GMT
Author: vgogate
Date: Wed Sep 28 07:17:41 2011
New Revision: 1176757

URL: http://svn.apache.org/viewvc?rev=1176757&view=rev
Log:
AMBARI-16

Added:
    incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Blueprints.java
    incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Stacks.java
      - copied, changed from r1176637, incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/Stacks.java
Removed:
    incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/ClusterNodes.java
    incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/Stacks.java
Modified:
    incubator/ambari/trunk/CHANGES.txt
    incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/Blueprint.java
    incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/Stack.java
    incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/components/impl/ClusterContextImpl.java
    incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/components/impl/HDFSPluginImpl.java
    incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/HeartbeatHandler.java
    incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/BlueprintResource.java
    incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/BlueprintsResource.java
    incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/StacksResource.java

Modified: incubator/ambari/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/CHANGES.txt?rev=1176757&r1=1176756&r2=1176757&view=diff
==============================================================================
--- incubator/ambari/trunk/CHANGES.txt (original)
+++ incubator/ambari/trunk/CHANGES.txt Wed Sep 28 07:17:41 2011
@@ -2,6 +2,10 @@ Ambari Change log
 
 Release 0.1.0 - unreleased
 
+  AMBARI-16. Implement Stacks resource API
+
+  AMBARI-14. Implement Ambari REST API for cluster resource
+
   AMBARI-13. Initial attempt at a website. (omalley)
 
   AMBARI-10. Initial checkin of the heartbeat handling code (ddas)

Modified: incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/Blueprint.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/Blueprint.java?rev=1176757&r1=1176756&r2=1176757&view=diff
==============================================================================
--- incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/Blueprint.java (original)
+++ incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/Blueprint.java Wed Sep 28 07:17:41 2011
@@ -63,114 +63,114 @@ public class Blueprint {
     @XmlElement(name = "Roles")
     protected List<Role> roles;
     
-        /**
-         * @return the name
-         */
-        public String getName() {
-                return name;
-        }
-        /**
-         * @param name the name to set
-         */
-        public void setName(String name) {
-                this.name = name;
-        }
-        /**
-         * @return the revision
-         */
-        public String getRevision() {
-                return revision;
-        }
-        /**
-         * @param revision the revision to set
-         */
-        public void setRevision(String revision) {
-                this.revision = revision;
-        }
-        /**
-         * @return the stackName
-         */
-        public String getStackName() {
-                return stackName;
-        }
-        /**
-         * @param stackName the stackName to set
-         */
-        public void setStackName(String stackName) {
-                this.stackName = stackName;
-        }
-        /**
-         * @return the parentName
-         */
-        public String getParentName() {
-                return parentName;
-        }
-        /**
-         * @param parentName the parentName to set
-         */
-        public void setParentName(String parentName) {
-                this.parentName = parentName;
-        }
-        /**
-         * @return the parentRevision
-         */
-        public String getParentRevision() {
-                return parentRevision;
-        }
-        /**
-         * @param parentRevision the parentRevision to set
-         */
-        public void setParentRevision(String parentRevision) {
-                this.parentRevision = parentRevision;
-        }
-        /**
-         * @return the packageRepositories
-         */
-        public List<PackageRepository> getPackageRepositories() {
-                return packageRepositories;
-        }
-        /**
-         * @param packageRepositories the packageRepositories to set
-         */
-        public void setPackageRepositories(
-                        List<PackageRepository> packageRepositories) {
-                this.packageRepositories = packageRepositories;
-        }
-        /**
-         * @return the configuration
-         */
-        public Configuration getConfiguration() {
-                return configuration;
-        }
-        /**
-         * @param configuration the configuration to set
-         */
-        public void setConfiguration(Configuration configuration) {
-                this.configuration = configuration;
-        }
-        /**
-         * @return the components
-         */
-        public List<Component> getComponents() {
-                return components;
-        }
-        /**
-         * @param components the components to set
-         */
-        public void setComponents(List<Component> components) {
-                this.components = components;
-        }
-        /**
-         * @return the roles
-         */
-        public List<Role> getRoles() {
-                return roles;
-        }
-        /**
-         * @param roles the roles to set
-         */
-        public void setRoles(List<Role> roles) {
-                this.roles = roles;
-        }
+    /**
+     * @return the name
+     */
+    public String getName() {
+            return name;
+    }
+    /**
+     * @param name the name to set
+     */
+    public void setName(String name) {
+            this.name = name;
+    }
+    /**
+     * @return the revision
+     */
+    public String getRevision() {
+            return revision;
+    }
+    /**
+     * @param revision the revision to set
+     */
+    public void setRevision(String revision) {
+            this.revision = revision;
+    }
+    /**
+     * @return the stackName
+     */
+    public String getStackName() {
+            return stackName;
+    }
+    /**
+     * @param stackName the stackName to set
+     */
+    public void setStackName(String stackName) {
+            this.stackName = stackName;
+    }
+    /**
+     * @return the parentName
+     */
+    public String getParentName() {
+            return parentName;
+    }
+    /**
+     * @param parentName the parentName to set
+     */
+    public void setParentName(String parentName) {
+            this.parentName = parentName;
+    }
+    /**
+     * @return the parentRevision
+     */
+    public String getParentRevision() {
+            return parentRevision;
+    }
+    /**
+     * @param parentRevision the parentRevision to set
+     */
+    public void setParentRevision(String parentRevision) {
+            this.parentRevision = parentRevision;
+    }
+    /**
+     * @return the packageRepositories
+     */
+    public List<PackageRepository> getPackageRepositories() {
+            return packageRepositories;
+    }
+    /**
+     * @param packageRepositories the packageRepositories to set
+     */
+    public void setPackageRepositories(
+                    List<PackageRepository> packageRepositories) {
+            this.packageRepositories = packageRepositories;
+    }
+    /**
+     * @return the configuration
+     */
+    public Configuration getConfiguration() {
+            return configuration;
+    }
+    /**
+     * @param configuration the configuration to set
+     */
+    public void setConfiguration(Configuration configuration) {
+            this.configuration = configuration;
+    }
+    /**
+     * @return the components
+     */
+    public List<Component> getComponents() {
+            return components;
+    }
+    /**
+     * @param components the components to set
+     */
+    public void setComponents(List<Component> components) {
+            this.components = components;
+    }
+    /**
+     * @return the roles
+     */
+    public List<Role> getRoles() {
+            return roles;
+    }
+    /**
+     * @param roles the roles to set
+     */
+    public void setRoles(List<Role> roles) {
+            this.roles = roles;
+    }
 
 }

Modified: incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/Stack.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/Stack.java?rev=1176757&r1=1176756&r2=1176757&view=diff
==============================================================================
--- incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/Stack.java (original)
+++ incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/Stack.java Wed Sep 28 07:17:41 2011
@@ -27,133 +27,71 @@ import javax.xml.bind.annotation.XmlElem
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlType;
 
-
-/**
- * <p>Java class for StackType complex type.
- * 
- * <p>The following schema fragment specifies the expected content contained within this class.
- * 
- * <pre>
- * &lt;complexType name="StackType">
- *   &lt;complexContent>
- *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
- *       &lt;sequence>
- *         &lt;element name="Name" type="{http://www.w3.org/2001/XMLSchema}string"/>
- *         &lt;element name="Description" type="{http://www.w3.org/2001/XMLSchema}string"/> 
- *         &lt;element name="DefaultBluePrint" type="{}BlueprintType"/>
- *         &lt;element name="Revision" type="{}String"/>
- *       &lt;/sequence>
- *     &lt;/restriction>
- *   &lt;/complexContent>
- * &lt;/complexType>
- * </pre>
- */
 @XmlAccessorType(XmlAccessType.FIELD)
 @XmlType(name = "StackType", propOrder = {
     "name",
     "description",
-    "locationURL"
+    "blueprintLocationURL",
+    "stackRevision"
 })
 @XmlRootElement(name = "Stack")
 public class Stack {
-        
+    
     @XmlElement(name = "Name", required = true)
     protected String name;
-        @XmlElement(name = "Description", required = true)
-    protected String description;
-    @XmlElement(name = "LocationURL", required = true)
-    protected String locationURL;
+    @XmlElement(name = "Description", required = true)
+    protected String description; 
+    @XmlElement(name = "StackRevision", required = true)
+    protected int stackRevision;
+    @XmlElement(name = "BlueprintLocationURL", required = true)
+    protected String blueprintLocationURL;
     
-    private HashMap<String, List<Blueprint>> blueprints = new HashMap<String, List<Blueprint>>();
-   
-    /*
-     * Get blueprint
-     */
-    public Blueprint getBlueprint(String blueprintName, int revision) throws Exception {
-        if (!blueprints.containsKey(blueprintName) || revision < 0 || revision >= blueprints.get(blueprintName).size()
-                || blueprints.get(blueprintName).get(revision) == null) {
-                throw new Exception ("Specified revision ["+revision+"] of ["+blueprintName+"] blueprint does not exists");
-        }
-        return blueprints.get(blueprintName).get(revision);     
+    /**
+     * @return the name
+     */
+    public String getName() {
+        return name;
     }
-    
-    
-    /*
-     * Add or update the blueprint
+    /**
+     * @param name the name to set
      */
-    public void putBlueprint(String blueprintName, Blueprint blueprint) {
-        if (!blueprints.containsKey(blueprintName)) {
-                blueprints.put(blueprintName, new ArrayList<Blueprint>());
-        }
-        blueprints.get(blueprintName).add(blueprint);    
+    public void setName(String name) {
+        this.name = name;
     }
-    
-    /*
-     * Delete the specified version of blueprint
+    /**
+     * @return the description
      */
-    public void deleteBlueprint(String blueprintName, int revision) throws Exception {
-        if (!blueprints.containsKey(blueprintName) || revision < 0 || revision >= blueprints.get(blueprintName).size()) {
-                throw new Exception ("Specified revision ["+revision+"] of ["+blueprintName+"] blueprint does not exists");
-        }
-        // This would change 
-        blueprints.get(blueprintName).set(revision, null);      
+    public String getDescription() {
+        return description;
     }
-    
     /**
-         * @param name the name to set
-         */
-        public void setName(String name) {
-                this.name = name;
-        }
-
+     * @param description the description to set
+     */
+    public void setDescription(String description) {
+        this.description = description;
+    }
     /**
-     * Gets the value of the name property.
-     * 
-     * @return
-     *     possible object is
-     *     {@link String }
-     *     
+     * @return the blueprintLocationURL
      */
-    public String getName() {
-        return name;
+    public String getBlueprintLocationURL() {
+        return blueprintLocationURL;
     }
-
     /**
-     * Gets the value of the description property.
-     * 
-     * @return
-     *     possible object is
-     *     {@link String }
-     *     
+     * @param blueprintLocationURL the blueprintLocationURL to set
      */
-    public String getDescription() {
-        return description;
+    public void setBlueprintLocationURL(String blueprintLocationURL) {
+        this.blueprintLocationURL = blueprintLocationURL;
     }
-
     /**
-     * Sets the value of the description property.
-     * 
-     * @param value
-     *     allowed object is
-     *     {@link String }
-     *     
+     * @return the stackRevision
      */
-    public void setDescription(String value) {
-        this.description = value;
+    public int getStackRevision() {
+        return stackRevision;
+    }
+    /**
+     * @param stackRevision the stackRevision to set
+     */
+    public void setStackRevision(int stackRevision) {
+        this.stackRevision = stackRevision;
     }
-
-        /**
-         * @return the defaultBlueprintDownloadURI
-         */
-        public String getLocationURL() {
-                return locationURL;
-        }
-
-
-        /**
-         * @param locationURL the locationURL to set
-         */
-        public void setLocationURL(String locationURL) {
-                this.locationURL = locationURL;
-        }
 }

Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/components/impl/ClusterContextImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/components/impl/ClusterContextImpl.java?rev=1176757&r1=1176756&r2=1176757&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/components/impl/ClusterContextImpl.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/components/impl/ClusterContextImpl.java Wed Sep 28 07:17:41 2011
@@ -1,3 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.apache.ambari.components.impl;
 
 import java.util.List;

Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/components/impl/HDFSPluginImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/components/impl/HDFSPluginImpl.java?rev=1176757&r1=1176756&r2=1176757&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/components/impl/HDFSPluginImpl.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/components/impl/HDFSPluginImpl.java Wed Sep 28 07:17:41 2011
@@ -1,3 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.apache.ambari.components.impl;
 
 import java.io.IOException;

Added: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Blueprints.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Blueprints.java?rev=1176757&view=auto
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Blueprints.java (added)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Blueprints.java Wed Sep 28 07:17:41 2011
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ambari.controller;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlType;
+
+import org.apache.ambari.common.rest.entities.Blueprint;
+import org.apache.ambari.common.rest.entities.Stack;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+
+public class Blueprints {
+
+    private static Blueprints BlueprintsRef=null;
+        
+    private Blueprints() {}
+    
+    public static synchronized Blueprints getInstance() {
+        if(BlueprintsRef == null) {
+            BlueprintsRef = new Blueprints();
+        }
+        return BlueprintsRef;
+    }
+
+    public Object clone() throws CloneNotSupportedException {
+        throw new CloneNotSupportedException();
+    }
+      
+    /*
+     * Blueprint name -> {revision -> Blueprint} .
+     */
+    protected ConcurrentHashMap<String, ConcurrentHashMap<Integer,Blueprint>> blueprints = new ConcurrentHashMap<String,ConcurrentHashMap<Integer,Blueprint>>();
+    
+    
+    /*
+     * Get blueprint
+     */
+    public Blueprint getBlueprint(String blueprintName, int revision) throws Exception {
+        Blueprint bp = this.blueprints.get(blueprintName).get(revision);
+        
+        if (bp == null) {
+            Exception e = new Exception ("Stack ["+blueprintName+"] revision ["+revision+"] does not exists");
+            throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
+        }
+        return bp;  
+    }
+     
+    /*
+     * Add or update the blueprint
+     */
+    public void addBlueprint(Blueprint bp) throws Exception {
+        
+        if (blueprints.containsKey(bp.getName())) {
+            if (blueprints.get(bp.getName()).containsKey(new Integer(bp.getRevision()))) {
+                Exception e = new Exception(
+                      "Specified blueprint [Name:"+bp.getName()+", Revision: ["+bp.getRevision()+"] is already imported");
+                throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
+            } else {
+                blueprints.get(bp.getName()).put(new Integer(bp.getRevision()), bp);
+            }
+        } else {
+            ConcurrentHashMap<Integer, Blueprint> x = new ConcurrentHashMap<Integer, Blueprint>();
+            x.put(new Integer(bp.getRevision()), bp);
+            this.blueprints.put(bp.getName(), x);
+        }
+    }
+    
+    /*
+     * Return list of blueprint names
+     */
+    public List<String> getBlueprintList() throws Exception {
+        List<String> list = new ArrayList<String>();
+        list.addAll(this.blueprints.keySet());
+        return list;
+    }
+    /*
+     * Delete the specified version of blueprint
+     */
+    public void deleteBlueprint(String blueprintName, int revision) throws Exception {
+        this.blueprints.get(blueprintName).remove(revision);
+        if (this.blueprints.get(blueprintName).keySet().isEmpty()) {
+            this.blueprints.remove(blueprintName);
+        }    
+    }
+}

Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/HeartbeatHandler.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/HeartbeatHandler.java?rev=1176757&r1=1176756&r2=1176757&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/HeartbeatHandler.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/HeartbeatHandler.java Wed Sep 28 07:17:41 2011
@@ -1,3 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.apache.ambari.controller;
 
 import java.util.ArrayList;

Copied: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Stacks.java (from r1176637, incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/Stacks.java)
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Stacks.java?p2=incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Stacks.java&p1=incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/Stacks.java&r1=1176637&r2=1176757&rev=1176757&view=diff
==============================================================================
--- incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/Stacks.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Stacks.java Wed Sep 28 07:17:41 2011
@@ -15,33 +15,55 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.ambari.common.rest.entities;
+package org.apache.ambari.controller;
 
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
 
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlType;
 
-@XmlAccessorType(XmlAccessType.FIELD)
-@XmlType(name = "Stacks", propOrder = {
-    "stack"
-})
-@XmlRootElement (name = "Stacks")
+import org.apache.ambari.common.rest.entities.Blueprint;
+import org.apache.ambari.common.rest.entities.Stack;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+
 public class Stacks {
 
-        @XmlElement(name = "Stack", required = true)
-    protected List<Stack> stack = new ArrayList<Stack>();
+    
+    /*
+     * Stack name -> {revision -> stack} .
+     */
+    protected ConcurrentHashMap<String, ConcurrentHashMap<Integer,Stack>> stacks = new ConcurrentHashMap<String,ConcurrentHashMap<Integer,Stack>>();
+    
+    /*
+     * Map {stack_name:revision, blueprint}
+     */
+    protected ConcurrentHashMap<String, Blueprint> default_blueprints = new ConcurrentHashMap<String,Blueprint>();
+    
     private static Stacks StacksTypeRef=null;
         
     private Stacks() {}
     
     public static synchronized Stacks getInstance() {
         if(StacksTypeRef == null) {
-                StacksTypeRef = new Stacks();
+            StacksTypeRef = new Stacks();
         }
         return StacksTypeRef;
     }
@@ -49,83 +71,143 @@ public class Stacks {
     public Object clone() throws CloneNotSupportedException {
         throw new CloneNotSupportedException();
     }
-
+      
     /* 
-     * Add StackType to Stack list 
+     * Import the specific revision of the stack to Ambari  
     */
-    public void addStack(Stack s) throws Exception {
+    public Stack importStackDescription (String stackLocation) throws Exception {
         /*
-         * TODO: Validate the cluster definition that could not be
-         * done on client side.
+         * Check for stackLocation not null 
          */
-        synchronized (stack) {
-                for (Stack cls : stack) {
-                        if (cls.getName().equals(s.getName())) {
-                                throw new Exception("Stack Already Exists");
-                        }
-                }
-                stack.add(s);
-                
-                /*
-                 * TODO: Persist the cluster definition to data store
-                 * as a initial version r0. 
-                 */
+        if (stackLocation == null || stackLocation.equals("")) {
+            Exception e = new Exception("Query parameter url must be specified");
+            throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
         }
-        return;
-    } 
-    
-    /* 
-     * Update the existing ClusterType from cluster list 
-    */
-    public void updateStack(Stack s) throws Exception {
+        
         /*
-         * TODO: Validate the StackType element?
+         * Convert the string to URL
+         * TODO: Map MalformedURLException to appropriate REST exception and response code
+         */ 
+        URL stackLocationURL;
+        try {
+            stackLocationURL = new URL(stackLocation);
+        } catch (MalformedURLException e) {
+            throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
+        }
+        
+        /*
+         * Fetch the stack definition as JSON and create Stack object and 
+         * add to stacks list
          */
-        synchronized (stack) {
-                int i;
-                for (i=0; i<stack.size(); i++) {
-                        if (stack.get(i).getName().equals(s.getName())) {
-                                if (s.getDescription() != null) stack.get(i).setDescription(s.getDescription());
-                                if (s.getLocationURL() != null) stack.get(i).setLocationURL(s.getLocationURL());
-                        }
-                }
-                if (i==stack.size()) {
-                        throw new Exception ("Stack:["+s.getName()+"] does not exists");
-                }
+        ObjectMapper m = new ObjectMapper();
+        InputStream is = stackLocationURL.openStream();
+        Stack stack = m.readValue(is, Stack.class);
+        if (stacks.containsKey(stack.getName())) {
+            if (stacks.get(stack.getName()).containsKey(stack.getStackRevision())) {
+                Exception e = new Exception(
+                      "Specified stack [Name:"+stack.getName()+", Revision: ["+stack.getStackRevision()+"] is already imported");
+                throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
+            } else {
+                stacks.get(stack.getName()).put(stack.getStackRevision(), stack);
+                Blueprint blueprint = importDefaultBlueprint(stack);
+                this.default_blueprints.put(stack.getName()+":"+stack.getStackRevision(), blueprint);
+            }
+        } else {
+            ConcurrentHashMap<Integer, Stack> x = new ConcurrentHashMap<Integer, Stack>();
+            x.put(stack.getStackRevision(), stack);
+            this.stacks.put(stack.getName(), x);
+            Blueprint blueprint = importDefaultBlueprint(stack);
+            this.default_blueprints.put(stack.getName()+":"+stack.getStackRevision(), blueprint);
         }
-        return;
-    }
+        return stack;
+    } 
     
+    /*
+     * Import the default blueprint from the URL location
+     */
+    public Blueprint importDefaultBlueprint (Stack stack) throws Exception {
+        Blueprint blueprint;
+        try {
+            URL blueprintUrl = new URL(stack.getBlueprintLocationURL());
+            ObjectMapper m = new ObjectMapper();
+            InputStream is = blueprintUrl.openStream();
+            blueprint = m.readValue(is, Blueprint.class);
+        } catch (Exception e) {
+            this.stacks.get(stack.getName()).remove(stack.getStackRevision());
+            if (this.stacks.get(stack.getName()).keySet().isEmpty()) {
+                this.stacks.remove(stack.getName());
+            }
+            throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
+        }
+        return blueprint;
+    }
+   
     /* 
      * Delete stack 
     */
-    public void deleteStack(String stackName) throws Exception {
-        /* 
-         * 
-         */
-        synchronized (stack) {
-                for (Stack stk : stack) {
-                        if (stk.getName().equals(stackName)) {
-                                stack.remove(stk);
-                        }
-                }
-        }       
-        return;
+    public void deleteStack(String stackName, int revision) throws Exception {
+        this.stacks.get(stackName).remove(revision);
+        if (this.stacks.get(stackName).keySet().isEmpty()) {
+            this.stacks.remove(stackName);
+        }
     }
 
     /* 
      * Get StackType from stack list given its name 
     */
-    public Stack getStack(String stackName) throws Exception {
-        for (Stack stk : stack) {
-                        if (stk.getName().equals(stackName)) {
-                                return stk;
-                        }
-                }
-        throw new Exception ("Stack:["+stackName+"] does not exists");
+    public Stack getStack(String stackName, int revision) throws Exception {
+
+        Stack sk = this.stacks.get(stackName).get(revision);
+       
+        if (sk == null) {
+            Exception e = new Exception ("Stack ["+stackName+"] revision ["+revision+"] does not exists");
+            throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
+        }
+        return sk;
     }
     
-    public synchronized List<Stack> getStackList() {
-        return stack;
+    /* 
+     * Get default blueprint for given stack 
+    */
+    public Blueprint getDefaultBlueprint(String stackName, int revision) throws Exception {
+        
+        Blueprint bp = this.default_blueprints.get(stackName+":"+revision);
+        
+        if (bp == null) {
+            Exception e = new Exception ("Stack ["+stackName+"] revision ["+revision+"] does not exists");
+            throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
+        }
+        return bp;
+    }
+    
+    /*
+     * Returns stack names
+     */
+    public List<String> getStackList() {
+        List<String> list = new ArrayList<String>();
+        list.addAll(this.stacks.keySet());
+        return list;
+    }
+    
+    private static String readAll(Reader rd) throws IOException {
+        StringBuilder sb = new StringBuilder();
+        int cp;
+        while ((cp = rd.read()) != -1) {
+            sb.append((char) cp);
+        }
+        return sb.toString();
+    }
+
+    public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
+        ObjectMapper m = new ObjectMapper();
+        InputStream is = new URL(url).openStream();
+        try {
+            BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
+            String jsonText = readAll(rd);
+            JSONObject json = new JSONObject(jsonText);
+            return json;
+        } finally {
+            is.close();
+        }
     }
 }

Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/BlueprintResource.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/BlueprintResource.java?rev=1176757&r1=1176756&r2=1176757&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/BlueprintResource.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/BlueprintResource.java Wed Sep 28 07:17:41 2011
@@ -21,6 +21,7 @@ import java.util.List;
 
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
+import javax.ws.rs.DefaultValue;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
 import javax.ws.rs.PUT;
@@ -28,8 +29,12 @@ import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
 
 import org.apache.ambari.common.rest.entities.Blueprint;
+import org.apache.ambari.controller.Blueprints;
+import org.apache.ambari.controller.Stacks;
 
 /** BlueprintResource represents a Hadoop blueprint to be installed on a 
  *  cluster. Blueprints define a collection of Hadoop components that are
@@ -38,71 +43,90 @@ import org.apache.ambari.common.rest.ent
 @Path(value = "/blueprints/{blueprintName}")
 public class BlueprintResource {
         
-        /** Get a blueprint
-         * 
-         *  <p>
-         *  REST:<br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;URL Path                                    : /blueprints/{blueprintName}<br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Method                                 : GET <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Request Header                         : <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Response Header                        : <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
-         *  <p> 
-         *  
-         *      @param  blueprintName   Name of the blueprint
-         *      @param  revision        The optional blueprint revision to get
-         *      @return                 blueprint definition
-         *      @throws Exception       throws Exception (TBD)
+    /** Get a blueprint
+     * 
+     *  <p>
+     *  REST:<br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;URL Path                                    : /blueprints/{blueprintName}<br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Method                                 : GET <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Request Header                         : <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Response Header                        : <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
+     *  <p> 
+     *  
+     *      @param  blueprintName   Name of the blueprint
+     *      @param  revision        The optional blueprint revision to get
+     *      @return                 blueprint definition
+     *      @throws Exception       throws Exception (TBD)
      */
     @GET
-        @Produces({"application/json", "application/xml"})
-      public Blueprint getBlueprint(@PathParam("blueprintName") String blueprintName, 
-                                    @QueryParam("revision") int revision) throws Exception {
-      return null;
-        }
+    @Produces({"application/json", "application/xml"})
+    public Blueprint getBlueprint(@PathParam("blueprintName") String blueprintName, 
+            @DefaultValue("") @QueryParam("revision") String revision) throws Exception {
+        try {
+            if (revision == null || revision.equals("")) {
+                Exception e = new Exception ("Revision number not specified");
+                throw new WebApplicationException (e, Response.Status.BAD_REQUEST);
+            }
+            return Blueprints.getInstance().getBlueprint(blueprintName, Integer.parseInt(revision));
+        }catch (WebApplicationException we) {
+            throw we;
+        }catch (Exception e) {
+            throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
+        }     
+    }
     
-    /* 
-     * Delete blueprint
-     */
     /** Delete the blueprint
      * 
      *  <p>
-         *  REST:<br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;URL Path                                    : /blueprints/{blueprintName}<br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Method                                 : DELETE <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Request Header                         : <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Response Header                        : <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
-         *  <p> 
-         *  
-     * @param stackName         Name of the Hadoop stack
+     *  REST:<br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;URL Path                                    : /blueprints/{blueprintName}<br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Method                                 : DELETE <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Request Header                         : <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Response Header                        : <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
+     *  <p> 
+     *  
+     * @param  blueprintName    Name of the blueprint
      * @throws Exception        throws Exception (TBD)
      */
     @DELETE
-        @Consumes({"application/json", "application/xml"})
-        public void deleteBlueprint(@PathParam("blueprintName") String blueprintName) throws Exception {
-        }
+    @Consumes({"application/json", "application/xml"})
+    public void deleteBlueprint(@PathParam("blueprintName") String blueprintName,
+            @DefaultValue("") @QueryParam("revision") String revision ) throws Exception {       
+        try {
+            if (revision == null || revision.equals("")) {
+                Exception e = new Exception ("Revision number not specified");
+                throw new WebApplicationException (e, Response.Status.BAD_REQUEST);
+            }
+            Blueprints.getInstance().deleteBlueprint(blueprintName, Integer.parseInt(revision));
+        }catch (WebApplicationException we) {
+            throw we;
+        }catch (Exception e) {
+            throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
+        }     
+    }
     
     /** Update a current blueprint.
      *  <p>
      *  Updates a current blueprint to update some of its fields.
      *  <p>
-         *  REST:<br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;URL Path                                    : /blueprints/{blueprintName}<br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Method                                 : PUT <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Request Header                         : <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Response Header                        : <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
-         *  <p> 
+     *  REST:<br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;URL Path                                    : /blueprints/{blueprintName}<br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Method                                 : PUT <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Request Header                         : <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Response Header                        : <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
+     *  <p> 
      * 
      * @param blueprintName             Name of the blueprint
      * @param blueprint                 Input blueprint object specifying the blueprint definition

Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/BlueprintsResource.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/BlueprintsResource.java?rev=1176757&r1=1176756&r2=1176757&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/BlueprintsResource.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/BlueprintsResource.java Wed Sep 28 07:17:41 2011
@@ -28,8 +28,12 @@ import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
 
 import org.apache.ambari.common.rest.entities.Blueprint;
+import org.apache.ambari.controller.Blueprints;
+import org.apache.ambari.controller.Stacks;
 
 /** BlueprintResource represents a Hadoop blueprint to be installed on a 
  *  cluster. Blueprints define a collection of Hadoop components that are
@@ -42,16 +46,16 @@ public class BlueprintsResource {
      *  <p>
      *  If named blueprint does not exists already, then it creates new one i.e. revision zero.
      *  <p>
-         *  REST:<br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;URL Path                                    : /blueprints/<br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Method                                 : POST <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Request Header                         : <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Response Header                        : <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
-         *  <p> 
+     *  REST:<br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;URL Path                                    : /blueprints/<br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Method                                 : POST <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Request Header                         : <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Response Header                        : <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
+     *  <p> 
      * 
      * @param blueprint                 Input blueprint object specifying the blueprint definition
      * @return                                  Returns the newly created revision of the blueprint
@@ -59,30 +63,41 @@ public class BlueprintsResource {
      */
     @POST
     @Consumes
-    public Blueprint createBlueprint(Blueprint blueprint) throws Exception {
-        return null;
+    public void createBlueprint(Blueprint blueprint) throws Exception {
+        Blueprints.getInstance().addBlueprint(blueprint);
     }
 
     /** Get the list of blueprint names
      *  <p>
-         *  REST:<br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;URL Path                                    : /blueprints<br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Method                                 : GET <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Request Header                         : <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Response Header                        : <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
-         *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
-         *  <p> 
+     *  REST:<br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;URL Path                                    : /blueprints<br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Method                                 : GET <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Request Header                         : <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Response Header                        : <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
+     *  <p> 
      * 
-     * @return                                  Returns the list of blueprint names
+     * @return                          Returns the list of blueprint names
      * @throws Exception                throws Exception
      */
     @GET
     @Consumes
     public List<String> listBlueprints() throws Exception {
-        return null;
+        try {
+            List <String> list = Blueprints.getInstance().getBlueprintList();
+            if (list.isEmpty()) {
+                Exception e = new Exception ("No user defined blueprints found");
+                throw new WebApplicationException (e, Response.Status.NO_CONTENT);
+            }
+            return list;
+        }catch (WebApplicationException we) {
+            throw we;
+        }catch (Exception e) {
+            throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
+        } 
     }
     
 }

Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/StacksResource.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/StacksResource.java?rev=1176757&r1=1176756&r2=1176757&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/StacksResource.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/StacksResource.java Wed Sep 28 07:17:41 2011
@@ -29,31 +29,29 @@ import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
 import javax.xml.bind.annotation.XmlElement;
 
 import org.apache.ambari.common.rest.entities.Blueprint;
 import org.apache.ambari.common.rest.entities.Cluster;
 import org.apache.ambari.common.rest.entities.Stack;
-import org.apache.ambari.common.rest.entities.Stacks;
 import org.apache.ambari.controller.Clusters;
+import org.apache.ambari.controller.Stacks;
 
+import com.sun.jersey.spi.container.WebApplication;
 import com.sun.jersey.spi.resource.Singleton;
 
 /** Stacks resource represents a collection of Hadoop Stacks
  */
 @Singleton
 @Path(value = "/stacks")
-public class StacksResource {
-           
-        Stacks stacks = Stacks.getInstance();
-        
-    public StacksResource() throws Exception {  
-        Stack stack123 = new Stack();
-        stacks.addStack(stack123);
-    }  
+public class StacksResource {       
     
     /** Import a new Hadoop Stack description
-     * 
+     *  <p>
+     *  Specific revision of stack is imported/created into Ambari, if not already present. It returns the 
+     *  description of the stack if imported successfully. If 
      *  <p>
      *  REST:<br>
      *  &nbsp;&nbsp;&nbsp;&nbsp;URL Path                                    : /stacks/<br>
@@ -69,10 +67,16 @@ public class StacksResource {
      * @param   url Location of the new stack definition
      * @throws  Exception               throws Exception
      */
-    @Path(value = "/stacks/")
-    @PUT
+    @POST
     @Consumes({"application/json", "application/xml"})
-    public synchronized void importStackDescription(String url) throws Exception {
+    public Stack importStackDescription(@DefaultValue("") @QueryParam("url") String url) throws WebApplicationException {       
+        try {
+            return Stacks.getInstance().importStackDescription(url);
+        }catch (WebApplicationException we) {
+            throw we;
+        }catch (Exception e) {
+            throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
+        }
     }
     
     /** Get the list of stacks installed with Ambari controller.
@@ -93,25 +97,25 @@ public class StacksResource {
      * @return                  Returns list of stack definitions
      * @throws Exception        throws Exception
      */
-    /*@GET
-    public List<Stack> getStackList (@DefaultValue("") @QueryParam("search") String searchToken) throws Exception {
-        List<Stack> searchResults = Stacks.getInstance().getStackList();
-        if (!searchToken.equals("")) {
-                searchResults = new ArrayList<Stack>();
-                for (Stack cls : Stacks.getInstance().getStackList()) {
-                        if (cls.getName().matches("^.*"+searchToken+".*$")) {
-                                searchResults.add(cls);
-                        }
-                }
-        }
-    
-        if (searchResults.isEmpty()) {
-                throw new Exception ("No matching stacks found!");
-        }
-        return null;
-    }*/
+    @GET
+    @Consumes({"application/json", "application/xml"})
+    public List<String> getStackList (@DefaultValue("") @QueryParam("search") String searchToken) throws Exception {
+        
+        try {
+            List <String> list = Stacks.getInstance().getStackList();
+            if (list.isEmpty()) {
+                Exception e = new Exception ("No stacks found");
+                throw new WebApplicationException (e, Response.Status.NO_CONTENT);
+            }
+            return list;
+        }catch (WebApplicationException we) {
+            throw we;
+        }catch (Exception e) {
+            throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
+        } 
+    }
 
-    /** Get the default blueprint for a particular stack
+    /** Get the stack definition
      * 
      *  <p>
      *  REST:<br>
@@ -129,8 +133,57 @@ public class StacksResource {
      * @return                  The default blueprint for that stack
      * @throws Exception        throws Exception
      */
+    @Path(value = "/{stackName}")
+    @GET
+    @Produces({"application/json", "application/xml"})
+    public Stack getStackDefinition(@PathParam("stackName") String stackName,
+                     @DefaultValue("") @QueryParam("revision") String revision) throws Exception {  
+        try {
+            if (revision == null || revision.equals("")) {
+                Exception e = new Exception ("Revision number not specified");
+                throw new WebApplicationException (e, Response.Status.BAD_REQUEST);
+            }
+            return Stacks.getInstance().getStack(stackName, Integer.parseInt(revision));
+        }catch (WebApplicationException we) {
+            throw we;
+        }catch (Exception e) {
+            throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
+        }      
+    }
+    
+    /** Get the default blueprint for a particular stack
+     * 
+     *  <p>
+     *  REST:<br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;URL Path                                    : /stacks/{stackName}/default-blueprint<br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Method                                 : GET <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Request Header                         : <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;HTTP Response Header                        : <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-type        = application/json <br>
+     *  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accept              = application/json <br>
+     *  <p> 
+     * 
+     * @param stackName         The name of the stack to get the default blueprint
+     * @return                  The default blueprint for that stack
+     * @throws Exception        throws Exception
+     */
+    @Path(value = "/{stackName}/default-blueprint")
     @GET
-    public Blueprint getDefaultBlueprint(@PathParam("stackName") String stackName) throws Exception {
-      return null;
+    @Produces({"application/json", "application/xml"})
+    public Blueprint getDefaultBlueprint(@PathParam("stackName") String stackName,
+                     @DefaultValue("") @QueryParam("revision") String revision) throws Exception {
+        try {
+            if (revision == null || revision.equals("")) {
+                Exception e = new Exception ("Revision number not specified");
+                throw new WebApplicationException (e, Response.Status.BAD_REQUEST);
+            }
+            return Stacks.getInstance().getDefaultBlueprint(stackName, Integer.parseInt(revision));
+        }catch (WebApplicationException we) {
+            throw we;
+        }catch (Exception e) {
+            throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
+        }     
     }
 }



Mime
View raw message