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 @@
<artifactId>jersey-servlet</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.google.sitebricks</groupId>
<artifactId>slf4j</artifactId>
<version>0.8.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
......
......@@ -21,6 +21,8 @@ import org.onosproject.cord.gui.model.Bundle;
import org.onosproject.cord.gui.model.SubscriberUser;
import org.onosproject.cord.gui.model.XosFunction;
import org.onosproject.cord.gui.model.XosFunctionDescriptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Set;
......@@ -29,20 +31,21 @@ import java.util.Set;
*/
public class XosManager {
private static final String XOS_HOST = "10.254.1.22";
private static final String XOS_PORT = "8000";
private static final String URL_FMT = "http://%s:%s/xoslib/rs/subscriber/";
private static final String BASE_URL =
String.format(URL_FMT, XOS_HOST, XOS_PORT);
private static final String URI_BASE = "/rs/subscriber/";
private final XosManagerRestUtils xosUtils = new XosManagerRestUtils(URI_BASE);
private final Logger log = LoggerFactory.getLogger(getClass());
/**
* No instantiation (except via unit test).
*/
XosManager() {}
private String subId(int subscriberId) {
return String.format("%d/", subscriberId);
}
/**
* Configure XOS to enable the functions that compose the given bundle,
* and disable all the others, for the given subscriber.
......@@ -51,12 +54,14 @@ public class XosManager {
* @param bundle new bundle to set
*/
public void setNewBundle(int subscriberId, Bundle bundle) {
System.out.println("\n>> Set New Bundle : " + bundle.descriptor().id());
log.info("\n>> Set New Bundle : " + bundle.descriptor().id());
String urlFmt = xosUrl(subscriberId) + "services/%s/%s";
String uriFmt = subId(subscriberId) + "services/%s/%s";
Set<XosFunctionDescriptor> inBundle = bundle.descriptor().functions();
for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) {
xosEnableFunction(urlFmt, xfd, inBundle.contains(xfd));
String uri = String.format(uriFmt, xfd.id(), inBundle.contains(xfd));
String result = xosUtils.putRest(uri);
// TODO: convert JSON result to object and check (if we care)
}
}
......@@ -69,35 +74,17 @@ public class XosManager {
* @param user user (containing function state)
*/
public void apply(int subscriberId, XosFunction func, SubscriberUser user) {
System.out.println("\n>> Apply : " + func + " for " + user);
log.info("\n>> Apply : " + func + " for " + user);
String urlPrefix = xosUrl(subscriberId) + "users/" + user.id() + "/";
String url = urlPrefix + func.xosUrlApply(user);
restPut(url);
String uriPrefix = subId(subscriberId) + "users/" + user.id() + "/";
String uri = uriPrefix + func.xosUrlApply(user);
String result = xosUtils.putRest(uri);
// TODO: convert JSON result to object and check (if we care)
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
private String xosUrl(int subscriberId) {
return BASE_URL + String.format("%d/", subscriberId);
}
private void xosEnableFunction(String urlFmt, XosFunctionDescriptor xfd,
boolean enable) {
String url = String.format(urlFmt, xfd.id(), enable);
restPut(url);
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
private void restPut(String url) {
// TODO: wire up to Jackson client...
System.out.println("<<PUT>> " + url);
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
/**
* Singleton instance.
*/
......
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed 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.onosproject.cord.gui;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import org.slf4j.Logger;
import static com.google.common.net.MediaType.JSON_UTF_8;
import static java.net.HttpURLConnection.*;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Utility RESTful methods for dealing with the XOS server.
*/
public class XosManagerRestUtils {
private static final String TEST_XOS_SERVER_ADDRESS = "10.254.1.22";
private static final int TEST_XOS_SERVER_PORT = 8000;
private static final String XOSLIB = "/xoslib";
private static final String AUTH_USER = "padmin@vicci.org";
private static final String AUTH_PASS = "letmein";
private static final String UTF_8 = JSON_UTF_8.toString();
private final Logger log = getLogger(getClass());
private final String xosServerAddress;
private final int xosServerPort;
private final String baseUri;
/**
* Constructs a utility class for the default server address and port,
* using the given base URI.
* <p>
* Note that the uri should start and end with a slash; for example:
* {@code "/volttenant/"}. This example would result in URIs of the form:
* <pre>
* "http://10.254.1.22:8000/xoslib/volttenant/"
* </pre>
*
* @param baseUri base URI
*/
public XosManagerRestUtils(String baseUri) {
this(TEST_XOS_SERVER_ADDRESS, TEST_XOS_SERVER_PORT, baseUri);
}
/**
* Constructs a utility class, using the supplied server address and port,
* using the given base URI.
* <p>
* Note that the uri should start and end with a slash; for example:
* {@code "/volttenant/"}. This example would result in URIs of the form:
* <pre>
* "http://[server]:[port]/xoslib/volttenant/"
* </pre>
*
* @param xosServerAddress server IP address
* @param xosServerPort server port
* @param baseUri base URI
*/
public XosManagerRestUtils(String xosServerAddress, int xosServerPort,
String baseUri) {
this.xosServerAddress = xosServerAddress;
this.xosServerPort = xosServerPort;
this.baseUri = baseUri;
}
// build the base URL from the pieces we know...
private String baseUrl() {
return "http://" + xosServerAddress + ":" +
Integer.toString(xosServerPort) + XOSLIB + baseUri;
}
/**
* Gets a client web resource builder for the base XOS REST API
* with no additional URI.
*
* @return web resource builder
*/
public WebResource.Builder getClientBuilder() {
return getClientBuilder("");
}
/**
* Gets a client web resource builder for the base XOS REST API
* with an optional additional URI.
*
* @param uri URI suffix to append to base URI
* @return web resource builder
*/
public WebResource.Builder getClientBuilder(String uri) {
Client client = Client.create();
client.addFilter(new HTTPBasicAuthFilter(AUTH_USER, AUTH_PASS));
WebResource resource = client.resource(baseUrl() + uri);
return resource.accept(UTF_8).type(UTF_8);
}
/**
* Performs a REST GET operation on the base XOS REST URI.
*
* @return JSON string fetched by the GET operation
*/
public String getRest() {
return getRest("");
}
/**
* Performs a REST GET operation on the base XOS REST URI with
* an optional additional URI.
*
* @param uri URI suffix to append to base URI
* @return JSON string fetched by the GET operation
*/
public String getRest(String uri) {
WebResource.Builder builder = getClientBuilder(uri);
ClientResponse response = builder.get(ClientResponse.class);
if (response.getStatus() != HTTP_OK) {
log.info("REST GET request returned error code {}",
response.getStatus());
}
String jsonString = response.getEntity(String.class);
log.info("JSON read:\n{}", jsonString);
return jsonString;
}
/**
* Performs a REST PUT operation on the base XOS REST URI.
*
* @return JSON string returned by the PUT operation
*/
public String putRest() {
return putRest("");
}
/**
* Performs a REST PUT operation on the base XOS REST URI with
* an optional additional URI.
*
* @param uri URI suffix to append to base URI
* @return JSON string returned by the PUT operation
*/
public String putRest(String uri) {
WebResource.Builder builder = getClientBuilder(uri);
ClientResponse response;
try {
response = builder.put(ClientResponse.class);
} catch (ClientHandlerException e) {
log.warn("Unable to contact REST server: {}", e.getMessage());
return "";
}
if (response.getStatus() != HTTP_OK) {
log.info("REST PUT request returned error code {}",
response.getStatus());
}
String jsonString = response.getEntity(String.class);
log.info("JSON read:\n{}", jsonString);
return jsonString;
}
/**
* Performs a REST POST operation of a json string on the base
* XOS REST URI with an optional additional URI.
*
* @param json JSON string to post
*/
public void postRest(String json) {
postRest("", json);
}
/**
* Performs a REST POST operation of a json string on the base
* XOS REST URI with an optional additional URI suffix.
*
* @param uri URI suffix to append to base URI
* @param json JSON string to post
*/
public void postRest(String uri, String json) {
WebResource.Builder builder = getClientBuilder(uri);
ClientResponse response;
try {
response = builder.post(ClientResponse.class, json);
} catch (ClientHandlerException e) {
log.warn("Unable to contact REST server: {}", e.getMessage());
return;
}
if (response.getStatus() != HTTP_CREATED) {
log.info("REST POST request returned error code {}",
response.getStatus());
}
}
/**
* Performs a REST DELETE operation on the base
* XOS REST URI with an optional additional URI.
*
* @param uri URI suffix to append to base URI
*/
public void deleteRest(String uri) {
WebResource.Builder builder = getClientBuilder(uri);
ClientResponse response = builder.delete(ClientResponse.class);
if (response.getStatus() != HTTP_NO_CONTENT) {
log.info("REST DELETE request returned error code {}",
response.getStatus());
}
}
}
......@@ -176,6 +176,7 @@ public class OnosXOSIntegrationManager implements VoltTenantService {
*
* @return web resource builder
*/
@Deprecated
private WebResource.Builder getClientBuilder() {
return getClientBuilder("");
}
......@@ -186,6 +187,7 @@ public class OnosXOSIntegrationManager implements VoltTenantService {
*
* @return web resource builder
*/
@Deprecated
private WebResource.Builder getClientBuilder(String uri) {
String baseUrl = "http://" + xosServerAddress + ":"
+ Integer.toString(xosServerPort);
......@@ -202,6 +204,7 @@ public class OnosXOSIntegrationManager implements VoltTenantService {
*
* @return JSON string fetched by the GET operation
*/
@Deprecated
private String getRest() {
return getRest("");
}
......@@ -212,6 +215,7 @@ public class OnosXOSIntegrationManager implements VoltTenantService {
*
* @return JSON string fetched by the GET operation
*/
@Deprecated
private String getRest(String uri) {
WebResource.Builder builder = getClientBuilder(uri);
ClientResponse response = builder.get(ClientResponse.class);
......@@ -232,6 +236,7 @@ public class OnosXOSIntegrationManager implements VoltTenantService {
*
* @param json JSON string to post
*/
@Deprecated
private void postRest(String json) {
WebResource.Builder builder = getClientBuilder();
ClientResponse response;
......@@ -255,6 +260,7 @@ public class OnosXOSIntegrationManager implements VoltTenantService {
*
* @param uri optional additional URI
*/
@Deprecated
private void deleteRest(String uri) {
WebResource.Builder builder = getClientBuilder(uri);
ClientResponse response = builder.delete(ClientResponse.class);
......