Simon Hunt
Committed by Gerrit Code Review

CORD Subscriber GUI - Created XosManagerRestUtils (based on OnosXOSIntegrationManager).

 - refactored XosManager appropriately.

Change-Id: I34c4627b5f5b55f56b46ead65e7da8b16bf07e54
...@@ -48,8 +48,18 @@ ...@@ -48,8 +48,18 @@
48 <artifactId>jersey-servlet</artifactId> 48 <artifactId>jersey-servlet</artifactId>
49 <version>1.19</version> 49 <version>1.19</version>
50 </dependency> 50 </dependency>
51 + <dependency>
52 + <groupId>com.sun.jersey</groupId>
53 + <artifactId>jersey-client</artifactId>
54 + <version>1.19</version>
55 + </dependency>
51 56
52 <dependency> 57 <dependency>
58 + <groupId>com.google.sitebricks</groupId>
59 + <artifactId>slf4j</artifactId>
60 + <version>0.8.3</version>
61 + </dependency>
62 + <dependency>
53 <groupId>commons-io</groupId> 63 <groupId>commons-io</groupId>
54 <artifactId>commons-io</artifactId> 64 <artifactId>commons-io</artifactId>
55 <version>2.4</version> 65 <version>2.4</version>
......
...@@ -21,6 +21,8 @@ import org.onosproject.cord.gui.model.Bundle; ...@@ -21,6 +21,8 @@ import org.onosproject.cord.gui.model.Bundle;
21 import org.onosproject.cord.gui.model.SubscriberUser; 21 import org.onosproject.cord.gui.model.SubscriberUser;
22 import org.onosproject.cord.gui.model.XosFunction; 22 import org.onosproject.cord.gui.model.XosFunction;
23 import org.onosproject.cord.gui.model.XosFunctionDescriptor; 23 import org.onosproject.cord.gui.model.XosFunctionDescriptor;
24 +import org.slf4j.Logger;
25 +import org.slf4j.LoggerFactory;
24 26
25 import java.util.Set; 27 import java.util.Set;
26 28
...@@ -29,20 +31,21 @@ import java.util.Set; ...@@ -29,20 +31,21 @@ import java.util.Set;
29 */ 31 */
30 public class XosManager { 32 public class XosManager {
31 33
32 - private static final String XOS_HOST = "10.254.1.22"; 34 + private static final String URI_BASE = "/rs/subscriber/";
33 - private static final String XOS_PORT = "8000";
34 -
35 - private static final String URL_FMT = "http://%s:%s/xoslib/rs/subscriber/";
36 -
37 - private static final String BASE_URL =
38 - String.format(URL_FMT, XOS_HOST, XOS_PORT);
39 35
36 + private final XosManagerRestUtils xosUtils = new XosManagerRestUtils(URI_BASE);
37 + private final Logger log = LoggerFactory.getLogger(getClass());
40 38
41 /** 39 /**
42 * No instantiation (except via unit test). 40 * No instantiation (except via unit test).
43 */ 41 */
44 XosManager() {} 42 XosManager() {}
45 43
44 +
45 + private String subId(int subscriberId) {
46 + return String.format("%d/", subscriberId);
47 + }
48 +
46 /** 49 /**
47 * Configure XOS to enable the functions that compose the given bundle, 50 * Configure XOS to enable the functions that compose the given bundle,
48 * and disable all the others, for the given subscriber. 51 * and disable all the others, for the given subscriber.
...@@ -51,12 +54,14 @@ public class XosManager { ...@@ -51,12 +54,14 @@ public class XosManager {
51 * @param bundle new bundle to set 54 * @param bundle new bundle to set
52 */ 55 */
53 public void setNewBundle(int subscriberId, Bundle bundle) { 56 public void setNewBundle(int subscriberId, Bundle bundle) {
54 - System.out.println("\n>> Set New Bundle : " + bundle.descriptor().id()); 57 + log.info("\n>> Set New Bundle : " + bundle.descriptor().id());
55 58
56 - String urlFmt = xosUrl(subscriberId) + "services/%s/%s"; 59 + String uriFmt = subId(subscriberId) + "services/%s/%s";
57 Set<XosFunctionDescriptor> inBundle = bundle.descriptor().functions(); 60 Set<XosFunctionDescriptor> inBundle = bundle.descriptor().functions();
58 for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) { 61 for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) {
59 - xosEnableFunction(urlFmt, xfd, inBundle.contains(xfd)); 62 + String uri = String.format(uriFmt, xfd.id(), inBundle.contains(xfd));
63 + String result = xosUtils.putRest(uri);
64 + // TODO: convert JSON result to object and check (if we care)
60 } 65 }
61 } 66 }
62 67
...@@ -69,35 +74,17 @@ public class XosManager { ...@@ -69,35 +74,17 @@ public class XosManager {
69 * @param user user (containing function state) 74 * @param user user (containing function state)
70 */ 75 */
71 public void apply(int subscriberId, XosFunction func, SubscriberUser user) { 76 public void apply(int subscriberId, XosFunction func, SubscriberUser user) {
72 - System.out.println("\n>> Apply : " + func + " for " + user); 77 + log.info("\n>> Apply : " + func + " for " + user);
73 78
74 - String urlPrefix = xosUrl(subscriberId) + "users/" + user.id() + "/"; 79 + String uriPrefix = subId(subscriberId) + "users/" + user.id() + "/";
75 - String url = urlPrefix + func.xosUrlApply(user); 80 + String uri = uriPrefix + func.xosUrlApply(user);
76 - restPut(url); 81 + String result = xosUtils.putRest(uri);
82 + // TODO: convert JSON result to object and check (if we care)
77 } 83 }
78 84
79 85
80 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 86 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
81 87
82 - private String xosUrl(int subscriberId) {
83 - return BASE_URL + String.format("%d/", subscriberId);
84 - }
85 -
86 - private void xosEnableFunction(String urlFmt, XosFunctionDescriptor xfd,
87 - boolean enable) {
88 - String url = String.format(urlFmt, xfd.id(), enable);
89 - restPut(url);
90 - }
91 -
92 - // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
93 -
94 - private void restPut(String url) {
95 - // TODO: wire up to Jackson client...
96 - System.out.println("<<PUT>> " + url);
97 - }
98 -
99 - // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
100 -
101 /** 88 /**
102 * Singleton instance. 89 * Singleton instance.
103 */ 90 */
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + *
16 + */
17 +
18 +package org.onosproject.cord.gui;
19 +
20 +import com.sun.jersey.api.client.Client;
21 +import com.sun.jersey.api.client.ClientHandlerException;
22 +import com.sun.jersey.api.client.ClientResponse;
23 +import com.sun.jersey.api.client.WebResource;
24 +import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
25 +import org.slf4j.Logger;
26 +
27 +import static com.google.common.net.MediaType.JSON_UTF_8;
28 +import static java.net.HttpURLConnection.*;
29 +import static org.slf4j.LoggerFactory.getLogger;
30 +
31 +/**
32 + * Utility RESTful methods for dealing with the XOS server.
33 + */
34 +public class XosManagerRestUtils {
35 + private static final String TEST_XOS_SERVER_ADDRESS = "10.254.1.22";
36 + private static final int TEST_XOS_SERVER_PORT = 8000;
37 + private static final String XOSLIB = "/xoslib";
38 + private static final String AUTH_USER = "padmin@vicci.org";
39 + private static final String AUTH_PASS = "letmein";
40 +
41 + private static final String UTF_8 = JSON_UTF_8.toString();
42 +
43 + private final Logger log = getLogger(getClass());
44 +
45 + private final String xosServerAddress;
46 + private final int xosServerPort;
47 + private final String baseUri;
48 +
49 + /**
50 + * Constructs a utility class for the default server address and port,
51 + * using the given base URI.
52 + * <p>
53 + * Note that the uri should start and end with a slash; for example:
54 + * {@code "/volttenant/"}. This example would result in URIs of the form:
55 + * <pre>
56 + * "http://10.254.1.22:8000/xoslib/volttenant/"
57 + * </pre>
58 + *
59 + * @param baseUri base URI
60 + */
61 + public XosManagerRestUtils(String baseUri) {
62 + this(TEST_XOS_SERVER_ADDRESS, TEST_XOS_SERVER_PORT, baseUri);
63 + }
64 +
65 + /**
66 + * Constructs a utility class, using the supplied server address and port,
67 + * using the given base URI.
68 + * <p>
69 + * Note that the uri should start and end with a slash; for example:
70 + * {@code "/volttenant/"}. This example would result in URIs of the form:
71 + * <pre>
72 + * "http://[server]:[port]/xoslib/volttenant/"
73 + * </pre>
74 + *
75 + * @param xosServerAddress server IP address
76 + * @param xosServerPort server port
77 + * @param baseUri base URI
78 + */
79 + public XosManagerRestUtils(String xosServerAddress, int xosServerPort,
80 + String baseUri) {
81 + this.xosServerAddress = xosServerAddress;
82 + this.xosServerPort = xosServerPort;
83 + this.baseUri = baseUri;
84 + }
85 +
86 + // build the base URL from the pieces we know...
87 + private String baseUrl() {
88 + return "http://" + xosServerAddress + ":" +
89 + Integer.toString(xosServerPort) + XOSLIB + baseUri;
90 + }
91 +
92 + /**
93 + * Gets a client web resource builder for the base XOS REST API
94 + * with no additional URI.
95 + *
96 + * @return web resource builder
97 + */
98 + public WebResource.Builder getClientBuilder() {
99 + return getClientBuilder("");
100 + }
101 +
102 + /**
103 + * Gets a client web resource builder for the base XOS REST API
104 + * with an optional additional URI.
105 + *
106 + * @param uri URI suffix to append to base URI
107 + * @return web resource builder
108 + */
109 + public WebResource.Builder getClientBuilder(String uri) {
110 + Client client = Client.create();
111 + client.addFilter(new HTTPBasicAuthFilter(AUTH_USER, AUTH_PASS));
112 + WebResource resource = client.resource(baseUrl() + uri);
113 + return resource.accept(UTF_8).type(UTF_8);
114 + }
115 +
116 + /**
117 + * Performs a REST GET operation on the base XOS REST URI.
118 + *
119 + * @return JSON string fetched by the GET operation
120 + */
121 + public String getRest() {
122 + return getRest("");
123 + }
124 +
125 + /**
126 + * Performs a REST GET operation on the base XOS REST URI with
127 + * an optional additional URI.
128 + *
129 + * @param uri URI suffix to append to base URI
130 + * @return JSON string fetched by the GET operation
131 + */
132 + public String getRest(String uri) {
133 + WebResource.Builder builder = getClientBuilder(uri);
134 + ClientResponse response = builder.get(ClientResponse.class);
135 +
136 + if (response.getStatus() != HTTP_OK) {
137 + log.info("REST GET request returned error code {}",
138 + response.getStatus());
139 + }
140 + String jsonString = response.getEntity(String.class);
141 + log.info("JSON read:\n{}", jsonString);
142 +
143 + return jsonString;
144 + }
145 +
146 + /**
147 + * Performs a REST PUT operation on the base XOS REST URI.
148 + *
149 + * @return JSON string returned by the PUT operation
150 + */
151 + public String putRest() {
152 + return putRest("");
153 + }
154 +
155 + /**
156 + * Performs a REST PUT operation on the base XOS REST URI with
157 + * an optional additional URI.
158 + *
159 + * @param uri URI suffix to append to base URI
160 + * @return JSON string returned by the PUT operation
161 + */
162 + public String putRest(String uri) {
163 + WebResource.Builder builder = getClientBuilder(uri);
164 + ClientResponse response;
165 +
166 + try {
167 + response = builder.put(ClientResponse.class);
168 + } catch (ClientHandlerException e) {
169 + log.warn("Unable to contact REST server: {}", e.getMessage());
170 + return "";
171 + }
172 +
173 + if (response.getStatus() != HTTP_OK) {
174 + log.info("REST PUT request returned error code {}",
175 + response.getStatus());
176 + }
177 + String jsonString = response.getEntity(String.class);
178 + log.info("JSON read:\n{}", jsonString);
179 +
180 + return jsonString;
181 + }
182 +
183 + /**
184 + * Performs a REST POST operation of a json string on the base
185 + * XOS REST URI with an optional additional URI.
186 + *
187 + * @param json JSON string to post
188 + */
189 + public void postRest(String json) {
190 + postRest("", json);
191 + }
192 +
193 + /**
194 + * Performs a REST POST operation of a json string on the base
195 + * XOS REST URI with an optional additional URI suffix.
196 + *
197 + * @param uri URI suffix to append to base URI
198 + * @param json JSON string to post
199 + */
200 + public void postRest(String uri, String json) {
201 + WebResource.Builder builder = getClientBuilder(uri);
202 + ClientResponse response;
203 +
204 + try {
205 + response = builder.post(ClientResponse.class, json);
206 + } catch (ClientHandlerException e) {
207 + log.warn("Unable to contact REST server: {}", e.getMessage());
208 + return;
209 + }
210 +
211 + if (response.getStatus() != HTTP_CREATED) {
212 + log.info("REST POST request returned error code {}",
213 + response.getStatus());
214 + }
215 + }
216 +
217 + /**
218 + * Performs a REST DELETE operation on the base
219 + * XOS REST URI with an optional additional URI.
220 + *
221 + * @param uri URI suffix to append to base URI
222 + */
223 + public void deleteRest(String uri) {
224 + WebResource.Builder builder = getClientBuilder(uri);
225 + ClientResponse response = builder.delete(ClientResponse.class);
226 +
227 + if (response.getStatus() != HTTP_NO_CONTENT) {
228 + log.info("REST DELETE request returned error code {}",
229 + response.getStatus());
230 + }
231 + }
232 +
233 +}
...@@ -176,6 +176,7 @@ public class OnosXOSIntegrationManager implements VoltTenantService { ...@@ -176,6 +176,7 @@ public class OnosXOSIntegrationManager implements VoltTenantService {
176 * 176 *
177 * @return web resource builder 177 * @return web resource builder
178 */ 178 */
179 + @Deprecated
179 private WebResource.Builder getClientBuilder() { 180 private WebResource.Builder getClientBuilder() {
180 return getClientBuilder(""); 181 return getClientBuilder("");
181 } 182 }
...@@ -186,6 +187,7 @@ public class OnosXOSIntegrationManager implements VoltTenantService { ...@@ -186,6 +187,7 @@ public class OnosXOSIntegrationManager implements VoltTenantService {
186 * 187 *
187 * @return web resource builder 188 * @return web resource builder
188 */ 189 */
190 + @Deprecated
189 private WebResource.Builder getClientBuilder(String uri) { 191 private WebResource.Builder getClientBuilder(String uri) {
190 String baseUrl = "http://" + xosServerAddress + ":" 192 String baseUrl = "http://" + xosServerAddress + ":"
191 + Integer.toString(xosServerPort); 193 + Integer.toString(xosServerPort);
...@@ -202,6 +204,7 @@ public class OnosXOSIntegrationManager implements VoltTenantService { ...@@ -202,6 +204,7 @@ public class OnosXOSIntegrationManager implements VoltTenantService {
202 * 204 *
203 * @return JSON string fetched by the GET operation 205 * @return JSON string fetched by the GET operation
204 */ 206 */
207 + @Deprecated
205 private String getRest() { 208 private String getRest() {
206 return getRest(""); 209 return getRest("");
207 } 210 }
...@@ -212,6 +215,7 @@ public class OnosXOSIntegrationManager implements VoltTenantService { ...@@ -212,6 +215,7 @@ public class OnosXOSIntegrationManager implements VoltTenantService {
212 * 215 *
213 * @return JSON string fetched by the GET operation 216 * @return JSON string fetched by the GET operation
214 */ 217 */
218 + @Deprecated
215 private String getRest(String uri) { 219 private String getRest(String uri) {
216 WebResource.Builder builder = getClientBuilder(uri); 220 WebResource.Builder builder = getClientBuilder(uri);
217 ClientResponse response = builder.get(ClientResponse.class); 221 ClientResponse response = builder.get(ClientResponse.class);
...@@ -232,6 +236,7 @@ public class OnosXOSIntegrationManager implements VoltTenantService { ...@@ -232,6 +236,7 @@ public class OnosXOSIntegrationManager implements VoltTenantService {
232 * 236 *
233 * @param json JSON string to post 237 * @param json JSON string to post
234 */ 238 */
239 + @Deprecated
235 private void postRest(String json) { 240 private void postRest(String json) {
236 WebResource.Builder builder = getClientBuilder(); 241 WebResource.Builder builder = getClientBuilder();
237 ClientResponse response; 242 ClientResponse response;
...@@ -255,6 +260,7 @@ public class OnosXOSIntegrationManager implements VoltTenantService { ...@@ -255,6 +260,7 @@ public class OnosXOSIntegrationManager implements VoltTenantService {
255 * 260 *
256 * @param uri optional additional URI 261 * @param uri optional additional URI
257 */ 262 */
263 + @Deprecated
258 private void deleteRest(String uri) { 264 private void deleteRest(String uri) {
259 WebResource.Builder builder = getClientBuilder(uri); 265 WebResource.Builder builder = getClientBuilder(uri);
260 ClientResponse response = builder.delete(ClientResponse.class); 266 ClientResponse response = builder.delete(ClientResponse.class);
......