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