blob: 789737b0c630dfcc8e7ff360a37fc930845e329f [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 Hunt41b943e2015-05-21 13:52:01 -070035import java.util.List;
Simon Hunt87b157c2015-05-22 12:09:59 -070036import java.util.Map;
37import java.util.TreeMap;
38
39import static com.google.common.base.Preconditions.checkNotNull;
Simon Hunt7d02c082015-05-29 12:17:09 -070040import static org.onosproject.cord.gui.model.XosFunctionDescriptor.URL_FILTER;
Simon Hunt41b943e2015-05-21 13:52:01 -070041
Simon Hunta29c87b2015-05-21 09:56:19 -070042/**
43 * In memory cache of the model of the subscriber's account.
44 */
Simon Hunt09a32db2015-05-21 15:00:42 -070045public class CordModelCache extends JsonFactory {
46
47 private static final String BUNDLE = "bundle";
48 private static final String USERS = "users";
Simon Huntee6a7372015-05-28 14:04:24 -070049 private static final String SUB_ID = "subId";
Simon Hunt7d02c082015-05-29 12:17:09 -070050 private static final String LEVEL = "level";
Simon Hunt41b943e2015-05-21 13:52:01 -070051
Simon Huntee6a7372015-05-28 14:04:24 -070052 private int subscriberId;
Simon Hunt41b943e2015-05-21 13:52:01 -070053 private Bundle currentBundle;
Simon Hunt87b157c2015-05-22 12:09:59 -070054
Simon Huntb1246412015-06-01 13:37:26 -070055 private final Logger log = LoggerFactory.getLogger(getClass());
56
Simon Hunt87b157c2015-05-22 12:09:59 -070057 // 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 Hunt41b943e2015-05-21 13:52:01 -070060
61 /**
Simon Hunt7d02c082015-05-29 12:17:09 -070062 * Constructs a model cache, (retrieving demo subscriber ID),
63 * initializing it with basic bundle, and fetching the list of users.
Simon Hunt41b943e2015-05-21 13:52:01 -070064 */
Simon Hunt09a32db2015-05-21 15:00:42 -070065 CordModelCache() {
Simon Huntb1246412015-06-01 13:37:26 -070066 log.info("Initialize model cache");
Simon Hunt7d02c082015-05-29 12:17:09 -070067 subscriberId = XosManager.INSTANCE.initDemoSubscriber();
Simon Hunt41b943e2015-05-21 13:52:01 -070068 currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE);
Simon Hunt7d02c082015-05-29 12:17:09 -070069 initUsers();
Simon Hunt41b943e2015-05-21 13:52:01 -070070 }
71
Simon Huntee6a7372015-05-28 14:04:24 -070072 private void initUsers() {
Simon Hunt7d02c082015-05-29 12:17:09 -070073 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 Huntb1246412015-06-01 13:37:26 -070088 log.info("..caching user {} (id:{})", name, id);
Simon Hunt7d02c082015-05-29 12:17:09 -070089 }
Simon Hunt41b943e2015-05-21 13:52:01 -070090 }
91
Simon Hunt7d02c082015-05-29 12:17:09 -070092 private SubscriberUser createUser(int uid, String name, String mac,
93 String level) {
94 SubscriberUser user = new SubscriberUser(uid, name, mac, level);
Simon Hunt6c2555b2015-05-21 18:17:56 -070095 for (XosFunction f: currentBundle.functions()) {
96 user.setMemento(f.descriptor(), f.createMemento());
97 }
98 return user;
99 }
100
Simon Hunt41b943e2015-05-21 13:52:01 -0700101 /**
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 Huntb1246412015-06-01 13:37:26 -0700117 log.info("set new bundle : {}", bundleId);
Simon Hunt6c2555b2015-05-21 18:17:56 -0700118 BundleDescriptor bd = BundleFactory.bundleFromId(bundleId);
119 currentBundle = new Bundle(bd);
120 // update the user mementos
Simon Hunt87b157c2015-05-22 12:09:59 -0700121 for (SubscriberUser user: userMap.values()) {
Simon Hunt6c2555b2015-05-21 18:17:56 -0700122 user.clearMementos();
123 for (XosFunction f: currentBundle.functions()) {
124 user.setMemento(f.descriptor(), f.createMemento());
Simon Hunt7d02c082015-05-29 12:17:09 -0700125 if (f.descriptor().equals(URL_FILTER)) {
126 applyUrlFilterLevel(user, user.urlFilterLevel());
127 }
Simon Hunt6c2555b2015-05-21 18:17:56 -0700128 }
129 }
130
Simon Hunt7d02c082015-05-29 12:17:09 -0700131 XosManager.INSTANCE.setNewBundle(currentBundle);
Simon Hunt41b943e2015-05-21 13:52:01 -0700132 }
133
Simon Hunt6c2555b2015-05-21 18:17:56 -0700134
Simon Hunt41b943e2015-05-21 13:52:01 -0700135 /**
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 Hunt87b157c2015-05-22 12:09:59 -0700141 return ImmutableList.copyOf(userMap.values());
Simon Hunt41b943e2015-05-21 13:52:01 -0700142 }
Simon Hunt09a32db2015-05-21 15:00:42 -0700143
Simon Hunt6c2555b2015-05-21 18:17:56 -0700144 /**
Simon Hunt7d02c082015-05-29 12:17:09 -0700145 * Applies a function parameter change for a user, pushing that
146 * change through to XOS.
Simon Hunt6c2555b2015-05-21 18:17:56 -0700147 *
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 Hunt87b157c2015-05-22 12:09:59 -0700155
Simon Hunt6c2555b2015-05-21 18:17:56 -0700156 int uid = Integer.parseInt(userId);
Simon Hunt87b157c2015-05-22 12:09:59 -0700157 SubscriberUser user = userMap.get(uid);
158 checkNotNull(user, "unknown user id: " + uid);
159
Simon Hunt6c2555b2015-05-21 18:17:56 -0700160 XosFunctionDescriptor xfd =
161 XosFunctionDescriptor.valueOf(funcId.toUpperCase());
Simon Hunt87b157c2015-05-22 12:09:59 -0700162
163 XosFunction func = currentBundle.findFunction(xfd);
164 checkNotNull(func, "function not part of bundle: " + funcId);
Simon Hunt7d02c082015-05-29 12:17:09 -0700165 applyParam(func, user, param, value, true);
Simon Hunt6c2555b2015-05-21 18:17:56 -0700166 }
167
168 // =============
169
Simon Hunt7d02c082015-05-29 12:17:09 -0700170 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 Hunt09a32db2015-05-21 15:00:42 -0700185 private ArrayNode userJsonArray() {
186 ArrayNode userList = arrayNode();
Simon Hunt87b157c2015-05-22 12:09:59 -0700187 for (SubscriberUser user: userMap.values()) {
Simon Hunt09a32db2015-05-21 15:00:42 -0700188 userList.add(UserFactory.toObjectNode(user));
189 }
190 return userList;
191 }
192
193 // ============= generate JSON for GUI rest calls..
194
Simon Huntee6a7372015-05-28 14:04:24 -0700195 private void addSubId(ObjectNode root) {
196 root.put(SUB_ID, subscriberId);
197 }
198
Simon Hunt09a32db2015-05-21 15:00:42 -0700199 /**
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 Huntee6a7372015-05-28 14:04:24 -0700208 addSubId(root);
Simon Hunt09a32db2015-05-21 15:00:42 -0700209 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 Huntee6a7372015-05-28 14:04:24 -0700218 ObjectNode root = BundleFactory.toObjectNode(currentBundle);
219 addSubId(root);
220 return root.toString();
Simon Hunt09a32db2015-05-21 15:00:42 -0700221 }
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 Huntee6a7372015-05-28 14:04:24 -0700231 addSubId(root);
Simon Hunt09a32db2015-05-21 15:00:42 -0700232 return root.toString();
233 }
234
235 /**
236 * Singleton instance.
237 */
238 public static final CordModelCache INSTANCE = new CordModelCache();
Simon Hunta29c87b2015-05-21 09:56:19 -0700239}