blob: f8979794dea3d458ea9bc9ad25bda26e52e77f9f [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 Hunt41b943e2015-05-21 13:52:01 -070032
Simon Hunt41b943e2015-05-21 13:52:01 -070033import java.util.List;
Simon Hunt87b157c2015-05-22 12:09:59 -070034import java.util.Map;
35import java.util.TreeMap;
36
37import static com.google.common.base.Preconditions.checkNotNull;
Simon Hunt7d02c082015-05-29 12:17:09 -070038import static org.onosproject.cord.gui.model.XosFunctionDescriptor.URL_FILTER;
Simon Hunt41b943e2015-05-21 13:52:01 -070039
Simon Hunta29c87b2015-05-21 09:56:19 -070040/**
41 * In memory cache of the model of the subscriber's account.
42 */
Simon Hunt09a32db2015-05-21 15:00:42 -070043public class CordModelCache extends JsonFactory {
44
45 private static final String BUNDLE = "bundle";
46 private static final String USERS = "users";
Simon Huntee6a7372015-05-28 14:04:24 -070047 private static final String SUB_ID = "subId";
Simon Hunt7d02c082015-05-29 12:17:09 -070048 private static final String LEVEL = "level";
Simon Hunt41b943e2015-05-21 13:52:01 -070049
Simon Huntee6a7372015-05-28 14:04:24 -070050 private int subscriberId;
Simon Hunt41b943e2015-05-21 13:52:01 -070051 private Bundle currentBundle;
Simon Hunt87b157c2015-05-22 12:09:59 -070052
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 Hunt41b943e2015-05-21 13:52:01 -070056
57 /**
Simon Hunt7d02c082015-05-29 12:17:09 -070058 * Constructs a model cache, (retrieving demo subscriber ID),
59 * initializing it with basic bundle, and fetching the list of users.
Simon Hunt41b943e2015-05-21 13:52:01 -070060 */
Simon Hunt09a32db2015-05-21 15:00:42 -070061 CordModelCache() {
Simon Hunt7d02c082015-05-29 12:17:09 -070062 subscriberId = XosManager.INSTANCE.initDemoSubscriber();
Simon Hunt41b943e2015-05-21 13:52:01 -070063 currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE);
Simon Hunt7d02c082015-05-29 12:17:09 -070064 initUsers();
Simon Hunt41b943e2015-05-21 13:52:01 -070065 }
66
Simon Huntee6a7372015-05-28 14:04:24 -070067 private void initUsers() {
Simon Hunt7d02c082015-05-29 12:17:09 -070068 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 Hunt41b943e2015-05-21 13:52:01 -070084 }
85
Simon Hunt7d02c082015-05-29 12:17:09 -070086 private SubscriberUser createUser(int uid, String name, String mac,
87 String level) {
88 SubscriberUser user = new SubscriberUser(uid, name, mac, level);
Simon Hunt6c2555b2015-05-21 18:17:56 -070089 for (XosFunction f: currentBundle.functions()) {
90 user.setMemento(f.descriptor(), f.createMemento());
91 }
92 return user;
93 }
94
Simon Hunt41b943e2015-05-21 13:52:01 -070095 /**
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 Hunt6c2555b2015-05-21 18:17:56 -0700111 BundleDescriptor bd = BundleFactory.bundleFromId(bundleId);
112 currentBundle = new Bundle(bd);
113 // update the user mementos
Simon Hunt87b157c2015-05-22 12:09:59 -0700114 for (SubscriberUser user: userMap.values()) {
Simon Hunt6c2555b2015-05-21 18:17:56 -0700115 user.clearMementos();
116 for (XosFunction f: currentBundle.functions()) {
117 user.setMemento(f.descriptor(), f.createMemento());
Simon Hunt7d02c082015-05-29 12:17:09 -0700118 if (f.descriptor().equals(URL_FILTER)) {
119 applyUrlFilterLevel(user, user.urlFilterLevel());
120 }
Simon Hunt6c2555b2015-05-21 18:17:56 -0700121 }
122 }
123
Simon Hunt7d02c082015-05-29 12:17:09 -0700124 XosManager.INSTANCE.setNewBundle(currentBundle);
Simon Hunt41b943e2015-05-21 13:52:01 -0700125 }
126
Simon Hunt6c2555b2015-05-21 18:17:56 -0700127
Simon Hunt41b943e2015-05-21 13:52:01 -0700128 /**
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 Hunt87b157c2015-05-22 12:09:59 -0700134 return ImmutableList.copyOf(userMap.values());
Simon Hunt41b943e2015-05-21 13:52:01 -0700135 }
Simon Hunt09a32db2015-05-21 15:00:42 -0700136
Simon Hunt6c2555b2015-05-21 18:17:56 -0700137 /**
Simon Hunt7d02c082015-05-29 12:17:09 -0700138 * Applies a function parameter change for a user, pushing that
139 * change through to XOS.
Simon Hunt6c2555b2015-05-21 18:17:56 -0700140 *
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 Hunt87b157c2015-05-22 12:09:59 -0700148
Simon Hunt6c2555b2015-05-21 18:17:56 -0700149 int uid = Integer.parseInt(userId);
Simon Hunt87b157c2015-05-22 12:09:59 -0700150 SubscriberUser user = userMap.get(uid);
151 checkNotNull(user, "unknown user id: " + uid);
152
Simon Hunt6c2555b2015-05-21 18:17:56 -0700153 XosFunctionDescriptor xfd =
154 XosFunctionDescriptor.valueOf(funcId.toUpperCase());
Simon Hunt87b157c2015-05-22 12:09:59 -0700155
156 XosFunction func = currentBundle.findFunction(xfd);
157 checkNotNull(func, "function not part of bundle: " + funcId);
Simon Hunt7d02c082015-05-29 12:17:09 -0700158 applyParam(func, user, param, value, true);
Simon Hunt6c2555b2015-05-21 18:17:56 -0700159 }
160
161 // =============
162
Simon Hunt7d02c082015-05-29 12:17:09 -0700163 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 Hunt09a32db2015-05-21 15:00:42 -0700178 private ArrayNode userJsonArray() {
179 ArrayNode userList = arrayNode();
Simon Hunt87b157c2015-05-22 12:09:59 -0700180 for (SubscriberUser user: userMap.values()) {
Simon Hunt09a32db2015-05-21 15:00:42 -0700181 userList.add(UserFactory.toObjectNode(user));
182 }
183 return userList;
184 }
185
186 // ============= generate JSON for GUI rest calls..
187
Simon Huntee6a7372015-05-28 14:04:24 -0700188 private void addSubId(ObjectNode root) {
189 root.put(SUB_ID, subscriberId);
190 }
191
Simon Hunt09a32db2015-05-21 15:00:42 -0700192 /**
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 Huntee6a7372015-05-28 14:04:24 -0700201 addSubId(root);
Simon Hunt09a32db2015-05-21 15:00:42 -0700202 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 Huntee6a7372015-05-28 14:04:24 -0700211 ObjectNode root = BundleFactory.toObjectNode(currentBundle);
212 addSubId(root);
213 return root.toString();
Simon Hunt09a32db2015-05-21 15:00:42 -0700214 }
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 Huntee6a7372015-05-28 14:04:24 -0700224 addSubId(root);
Simon Hunt09a32db2015-05-21 15:00:42 -0700225 return root.toString();
226 }
227
228 /**
229 * Singleton instance.
230 */
231 public static final CordModelCache INSTANCE = new CordModelCache();
Simon Hunta29c87b2015-05-21 09:56:19 -0700232}