blob: 87edd3dcddf0abc27507c3484f6bcfc40cc51885 [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 Hunt41b943e2015-05-21 13:52:01 -070065
Simon Huntc686c6a2015-06-05 14:33:30 -070066 private static final Map<Integer, Integer> LOOKUP = new HashMap<>();
67
Simon Huntee6a7372015-05-28 14:04:24 -070068 private int subscriberId;
Simon Huntc686c6a2015-06-05 14:33:30 -070069 private int ssid;
Simon Hunt41b943e2015-05-21 13:52:01 -070070 private Bundle currentBundle;
Simon Hunt87b157c2015-05-22 12:09:59 -070071
Simon Huntb1246412015-06-01 13:37:26 -070072 private final Logger log = LoggerFactory.getLogger(getClass());
73
Simon Hunt87b157c2015-05-22 12:09:59 -070074 // NOTE: use a tree map to maintain sorted order by user ID
75 private final Map<Integer, SubscriberUser> userMap =
76 new TreeMap<Integer, SubscriberUser>();
Simon Hunt41b943e2015-05-21 13:52:01 -070077
78 /**
Simon Huntc686c6a2015-06-05 14:33:30 -070079 * Constructs a model cache, retrieving a mapping of SSID to XOS Subscriber
80 * IDs from the XOS server.
Simon Hunt41b943e2015-05-21 13:52:01 -070081 */
Simon Hunt09a32db2015-05-21 15:00:42 -070082 CordModelCache() {
Simon Huntb1246412015-06-01 13:37:26 -070083 log.info("Initialize model cache");
Simon Huntc686c6a2015-06-05 14:33:30 -070084 ObjectNode map = XosManager.INSTANCE.initXosSubscriberLookups();
85 initLookupMap(map);
86 log.info("{} entries in SSID->SubID lookup map", LOOKUP.size());
Simon Hunt2739e6f82015-06-05 16:27:45 -070087 // force DEMO subscriber to be installed by default
88 init("foo@bar");
Simon Huntc686c6a2015-06-05 14:33:30 -070089 }
90
91 private void initLookupMap(ObjectNode map) {
92 ArrayNode array = (ArrayNode) map.get(KEY_SSID_MAP);
93 Iterator<JsonNode> iter = array.elements();
Simon Hunt2739e6f82015-06-05 16:27:45 -070094 StringBuilder msg = new StringBuilder();
Simon Huntc686c6a2015-06-05 14:33:30 -070095 while (iter.hasNext()) {
96 ObjectNode node = (ObjectNode) iter.next();
Simon Hunt42366082015-06-08 09:57:05 -070097 String ssidStr = node.get(KEY_SSID).asText();
Simon Huntc686c6a2015-06-05 14:33:30 -070098 int ssid = Integer.valueOf(ssidStr);
99 int subId = node.get(KEY_SUB_ID).asInt();
100 LOOKUP.put(ssid, subId);
Simon Hunt2739e6f82015-06-05 16:27:45 -0700101 msg.append(String.format("\n..binding SSID %s to sub-id %s", ssid, subId));
Simon Huntc686c6a2015-06-05 14:33:30 -0700102 }
Simon Hunt2739e6f82015-06-05 16:27:45 -0700103 log.info(msg.toString());
Simon Huntc686c6a2015-06-05 14:33:30 -0700104 }
105
106 private int lookupSubId(int ssid) {
107 Integer subId = LOOKUP.get(ssid);
108 if (subId == null) {
109 log.error("Unmapped SSID: {}", ssid);
110 return 0;
111 }
112 return subId;
113 }
114
115 /**
116 * Initializes the model for the subscriber account associated with
117 * the given email address.
118 *
119 * @param email the email address
120 */
121 void init(String email) {
122 // defaults to the demo account
123 int ssid = DEMO_SSID;
124
125 // obviously not scalable, but good enough for demo code...
126 if (EMAIL_0.equals(email)) {
127 ssid = 0;
128 } else if (EMAIL_1.equals(email)) {
129 ssid = 1;
130 }
131
132 this.ssid = ssid;
133 subscriberId = lookupSubId(ssid);
134 XosManager.INSTANCE.setXosUtilsForSubscriber(subscriberId);
135
136 // if we are using the demo account, tell XOS to reset it...
137 if (ssid == DEMO_SSID) {
138 XosManager.INSTANCE.initDemoSubscriber();
139 }
140
141 // NOTE: I think the following should work for non-DEMO account...
Simon Hunt41b943e2015-05-21 13:52:01 -0700142 currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE);
Simon Hunt7d02c082015-05-29 12:17:09 -0700143 initUsers();
Simon Hunt41b943e2015-05-21 13:52:01 -0700144 }
145
Simon Huntee6a7372015-05-28 14:04:24 -0700146 private void initUsers() {
Simon Hunt7d02c082015-05-29 12:17:09 -0700147 ArrayNode users = XosManager.INSTANCE.getUserList();
Simon Huntc686c6a2015-06-05 14:33:30 -0700148 if (users == null) {
149 log.warn("no user list for SSID {} (subid {})", ssid, subscriberId);
150 return;
151 }
152
Simon Hunt7d02c082015-05-29 12:17:09 -0700153 for (JsonNode u: users) {
154 ObjectNode user = (ObjectNode) u;
155
156 int id = user.get("id").asInt();
157 String name = user.get("name").asText();
158 String mac = user.get("mac").asText();
159 String level = user.get("level").asText();
160
161 // NOTE: We are just storing the current "url-filter" level.
162 // Since we are starting with the BASIC bundle, (that does
163 // not include URL_FILTER), we don't yet have the URL_FILTER
164 // memento in which to store the level.
165 SubscriberUser su = createUser(id, name, mac, level);
166 userMap.put(id, su);
Simon Huntb1246412015-06-01 13:37:26 -0700167 log.info("..caching user {} (id:{})", name, id);
Simon Hunt7d02c082015-05-29 12:17:09 -0700168 }
Simon Hunt41b943e2015-05-21 13:52:01 -0700169 }
170
Simon Hunt7d02c082015-05-29 12:17:09 -0700171 private SubscriberUser createUser(int uid, String name, String mac,
172 String level) {
173 SubscriberUser user = new SubscriberUser(uid, name, mac, level);
Simon Hunt6c2555b2015-05-21 18:17:56 -0700174 for (XosFunction f: currentBundle.functions()) {
175 user.setMemento(f.descriptor(), f.createMemento());
176 }
177 return user;
178 }
179
Simon Hunt41b943e2015-05-21 13:52:01 -0700180 /**
181 * Returns the currently selected bundle.
182 *
183 * @return current bundle
184 */
185 public Bundle getCurrentBundle() {
186 return currentBundle;
187 }
188
189 /**
190 * Sets a new bundle.
191 *
192 * @param bundleId bundle identifier
193 * @throws IllegalArgumentException if bundle ID is unknown
194 */
195 public void setCurrentBundle(String bundleId) {
Simon Huntb1246412015-06-01 13:37:26 -0700196 log.info("set new bundle : {}", bundleId);
Simon Hunt6c2555b2015-05-21 18:17:56 -0700197 BundleDescriptor bd = BundleFactory.bundleFromId(bundleId);
198 currentBundle = new Bundle(bd);
199 // update the user mementos
Simon Hunt87b157c2015-05-22 12:09:59 -0700200 for (SubscriberUser user: userMap.values()) {
Simon Hunt6c2555b2015-05-21 18:17:56 -0700201 user.clearMementos();
202 for (XosFunction f: currentBundle.functions()) {
203 user.setMemento(f.descriptor(), f.createMemento());
Simon Hunt7d02c082015-05-29 12:17:09 -0700204 if (f.descriptor().equals(URL_FILTER)) {
205 applyUrlFilterLevel(user, user.urlFilterLevel());
206 }
Simon Hunt6c2555b2015-05-21 18:17:56 -0700207 }
208 }
209
Simon Hunt7d02c082015-05-29 12:17:09 -0700210 XosManager.INSTANCE.setNewBundle(currentBundle);
Simon Hunt41b943e2015-05-21 13:52:01 -0700211 }
212
Simon Hunt6c2555b2015-05-21 18:17:56 -0700213
Simon Hunt41b943e2015-05-21 13:52:01 -0700214 /**
215 * Returns the list of current users for this subscriber account.
216 *
217 * @return the list of users
218 */
219 public List<SubscriberUser> getUsers() {
Simon Hunt87b157c2015-05-22 12:09:59 -0700220 return ImmutableList.copyOf(userMap.values());
Simon Hunt41b943e2015-05-21 13:52:01 -0700221 }
Simon Hunt09a32db2015-05-21 15:00:42 -0700222
Simon Hunt6c2555b2015-05-21 18:17:56 -0700223 /**
Simon Hunt7d02c082015-05-29 12:17:09 -0700224 * Applies a function parameter change for a user, pushing that
225 * change through to XOS.
Simon Hunt6c2555b2015-05-21 18:17:56 -0700226 *
227 * @param userId user identifier
228 * @param funcId function identifier
229 * @param param function parameter to change
230 * @param value new value for function parameter
231 */
232 public void applyPerUserParam(String userId, String funcId,
233 String param, String value) {
Simon Hunt87b157c2015-05-22 12:09:59 -0700234
Simon Hunt6c2555b2015-05-21 18:17:56 -0700235 int uid = Integer.parseInt(userId);
Simon Hunt87b157c2015-05-22 12:09:59 -0700236 SubscriberUser user = userMap.get(uid);
237 checkNotNull(user, "unknown user id: " + uid);
238
Simon Hunt6c2555b2015-05-21 18:17:56 -0700239 XosFunctionDescriptor xfd =
240 XosFunctionDescriptor.valueOf(funcId.toUpperCase());
Simon Hunt87b157c2015-05-22 12:09:59 -0700241
242 XosFunction func = currentBundle.findFunction(xfd);
243 checkNotNull(func, "function not part of bundle: " + funcId);
Simon Hunt7d02c082015-05-29 12:17:09 -0700244 applyParam(func, user, param, value, true);
Simon Hunt6c2555b2015-05-21 18:17:56 -0700245 }
246
247 // =============
248
Simon Hunt7d02c082015-05-29 12:17:09 -0700249 private void applyUrlFilterLevel(SubscriberUser user, String level) {
250 XosFunction urlFilter = currentBundle.findFunction(URL_FILTER);
251 if (urlFilter != null) {
252 applyParam(urlFilter, user, LEVEL, level, false);
253 }
254 }
255
256 private void applyParam(XosFunction func, SubscriberUser user,
257 String param, String value, boolean punchThrough) {
258 func.applyParam(user, param, value);
259 if (punchThrough) {
260 XosManager.INSTANCE.apply(func, user);
261 }
262 }
263
Simon Hunt09a32db2015-05-21 15:00:42 -0700264 private ArrayNode userJsonArray() {
265 ArrayNode userList = arrayNode();
Simon Hunt87b157c2015-05-22 12:09:59 -0700266 for (SubscriberUser user: userMap.values()) {
Simon Hunt09a32db2015-05-21 15:00:42 -0700267 userList.add(UserFactory.toObjectNode(user));
268 }
269 return userList;
270 }
271
272 // ============= generate JSON for GUI rest calls..
273
Simon Huntee6a7372015-05-28 14:04:24 -0700274 private void addSubId(ObjectNode root) {
275 root.put(SUB_ID, subscriberId);
Simon Huntc686c6a2015-06-05 14:33:30 -0700276 root.put(SSID, ssid);
277 }
278
279
280 /**
281 * Returns response JSON for login request.
282 * <p>
283 * Depending on which email is used, will bind the GUI to the
284 * appropriate XOS Subscriber ID.
285 *
286 * @param email the supplied email
287 * @return JSON acknowledgement
288 */
289 public String jsonLogin(String email) {
290 init(email);
291 ObjectNode root = objectNode();
292 root.put(EMAIL, email);
293 addSubId(root);
294 return root.toString();
Simon Huntee6a7372015-05-28 14:04:24 -0700295 }
296
Simon Hunt09a32db2015-05-21 15:00:42 -0700297 /**
298 * Returns the dashboard page data as JSON.
299 *
300 * @return dashboard page JSON data
301 */
302 public String jsonDashboard() {
303 ObjectNode root = objectNode();
304 root.put(BUNDLE, currentBundle.descriptor().displayName());
305 root.set(USERS, userJsonArray());
Simon Huntee6a7372015-05-28 14:04:24 -0700306 addSubId(root);
Simon Hunt09a32db2015-05-21 15:00:42 -0700307 return root.toString();
308 }
309
310 /**
311 * Returns the bundle page data as JSON.
312 *
313 * @return bundle page JSON data
314 */
315 public String jsonBundle() {
Simon Huntee6a7372015-05-28 14:04:24 -0700316 ObjectNode root = BundleFactory.toObjectNode(currentBundle);
317 addSubId(root);
318 return root.toString();
Simon Hunt09a32db2015-05-21 15:00:42 -0700319 }
320
321 /**
322 * Returns the users page data as JSON.
323 *
324 * @return users page JSON data
325 */
326 public String jsonUsers() {
327 ObjectNode root = objectNode();
328 root.set(USERS, userJsonArray());
Simon Huntee6a7372015-05-28 14:04:24 -0700329 addSubId(root);
Simon Hunt09a32db2015-05-21 15:00:42 -0700330 return root.toString();
331 }
332
333 /**
334 * Singleton instance.
335 */
336 public static final CordModelCache INSTANCE = new CordModelCache();
Simon Hunta29c87b2015-05-21 09:56:19 -0700337}