Simon Hunt
Committed by Gerrit Code Review

CORD GUI - Coded up email -> SSID -> Subscriber ID lookup on the server side.

Change-Id: I79715f090a692cfcce744cede88772f79b49f599
...@@ -32,6 +32,8 @@ import org.onosproject.cord.gui.model.XosFunctionDescriptor; ...@@ -32,6 +32,8 @@ import org.onosproject.cord.gui.model.XosFunctionDescriptor;
32 import org.slf4j.Logger; 32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory; 33 import org.slf4j.LoggerFactory;
34 34
35 +import java.util.HashMap;
36 +import java.util.Iterator;
35 import java.util.List; 37 import java.util.List;
36 import java.util.Map; 38 import java.util.Map;
37 import java.util.TreeMap; 39 import java.util.TreeMap;
...@@ -44,12 +46,28 @@ import static org.onosproject.cord.gui.model.XosFunctionDescriptor.URL_FILTER; ...@@ -44,12 +46,28 @@ import static org.onosproject.cord.gui.model.XosFunctionDescriptor.URL_FILTER;
44 */ 46 */
45 public class CordModelCache extends JsonFactory { 47 public class CordModelCache extends JsonFactory {
46 48
49 + private static final String KEY_SSID_MAP = "ssidmap";
50 + // FIXME: should not be a colon in the key..... Scott to fix on XOS
51 + private static final String KEY_SSID = "service_specific_id:";
52 + private static final String KEY_SUB_ID = "subscriber_id";
53 +
54 + private static final int DEMO_SSID = 1234;
55 +
56 + private static final String EMAIL_0 = "john@smith.org";
57 + private static final String EMAIL_1 = "john@doe.org";
58 +
59 + private static final String EMAIL = "email";
60 + private static final String SSID = "ssid";
61 + private static final String SUB_ID = "subId";
62 +
47 private static final String BUNDLE = "bundle"; 63 private static final String BUNDLE = "bundle";
48 private static final String USERS = "users"; 64 private static final String USERS = "users";
49 - private static final String SUB_ID = "subId";
50 private static final String LEVEL = "level"; 65 private static final String LEVEL = "level";
51 66
67 + private static final Map<Integer, Integer> LOOKUP = new HashMap<>();
68 +
52 private int subscriberId; 69 private int subscriberId;
70 + private int ssid;
53 private Bundle currentBundle; 71 private Bundle currentBundle;
54 72
55 private final Logger log = LoggerFactory.getLogger(getClass()); 73 private final Logger log = LoggerFactory.getLogger(getClass());
...@@ -59,18 +77,76 @@ public class CordModelCache extends JsonFactory { ...@@ -59,18 +77,76 @@ public class CordModelCache extends JsonFactory {
59 new TreeMap<Integer, SubscriberUser>(); 77 new TreeMap<Integer, SubscriberUser>();
60 78
61 /** 79 /**
62 - * Constructs a model cache, (retrieving demo subscriber ID), 80 + * Constructs a model cache, retrieving a mapping of SSID to XOS Subscriber
63 - * initializing it with basic bundle, and fetching the list of users. 81 + * IDs from the XOS server.
64 */ 82 */
65 CordModelCache() { 83 CordModelCache() {
66 log.info("Initialize model cache"); 84 log.info("Initialize model cache");
67 - subscriberId = XosManager.INSTANCE.initXosSubscriber(); 85 + ObjectNode map = XosManager.INSTANCE.initXosSubscriberLookups();
86 + initLookupMap(map);
87 + log.info("{} entries in SSID->SubID lookup map", LOOKUP.size());
88 + }
89 +
90 + private void initLookupMap(ObjectNode map) {
91 + ArrayNode array = (ArrayNode) map.get(KEY_SSID_MAP);
92 + Iterator<JsonNode> iter = array.elements();
93 + while (iter.hasNext()) {
94 + ObjectNode node = (ObjectNode) iter.next();
95 + String ssidStr = node.get(KEY_SSID).asText();
96 + int ssid = Integer.valueOf(ssidStr);
97 + int subId = node.get(KEY_SUB_ID).asInt();
98 + LOOKUP.put(ssid, subId);
99 + log.info("... binding SSID {} to sub-id {}", ssid, subId);
100 + }
101 + }
102 +
103 + private int lookupSubId(int ssid) {
104 + Integer subId = LOOKUP.get(ssid);
105 + if (subId == null) {
106 + log.error("Unmapped SSID: {}", ssid);
107 + return 0;
108 + }
109 + return subId;
110 + }
111 +
112 + /**
113 + * Initializes the model for the subscriber account associated with
114 + * the given email address.
115 + *
116 + * @param email the email address
117 + */
118 + void init(String email) {
119 + // defaults to the demo account
120 + int ssid = DEMO_SSID;
121 +
122 + // obviously not scalable, but good enough for demo code...
123 + if (EMAIL_0.equals(email)) {
124 + ssid = 0;
125 + } else if (EMAIL_1.equals(email)) {
126 + ssid = 1;
127 + }
128 +
129 + this.ssid = ssid;
130 + subscriberId = lookupSubId(ssid);
131 + XosManager.INSTANCE.setXosUtilsForSubscriber(subscriberId);
132 +
133 + // if we are using the demo account, tell XOS to reset it...
134 + if (ssid == DEMO_SSID) {
135 + XosManager.INSTANCE.initDemoSubscriber();
136 + }
137 +
138 + // NOTE: I think the following should work for non-DEMO account...
68 currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE); 139 currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE);
69 initUsers(); 140 initUsers();
70 } 141 }
71 142
72 private void initUsers() { 143 private void initUsers() {
73 ArrayNode users = XosManager.INSTANCE.getUserList(); 144 ArrayNode users = XosManager.INSTANCE.getUserList();
145 + if (users == null) {
146 + log.warn("no user list for SSID {} (subid {})", ssid, subscriberId);
147 + return;
148 + }
149 +
74 for (JsonNode u: users) { 150 for (JsonNode u: users) {
75 ObjectNode user = (ObjectNode) u; 151 ObjectNode user = (ObjectNode) u;
76 152
...@@ -194,6 +270,25 @@ public class CordModelCache extends JsonFactory { ...@@ -194,6 +270,25 @@ public class CordModelCache extends JsonFactory {
194 270
195 private void addSubId(ObjectNode root) { 271 private void addSubId(ObjectNode root) {
196 root.put(SUB_ID, subscriberId); 272 root.put(SUB_ID, subscriberId);
273 + root.put(SSID, ssid);
274 + }
275 +
276 +
277 + /**
278 + * Returns response JSON for login request.
279 + * <p>
280 + * Depending on which email is used, will bind the GUI to the
281 + * appropriate XOS Subscriber ID.
282 + *
283 + * @param email the supplied email
284 + * @return JSON acknowledgement
285 + */
286 + public String jsonLogin(String email) {
287 + init(email);
288 + ObjectNode root = objectNode();
289 + root.put(EMAIL, email);
290 + addSubId(root);
291 + return root.toString();
197 } 292 }
198 293
199 /** 294 /**
......
...@@ -54,6 +54,13 @@ public class CordWebResource { ...@@ -54,6 +54,13 @@ public class CordWebResource {
54 54
55 @GET 55 @GET
56 @Produces(MediaType.APPLICATION_JSON) 56 @Produces(MediaType.APPLICATION_JSON)
57 + @Path("login/{email}")
58 + public Response login(@PathParam("email") String email) {
59 + return Response.ok(CordModelCache.INSTANCE.jsonLogin(email)).build();
60 + }
61 +
62 + @GET
63 + @Produces(MediaType.APPLICATION_JSON)
57 @Path("bundle/{id}") 64 @Path("bundle/{id}")
58 public Response bundle(@PathParam("id") String bundleId) { 65 public Response bundle(@PathParam("id") String bundleId) {
59 CordModelCache.INSTANCE.setCurrentBundle(bundleId); 66 CordModelCache.INSTANCE.setCurrentBundle(bundleId);
......
...@@ -85,35 +85,46 @@ public class XosManager { ...@@ -85,35 +85,46 @@ public class XosManager {
85 } 85 }
86 86
87 /** 87 /**
88 - * Queries XOS for the Demo Subscriber ID and caches it for future calls. 88 + * Queries XOS for the Subscriber ID lookup data, and returns it.
89 */ 89 */
90 - public int initXosSubscriber() { 90 + public ObjectNode initXosSubscriberLookups() {
91 - log.info("intDemoSubscriber() called"); 91 + log.info("intDemoSubscriberLookups() called");
92 xosServerIp = getXosServerIp(); 92 xosServerIp = getXosServerIp();
93 xosServerPort = getXosServerPort(); 93 xosServerPort = getXosServerPort();
94 log.info("Using XOS server at {}:{}", xosServerIp, xosServerPort); 94 log.info("Using XOS server at {}:{}", xosServerIp, xosServerPort);
95 95
96 xosUtilsRs = new XosManagerRestUtils(xosServerIp, xosServerPort, URI_RS); 96 xosUtilsRs = new XosManagerRestUtils(xosServerIp, xosServerPort, URI_RS);
97 97
98 - // ask XOS for the subscriber ID of the canned Demo account... 98 + // ask XOS for the subscriber ID lookup info
99 - String result = xosUtilsRs.getRest("initdemo/"); 99 + String result = xosUtilsRs.getRest("subidlookup/");
100 - log.info("from XOS: {}", result); 100 + log.info("lookup data from XOS: {}", result);
101 101
102 JsonNode node; 102 JsonNode node;
103 try { 103 try {
104 node = MAPPER.readTree(result); 104 node = MAPPER.readTree(result);
105 } catch (IOException e) { 105 } catch (IOException e) {
106 - log.error("failed to read demo subscriber JSON", e); 106 + log.error("failed to read subscriber lookup JSON data", e);
107 - return 0; 107 + return null;
108 } 108 }
109 + return (ObjectNode) node;
110 + }
109 111
110 - ObjectNode obj = (ObjectNode) node; 112 + /**
111 - int demoId = obj.get("id").asInt(); 113 + * Sets a new XOS utils object to bind URL patterns for the
112 - log.info("Using DEMO subscriber ID {}.", demoId); 114 + * given XOS subscriber ID.
113 - 115 + *
114 - String uri = String.format(URI_SUBSCRIBER, demoId); 116 + * @param xosSubId XOS subscriber ID
117 + */
118 + public void setXosUtilsForSubscriber(int xosSubId) {
119 + String uri = String.format(URI_SUBSCRIBER, xosSubId);
115 xosUtils = new XosManagerRestUtils(xosServerIp, xosServerPort, uri); 120 xosUtils = new XosManagerRestUtils(xosServerIp, xosServerPort, uri);
116 - return demoId; 121 + }
122 +
123 +
124 + public void initDemoSubscriber() {
125 + log.info("initDemoSubscriber() called");
126 + String result = xosUtilsRs.getRest("initdemo/");
127 + log.info("initdemo data from XOS: {}", result);
117 } 128 }
118 129
119 /** 130 /**
......