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"; |
Simon Hunt | 2739e6f8 | 2015-06-05 16:27:45 -0700 | [diff] [blame] | 50 | private static final String KEY_SSID = "service_specific_id"; |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 51 | 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 Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 62 | private static final String BUNDLE = "bundle"; |
| 63 | private static final String USERS = "users"; |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 64 | private static final String LEVEL = "level"; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 65 | |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 66 | private static final Map<Integer, Integer> LOOKUP = new HashMap<>(); |
| 67 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 68 | private int subscriberId; |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 69 | private int ssid; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 70 | private Bundle currentBundle; |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 71 | |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 72 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 73 | |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 74 | // 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 Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 77 | |
| 78 | /** |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 79 | * Constructs a model cache, retrieving a mapping of SSID to XOS Subscriber |
| 80 | * IDs from the XOS server. |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 81 | */ |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 82 | CordModelCache() { |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 83 | log.info("Initialize model cache"); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 84 | ObjectNode map = XosManager.INSTANCE.initXosSubscriberLookups(); |
| 85 | initLookupMap(map); |
| 86 | log.info("{} entries in SSID->SubID lookup map", LOOKUP.size()); |
Simon Hunt | 2739e6f8 | 2015-06-05 16:27:45 -0700 | [diff] [blame] | 87 | // force DEMO subscriber to be installed by default |
| 88 | init("foo@bar"); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | private void initLookupMap(ObjectNode map) { |
| 92 | ArrayNode array = (ArrayNode) map.get(KEY_SSID_MAP); |
| 93 | Iterator<JsonNode> iter = array.elements(); |
Simon Hunt | 2739e6f8 | 2015-06-05 16:27:45 -0700 | [diff] [blame] | 94 | StringBuilder msg = new StringBuilder(); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 95 | while (iter.hasNext()) { |
| 96 | ObjectNode node = (ObjectNode) iter.next(); |
Simon Hunt | 4236608 | 2015-06-08 09:57:05 -0700 | [diff] [blame^] | 97 | String ssidStr = node.get(KEY_SSID).asText(); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 98 | int ssid = Integer.valueOf(ssidStr); |
| 99 | int subId = node.get(KEY_SUB_ID).asInt(); |
| 100 | LOOKUP.put(ssid, subId); |
Simon Hunt | 2739e6f8 | 2015-06-05 16:27:45 -0700 | [diff] [blame] | 101 | msg.append(String.format("\n..binding SSID %s to sub-id %s", ssid, subId)); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 102 | } |
Simon Hunt | 2739e6f8 | 2015-06-05 16:27:45 -0700 | [diff] [blame] | 103 | log.info(msg.toString()); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 104 | } |
| 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 Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 142 | currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 143 | initUsers(); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 146 | private void initUsers() { |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 147 | ArrayNode users = XosManager.INSTANCE.getUserList(); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 148 | if (users == null) { |
| 149 | log.warn("no user list for SSID {} (subid {})", ssid, subscriberId); |
| 150 | return; |
| 151 | } |
| 152 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 153 | 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 Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 167 | log.info("..caching user {} (id:{})", name, id); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 168 | } |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 171 | private SubscriberUser createUser(int uid, String name, String mac, |
| 172 | String level) { |
| 173 | SubscriberUser user = new SubscriberUser(uid, name, mac, level); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 174 | for (XosFunction f: currentBundle.functions()) { |
| 175 | user.setMemento(f.descriptor(), f.createMemento()); |
| 176 | } |
| 177 | return user; |
| 178 | } |
| 179 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 180 | /** |
| 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 Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 196 | log.info("set new bundle : {}", bundleId); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 197 | BundleDescriptor bd = BundleFactory.bundleFromId(bundleId); |
| 198 | currentBundle = new Bundle(bd); |
| 199 | // update the user mementos |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 200 | for (SubscriberUser user: userMap.values()) { |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 201 | user.clearMementos(); |
| 202 | for (XosFunction f: currentBundle.functions()) { |
| 203 | user.setMemento(f.descriptor(), f.createMemento()); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 204 | if (f.descriptor().equals(URL_FILTER)) { |
| 205 | applyUrlFilterLevel(user, user.urlFilterLevel()); |
| 206 | } |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 210 | XosManager.INSTANCE.setNewBundle(currentBundle); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 213 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 214 | /** |
| 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 Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 220 | return ImmutableList.copyOf(userMap.values()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 221 | } |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 222 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 223 | /** |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 224 | * Applies a function parameter change for a user, pushing that |
| 225 | * change through to XOS. |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 226 | * |
| 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 Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 234 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 235 | int uid = Integer.parseInt(userId); |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 236 | SubscriberUser user = userMap.get(uid); |
| 237 | checkNotNull(user, "unknown user id: " + uid); |
| 238 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 239 | XosFunctionDescriptor xfd = |
| 240 | XosFunctionDescriptor.valueOf(funcId.toUpperCase()); |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 241 | |
| 242 | XosFunction func = currentBundle.findFunction(xfd); |
| 243 | checkNotNull(func, "function not part of bundle: " + funcId); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 244 | applyParam(func, user, param, value, true); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // ============= |
| 248 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 249 | 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 Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 264 | private ArrayNode userJsonArray() { |
| 265 | ArrayNode userList = arrayNode(); |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 266 | for (SubscriberUser user: userMap.values()) { |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 267 | userList.add(UserFactory.toObjectNode(user)); |
| 268 | } |
| 269 | return userList; |
| 270 | } |
| 271 | |
| 272 | // ============= generate JSON for GUI rest calls.. |
| 273 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 274 | private void addSubId(ObjectNode root) { |
| 275 | root.put(SUB_ID, subscriberId); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 276 | 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 Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 297 | /** |
| 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 Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 306 | addSubId(root); |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 307 | 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 Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 316 | ObjectNode root = BundleFactory.toObjectNode(currentBundle); |
| 317 | addSubId(root); |
| 318 | return root.toString(); |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 319 | } |
| 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 Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 329 | addSubId(root); |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 330 | return root.toString(); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Singleton instance. |
| 335 | */ |
| 336 | public static final CordModelCache INSTANCE = new CordModelCache(); |
Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 337 | } |