Simon Hunt

CORD Subscriber GUI -- Added description field to bundle.

- deleted obsolete classes.

Change-Id: Ied2dbc36c0c74894789e2aba6ddddd29c2e90b49
......@@ -17,11 +17,15 @@
package org.onosproject.cord.gui;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.ImmutableList;
import org.onosproject.cord.gui.model.Bundle;
import org.onosproject.cord.gui.model.BundleDescriptor;
import org.onosproject.cord.gui.model.BundleFactory;
import org.onosproject.cord.gui.model.JsonFactory;
import org.onosproject.cord.gui.model.SubscriberUser;
import org.onosproject.cord.gui.model.UserFactory;
import java.util.ArrayList;
import java.util.List;
......@@ -29,7 +33,11 @@ import java.util.List;
/**
* In memory cache of the model of the subscriber's account.
*/
public class CordModelCache {
public class CordModelCache extends JsonFactory {
private static final String BUNDLE = "bundle";
private static final String USERS = "users";
// faked for the demo
private static final int SUBSCRIBER_ID = 92;
......@@ -44,7 +52,7 @@ public class CordModelCache {
/**
* Constructs a model cache, initializing it with basic bundle.
*/
public CordModelCache() {
CordModelCache() {
currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE);
users = new ArrayList<SubscriberUser>();
initUsers();
......@@ -88,4 +96,51 @@ public class CordModelCache {
public List<SubscriberUser> getUsers() {
return ImmutableList.copyOf(users);
}
private ArrayNode userJsonArray() {
ArrayNode userList = arrayNode();
for (SubscriberUser user: users) {
userList.add(UserFactory.toObjectNode(user));
}
return userList;
}
// ============= generate JSON for GUI rest calls..
/**
* Returns the dashboard page data as JSON.
*
* @return dashboard page JSON data
*/
public String jsonDashboard() {
ObjectNode root = objectNode();
root.put(BUNDLE, currentBundle.descriptor().displayName());
root.set(USERS, userJsonArray());
return root.toString();
}
/**
* Returns the bundle page data as JSON.
*
* @return bundle page JSON data
*/
public String jsonBundle() {
return BundleFactory.toJson(currentBundle);
}
/**
* Returns the users page data as JSON.
*
* @return users page JSON data
*/
public String jsonUsers() {
ObjectNode root = objectNode();
root.set(USERS, userJsonArray());
return root.toString();
}
/**
* Singleton instance.
*/
public static final CordModelCache INSTANCE = new CordModelCache();
}
......
......@@ -29,42 +29,35 @@ import javax.ws.rs.core.Response;
@Path("")
public class CordWebResource {
private Response fakeData(String which, String suffix) {
String path = "local/" + which + "-" + suffix + ".json";
String content = FakeUtils.slurp(path);
if (content == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
return Response.ok(content).build();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("dashboard/{suffix}")
public Response dashboard(@PathParam("suffix") String suffix) {
return fakeData("dashboard", suffix);
@Path("dashboard")
public Response dashboard() {
return Response.ok(CordModelCache.INSTANCE.jsonDashboard()).build();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("bundle/{suffix}")
public Response bundle(@PathParam("suffix") String suffix) {
return fakeData("bundle", suffix);
@Path("bundle")
public Response bundle() {
return Response.ok(CordModelCache.INSTANCE.jsonBundle()).build();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("users/{suffix}")
public Response users(@PathParam("suffix") String suffix) {
return fakeData("users", suffix);
@Path("users")
public Response users() {
return Response.ok(CordModelCache.INSTANCE.jsonUsers()).build();
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("dashboard")
public Response dashboard() {
// TODO:
return Response.ok().build();
@Path("bundle/{id}")
@Deprecated
public Response bundle(@PathParam("id") String bundleId) {
CordModelCache.INSTANCE.setCurrentBundle(bundleId);
return bundle();
}
}
......
......@@ -39,6 +39,13 @@ public interface BundleDescriptor {
String displayName();
/**
* Textual description of this bundle.
*
* @return description
*/
String description();
/**
* The set of functions in this bundle instance.
*
* @return the functions
......
......@@ -34,9 +34,14 @@ public class BundleFactory extends JsonFactory {
private static final String BASIC_ID = "basic";
private static final String BASIC_DISPLAY_NAME = "Basic Bundle";
private static final String BASIC_DESCRIPTION =
"Provides basic internet and firewall functions.";
private static final String FAMILY_ID = "family";
private static final String FAMILY_DISPLAY_NAME = "Family Bundle";
private static final String FAMILY_DESCRIPTION =
"Provides internet, firewall and parental control functions.";
// no instantiation
private BundleFactory() {}
......@@ -46,6 +51,7 @@ public class BundleFactory extends JsonFactory {
*/
public static final BundleDescriptor BASIC_BUNDLE =
new DefaultBundleDescriptor(BASIC_ID, BASIC_DISPLAY_NAME,
BASIC_DESCRIPTION,
XosFunctionDescriptor.INTERNET,
XosFunctionDescriptor.FIREWALL);
......@@ -54,6 +60,7 @@ public class BundleFactory extends JsonFactory {
*/
public static final BundleDescriptor FAMILY_BUNDLE =
new DefaultBundleDescriptor(FAMILY_ID, FAMILY_DISPLAY_NAME,
FAMILY_DESCRIPTION,
XosFunctionDescriptor.INTERNET,
XosFunctionDescriptor.FIREWALL,
XosFunctionDescriptor.URL_FILTER);
......@@ -97,10 +104,12 @@ public class BundleFactory extends JsonFactory {
*/
public static String toJson(Bundle bundle) {
ObjectNode root = objectNode();
BundleDescriptor descriptor = bundle.descriptor();
ObjectNode bnode = objectNode()
.put(ID, bundle.descriptor().id())
.put(NAME, bundle.descriptor().displayName());
.put(ID, descriptor.id())
.put(NAME, descriptor.displayName())
.put(DESC, descriptor.description());
ArrayNode funcs = arrayNode();
for (XosFunctionDescriptor xfd: bundle.descriptor().functions()) {
......@@ -113,11 +122,11 @@ public class BundleFactory extends JsonFactory {
for (BundleDescriptor bd: BundleFactory.availableBundles()) {
ObjectNode bdnode = objectNode()
.put(ID, bd.id())
.put(NAME, bd.displayName());
.put(NAME, bd.displayName())
.put(DESC, bd.description());
bundles.add(bdnode);
}
root.set(BUNDLES, bundles);
return root.toString();
}
}
......
......@@ -29,6 +29,7 @@ public class DefaultBundleDescriptor implements BundleDescriptor {
private final String id;
private final String displayName;
private final String description;
private final Set<XosFunctionDescriptor> functions;
/**
......@@ -38,10 +39,11 @@ public class DefaultBundleDescriptor implements BundleDescriptor {
* @param displayName bundle display name
* @param functions functions that make up this bundle
*/
DefaultBundleDescriptor(String id, String displayName,
DefaultBundleDescriptor(String id, String displayName, String description,
XosFunctionDescriptor... functions) {
this.id = id;
this.displayName = displayName;
this.description = description;
this.functions = ImmutableSet.copyOf(functions);
}
......@@ -54,6 +56,10 @@ public class DefaultBundleDescriptor implements BundleDescriptor {
return displayName;
}
public String description() {
return description;
}
public Set<XosFunctionDescriptor> functions() {
return functions;
}
......
/*
* 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.model;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* Default implementation of an XOS function.
*/
public class DefaultXosFunction implements XosFunction {
private static final ObjectMapper MAPPER = new ObjectMapper();
private final XosFunctionDescriptor descriptor;
public DefaultXosFunction(XosFunctionDescriptor xfd) {
descriptor = xfd;
}
public XosFunctionDescriptor descriptor() {
return descriptor;
}
public ObjectNode params() {
return MAPPER.createObjectNode();
}
public String toJson() {
return null;
}
public JsonNode toJsonNode() {
return null;
}
}
/*
* 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.model;
import com.fasterxml.jackson.databind.JsonNode;
/**
* An object that can be serialized to JSON.
*/
public interface JsonBlob {
/**
* Returns an Object Node representation of this object.
*
* @return object node hierarchy
*/
JsonNode toJsonNode();
/**
* Returns a JSON string representation of this object.
*
* @return JSON serialization
*/
String toJson();
}
......@@ -30,6 +30,7 @@ public abstract class JsonFactory {
protected static final String ID = "id";
protected static final String NAME = "name";
protected static final String DESC = "desc";
/**
* Returns a freshly minted object node.
......
......@@ -23,7 +23,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* Designates a specific instance of an XOS function.
*/
public interface XosFunction extends JsonBlob {
public interface XosFunction {
/**
* Returns the descriptor for this function.
......
......@@ -12,7 +12,6 @@ import java.util.Map;
*/
public class XosFunctionFactory extends JsonFactory {
private static final String DESC = "desc";
private static final String PARAMS = "params";
private static final String LEVEL = "level";
......