blob: f660c64b13b06d9ae1b5ff877c3e99f22c2c5f46 [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 Hunt09a32db2015-05-21 15:00:42 -070020import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Hunt41b943e2015-05-21 13:52:01 -070022import com.google.common.collect.ImmutableList;
23import org.onosproject.cord.gui.model.Bundle;
24import org.onosproject.cord.gui.model.BundleDescriptor;
25import org.onosproject.cord.gui.model.BundleFactory;
Simon Hunt09a32db2015-05-21 15:00:42 -070026import org.onosproject.cord.gui.model.JsonFactory;
Simon Hunt41b943e2015-05-21 13:52:01 -070027import org.onosproject.cord.gui.model.SubscriberUser;
Simon Hunt09a32db2015-05-21 15:00:42 -070028import org.onosproject.cord.gui.model.UserFactory;
Simon Hunt6c2555b2015-05-21 18:17:56 -070029import org.onosproject.cord.gui.model.XosFunction;
30import org.onosproject.cord.gui.model.XosFunctionDescriptor;
Simon Hunt41b943e2015-05-21 13:52:01 -070031
Simon Hunt41b943e2015-05-21 13:52:01 -070032import java.util.List;
Simon Hunt87b157c2015-05-22 12:09:59 -070033import java.util.Map;
34import java.util.TreeMap;
35
36import static com.google.common.base.Preconditions.checkNotNull;
Simon Hunt41b943e2015-05-21 13:52:01 -070037
Simon Hunta29c87b2015-05-21 09:56:19 -070038/**
39 * In memory cache of the model of the subscriber's account.
40 */
Simon Hunt09a32db2015-05-21 15:00:42 -070041public class CordModelCache extends JsonFactory {
42
43 private static final String BUNDLE = "bundle";
44 private static final String USERS = "users";
45
Simon Hunta29c87b2015-05-21 09:56:19 -070046
Simon Hunt41b943e2015-05-21 13:52:01 -070047 // faked for the demo
48 private static final int SUBSCRIBER_ID = 92;
49 private static final String MAC_1 = "010203040506";
50 private static final String MAC_2 = "010203040507";
51 private static final String MAC_3 = "010203040508";
52 private static final String MAC_4 = "010203040509";
53
54 private Bundle currentBundle;
Simon Hunt87b157c2015-05-22 12:09:59 -070055
56 // NOTE: use a tree map to maintain sorted order by user ID
57 private final Map<Integer, SubscriberUser> userMap =
58 new TreeMap<Integer, SubscriberUser>();
Simon Hunt41b943e2015-05-21 13:52:01 -070059
60 /**
61 * Constructs a model cache, initializing it with basic bundle.
62 */
Simon Hunt09a32db2015-05-21 15:00:42 -070063 CordModelCache() {
Simon Hunt41b943e2015-05-21 13:52:01 -070064 currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE);
Simon Hunt41b943e2015-05-21 13:52:01 -070065 initUsers();
66 }
67
68 /**
69 * Used to initialize users for the demo. These are currently fake.
70 */
71 public void initUsers() {
Simon Hunt87b157c2015-05-22 12:09:59 -070072 userMap.put(1, createUser(1, "Mom's MacBook", MAC_1));
73 userMap.put(2, createUser(2, "Dad's iPad", MAC_2));
74 userMap.put(3, createUser(3, "Dick's laptop", MAC_3));
75 userMap.put(4, createUser(4, "Jane's laptop", MAC_4));
Simon Hunt41b943e2015-05-21 13:52:01 -070076 }
77
Simon Hunt6c2555b2015-05-21 18:17:56 -070078 private SubscriberUser createUser(int uid, String name, String mac) {
79 SubscriberUser user = new SubscriberUser(uid, name, mac);
80 for (XosFunction f: currentBundle.functions()) {
81 user.setMemento(f.descriptor(), f.createMemento());
82 }
83 return user;
84 }
85
Simon Hunt41b943e2015-05-21 13:52:01 -070086 /**
87 * Returns the currently selected bundle.
88 *
89 * @return current bundle
90 */
91 public Bundle getCurrentBundle() {
92 return currentBundle;
93 }
94
95 /**
96 * Sets a new bundle.
97 *
98 * @param bundleId bundle identifier
99 * @throws IllegalArgumentException if bundle ID is unknown
100 */
101 public void setCurrentBundle(String bundleId) {
Simon Hunt6c2555b2015-05-21 18:17:56 -0700102 BundleDescriptor bd = BundleFactory.bundleFromId(bundleId);
103 currentBundle = new Bundle(bd);
104 // update the user mementos
Simon Hunt87b157c2015-05-22 12:09:59 -0700105 for (SubscriberUser user: userMap.values()) {
Simon Hunt6c2555b2015-05-21 18:17:56 -0700106 user.clearMementos();
107 for (XosFunction f: currentBundle.functions()) {
108 user.setMemento(f.descriptor(), f.createMemento());
109 }
110 }
111
Simon Hunta00b0ce2015-05-22 15:57:11 -0700112 XosManager.INSTANCE.setNewBundle(SUBSCRIBER_ID, currentBundle);
Simon Hunt41b943e2015-05-21 13:52:01 -0700113 }
114
Simon Hunt6c2555b2015-05-21 18:17:56 -0700115
Simon Hunt41b943e2015-05-21 13:52:01 -0700116 /**
117 * Returns the list of current users for this subscriber account.
118 *
119 * @return the list of users
120 */
121 public List<SubscriberUser> getUsers() {
Simon Hunt87b157c2015-05-22 12:09:59 -0700122 return ImmutableList.copyOf(userMap.values());
Simon Hunt41b943e2015-05-21 13:52:01 -0700123 }
Simon Hunt09a32db2015-05-21 15:00:42 -0700124
Simon Hunt6c2555b2015-05-21 18:17:56 -0700125 /**
126 * Applies a function parameter change for a user.
127 *
128 * @param userId user identifier
129 * @param funcId function identifier
130 * @param param function parameter to change
131 * @param value new value for function parameter
132 */
133 public void applyPerUserParam(String userId, String funcId,
134 String param, String value) {
Simon Hunt87b157c2015-05-22 12:09:59 -0700135
Simon Hunt6c2555b2015-05-21 18:17:56 -0700136 int uid = Integer.parseInt(userId);
Simon Hunt87b157c2015-05-22 12:09:59 -0700137 SubscriberUser user = userMap.get(uid);
138 checkNotNull(user, "unknown user id: " + uid);
139
Simon Hunt6c2555b2015-05-21 18:17:56 -0700140 XosFunctionDescriptor xfd =
141 XosFunctionDescriptor.valueOf(funcId.toUpperCase());
Simon Hunt87b157c2015-05-22 12:09:59 -0700142
143 XosFunction func = currentBundle.findFunction(xfd);
144 checkNotNull(func, "function not part of bundle: " + funcId);
145
146 func.applyParam(user, param, value);
Simon Hunta00b0ce2015-05-22 15:57:11 -0700147 XosManager.INSTANCE.apply(SUBSCRIBER_ID, func, user);
Simon Hunt6c2555b2015-05-21 18:17:56 -0700148 }
149
150 // =============
151
Simon Hunt09a32db2015-05-21 15:00:42 -0700152 private ArrayNode userJsonArray() {
153 ArrayNode userList = arrayNode();
Simon Hunt87b157c2015-05-22 12:09:59 -0700154 for (SubscriberUser user: userMap.values()) {
Simon Hunt09a32db2015-05-21 15:00:42 -0700155 userList.add(UserFactory.toObjectNode(user));
156 }
157 return userList;
158 }
159
160 // ============= generate JSON for GUI rest calls..
161
162 /**
163 * Returns the dashboard page data as JSON.
164 *
165 * @return dashboard page JSON data
166 */
167 public String jsonDashboard() {
168 ObjectNode root = objectNode();
169 root.put(BUNDLE, currentBundle.descriptor().displayName());
170 root.set(USERS, userJsonArray());
171 return root.toString();
172 }
173
174 /**
175 * Returns the bundle page data as JSON.
176 *
177 * @return bundle page JSON data
178 */
179 public String jsonBundle() {
180 return BundleFactory.toJson(currentBundle);
181 }
182
183 /**
184 * Returns the users page data as JSON.
185 *
186 * @return users page JSON data
187 */
188 public String jsonUsers() {
189 ObjectNode root = objectNode();
190 root.set(USERS, userJsonArray());
191 return root.toString();
192 }
193
194 /**
195 * Singleton instance.
196 */
197 public static final CordModelCache INSTANCE = new CordModelCache();
Simon Hunta29c87b2015-05-21 09:56:19 -0700198}