CORD Subscriber GUI - Final wiring : we should be good for the demo.

Change-Id: Iad7444503bcce9e23556dcdc21f98088e6e10a5a
diff --git a/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/XosManager.java b/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/XosManager.java
index 2af231f..e856270 100644
--- a/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/XosManager.java
+++ b/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/XosManager.java
@@ -40,66 +40,83 @@
 
     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 URI_BASE = "/rs/subscriber/";
+    private static final String URI_RS = "/rs/";
+    private static final String URI_SUBSCRIBER = "/rs/subscriber/%d/";
 
-    private final XosManagerRestUtils xosUtils =
+    private final XosManagerRestUtils xosUtilsRs =
             new XosManagerRestUtils(TEST_XOS_SERVER_ADDRESS,
-                                    TEST_XOS_SERVER_PORT, URI_BASE);
+                                    TEST_XOS_SERVER_PORT, URI_RS);
+
+    private XosManagerRestUtils xosUtils;
+
+
     private final Logger log = LoggerFactory.getLogger(getClass());
 
+    private int demoId;
+
     /**
      * No instantiation (except via unit test).
      */
     XosManager() {}
 
     /**
-     * Returns the subscriber ID to use for calls to the XOS backend.
-     * Right now, this is implemented to get a list of all subscribers
-     * in the system and return the first one.
-     *
-     * @return subscriber ID
+     * Queries XOS for the Demo Subscriber ID and caches it for future calls.
      */
-    public int getSubscriberId() {
-        log.info("getSubscriberId() called");
-        String result = xosUtils.getRest();
+    public int initDemoSubscriber() {
+        log.info("intDemoSubscriber() called");
+        String result = xosUtilsRs.getRest("initdemo/");
         log.info("from XOS: {}", result);
 
         JsonNode node;
         try {
             node = MAPPER.readTree(result);
         } catch (IOException e) {
-            log.error("failed to read subscriber JSON", e);
+            log.error("failed to read demo subscriber JSON", e);
             return 0;
         }
 
-        ArrayNode subscribers = (ArrayNode) node.get("subscribers");
-        if (subscribers.size() == 0) {
-            log.error("no subscribers found");
-            return 0;
+        ObjectNode obj = (ObjectNode) node;
+        demoId = obj.get("id").asInt();
+        log.info("Using DEMO subscriber ID {}.", demoId);
+
+        String uri = String.format(URI_SUBSCRIBER, demoId);
+        xosUtils = new XosManagerRestUtils(TEST_XOS_SERVER_ADDRESS,
+                                           TEST_XOS_SERVER_PORT, uri);
+        return demoId;
+    }
+
+    /**
+     * Returns the array of users for the subscriber.
+     *
+     * @return list of users
+     */
+    public ArrayNode getUserList() {
+        log.info("getUserList() called");
+        String result = xosUtils.getRest("users/");
+
+        JsonNode node;
+        try {
+            node = MAPPER.readTree(result);
+        } catch (IOException e) {
+            log.error("failed to read user list JSON", e);
+            return null;
         }
 
-        ObjectNode first = (ObjectNode) subscribers.get(0);
-        int id = first.get("id").asInt();
-        log.info("Using subscriber id {}.", id);
-        return id;
+        ObjectNode obj = (ObjectNode) node;
+        return (ArrayNode) obj.get("users");
     }
 
 
-    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.
      *
-     * @param subscriberId subscriber identifier
      * @param bundle new bundle to set
      */
-    public void setNewBundle(int subscriberId, Bundle bundle) {
+    public void setNewBundle(Bundle bundle) {
         log.info("\n>> Set New Bundle : " + bundle.descriptor().id());
 
-        String uriFmt = subId(subscriberId) + "services/%s/%s";
+        String uriFmt = "services/%s/%s";
         Set<XosFunctionDescriptor> inBundle = bundle.descriptor().functions();
         for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) {
             // only process the functions that have a real back-end on XOS
@@ -115,14 +132,13 @@
      * Configure XOS with new setting for given user and function, for the
      * given subscriber account.
      *
-     * @param subscriberId subscriber identifier
      * @param func specific XOS function
      * @param user user (containing function state)
      */
-    public void apply(int subscriberId, XosFunction func, SubscriberUser user) {
+    public void apply(XosFunction func, SubscriberUser user) {
         log.info("\n>> Apply : " + func + " for " + user);
 
-        String uriPrefix = subId(subscriberId) + "users/" + user.id() + "/";
+        String uriPrefix = "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)