blob: 494fa2bb98bc9512b3c9b96d16e1ce2bbacbe06f [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";
Simon Huntee6a7372015-05-28 14:04:24 -070045 private static final String SUB_ID = "subId";
Simon Hunt09a32db2015-05-21 15:00:42 -070046
Simon Hunta29c87b2015-05-21 09:56:19 -070047
Simon Hunt41b943e2015-05-21 13:52:01 -070048 // faked for the demo
Simon Hunt41b943e2015-05-21 13:52:01 -070049 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
Simon Huntee6a7372015-05-28 14:04:24 -070054 private int subscriberId;
Simon Hunt41b943e2015-05-21 13:52:01 -070055 private Bundle currentBundle;
Simon Hunt87b157c2015-05-22 12:09:59 -070056
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 Hunt41b943e2015-05-21 13:52:01 -070060
61 /**
62 * Constructs a model cache, initializing it with basic bundle.
63 */
Simon Hunt09a32db2015-05-21 15:00:42 -070064 CordModelCache() {
Simon Hunt41b943e2015-05-21 13:52:01 -070065 currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE);
Simon Huntee6a7372015-05-28 14:04:24 -070066 subscriberId = XosManager.INSTANCE.getSubscriberId();
Simon Hunt41b943e2015-05-21 13:52:01 -070067 }
68
69 /**
70 * Used to initialize users for the demo. These are currently fake.
71 */
Simon Huntee6a7372015-05-28 14:04:24 -070072 @Deprecated
73 private void initUsers() {
Simon Hunt87b157c2015-05-22 12:09:59 -070074 userMap.put(1, createUser(1, "Mom's MacBook", MAC_1));
75 userMap.put(2, createUser(2, "Dad's iPad", MAC_2));
76 userMap.put(3, createUser(3, "Dick's laptop", MAC_3));
77 userMap.put(4, createUser(4, "Jane's laptop", MAC_4));
Simon Hunt41b943e2015-05-21 13:52:01 -070078 }
79
Simon Hunt6c2555b2015-05-21 18:17:56 -070080 private SubscriberUser createUser(int uid, String name, String mac) {
81 SubscriberUser user = new SubscriberUser(uid, name, mac);
82 for (XosFunction f: currentBundle.functions()) {
83 user.setMemento(f.descriptor(), f.createMemento());
84 }
85 return user;
86 }
87
Simon Hunt41b943e2015-05-21 13:52:01 -070088 /**
89 * Returns the currently selected bundle.
90 *
91 * @return current bundle
92 */
93 public Bundle getCurrentBundle() {
94 return currentBundle;
95 }
96
97 /**
98 * Sets a new bundle.
99 *
100 * @param bundleId bundle identifier
101 * @throws IllegalArgumentException if bundle ID is unknown
102 */
103 public void setCurrentBundle(String bundleId) {
Simon Hunt6c2555b2015-05-21 18:17:56 -0700104 BundleDescriptor bd = BundleFactory.bundleFromId(bundleId);
105 currentBundle = new Bundle(bd);
106 // update the user mementos
Simon Hunt87b157c2015-05-22 12:09:59 -0700107 for (SubscriberUser user: userMap.values()) {
Simon Hunt6c2555b2015-05-21 18:17:56 -0700108 user.clearMementos();
109 for (XosFunction f: currentBundle.functions()) {
110 user.setMemento(f.descriptor(), f.createMemento());
111 }
112 }
113
Simon Huntee6a7372015-05-28 14:04:24 -0700114 XosManager.INSTANCE.setNewBundle(subscriberId, currentBundle);
Simon Hunt41b943e2015-05-21 13:52:01 -0700115 }
116
Simon Hunt6c2555b2015-05-21 18:17:56 -0700117
Simon Hunt41b943e2015-05-21 13:52:01 -0700118 /**
119 * Returns the list of current users for this subscriber account.
120 *
121 * @return the list of users
122 */
123 public List<SubscriberUser> getUsers() {
Simon Hunt87b157c2015-05-22 12:09:59 -0700124 return ImmutableList.copyOf(userMap.values());
Simon Hunt41b943e2015-05-21 13:52:01 -0700125 }
Simon Hunt09a32db2015-05-21 15:00:42 -0700126
Simon Hunt6c2555b2015-05-21 18:17:56 -0700127 /**
128 * Applies a function parameter change for a user.
129 *
130 * @param userId user identifier
131 * @param funcId function identifier
132 * @param param function parameter to change
133 * @param value new value for function parameter
134 */
135 public void applyPerUserParam(String userId, String funcId,
136 String param, String value) {
Simon Hunt87b157c2015-05-22 12:09:59 -0700137
Simon Hunt6c2555b2015-05-21 18:17:56 -0700138 int uid = Integer.parseInt(userId);
Simon Hunt87b157c2015-05-22 12:09:59 -0700139 SubscriberUser user = userMap.get(uid);
140 checkNotNull(user, "unknown user id: " + uid);
141
Simon Hunt6c2555b2015-05-21 18:17:56 -0700142 XosFunctionDescriptor xfd =
143 XosFunctionDescriptor.valueOf(funcId.toUpperCase());
Simon Hunt87b157c2015-05-22 12:09:59 -0700144
145 XosFunction func = currentBundle.findFunction(xfd);
146 checkNotNull(func, "function not part of bundle: " + funcId);
147
148 func.applyParam(user, param, value);
Simon Huntee6a7372015-05-28 14:04:24 -0700149 XosManager.INSTANCE.apply(subscriberId, func, user);
Simon Hunt6c2555b2015-05-21 18:17:56 -0700150 }
151
152 // =============
153
Simon Hunt09a32db2015-05-21 15:00:42 -0700154 private ArrayNode userJsonArray() {
155 ArrayNode userList = arrayNode();
Simon Hunt87b157c2015-05-22 12:09:59 -0700156 for (SubscriberUser user: userMap.values()) {
Simon Hunt09a32db2015-05-21 15:00:42 -0700157 userList.add(UserFactory.toObjectNode(user));
158 }
159 return userList;
160 }
161
162 // ============= generate JSON for GUI rest calls..
163
Simon Huntee6a7372015-05-28 14:04:24 -0700164 private void addSubId(ObjectNode root) {
165 root.put(SUB_ID, subscriberId);
166 }
167
Simon Hunt09a32db2015-05-21 15:00:42 -0700168 /**
169 * Returns the dashboard page data as JSON.
170 *
171 * @return dashboard page JSON data
172 */
173 public String jsonDashboard() {
174 ObjectNode root = objectNode();
175 root.put(BUNDLE, currentBundle.descriptor().displayName());
176 root.set(USERS, userJsonArray());
Simon Huntee6a7372015-05-28 14:04:24 -0700177 addSubId(root);
Simon Hunt09a32db2015-05-21 15:00:42 -0700178 return root.toString();
179 }
180
181 /**
182 * Returns the bundle page data as JSON.
183 *
184 * @return bundle page JSON data
185 */
186 public String jsonBundle() {
Simon Huntee6a7372015-05-28 14:04:24 -0700187 ObjectNode root = BundleFactory.toObjectNode(currentBundle);
188 addSubId(root);
189 return root.toString();
Simon Hunt09a32db2015-05-21 15:00:42 -0700190 }
191
192 /**
193 * Returns the users page data as JSON.
194 *
195 * @return users page JSON data
196 */
197 public String jsonUsers() {
198 ObjectNode root = objectNode();
199 root.set(USERS, userJsonArray());
Simon Huntee6a7372015-05-28 14:04:24 -0700200 addSubId(root);
Simon Hunt09a32db2015-05-21 15:00:42 -0700201 return root.toString();
202 }
203
204 /**
205 * Singleton instance.
206 */
207 public static final CordModelCache INSTANCE = new CordModelCache();
Simon Hunta29c87b2015-05-21 09:56:19 -0700208}