CORD Subscriber GUI - Plumbing CordModelCache through to XOS service, to pick first subscriber.

Change-Id: I7ebe991b2ba25b7129a5ca8e35dc6656d6c8c9e4
diff --git a/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/CordModelCache.java b/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/CordModelCache.java
index f660c64..494fa2b 100644
--- a/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/CordModelCache.java
+++ b/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/CordModelCache.java
@@ -42,15 +42,16 @@
 
     private static final String BUNDLE = "bundle";
     private static final String USERS = "users";
+    private static final String SUB_ID = "subId";
 
 
     // faked for the demo
-    private static final int SUBSCRIBER_ID = 92;
     private static final String MAC_1 = "010203040506";
     private static final String MAC_2 = "010203040507";
     private static final String MAC_3 = "010203040508";
     private static final String MAC_4 = "010203040509";
 
+    private int subscriberId;
     private Bundle currentBundle;
 
     // NOTE: use a tree map to maintain sorted order by user ID
@@ -62,13 +63,14 @@
      */
     CordModelCache() {
         currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE);
-        initUsers();
+        subscriberId = XosManager.INSTANCE.getSubscriberId();
     }
 
     /**
      * Used to initialize users for the demo. These are currently fake.
      */
-    public void initUsers() {
+    @Deprecated
+    private void initUsers() {
         userMap.put(1, createUser(1, "Mom's MacBook", MAC_1));
         userMap.put(2, createUser(2, "Dad's iPad", MAC_2));
         userMap.put(3, createUser(3, "Dick's laptop", MAC_3));
@@ -109,7 +111,7 @@
             }
         }
 
-        XosManager.INSTANCE.setNewBundle(SUBSCRIBER_ID, currentBundle);
+        XosManager.INSTANCE.setNewBundle(subscriberId, currentBundle);
     }
 
 
@@ -144,7 +146,7 @@
         checkNotNull(func, "function not part of bundle: " + funcId);
 
         func.applyParam(user, param, value);
-        XosManager.INSTANCE.apply(SUBSCRIBER_ID, func, user);
+        XosManager.INSTANCE.apply(subscriberId, func, user);
     }
 
     // =============
@@ -159,6 +161,10 @@
 
     // ============= generate JSON for GUI rest calls..
 
