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 | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 35 | import java.util.List; |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 36 | import java.util.Map; |
| 37 | import java.util.TreeMap; |
| 38 | |
| 39 | import static com.google.common.base.Preconditions.checkNotNull; |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 40 | import static org.onosproject.cord.gui.model.XosFunctionDescriptor.URL_FILTER; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 41 | |
Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 42 | /** |
| 43 | * In memory cache of the model of the subscriber's account. |
| 44 | */ |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 45 | public class CordModelCache extends JsonFactory { |
| 46 | |
| 47 | private static final String BUNDLE = "bundle"; |
| 48 | private static final String USERS = "users"; |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 49 | private static final String SUB_ID = "subId"; |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 50 | private static final String LEVEL = "level"; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 51 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 52 | private int subscriberId; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 53 | private Bundle currentBundle; |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 54 | |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame^] | 55 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 56 | |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 57 | // NOTE: use a tree map to maintain sorted order by user ID |
| 58 | private final Map<Integer, SubscriberUser> userMap = |
| 59 | new TreeMap<Integer, SubscriberUser>(); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 62 | * Constructs a model cache, (retrieving demo subscriber ID), |
| 63 | * initializing it with basic bundle, and fetching the list of users. |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 64 | */ |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 65 | CordModelCache() { |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame^] | 66 | log.info("Initialize model cache"); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 67 | subscriberId = XosManager.INSTANCE.initDemoSubscriber(); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 68 | currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 69 | initUsers(); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 72 | private void initUsers() { |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 73 | ArrayNode users = XosManager.INSTANCE.getUserList(); |
| 74 | for (JsonNode u: users) { |
| 75 | ObjectNode user = (ObjectNode) u; |
| 76 | |
| 77 | int id = user.get("id").asInt(); |
| 78 | String name = user.get("name").asText(); |
| 79 | String mac = user.get("mac").asText(); |
| 80 | String level = user.get("level").asText(); |
| 81 | |
| 82 | // NOTE: We are just storing the current "url-filter" level. |
| 83 | // Since we are starting with the BASIC bundle, (that does |
| 84 | // not include URL_FILTER), we don't yet have the URL_FILTER |
| 85 | // memento in which to store the level. |
| 86 | SubscriberUser su = createUser(id, name, mac, level); |
| 87 | userMap.put(id, su); |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame^] | 88 | log.info("..caching user {} (id:{})", name, id); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 89 | } |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 92 | private SubscriberUser createUser(int uid, String name, String mac, |
| 93 | String level) { |
| 94 | SubscriberUser user = new SubscriberUser(uid, name, mac, level); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 95 | for (XosFunction f: currentBundle.functions()) { |
| 96 | user.setMemento(f.descriptor(), f.createMemento()); |
| 97 | } |
| 98 | return user; |
| 99 | } |
| 100 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Returns the currently selected bundle. |
| 103 | * |
| 104 | * @return current bundle |
| 105 | */ |
| 106 | public Bundle getCurrentBundle() { |
| 107 | return currentBundle; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Sets a new bundle. |
| 112 | * |
| 113 | * @param bundleId bundle identifier |
| 114 | * @throws IllegalArgumentException if bundle ID is unknown |
| 115 | */ |
| 116 | public void setCurrentBundle(String bundleId) { |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame^] | 117 | log.info("set new bundle : {}", bundleId); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 118 | BundleDescriptor bd = BundleFactory.bundleFromId(bundleId); |
| 119 | currentBundle = new Bundle(bd); |
| 120 | // update the user mementos |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 121 | for (SubscriberUser user: userMap.values()) { |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 122 | user.clearMementos(); |
| 123 | for (XosFunction f: currentBundle.functions()) { |
| 124 | user.setMemento(f.descriptor(), f.createMemento()); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 125 | if (f.descriptor().equals(URL_FILTER)) { |
| 126 | applyUrlFilterLevel(user, user.urlFilterLevel()); |
| 127 | } |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 131 | XosManager.INSTANCE.setNewBundle(currentBundle); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 134 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Returns the list of current users for this subscriber account. |
| 137 | * |
| 138 | * @return the list of users |
| 139 | */ |
| 140 | public List<SubscriberUser> getUsers() { |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 141 | return ImmutableList.copyOf(userMap.values()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 142 | } |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 143 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 144 | /** |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 145 | * Applies a function parameter change for a user, pushing that |
| 146 | * change through to XOS. |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 147 | * |
| 148 | * @param userId user identifier |
| 149 | * @param funcId function identifier |
| 150 | * @param param function parameter to change |
| 151 | * @param value new value for function parameter |
| 152 | */ |
| 153 | public void applyPerUserParam(String userId, String funcId, |
| 154 | String param, String value) { |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 155 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 156 | int uid = Integer.parseInt(userId); |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 157 | SubscriberUser user = userMap.get(uid); |
| 158 | checkNotNull(user, "unknown user id: " + uid); |
| 159 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 160 | XosFunctionDescriptor xfd = |
| 161 | XosFunctionDescriptor.valueOf(funcId.toUpperCase()); |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 162 | |
| 163 | XosFunction func = currentBundle.findFunction(xfd); |
| 164 | checkNotNull(func, "function not part of bundle: " + funcId); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 165 | applyParam(func, user, param, value, true); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // ============= |
| 169 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 170 | private void applyUrlFilterLevel(SubscriberUser user, String level) { |
| 171 | XosFunction urlFilter = currentBundle.findFunction(URL_FILTER); |
| 172 | if (urlFilter != null) { |
| 173 | applyParam(urlFilter, user, LEVEL, level, false); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | private void applyParam(XosFunction func, SubscriberUser user, |
| 178 | String param, String value, boolean punchThrough) { |
| 179 | func.applyParam(user, param, value); |
| 180 | if (punchThrough) { |
| 181 | XosManager.INSTANCE.apply(func, user); |
| 182 | } |
| 183 | } |
| 184 | |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 185 | private ArrayNode userJsonArray() { |
| 186 | ArrayNode userList = arrayNode(); |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 187 | for (SubscriberUser user: userMap.values()) { |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 188 | userList.add(UserFactory.toObjectNode(user)); |
| 189 | } |
| 190 | return userList; |
| 191 | } |
| 192 | |
| 193 | // ============= generate JSON for GUI rest calls.. |
| 194 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 195 | private void addSubId(ObjectNode root) { |
| 196 | root.put(SUB_ID, subscriberId); |
| 197 | } |
| 198 | |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 199 | /** |
| 200 | * Returns the dashboard page data as JSON. |
| 201 | * |
| 202 | * @return dashboard page JSON data |
| 203 | */ |
| 204 | public String jsonDashboard() { |
| 205 | ObjectNode root = objectNode(); |
| 206 | root.put(BUNDLE, currentBundle.descriptor().displayName()); |
| 207 | root.set(USERS, userJsonArray()); |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 208 | addSubId(root); |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 209 | return root.toString(); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Returns the bundle page data as JSON. |
| 214 | * |
| 215 | * @return bundle page JSON data |
| 216 | */ |
| 217 | public String jsonBundle() { |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 218 | ObjectNode root = BundleFactory.toObjectNode(currentBundle); |
| 219 | addSubId(root); |
| 220 | return root.toString(); |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Returns the users page data as JSON. |
| 225 | * |
| 226 | * @return users page JSON data |
| 227 | */ |
| 228 | public String jsonUsers() { |
| 229 | ObjectNode root = objectNode(); |
| 230 | root.set(USERS, userJsonArray()); |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 231 | addSubId(root); |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 232 | return root.toString(); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Singleton instance. |
| 237 | */ |
| 238 | public static final CordModelCache INSTANCE = new CordModelCache(); |
Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 239 | } |