alshabib
Committed by Gerrit Code Review

allow applications to be installed by URL from REST API

Change-Id: I6c28d91e1bbc6e77eefba00b8f106fe22edc475b
...@@ -9,6 +9,7 @@ app=${3} ...@@ -9,6 +9,7 @@ app=${3}
9 9
10 export URL=http://$node:8181/onos/v1/applications 10 export URL=http://$node:8181/onos/v1/applications
11 export HDR="-HContent-Type:application/octet-stream" 11 export HDR="-HContent-Type:application/octet-stream"
12 +export HAJ="-HContent-Type:application/json"
12 export curl="curl -sS --user $ONOS_WEB_USER:$ONOS_WEB_PASS" 13 export curl="curl -sS --user $ONOS_WEB_USER:$ONOS_WEB_PASS"
13 14
14 # Prints usage help 15 # Prints usage help
...@@ -34,6 +35,13 @@ function appName { ...@@ -34,6 +35,13 @@ function appName {
34 35
35 case $cmd in 36 case $cmd in
36 list) $curl -X GET $URL;; 37 list) $curl -X GET $URL;;
38 + installUrl!|installUrl)
39 + activate="false"
40 + [ $cmd = "installUrl!" ] && activate="true"
41 + [ $# -lt 3 ] && usage
42 + appurl=$3
43 + $curl -X POST $HAJ -d '{"url" : "'"$appurl"'", "activate" : "'$activate'" }' $URL
44 + ;;
37 install!|install) 45 install!|install)
38 [ $cmd = "install!" ] && activate="?activate=true" 46 [ $cmd = "install!" ] && activate="?activate=true"
39 [ $# -lt 3 -o ! -f $app ] && usage 47 [ $# -lt 3 -o ! -f $app ] && usage
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
15 */ 15 */
16 package org.onosproject.rest.resources; 16 package org.onosproject.rest.resources;
17 17
18 +import com.fasterxml.jackson.databind.node.ObjectNode;
18 import org.onosproject.app.ApplicationAdminService; 19 import org.onosproject.app.ApplicationAdminService;
19 import org.onosproject.core.Application; 20 import org.onosproject.core.Application;
20 import org.onosproject.core.ApplicationId; 21 import org.onosproject.core.ApplicationId;
...@@ -32,7 +33,9 @@ import javax.ws.rs.Produces; ...@@ -32,7 +33,9 @@ import javax.ws.rs.Produces;
32 import javax.ws.rs.QueryParam; 33 import javax.ws.rs.QueryParam;
33 import javax.ws.rs.core.MediaType; 34 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response; 35 import javax.ws.rs.core.Response;
36 +import java.io.IOException;
35 import java.io.InputStream; 37 import java.io.InputStream;
38 +import java.net.URL;
36 import java.util.Set; 39 import java.util.Set;
37 40
38 import static org.onlab.util.Tools.nullIsNotFound; 41 import static org.onlab.util.Tools.nullIsNotFound;
...@@ -46,6 +49,9 @@ public class ApplicationsWebResource extends AbstractWebResource { ...@@ -46,6 +49,9 @@ public class ApplicationsWebResource extends AbstractWebResource {
46 private static final String APP_ID_NOT_FOUND = "Application ID is not found"; 49 private static final String APP_ID_NOT_FOUND = "Application ID is not found";
47 private static final String APP_NOT_FOUND = "Application is not found"; 50 private static final String APP_NOT_FOUND = "Application is not found";
48 51
52 + private static final String URL = "url";
53 + private static final String ACTIVATE = "activate";
54 +
49 /** 55 /**
50 * Get all installed applications. 56 * Get all installed applications.
51 * Returns array of all installed applications. 57 * Returns array of all installed applications.
...@@ -80,6 +86,38 @@ public class ApplicationsWebResource extends AbstractWebResource { ...@@ -80,6 +86,38 @@ public class ApplicationsWebResource extends AbstractWebResource {
80 * Install a new application. 86 * Install a new application.
81 * Uploads application archive stream and optionally activates the 87 * Uploads application archive stream and optionally activates the
82 * application. 88 * application.
89 +
90 + * @param raw json object containing location (url) of application oar
91 + * @return 200 OK; 404; 401
92 + */
93 + @POST
94 + @Consumes(MediaType.APPLICATION_JSON)
95 + @Produces(MediaType.APPLICATION_JSON)
96 + public Response installApp(InputStream raw) {
97 + Application app;
98 + try {
99 + ObjectNode jsonTree = (ObjectNode) mapper().readTree(raw);
100 + URL url = new URL(jsonTree.get(URL).asText());
101 + boolean activate = false;
102 + if (jsonTree.has(ACTIVATE)) {
103 + activate = jsonTree.get(ACTIVATE).asBoolean();
104 + }
105 +
106 + ApplicationAdminService service = get(ApplicationAdminService.class);
107 + app = service.install(url.openStream());
108 + if (activate) {
109 + service.activate(app.id());
110 + }
111 + } catch (IOException ex) {
112 + throw new IllegalArgumentException(ex);
113 + }
114 + return ok(codec(Application.class).encode(app, this)).build();
115 + }
116 +
117 + /**
118 + * Install a new application.
119 + * Uploads application archive stream and optionally activates the
120 + * application.
83 * 121 *
84 * @param activate true to activate app also 122 * @param activate true to activate app also
85 * @param stream application archive stream 123 * @param stream application archive stream
......