+    private void addSubId(ObjectNode root) {
+        root.put(SUB_ID, subscriberId);
+    }
+
     /**
      * Returns the dashboard page data as JSON.
      *
@@ -168,6 +174,7 @@
         ObjectNode root = objectNode();
         root.put(BUNDLE, currentBundle.descriptor().displayName());
         root.set(USERS, userJsonArray());
+        addSubId(root);
         return root.toString();
     }
 
@@ -177,7 +184,9 @@
      * @return bundle page JSON data
      */
     public String jsonBundle() {
-        return BundleFactory.toJson(currentBundle);
+        ObjectNode root = BundleFactory.toObjectNode(currentBundle);
+        addSubId(root);
+        return root.toString();
     }
 
     /**
@@ -188,6 +197,7 @@
     public String jsonUsers() {
         ObjectNode root = objectNode();
         root.set(USERS, userJsonArray());
+        addSubId(root);
         return root.toString();
     }
 
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 20b1baf..2af231f 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
@@ -17,6 +17,10 @@
 
 package org.onosproject.cord.gui;
 
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.onosproject.cord.gui.model.Bundle;
 import org.onosproject.cord.gui.model.SubscriberUser;
 import org.onosproject.cord.gui.model.XosFunction;
@@ -24,6 +28,7 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.IOException;
 import java.util.Set;
 
 /**
@@ -31,9 +36,15 @@
  */
 public class XosManager {
 
+    private static final ObjectMapper MAPPER = new ObjectMapper();
+
+    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 final XosManagerRestUtils xosUtils = new XosManagerRestUtils(URI_BASE);
+    private final XosManagerRestUtils xosUtils =
+            new XosManagerRestUtils(TEST_XOS_SERVER_ADDRESS,
+                                    TEST_XOS_SERVER_PORT, URI_BASE);
     private final Logger log = LoggerFactory.getLogger(getClass());
 
     /**
@@ -41,6 +52,38 @@
      */
     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
+     */
+    public int getSubscriberId() {
+        log.info("getSubscriberId() called");
+        String result = xosUtils.getRest();
+        log.info("from XOS: {}", result);
+
+        JsonNode node;
+        try {
+            node = MAPPER.readTree(result);
+        } catch (IOException e) {
+            log.error("failed to read subscriber JSON", e);
+            return 0;
+        }
+
+        ArrayNode subscribers = (ArrayNode) node.get("subscribers");
+        if (subscribers.size() == 0) {
+            log.error("no subscribers found");
+            return 0;
+        }
+
+        ObjectNode first = (ObjectNode) subscribers.get(0);
+        int id = first.get("id").asInt();
+        log.info("Using subscriber id {}.", id);
+        return id;
+    }
+
 
     private String subId(int subscriberId) {
         return String.format("%d/", subscriberId);
diff --git a/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/XosManagerRestUtils.java b/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/XosManagerRestUtils.java
index b8e8a4c..bd9f5ae 100644
--- a/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/XosManagerRestUtils.java
+++ b/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/XosManagerRestUtils.java
@@ -32,8 +32,6 @@
  * 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";
@@ -46,21 +44,6 @@
     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,
diff --git a/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/model/BundleFactory.java b/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/model/BundleFactory.java
index f951409..4c9e621 100644
--- a/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/model/BundleFactory.java
+++ b/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/model/BundleFactory.java
@@ -97,12 +97,12 @@
     }
 
     /**
-     * Returns a JSON string representation of the given bundle.
+     * Returns an object node representation of the given bundle.
      *
      * @param bundle the bundle
-     * @return JSON string
+     * @return object node
      */
-    public static String toJson(Bundle bundle) {
+    public static ObjectNode toObjectNode(Bundle bundle) {
         ObjectNode root = objectNode();
         BundleDescriptor descriptor = bundle.descriptor();
 
@@ -127,6 +127,6 @@
             bundles.add(bdnode);
         }
         root.set(BUNDLES, bundles);
-        return root.toString();
+        return root;
     }
 }
diff --git a/apps/demo/cord-gui/src/test/org/onosproject/cord/gui/CoreModelCacheTest.java b/apps/demo/cord-gui/src/test/org/onosproject/cord/gui/CoreModelCacheTest.java
index 746d145..69c5dd0 100644
--- a/apps/demo/cord-gui/src/test/org/onosproject/cord/gui/CoreModelCacheTest.java
+++ b/apps/demo/cord-gui/src/test/org/onosproject/cord/gui/CoreModelCacheTest.java
@@ -19,6 +19,7 @@
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.junit.Before;
 import org.junit.Test;
 import org.onosproject.cord.gui.model.BundleFactory;
@@ -50,7 +51,8 @@
 
     @Test
     public void basicBundleJson() {
-        String json = BundleFactory.toJson(cache.getCurrentBundle());
+        ObjectNode node = BundleFactory.toObjectNode(cache.getCurrentBundle());
+        String json = node.toString();
         System.out.println(json);
         assertTrue("bad basic json", sameJson(BASIC_BUNDLE_JSON, json));
     }
@@ -65,7 +67,8 @@
     @Test
     public void familyBundleJson() {
         cache.setCurrentBundle("family");
-        String json = BundleFactory.toJson(cache.getCurrentBundle());
+        ObjectNode node = BundleFactory.toObjectNode(cache.getCurrentBundle());
+        String json = node.toString();
         System.out.println(json);
         assertTrue("bad family json", sameJson(FAMILY_BUNDLE_JSON, json));
     }