blob: 273bdec6b236663a105a76ffd22eaa8cf7dd8daf [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";
51 // FIXME: remove once the key has been fixed
52 private static final String KEY_SSID_ALT = "service_specific_id:";
Simon Huntc686c6a2015-06-05 14:33:30 -070053 private static final String KEY_SUB_ID = "subscriber_id";
54
55 private static final int DEMO_SSID = 1234;
56
57 private static final String EMAIL_0 = "john@smith.org";
58 private static final String EMAIL_1 = "john@doe.org";
59
60 private static final String EMAIL = "email";
61 private static final String SSID = "ssid";
62 private static final String SUB_ID = "subId";
63
Simon Hunt09a32db2015-05-21 15:00:42 -070064 private static final String BUNDLE = "bundle";
65 private static final String USERS = "users";
Simon Hunt7d02c082015-05-29 12:17:09 -070066 private static final String LEVEL = "level";
Simon Hunt41b943e2015-05-21 13:52:01 -070067
Simon Huntc686c6a2015-06-05 14:33:30 -070068 private static final Map<Integer, Integer> LOOKUP = new HashMap<>();
69
Simon Huntee6a7372015-05-28 14:04:24 -070070 private int subscriberId;
Simon Huntc686c6a2015-06-05 14:33:30 -070071 private int ssid;
Simon Hunt41b943e2015-05-21 13:52:01 -070072 private Bundle currentBundle;
Simon Hunt87b157c2015-05-22 12:09:59 -070073
Simon Huntb1246412015-06-01 13:37:26 -070074 private final Logger log = LoggerFactory.getLogger(getClass());
75
Simon Hunt87b157c2015-05-22 12:09:59 -070076 // NOTE: use a tree map to maintain sorted order by user ID
77 private final Map<Integer, SubscriberUser> userMap =
78 new TreeMap<Integer, SubscriberUser>();
Simon Hunt41b943e2015-05-21 13:52:01 -070079
80 /**
Simon Huntc686c6a2015-06-05 14:33:30 -070081 * Constructs a model cache, retrieving a mapping of SSID to XOS Subscriber
82 * IDs from the XOS server.
Simon Hunt41b943e2015-05-21 13:52:01 -070083 */
Simon Hunt09a32db2015-05-21 15:00:42 -070084 CordModelCache() {
Simon Huntb1246412015-06-01 13:37:26 -070085 log.info("Initialize model cache");
Simon Huntc686c6a2015-06-05 14:33:30 -070086 ObjectNode map = XosManager.INSTANCE.initXosSubscriberLookups();
87 initLookupMap(map);
88 log.info("{} entries in SSID->SubID lookup map", LOOKUP.size());
Simon Hunt2739e6f82015-06-05 16:27:45 -070089 // force DEMO subscriber to be installed by default
90 init("foo@bar");
Simon Huntc686c6a2015-06-05 14:33:30 -070091 }
92
93 private void initLookupMap(ObjectNode map) {
94 ArrayNode array = (ArrayNode) map.get(KEY_SSID_MAP);
95 Iterator<JsonNode> iter = array.elements();
Simon Hunt2739e6f82015-06-05 16:27:45 -070096 StringBuilder msg = new StringBuilder();
Simon Huntc686c6a2015-06-05 14:33:30 -070097 while (iter.hasNext()) {
98 ObjectNode node = (ObjectNode) iter.next();
Simon Hunt2739e6f82015-06-05 16:27:45 -070099
100 // FIXME: clean up once the colon has been removed from the key
101 JsonNode s = node.get(KEY_SSID);
102 if (s == null) {
103 s = node.get(KEY_SSID_ALT);
104 if (s == null) {
105 log.error("missing {} property!", KEY_SSID);
106 continue;
107 }
108 }
109
110 String ssidStr = s.asText();
Simon Huntc686c6a2015-06-05 14:33:30 -0700111 int ssid = Integer.valueOf(ssidStr);
112 int subId = node.get(KEY_SUB_ID).asInt();
113 LOOKUP.put(ssid, subId);
Simon Hunt2739e6f82015-06-05 16:27:45 -0700114 msg.append(String.format("\n..binding SSID %s to sub-id %s", ssid, subId));
Simon Huntc686c6a2015-06-05 14:33:30 -0700115 }
Simon Hunt2739e6f82015-06-05 16:27:45 -0700116 log.info(msg.toString());
Simon Huntc686c6a2015-06-05 14:33:30 -0700117 }
118
119 private int lookupSubId(int ssid) {
120 Integer subId = LOOKUP.get(ssid);
121 if (subId == null) {
122 log.error("Unmapped SSID: {}", ssid);
123 return 0;
124 }
125 return subId;
126 }
127
128 /**
129 * Initializes the model for the subscriber account associated with
130 * the given email address.
131 *
132 * @param email the email address
133 */
134 void init(String email) {
135 // defaults to the demo account
136 int ssid = DEMO_SSID;
137
138 // obviously not scalable, but good enough for demo code...
139 if (EMAIL_0.equals(email)) {
140 ssid = 0;
141 } else if (EMAIL_1.equals(email)) {
142 ssid = 1;
143 }
144
145 this.ssid = ssid;
146 subscriberId = lookupSubId(ssid);
147 XosManager.INSTANCE.setXosUtilsForSubscriber(subscriberId);
148
149 // if we are using the demo account, tell XOS to reset it...
150 if (ssid == DEMO_SSID) {
151 XosManager.INSTANCE.initDemoSubscriber();
152 }
153
154 // NOTE: I think the following should work for non-DEMO account...
Simon Hunt41b943e2015-05-21 13:52:01 -0700155 currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE);
Simon Hunt7d02c082015-05-29 12:17:09 -0700156 initUsers();
Simon Hunt41b943e2015-05-21 13:52:01 -0700157 }
158
Simon Huntee6a7372015-05-28 14:04:24 -0700159 private void initUsers() {
Simon Hunt7d02c082015-05-29 12:17:09 -0700160 ArrayNode users = XosManager.INSTANCE.getUserList();
Simon Huntc686c6a2015-06-05 14:33:30 -0700161 if (users == null) {
162 log.warn("no user list for SSID {} (subid {})", ssid, subscriberId);
163 return;
164 }
165
Simon Hunt7d02c082015-05-29 12:17:09 -0700166 for (JsonNode u: users) {
167 ObjectNode user = (ObjectNode) u;
168
169 int id = user.get("id").asInt();
170 String name = user.get("name").asText();
171 String mac = user.get("mac").asText();
172 String level = user.get("level").asText();
173
174 // NOTE: We are just storing the current "url-filter" level.
175 // Since we are starting with the BASIC bundle, (that does
176 // not include URL_FILTER), we don't yet have the URL_FILTER
177 // memento in which to store the level.
178 SubscriberUser su = createUser(id, name, mac, level);
179 userMap.put(id, su);
Simon Huntb1246412015-06-01 13:37:26 -0700180 log.info("..caching user {} (id:{})", name, id);
Simon Hunt7d02c082015-05-29 12:17:09 -0700181 }
Simon Hunt41b943e2015-05-21 13:52:01 -0700182 }
183
Simon Hunt7d02c082015-05-29 12:17:09 -0700184 private SubscriberUser createUser(int uid, String name, String mac,
185 String level) {
186 SubscriberUser user = new SubscriberUser(uid, name, mac, level);
Simon Hunt6c2555b2015-05-21 18:17:56 -0700187 for (XosFunction f: currentBundle.functions()) {
188 user.setMemento(f.descriptor(), f.createMemento());
189 }
190 return user;
191 }
192
Simon Hunt41b943e2015-05-21 13:52:01 -0700193 /**
194 * Returns the currently selected bundle.
195 *
196 * @return current bundle
197 */
198 public Bundle getCurrentBundle() {
199 return currentBundle;
200 }
201
202 /**
203 * Sets a new bundle.
204 *
205 * @param bundleId bundle identifier
206 * @throws IllegalArgumentException if bundle ID is unknown
207 */
208 public void setCurrentBundle(String bundleId) {
Simon Huntb1246412015-06-01 13:37:26 -0700209 log.info("set new bundle : {}", bundleId);
Simon Hunt6c2555b2015-05-21 18:17:56 -0700210 BundleDescriptor bd = BundleFactory.bundleFromId(bundleId);
211 currentBundle = new Bundle(bd);
212 // update the user mementos
Simon Hunt87b157c2015-05-22 12:09:59 -0700213 for (SubscriberUser user: userMap.values()) {
Simon Hunt6c2555b2015-05-21 18:17:56 -0700214 user.clearMementos();
215 for (XosFunction f: currentBundle.functions()) {
216 user.setMemento(f.descriptor(), f.createMemento());
Simon Hunt7d02c082015-05-29 12:17:09 -0700217 if (f.descriptor().equals(URL_FILTER)) {
218 applyUrlFilterLevel(user, user.urlFilterLevel());
219 }
Simon Hunt6c2555b2015-05-21 18:17:56 -0700220 }
221 }
222
Simon Hunt7d02c082015-05-29 12:17:09 -0700223 XosManager.INSTANCE.setNewBundle(currentBundle);
Simon Hunt41b943e2015-05-21 13:52:01 -0700224 }
225
Simon Hunt6c2555b2015-05-21 18:17:56 -0700226
Simon Hunt41b943e2015-05-21 13:52:01 -0700227 /**
228 * Returns the list of current users for this subscriber account.
229 *
230 * @return the list of users
231 */
232 public List<SubscriberUser> getUsers() {
Simon Hunt87b157c2015-05-22 12:09:59 -0700233 return ImmutableList.copyOf(userMap.values());
Simon Hunt41b943e2015-05-21 13:52:01 -0700234 }
Simon Hunt09a32db2015-05-21 15:00:42 -0700235
Simon Hunt6c2555b2015-05-21 18:17:56 -0700236 /**
Simon Hunt7d02c082015-05-29 12:17:09 -0700237 * Applies a function parameter change for a user, pushing that
238 * change through to XOS.
Simon Hunt6c2555b2015-05-21 18:17:56 -0700239 *
240 * @param userId user identifier
241 * @param funcId function identifier
242 * @param param function parameter to change
243 * @param value new value for function parameter
244 */
245 public void applyPerUserParam(String userId, String funcId,
246 String param, String value) {
Simon Hunt87b157c2015-05-22 12:09:59 -0700247
Simon Hunt6c2555b2015-05-21 18:17:56 -0700248 int uid = Integer.parseInt(userId);
Simon Hunt87b157c2015-05-22 12:09:59 -0700249 SubscriberUser user = userMap.get(uid);
250 checkNotNull(user, "unknown user id: " + uid);
251
Simon Hunt6c2555b2015-05-21 18:17:56 -0700252 XosFunctionDescriptor xfd =
253 XosFunctionDescriptor.valueOf(funcId.toUpperCase());
Simon Hunt87b157c2015-05-22 12:09:59 -0700254
255 XosFunction func = currentBundle.findFunction(xfd);
256 checkNotNull(func, "function not part of bundle: " + funcId);
Simon Hunt7d02c082015-05-29 12:17:09 -0700257 applyParam(func, user, param, value, true);
Simon Hunt6c2555b2015-05-21 18:17:56 -0700258 }
259
260 // =============
261
Simon Hunt7d02c082015-05-29 12:17:09 -0700262 private void applyUrlFilterLevel(SubscriberUser user, String level) {
263 XosFunction urlFilter = currentBundle.findFunction(URL_FILTER);
264 if (urlFilter != null) {
265 applyParam(urlFilter, user, LEVEL, level, false);
266 }
267 }
268
269 private void applyParam(XosFunction func, SubscriberUser user,
270 String param, String value, boolean punchThrough) {
271 func.applyParam(user, param, value);
272 if (punchThrough) {
273 XosManager.INSTANCE.apply(func, user);
274 }
275 }
276
Simon Hunt09a32db2015-05-21 15:00:42 -0700277 private ArrayNode userJsonArray() {
278 ArrayNode userList = arrayNode();
Simon Hunt87b157c2015-05-22 12:09:59 -0700279 for (SubscriberUser user: userMap.values()) {
Simon Hunt09a32db2015-05-21 15:00:42 -0700280 userList.add(UserFactory.toObjectNode(user));
281 }
282 return userList;
283 }
284
285 // ============= generate JSON for GUI rest calls..
286
Simon Huntee6a7372015-05-28 14:04:24 -0700287 private void addSubId(ObjectNode root) {
288 root.put(SUB_ID, subscriberId);
Simon Huntc686c6a2015-06-05 14:33:30 -0700289 root.put(SSID, ssid);
290 }
291
292
293 /**
294 * Returns response JSON for login request.
295 * <p>
296 * Depending on which email is used, will bind the GUI to the
297 * appropriate XOS Subscriber ID.
298 *
299 * @param email the supplied email
300 * @return JSON acknowledgement
301 */
302 public String jsonLogin(String email) {
303 init(email);
304 ObjectNode root = objectNode();
305 root.put(EMAIL, email);
306 addSubId(root);
307 return root.toString();
Simon Huntee6a7372015-05-28 14:04:24 -0700308 }
309
Simon Hunt09a32db2015-05-21 15:00:42 -0700310 /**
311 * Returns the dashboard page data as JSON.
312 *
313 * @return dashboard page JSON data
314 */
315 public String jsonDashboard() {
316 ObjectNode root = objectNode();
317 root.put(BUNDLE, currentBundle.descriptor().displayName());
318 root.set(USERS, userJsonArray());
Simon Huntee6a7372015-05-28 14:04:24 -0700319 addSubId(root);
Simon Hunt09a32db2015-05-21 15:00:42 -0700320 return root.toString();
321 }
322
323 /**
324 * Returns the bundle page data as JSON.
325 *
326 * @return bundle page JSON data
327 */
328 public String jsonBundle() {
Simon Huntee6a7372015-05-28 14:04:24 -0700329 ObjectNode root = BundleFactory.toObjectNode(currentBundle);
330 addSubId(root);
331 return root.toString();
Simon Hunt09a32db2015-05-21 15:00:42 -0700332 }
333
334 /**
335 * Returns the users page data as JSON.
336 *
337 * @return users page JSON data
338 */
339 public String jsonUsers() {
340 ObjectNode root = objectNode();
341 root.set(USERS, userJsonArray());
Simon Huntee6a7372015-05-28 14:04:24 -0700342 addSubId(root);
Simon Hunt09a32db2015-05-21 15:00:42 -0700343 return root.toString();
344 }
345
346 /**
347 * Singleton instance.
348 */
349 public static final CordModelCache INSTANCE = new CordModelCache();
Simon Hunta29c87b2015-05-21 09:56:19 -0700350}