Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package org.onosproject.cord.gui; |
| 19 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.JsonNode; |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 22 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableList; |
| 24 | import org.onosproject.cord.gui.model.Bundle; |
| 25 | import org.onosproject.cord.gui.model.BundleDescriptor; |
| 26 | import org.onosproject.cord.gui.model.BundleFactory; |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 27 | import org.onosproject.cord.gui.model.JsonFactory; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 28 | import org.onosproject.cord.gui.model.SubscriberUser; |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 29 | import org.onosproject.cord.gui.model.UserFactory; |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 30 | import org.onosproject.cord.gui.model.XosFunction; |
| 31 | import org.onosproject.cord.gui.model.XosFunctionDescriptor; |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 34 | |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame^] | 35 | import java.util.HashMap; |
| 36 | import java.util.Iterator; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 37 | import java.util.List; |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 38 | import java.util.Map; |
| 39 | import java.util.TreeMap; |
| 40 | |
| 41 | import static com.google.common.base.Preconditions.checkNotNull; |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 42 | import static org.onosproject.cord.gui.model.XosFunctionDescriptor.URL_FILTER; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 43 | |
Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 44 | /** |
| 45 | * In memory cache of the model of the subscriber's account. |
| 46 | */ |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 47 | public class CordModelCache extends JsonFactory { |
| 48 | |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame^] | 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 | |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 63 | private static final String BUNDLE = "bundle"; |
| 64 | private static final String USERS = "users"; |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 65 | private static final String LEVEL = "level"; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 66 | |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame^] | 67 | private static final Map<Integer, Integer> LOOKUP = new HashMap<>(); |
| 68 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 69 | private int subscriberId; |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame^] | 70 | private int ssid; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 71 | private Bundle currentBundle; |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 72 | |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 73 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 74 | |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 75 | // NOTE: use a tree map to maintain sorted order by user ID |
| 76 | private final Map<Integer, SubscriberUser> userMap = |
| 77 | new TreeMap<Integer, SubscriberUser>(); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame^] | 80 | * Constructs a model cache, retrieving a mapping of SSID to XOS Subscriber |
| 81 | * IDs from the XOS server. |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 82 | */ |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 83 | CordModelCache() { |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 84 | log.info("Initialize model cache"); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame^] | 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... |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 139 | currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 140 | initUsers(); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 143 | private void initUsers() { |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 144 | ArrayNode users = XosManager.INSTANCE.getUserList(); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame^] | 145 | if (users == null) { |
| 146 | log.warn("no user list for SSID {} (subid {})", ssid, subscriberId); |
| 147 | return; |
| 148 | } |
| 149 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 150 | for (JsonNode u: users) { |
| 151 | ObjectNode user = (ObjectNode) u; |
| 152 | |
| 153 | int id = user.get("id").asInt(); |
| 154 | String name = user.get("name").asText(); |
| 155 | String mac = user.get("mac").asText(); |
| 156 | String level = user.get("level").asText(); |
| 157 | |
| 158 | // NOTE: We are just storing the current "url-filter" level. |
| 159 | // Since we are starting with the BASIC bundle, (that does |
| 160 | // not include URL_FILTER), we don't yet have the URL_FILTER |
| 161 | // memento in which to store the level. |
| 162 | SubscriberUser su = createUser(id, name, mac, level); |
| 163 | userMap.put(id, su); |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 164 | log.info("..caching user {} (id:{})", name, id); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 165 | } |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 168 | private SubscriberUser createUser(int uid, String name, String mac, |
| 169 | String level) { |
| 170 | SubscriberUser user = new SubscriberUser(uid, name, mac, level); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 171 | for (XosFunction f: currentBundle.functions()) { |
| 172 | user.setMemento(f.descriptor(), f.createMemento()); |
| 173 | } |
| 174 | return user; |
| 175 | } |
| 176 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 177 | /** |
| 178 | * Returns the currently selected bundle. |
| 179 | * |
| 180 | * @return current bundle |
| 181 | */ |
| 182 | public Bundle getCurrentBundle() { |
| 183 | return currentBundle; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Sets a new bundle. |
| 188 | * |
| 189 | * @param bundleId bundle identifier |
| 190 | * @throws IllegalArgumentException if bundle ID is unknown |
| 191 | */ |
| 192 | public void setCurrentBundle(String bundleId) { |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 193 | log.info("set new bundle : {}", bundleId); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 194 | BundleDescriptor bd = BundleFactory.bundleFromId(bundleId); |
| 195 | currentBundle = new Bundle(bd); |
| 196 | // update the user mementos |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 197 | for (SubscriberUser user: userMap.values()) { |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 198 | user.clearMementos(); |
| 199 | for (XosFunction f: currentBundle.functions()) { |
| 200 | user.setMemento(f.descriptor(), f.createMemento()); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 201 | if (f.descriptor().equals(URL_FILTER)) { |
| 202 | applyUrlFilterLevel(user, user.urlFilterLevel()); |
| 203 | } |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 207 | XosManager.INSTANCE.setNewBundle(currentBundle); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 210 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 211 | /** |
| 212 | * Returns the list of current users for this subscriber account. |
| 213 | * |
| 214 | * @return the list of users |
| 215 | */ |
| 216 | public List<SubscriberUser> getUsers() { |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 217 | return ImmutableList.copyOf(userMap.values()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 218 | } |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 219 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 220 | /** |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 221 | * Applies a function parameter change for a user, pushing that |
| 222 | * change through to XOS. |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 223 | * |
| 224 | * @param userId user identifier |
| 225 | * @param funcId function identifier |
| 226 | * @param param function parameter to change |
| 227 | * @param value new value for function parameter |
| 228 | */ |
| 229 | public void applyPerUserParam(String userId, String funcId, |
| 230 | String param, String value) { |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 231 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 232 | int uid = Integer.parseInt(userId); |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 233 | SubscriberUser user = userMap.get(uid); |
| 234 | checkNotNull(user, "unknown user id: " + uid); |
| 235 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 236 | XosFunctionDescriptor xfd = |
| 237 | XosFunctionDescriptor.valueOf(funcId.toUpperCase()); |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 238 | |
| 239 | XosFunction func = currentBundle.findFunction(xfd); |
| 240 | checkNotNull(func, "function not part of bundle: " + funcId); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 241 | applyParam(func, user, param, value, true); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // ============= |
| 245 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 246 | private void applyUrlFilterLevel(SubscriberUser user, String level) { |
| 247 | XosFunction urlFilter = currentBundle.findFunction(URL_FILTER); |
| 248 | if (urlFilter != null) { |
| 249 | applyParam(urlFilter, user, LEVEL, level, false); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | private void applyParam(XosFunction func, SubscriberUser user, |
| 254 | String param, String value, boolean punchThrough) { |
| 255 | func.applyParam(user, param, value); |
| 256 | if (punchThrough) { |
| 257 | XosManager.INSTANCE.apply(func, user); |
| 258 | } |
| 259 | } |
| 260 | |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 261 | private ArrayNode userJsonArray() { |
| 262 | ArrayNode userList = arrayNode(); |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 263 | for (SubscriberUser user: userMap.values()) { |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 264 | userList.add(UserFactory.toObjectNode(user)); |
| 265 | } |
| 266 | return userList; |
| 267 | } |
| 268 | |
| 269 | // ============= generate JSON for GUI rest calls.. |
| 270 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 271 | private void addSubId(ObjectNode root) { |
| 272 | root.put(SUB_ID, subscriberId); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame^] | 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(); |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 294 | /** |
| 295 | * Returns the dashboard page data as JSON. |
| 296 | * |
| 297 | * @return dashboard page JSON data |
| 298 | */ |
| 299 | public String jsonDashboard() { |
| 300 | ObjectNode root = objectNode(); |
| 301 | root.put(BUNDLE, currentBundle.descriptor().displayName()); |
| 302 | root.set(USERS, userJsonArray()); |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 303 | addSubId(root); |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 304 | return root.toString(); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Returns the bundle page data as JSON. |
| 309 | * |
| 310 | * @return bundle page JSON data |
| 311 | */ |
| 312 | public String jsonBundle() { |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 313 | ObjectNode root = BundleFactory.toObjectNode(currentBundle); |
| 314 | addSubId(root); |
| 315 | return root.toString(); |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Returns the users page data as JSON. |
| 320 | * |
| 321 | * @return users page JSON data |
| 322 | */ |
| 323 | public String jsonUsers() { |
| 324 | ObjectNode root = objectNode(); |
| 325 | root.set(USERS, userJsonArray()); |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 326 | addSubId(root); |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 327 | return root.toString(); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Singleton instance. |
| 332 | */ |
| 333 | public static final CordModelCache INSTANCE = new CordModelCache(); |
Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 334 | } |