Committed by
Gerrit Code Review
CORD Subscriber GUI - Plumbing CordModelCache through to XOS service, to pick first subscriber.
Change-Id: I7ebe991b2ba25b7129a5ca8e35dc6656d6c8c9e4
Showing
5 changed files
with
69 additions
and
30 deletions
... | @@ -42,15 +42,16 @@ public class CordModelCache extends JsonFactory { | ... | @@ -42,15 +42,16 @@ public class CordModelCache extends JsonFactory { |
42 | 42 | ||
43 | private static final String BUNDLE = "bundle"; | 43 | private static final String BUNDLE = "bundle"; |
44 | private static final String USERS = "users"; | 44 | private static final String USERS = "users"; |
45 | + private static final String SUB_ID = "subId"; | ||
45 | 46 | ||
46 | 47 | ||
47 | // faked for the demo | 48 | // faked for the demo |
48 | - private static final int SUBSCRIBER_ID = 92; | ||
49 | private static final String MAC_1 = "010203040506"; | 49 | private static final String MAC_1 = "010203040506"; |
50 | private static final String MAC_2 = "010203040507"; | 50 | private static final String MAC_2 = "010203040507"; |
51 | private static final String MAC_3 = "010203040508"; | 51 | private static final String MAC_3 = "010203040508"; |
52 | private static final String MAC_4 = "010203040509"; | 52 | private static final String MAC_4 = "010203040509"; |
53 | 53 | ||
54 | + private int subscriberId; | ||
54 | private Bundle currentBundle; | 55 | private Bundle currentBundle; |
55 | 56 | ||
56 | // NOTE: use a tree map to maintain sorted order by user ID | 57 | // NOTE: use a tree map to maintain sorted order by user ID |
... | @@ -62,13 +63,14 @@ public class CordModelCache extends JsonFactory { | ... | @@ -62,13 +63,14 @@ public class CordModelCache extends JsonFactory { |
62 | */ | 63 | */ |
63 | CordModelCache() { | 64 | CordModelCache() { |
64 | currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE); | 65 | currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE); |
65 | - initUsers(); | 66 | + subscriberId = XosManager.INSTANCE.getSubscriberId(); |
66 | } | 67 | } |
67 | 68 | ||
68 | /** | 69 | /** |
69 | * Used to initialize users for the demo. These are currently fake. | 70 | * Used to initialize users for the demo. These are currently fake. |
70 | */ | 71 | */ |
71 | - public void initUsers() { | 72 | + @Deprecated |
73 | + private void initUsers() { | ||
72 | userMap.put(1, createUser(1, "Mom's MacBook", MAC_1)); | 74 | userMap.put(1, createUser(1, "Mom's MacBook", MAC_1)); |
73 | userMap.put(2, createUser(2, "Dad's iPad", MAC_2)); | 75 | userMap.put(2, createUser(2, "Dad's iPad", MAC_2)); |
74 | userMap.put(3, createUser(3, "Dick's laptop", MAC_3)); | 76 | userMap.put(3, createUser(3, "Dick's laptop", MAC_3)); |
... | @@ -109,7 +111,7 @@ public class CordModelCache extends JsonFactory { | ... | @@ -109,7 +111,7 @@ public class CordModelCache extends JsonFactory { |
109 | } | 111 | } |
110 | } | 112 | } |
111 | 113 | ||
112 | - XosManager.INSTANCE.setNewBundle(SUBSCRIBER_ID, currentBundle); | 114 | + XosManager.INSTANCE.setNewBundle(subscriberId, currentBundle); |
113 | } | 115 | } |
114 | 116 | ||
115 | 117 | ||
... | @@ -144,7 +146,7 @@ public class CordModelCache extends JsonFactory { | ... | @@ -144,7 +146,7 @@ public class CordModelCache extends JsonFactory { |
144 | checkNotNull(func, "function not part of bundle: " + funcId); | 146 | checkNotNull(func, "function not part of bundle: " + funcId); |
145 | 147 | ||
146 | func.applyParam(user, param, value); | 148 | func.applyParam(user, param, value); |
147 | - XosManager.INSTANCE.apply(SUBSCRIBER_ID, func, user); | 149 | + XosManager.INSTANCE.apply(subscriberId, func, user); |
148 | } | 150 | } |
149 | 151 | ||
150 | // ============= | 152 | // ============= |
... | @@ -159,6 +161,10 @@ public class CordModelCache extends JsonFactory { | ... | @@ -159,6 +161,10 @@ public class CordModelCache extends JsonFactory { |
159 | 161 | ||
160 | // ============= generate JSON for GUI rest calls.. | 162 | // ============= generate JSON for GUI rest calls.. |
161 | 163 | ||
164 | + private void addSubId(ObjectNode root) { | ||
165 | + root.put(SUB_ID, subscriberId); | ||
166 | + } | ||
167 | + | ||
162 | /** | 168 | /** |
163 | * Returns the dashboard page data as JSON. | 169 | * Returns the dashboard page data as JSON. |
164 | * | 170 | * |
... | @@ -168,6 +174,7 @@ public class CordModelCache extends JsonFactory { | ... | @@ -168,6 +174,7 @@ public class CordModelCache extends JsonFactory { |
168 | ObjectNode root = objectNode(); | 174 | ObjectNode root = objectNode(); |
169 | root.put(BUNDLE, currentBundle.descriptor().displayName()); | 175 | root.put(BUNDLE, currentBundle.descriptor().displayName()); |
170 | root.set(USERS, userJsonArray()); | 176 | root.set(USERS, userJsonArray()); |
177 | + addSubId(root); | ||
171 | return root.toString(); | 178 | return root.toString(); |
172 | } | 179 | } |
173 | 180 | ||
... | @@ -177,7 +184,9 @@ public class CordModelCache extends JsonFactory { | ... | @@ -177,7 +184,9 @@ public class CordModelCache extends JsonFactory { |
177 | * @return bundle page JSON data | 184 | * @return bundle page JSON data |
178 | */ | 185 | */ |
179 | public String jsonBundle() { | 186 | public String jsonBundle() { |
180 | - return BundleFactory.toJson(currentBundle); | 187 | + ObjectNode root = BundleFactory.toObjectNode(currentBundle); |
188 | + addSubId(root); | ||
189 | + return root.toString(); | ||
181 | } | 190 | } |
182 | 191 | ||
183 | /** | 192 | /** |
... | @@ -188,6 +197,7 @@ public class CordModelCache extends JsonFactory { | ... | @@ -188,6 +197,7 @@ public class CordModelCache extends JsonFactory { |
188 | public String jsonUsers() { | 197 | public String jsonUsers() { |
189 | ObjectNode root = objectNode(); | 198 | ObjectNode root = objectNode(); |
190 | root.set(USERS, userJsonArray()); | 199 | root.set(USERS, userJsonArray()); |
200 | + addSubId(root); | ||
191 | return root.toString(); | 201 | return root.toString(); |
192 | } | 202 | } |
193 | 203 | ... | ... |
... | @@ -17,6 +17,10 @@ | ... | @@ -17,6 +17,10 @@ |
17 | 17 | ||
18 | package org.onosproject.cord.gui; | 18 | package org.onosproject.cord.gui; |
19 | 19 | ||
20 | +import com.fasterxml.jackson.databind.JsonNode; | ||
21 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
22 | +import com.fasterxml.jackson.databind.node.ArrayNode; | ||
23 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
20 | import org.onosproject.cord.gui.model.Bundle; | 24 | import org.onosproject.cord.gui.model.Bundle; |
21 | import org.onosproject.cord.gui.model.SubscriberUser; | 25 | import org.onosproject.cord.gui.model.SubscriberUser; |
22 | import org.onosproject.cord.gui.model.XosFunction; | 26 | import org.onosproject.cord.gui.model.XosFunction; |
... | @@ -24,6 +28,7 @@ import org.onosproject.cord.gui.model.XosFunctionDescriptor; | ... | @@ -24,6 +28,7 @@ import org.onosproject.cord.gui.model.XosFunctionDescriptor; |
24 | import org.slf4j.Logger; | 28 | import org.slf4j.Logger; |
25 | import org.slf4j.LoggerFactory; | 29 | import org.slf4j.LoggerFactory; |
26 | 30 | ||
31 | +import java.io.IOException; | ||
27 | import java.util.Set; | 32 | import java.util.Set; |
28 | 33 | ||
29 | /** | 34 | /** |
... | @@ -31,9 +36,15 @@ import java.util.Set; | ... | @@ -31,9 +36,15 @@ import java.util.Set; |
31 | */ | 36 | */ |
32 | public class XosManager { | 37 | public class XosManager { |
33 | 38 | ||
39 | + private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
40 | + | ||
41 | + private static final String TEST_XOS_SERVER_ADDRESS = "10.254.1.22"; | ||
42 | + private static final int TEST_XOS_SERVER_PORT = 8000; | ||
34 | private static final String URI_BASE = "/rs/subscriber/"; | 43 | private static final String URI_BASE = "/rs/subscriber/"; |
35 | 44 | ||
36 | - private final XosManagerRestUtils xosUtils = new XosManagerRestUtils(URI_BASE); | 45 | + private final XosManagerRestUtils xosUtils = |
46 | + new XosManagerRestUtils(TEST_XOS_SERVER_ADDRESS, | ||
47 | + TEST_XOS_SERVER_PORT, URI_BASE); | ||
37 | private final Logger log = LoggerFactory.getLogger(getClass()); | 48 | private final Logger log = LoggerFactory.getLogger(getClass()); |
38 | 49 | ||
39 | /** | 50 | /** |
... | @@ -41,6 +52,38 @@ public class XosManager { | ... | @@ -41,6 +52,38 @@ public class XosManager { |
41 | */ | 52 | */ |
42 | XosManager() {} | 53 | XosManager() {} |
43 | 54 | ||
55 | + /** | ||
56 | + * Returns the subscriber ID to use for calls to the XOS backend. | ||
57 | + * Right now, this is implemented to get a list of all subscribers | ||
58 | + * in the system and return the first one. | ||
59 | + * | ||
60 | + * @return subscriber ID | ||
61 | + */ | ||
62 | + public int getSubscriberId() { | ||
63 | + log.info("getSubscriberId() called"); | ||
64 | + String result = xosUtils.getRest(); | ||
65 | + log.info("from XOS: {}", result); | ||
66 | + | ||
67 | + JsonNode node; | ||
68 | + try { | ||
69 | + node = MAPPER.readTree(result); | ||
70 | + } catch (IOException e) { | ||
71 | + log.error("failed to read subscriber JSON", e); | ||
72 | + return 0; | ||
73 | + } | ||
74 | + | ||
75 | + ArrayNode subscribers = (ArrayNode) node.get("subscribers"); | ||
76 | + if (subscribers.size() == 0) { | ||
77 | + log.error("no subscribers found"); | ||
78 | + return 0; | ||
79 | + } | ||
80 | + | ||
81 | + ObjectNode first = (ObjectNode) subscribers.get(0); | ||
82 | + int id = first.get("id").asInt(); | ||
83 | + log.info("Using subscriber id {}.", id); | ||
84 | + return id; | ||
85 | + } | ||
86 | + | ||
44 | 87 | ||
45 | private String subId(int subscriberId) { | 88 | private String subId(int subscriberId) { |
46 | return String.format("%d/", subscriberId); | 89 | return String.format("%d/", subscriberId); | ... | ... |
... | @@ -32,8 +32,6 @@ import static org.slf4j.LoggerFactory.getLogger; | ... | @@ -32,8 +32,6 @@ import static org.slf4j.LoggerFactory.getLogger; |
32 | * Utility RESTful methods for dealing with the XOS server. | 32 | * Utility RESTful methods for dealing with the XOS server. |
33 | */ | 33 | */ |
34 | public class XosManagerRestUtils { | 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"; | 35 | private static final String XOSLIB = "/xoslib"; |
38 | private static final String AUTH_USER = "padmin@vicci.org"; | 36 | private static final String AUTH_USER = "padmin@vicci.org"; |
39 | private static final String AUTH_PASS = "letmein"; | 37 | private static final String AUTH_PASS = "letmein"; |
... | @@ -46,21 +44,6 @@ public class XosManagerRestUtils { | ... | @@ -46,21 +44,6 @@ public class XosManagerRestUtils { |
46 | private final int xosServerPort; | 44 | private final int xosServerPort; |
47 | private final String baseUri; | 45 | private final String baseUri; |
48 | 46 | ||
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 | 47 | ||
65 | /** | 48 | /** |
66 | * Constructs a utility class, using the supplied server address and port, | 49 | * Constructs a utility class, using the supplied server address and port, | ... | ... |
... | @@ -97,12 +97,12 @@ public class BundleFactory extends JsonFactory { | ... | @@ -97,12 +97,12 @@ public class BundleFactory extends JsonFactory { |
97 | } | 97 | } |
98 | 98 | ||
99 | /** | 99 | /** |
100 | - * Returns a JSON string representation of the given bundle. | 100 | + * Returns an object node representation of the given bundle. |
101 | * | 101 | * |
102 | * @param bundle the bundle | 102 | * @param bundle the bundle |
103 | - * @return JSON string | 103 | + * @return object node |
104 | */ | 104 | */ |
105 | - public static String toJson(Bundle bundle) { | 105 | + public static ObjectNode toObjectNode(Bundle bundle) { |
106 | ObjectNode root = objectNode(); | 106 | ObjectNode root = objectNode(); |
107 | BundleDescriptor descriptor = bundle.descriptor(); | 107 | BundleDescriptor descriptor = bundle.descriptor(); |
108 | 108 | ||
... | @@ -127,6 +127,6 @@ public class BundleFactory extends JsonFactory { | ... | @@ -127,6 +127,6 @@ public class BundleFactory extends JsonFactory { |
127 | bundles.add(bdnode); | 127 | bundles.add(bdnode); |
128 | } | 128 | } |
129 | root.set(BUNDLES, bundles); | 129 | root.set(BUNDLES, bundles); |
130 | - return root.toString(); | 130 | + return root; |
131 | } | 131 | } |
132 | } | 132 | } | ... | ... |
... | @@ -19,6 +19,7 @@ package org.onosproject.cord.gui; | ... | @@ -19,6 +19,7 @@ package org.onosproject.cord.gui; |
19 | 19 | ||
20 | import com.fasterxml.jackson.databind.JsonNode; | 20 | import com.fasterxml.jackson.databind.JsonNode; |
21 | import com.fasterxml.jackson.databind.ObjectMapper; | 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
22 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
22 | import org.junit.Before; | 23 | import org.junit.Before; |
23 | import org.junit.Test; | 24 | import org.junit.Test; |
24 | import org.onosproject.cord.gui.model.BundleFactory; | 25 | import org.onosproject.cord.gui.model.BundleFactory; |
... | @@ -50,7 +51,8 @@ public class CoreModelCacheTest { | ... | @@ -50,7 +51,8 @@ public class CoreModelCacheTest { |
50 | 51 | ||
51 | @Test | 52 | @Test |
52 | public void basicBundleJson() { | 53 | public void basicBundleJson() { |
53 | - String json = BundleFactory.toJson(cache.getCurrentBundle()); | 54 | + ObjectNode node = BundleFactory.toObjectNode(cache.getCurrentBundle()); |
55 | + String json = node.toString(); | ||
54 | System.out.println(json); | 56 | System.out.println(json); |
55 | assertTrue("bad basic json", sameJson(BASIC_BUNDLE_JSON, json)); | 57 | assertTrue("bad basic json", sameJson(BASIC_BUNDLE_JSON, json)); |
56 | } | 58 | } |
... | @@ -65,7 +67,8 @@ public class CoreModelCacheTest { | ... | @@ -65,7 +67,8 @@ public class CoreModelCacheTest { |
65 | @Test | 67 | @Test |
66 | public void familyBundleJson() { | 68 | public void familyBundleJson() { |
67 | cache.setCurrentBundle("family"); | 69 | cache.setCurrentBundle("family"); |
68 | - String json = BundleFactory.toJson(cache.getCurrentBundle()); | 70 | + ObjectNode node = BundleFactory.toObjectNode(cache.getCurrentBundle()); |
71 | + String json = node.toString(); | ||
69 | System.out.println(json); | 72 | System.out.println(json); |
70 | assertTrue("bad family json", sameJson(FAMILY_BUNDLE_JSON, json)); | 73 | assertTrue("bad family json", sameJson(FAMILY_BUNDLE_JSON, json)); |
71 | } | 74 | } | ... | ... |
-
Please register or login to post a comment