blob: a098e8dd3dbf40a29d98cbfc6984c009a030230a [file] [log] [blame]
Simon Hunta29c87b2015-05-21 09:56:19 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.onosproject.cord.gui;
19
Simon Hunt7d02c082015-05-29 12:17:09 -070020import com.fasterxml.jackson.databind.JsonNode;
Simon Hunt09a32db2015-05-21 15:00:42 -070021import com.fasterxml.jackson.databind.node.ArrayNode;
22import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Hunt41b943e2015-05-21 13:52:01 -070023import com.google.common.collect.ImmutableList;
24import org.onosproject.cord.gui.model.Bundle;
25import org.onosproject.cord.gui.model.BundleDescriptor;
26import org.onosproject.cord.gui.model.BundleFactory;
Simon Hunt09a32db2015-05-21 15:00:42 -070027import org.onosproject.cord.gui.model.JsonFactory;
Simon Hunt41b943e2015-05-21 13:52:01 -070028import org.onosproject.cord.gui.model.SubscriberUser;
Simon Hunt09a32db2015-05-21 15:00:42 -070029import org.onosproject.cord.gui.model.UserFactory;
Simon Hunt6c2555b2015-05-21 18:17:56 -070030import org.onosproject.cord.gui.model.XosFunction;
31import org.onosproject.cord.gui.model.XosFunctionDescriptor;
Simon Huntb1246412015-06-01 13:37:26 -070032import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
Simon Hunt41b943e2015-05-21 13:52:01 -070034
Simon Huntc686c6a2015-06-05 14:33:30 -070035import java.util.HashMap;
36import java.util.Iterator;
Simon Hunt41b943e2015-05-21 13:52:01 -070037import java.util.List;
Simon Hunt87b157c2015-05-22 12:09:59 -070038import java.util.Map;
39import java.util.TreeMap;
40
41import static com.google.common.base.Preconditions.checkNotNull;
Simon Hunt7d02c082015-05-29 12:17:09 -070042import static org.onosproject.cord.gui.model.XosFunctionDescriptor.URL_FILTER;
Simon Hunt41b943e2015-05-21 13:52:01 -070043
Simon Hunta29c87b2015-05-21 09:56:19 -070044/**
45 * In memory cache of the model of the subscriber's account.
46 */
Simon Hunt09a32db2015-05-21 15:00:42 -070047public class CordModelCache extends JsonFactory {
48
Simon Huntc686c6a2015-06-05 14:33:30 -070049 private static final String KEY_SSID_MAP = "ssidmap";
Simon Hunt2739e6f82015-06-05 16:27:45 -070050 private static final String KEY_SSID = "service_specific_id";
Simon Huntc686c6a2015-06-05 14:33:30 -070051 private static final String KEY_SUB_ID = "subscriber_id";
52
53 private static final int DEMO_SSID = 1234;
54
55 private static final String EMAIL_0 = "john@smith.org";
56 private static final String EMAIL_1 = "john@doe.org";
57
58 private static final String EMAIL = "email";
59 private static final String SSID = "ssid";
60 private static final String SUB_ID = "subId";
61
Simon Hunt09a32db2015-05-21 15:00:42 -070062 private static final String BUNDLE = "bundle";
63 private static final String USERS = "users";
Simon Hunt7d02c082015-05-29 12:17:09 -070064 private static final String LEVEL = "level";
Simon Huntc296d2c2015-06-08 12:54:57 -070065 private static final String LOGOUT = "logout";
Simon Hunt41b943e2015-05-21 13:52:01 -070066
Bri Prebilic Cole8b66a852015-06-10 17:15:10 -070067 private static final String BUNDLE_NAME = BUNDLE + "_name";
68 private static final String BUNDLE_DESC = BUNDLE + "_desc";
69
Simon Huntc686c6a2015-06-05 14:33:30 -070070 private static final Map<Integer, Integer> LOOKUP = new HashMap<>();
71
Simon Huntc296d2c2015-06-08 12:54:57 -070072 private String email = null;
Simon Huntee6a7372015-05-28 14:04:24 -070073 private int subscriberId;
Simon Huntc686c6a2015-06-05 14:33:30 -070074 private int ssid;
Simon Hunt41b943e2015-05-21 13:52:01 -070075 private Bundle currentBundle;
Simon Hunt87b157c2015-05-22 12:09:59 -070076
Simon Huntb1246412015-06-01 13:37:26 -070077 private final Logger log = LoggerFactory.getLogger(getClass());
78
Simon Hunt87b157c2015-05-22 12:09:59 -070079 // NOTE: use a tree map to maintain sorted order by user ID
80 private final Map<Integer, SubscriberUser> userMap =
81 new TreeMap<Integer, SubscriberUser>();
Simon Hunt41b943e2015-05-21 13:52:01 -070082
83 /**
Simon Huntc686c6a2015-06-05 14:33:30 -070084 * Constructs a model cache, retrieving a mapping of SSID to XOS Subscriber
85 * IDs from the XOS server.
Simon Hunt41b943e2015-05-21 13:52:01 -070086 */
Simon Hunt09a32db2015-05-21 15:00:42 -070087 CordModelCache() {
Simon Huntb1246412015-06-01 13:37:26 -070088 log.info("Initialize model cache");
Simon Huntc686c6a2015-06-05 14:33:30 -070089 ObjectNode map = XosManager.INSTANCE.initXosSubscriberLookups();
90 initLookupMap(map);
91 log.info("{} entries in SSID->SubID lookup map", LOOKUP.size());
92 }
93
94 private void initLookupMap(ObjectNode map) {
95 ArrayNode array = (ArrayNode) map.get(KEY_SSID_MAP);
96 Iterator<JsonNode> iter = array.elements();
Simon Hunt2739e6f82015-06-05 16:27:45 -070097 StringBuilder msg = new StringBuilder();
Simon Huntc686c6a2015-06-05 14:33:30 -070098 while (iter.hasNext()) {
99 ObjectNode node = (ObjectNode) iter.next();
Simon Hunt42366082015-06-08 09:57:05 -0700100 String ssidStr = node.get(KEY_SSID).asText();
Simon Huntc686c6a2015-06-05 14:33:30 -0700101 int ssid = Integer.valueOf(ssidStr);
102 int subId = node.get(KEY_SUB_ID).asInt();
103 LOOKUP.put(ssid, subId);
Simon Hunt2739e6f82015-06-05 16:27:45 -0700104 msg.append(String.format("\n..binding SSID %s to sub-id %s", ssid, subId));
Simon Huntc686c6a2015-06-05 14:33:30 -0700105 }
Simon Hunt2739e6f82015-06-05 16:27:45 -0700106 log.info(msg.toString());
Simon Huntc686c6a2015-06-05 14:33:30 -0700107 }
108
109 private int lookupSubId(int ssid) {
110 Integer subId = LOOKUP.get(ssid);
111 if (subId == null) {
112 log.error("Unmapped SSID: {}", ssid);
113 return 0;
114 }
115 return subId;
116 }
117
118 /**
119 * Initializes the model for the subscriber account associated with
120 * the given email address.
121 *
122 * @param email the email address
123 */
124 void init(String email) {
125 // defaults to the demo account
126 int ssid = DEMO_SSID;
127
Simon Huntc296d2c2015-06-08 12:54:57 -0700128 this.email = email;
129
Simon Huntc686c6a2015-06-05 14:33:30 -0700130 // obviously not scalable, but good enough for demo code...
131 if (EMAIL_0.equals(email)) {
132 ssid = 0;
133 } else if (EMAIL_1.equals(email)) {
134 ssid = 1;
135 }
136
137 this.ssid = ssid;
138 subscriberId = lookupSubId(ssid);
139 XosManager.INSTANCE.setXosUtilsForSubscriber(subscriberId);
140
141 // if we are using the demo account, tell XOS to reset it...
142 if (ssid == DEMO_SSID) {
143 XosManager.INSTANCE.initDemoSubscriber();
144 }
145
146 // NOTE: I think the following should work for non-DEMO account...
Simon Hunt41b943e2015-05-21 13:52:01 -0700147 currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE);
Simon Hunt7d02c082015-05-29 12:17:09 -0700148 initUsers();
Simon Hunt41b943e2015-05-21 13:52:01 -0700149 }
150
Simon Huntee6a7372015-05-28 14:04:24 -0700151 private void initUsers() {
Simon Huntc296d2c2015-06-08 12:54:57 -0700152 // start with a clean slate
153 userMap.clear();
154
Simon Hunt7d02c082015-05-29 12:17:09 -0700155 ArrayNode users = XosManager.INSTANCE.getUserList();
Simon Huntc686c6a2015-06-05 14:33:30 -0700156 if (users == null) {
157 log.warn("no user list for SSID {} (subid {})", ssid, subscriberId);
158 return;
159 }
160
Simon Huntc296d2c2015-06-08 12:54:57 -0700161 StringBuilder sb = new StringBuilder();
Simon Hunt7d02c082015-05-29 12:17:09 -0700162 for (JsonNode u: users) {
163 ObjectNode user = (ObjectNode) u;
164
165 int id = user.get("id").asInt();
166 String name = user.get("name").asText();
167 String mac = user.get("mac").asText();
168 String level = user.get("level").asText();
169
170 // NOTE: We are just storing the current "url-filter" level.
171 // Since we are starting with the BASIC bundle, (that does
172 // not include URL_FILTER), we don't yet have the URL_FILTER
173 // memento in which to store the level.
174 SubscriberUser su = createUser(id, name, mac, level);
175 userMap.put(id, su);
Simon Huntc296d2c2015-06-08 12:54:57 -0700176 sb.append(String.format("\n..cache user %s [%d], %s, %s",
177 name, id, mac, level));
Simon Hunt7d02c082015-05-29 12:17:09 -0700178 }
Simon Huntc296d2c2015-06-08 12:54:57 -0700179 log.info(sb.toString());
Simon Hunt41b943e2015-05-21 13:52:01 -0700180 }
181
Simon Hunt7d02c082015-05-29 12:17:09 -0700182 private SubscriberUser createUser(int uid, String name, String mac,
183 String level) {
184 SubscriberUser user = new SubscriberUser(uid, name, mac, level);
Simon Hunt6c2555b2015-05-21 18:17:56 -0700185 for (XosFunction f: currentBundle.functions()) {
186 user.setMemento(f.descriptor(), f.createMemento());
187 }
188 return user;
189 }
190
Simon Hunt41b943e2015-05-21 13:52:01 -0700191 /**
192 * Returns the currently selected bundle.
193 *
194 * @return current bundle
195 */
196 public Bundle getCurrentBundle() {
197 return currentBundle;
198 }
199
200 /**
201 * Sets a new bundle.
202 *
203 * @param bundleId bundle identifier
204 * @throws IllegalArgumentException if bundle ID is unknown
205 */
206 public void setCurrentBundle(String bundleId) {
Simon Huntb1246412015-06-01 13:37:26 -0700207 log.info("set new bundle : {}", bundleId);
Simon Hunt6c2555b2015-05-21 18:17:56 -0700208 BundleDescriptor bd = BundleFactory.bundleFromId(bundleId);
209 currentBundle = new Bundle(bd);
210 // update the user mementos
Simon Hunt87b157c2015-05-22 12:09:59 -0700211 for (SubscriberUser user: userMap.values()) {
Simon Hunt6c2555b2015-05-21 18:17:56 -0700212 user.clearMementos();
213 for (XosFunction f: currentBundle.functions()) {
214 user.setMemento(f.descriptor(), f.createMemento());
Simon Hunt7d02c082015-05-29 12:17:09 -0700215 if (f.descriptor().equals(URL_FILTER)) {
216 applyUrlFilterLevel(user, user.urlFilterLevel());
217 }
Simon Hunt6c2555b2015-05-21 18:17:56 -0700218 }
219 }
220
Simon Hunt7d02c082015-05-29 12:17:09 -0700221 XosManager.INSTANCE.setNewBundle(currentBundle);
Simon Hunt41b943e2015-05-21 13:52:01 -0700222 }
223
Simon Hunt6c2555b2015-05-21 18:17:56 -0700224
Simon Hunt41b943e2015-05-21 13:52:01 -0700225 /**
226 * Returns the list of current users for this subscriber account.
227 *
228 * @return the list of users
229 */
230 public List<SubscriberUser> getUsers() {
Simon Hunt87b157c2015-05-22 12:09:59 -0700231 return ImmutableList.copyOf(userMap.values());
Simon Hunt41b943e2015-05-21 13:52:01 -0700232 }
Simon Hunt09a32db2015-05-21 15:00:42 -0700233
Simon Hunt6c2555b2015-05-21 18:17:56 -0700234 /**
Simon Hunt7d02c082015-05-29 12:17:09 -0700235 * Applies a function parameter change for a user, pushing that
236 * change through to XOS.
Simon Hunt6c2555b2015-05-21 18:17:56 -0700237 *
238 * @param userId user identifier
239 * @param funcId function identifier
240 * @param param function parameter to change
241 * @param value new value for function parameter
242 */
243 public void applyPerUserParam(String userId, String funcId,
244 String param, String value) {
Simon Hunt87b157c2015-05-22 12:09:59 -0700245
Simon Hunt6c2555b2015-05-21 18:17:56 -0700246 int uid = Integer.parseInt(userId);
Simon Hunt87b157c2015-05-22 12:09:59 -0700247 SubscriberUser user = userMap.get(uid);
248 checkNotNull(user, "unknown user id: " + uid);
249
Simon Hunt6c2555b2015-05-21 18:17:56 -0700250 XosFunctionDescriptor xfd =
251 XosFunctionDescriptor.valueOf(funcId.toUpperCase());
Simon Hunt87b157c2015-05-22 12:09:59 -0700252
253 XosFunction func = currentBundle.findFunction(xfd);
254 checkNotNull(func, "function not part of bundle: " + funcId);
Simon Hunt7d02c082015-05-29 12:17:09 -0700255 applyParam(func, user, param, value, true);
Simon Hunt6c2555b2015-05-21 18:17:56 -0700256 }
257
258 // =============
259
Simon Hunt7d02c082015-05-29 12:17:09 -0700260 private void applyUrlFilterLevel(SubscriberUser user, String level) {
261 XosFunction urlFilter = currentBundle.findFunction(URL_FILTER);
262 if (urlFilter != null) {
263 applyParam(urlFilter, user, LEVEL, level, false);
264 }
265 }
266
267 private void applyParam(XosFunction func, SubscriberUser user,
268 String param, String value, boolean punchThrough) {
269 func.applyParam(user, param, value);
270 if (punchThrough) {
271 XosManager.INSTANCE.apply(func, user);
272 }
273 }
274
Simon Hunt09a32db2015-05-21 15:00:42 -0700275 private ArrayNode userJsonArray() {
276 ArrayNode userList = arrayNode();
Simon Hunt87b157c2015-05-22 12:09:59 -0700277 for (SubscriberUser user: userMap.values()) {
Simon Hunt09a32db2015-05-21 15:00:42 -0700278 userList.add(UserFactory.toObjectNode(user));
279 }
280 return userList;
281 }
282
283 // ============= generate JSON for GUI rest calls..
284
Simon Huntee6a7372015-05-28 14:04:24 -0700285 private void addSubId(ObjectNode root) {
286 root.put(SUB_ID, subscriberId);
Simon Huntc686c6a2015-06-05 14:33:30 -0700287 root.put(SSID, ssid);
Simon Huntc296d2c2015-06-08 12:54:57 -0700288 root.put(EMAIL, email);
Simon Huntc686c6a2015-06-05 14:33:30 -0700289 }
290
291
292 /**
293 * Returns response JSON for login request.
294 * <p>
295 * Depending on which email is used, will bind the GUI to the
296 * appropriate XOS Subscriber ID.
297 *
298 * @param email the supplied email
299 * @return JSON acknowledgement
300 */
Simon Hunt6f0afae2015-06-08 21:40:41 -0700301 public synchronized String jsonLogin(String email) {
Simon Huntc296d2c2015-06-08 12:54:57 -0700302 log.info("jsonLogin(\"{}\")", email);
Simon Huntc686c6a2015-06-05 14:33:30 -0700303 init(email);
304 ObjectNode root = objectNode();
Simon Huntc686c6a2015-06-05 14:33:30 -0700305 addSubId(root);
306 return root.toString();
Simon Huntee6a7372015-05-28 14:04:24 -0700307 }
308
Simon Hunt09a32db2015-05-21 15:00:42 -0700309 /**
310 * Returns the dashboard page data as JSON.
311 *
312 * @return dashboard page JSON data
313 */
Simon Hunt6f0afae2015-06-08 21:40:41 -0700314 public synchronized String jsonDashboard() {
Simon Huntc296d2c2015-06-08 12:54:57 -0700315 log.info("jsonDashboard()");
316
317 if (email == null) {
318 return jsonLogout();
319 }
320
Bri Prebilic Cole8b66a852015-06-10 17:15:10 -0700321 BundleDescriptor bundleDescriptor = currentBundle.descriptor();
Simon Hunt09a32db2015-05-21 15:00:42 -0700322 ObjectNode root = objectNode();
Bri Prebilic Cole8b66a852015-06-10 17:15:10 -0700323 root.put(BUNDLE_NAME, bundleDescriptor.displayName());
324 root.put(BUNDLE_DESC, bundleDescriptor.description());
Simon Hunt09a32db2015-05-21 15:00:42 -0700325 root.set(USERS, userJsonArray());
Simon Huntee6a7372015-05-28 14:04:24 -0700326 addSubId(root);
Simon Hunt09a32db2015-05-21 15:00:42 -0700327 return root.toString();
328 }
329
330 /**
331 * Returns the bundle page data as JSON.
332 *
333 * @return bundle page JSON data
334 */
Simon Hunt6f0afae2015-06-08 21:40:41 -0700335 public synchronized String jsonBundle() {
Simon Huntc296d2c2015-06-08 12:54:57 -0700336 log.info("jsonBundle()");
337
338 if (email == null) {
339 return jsonLogout();
340 }
341
Simon Huntee6a7372015-05-28 14:04:24 -0700342 ObjectNode root = BundleFactory.toObjectNode(currentBundle);
343 addSubId(root);
344 return root.toString();
Simon Hunt09a32db2015-05-21 15:00:42 -0700345 }
346
347 /**
348 * Returns the users page data as JSON.
349 *
350 * @return users page JSON data
351 */
Simon Hunt6f0afae2015-06-08 21:40:41 -0700352 public synchronized String jsonUsers() {
Simon Huntc296d2c2015-06-08 12:54:57 -0700353 log.info("jsonUsers()");
354
355 if (email == null) {
356 return jsonLogout();
357 }
358
Simon Hunt09a32db2015-05-21 15:00:42 -0700359 ObjectNode root = objectNode();
360 root.set(USERS, userJsonArray());
Simon Huntee6a7372015-05-28 14:04:24 -0700361 addSubId(root);
Simon Hunt09a32db2015-05-21 15:00:42 -0700362 return root.toString();
363 }
364
365 /**
Simon Huntc296d2c2015-06-08 12:54:57 -0700366 * Returns logout acknowledgement as JSON.
367 *
368 * @return logout acknowledgement
369 */
Simon Hunt6f0afae2015-06-08 21:40:41 -0700370 public synchronized String jsonLogout() {
Simon Huntc296d2c2015-06-08 12:54:57 -0700371 log.info("jsonLogout()");
372 ObjectNode root = objectNode().put(LOGOUT, true);
373 addSubId(root);
374
375 email = null; // signifies no one logged in
376
377 return root.toString();
378 }
379
380 /**
Simon Hunt09a32db2015-05-21 15:00:42 -0700381 * Singleton instance.
382 */
383 public static final CordModelCache INSTANCE = new CordModelCache();
Simon Hunta29c87b2015-05-21 09:56:19 -0700384}