blob: c71477f2487b2c4267405fa7a11b8dc4577f8f5b [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;
31import org.onosproject.cord.gui.model.XosFunctionFactory;
Simon Hunt41b943e2015-05-21 13:52:01 -070032
33import java.util.ArrayList;
34import java.util.List;
35
Simon Hunta29c87b2015-05-21 09:56:19 -070036/**
37 * In memory cache of the model of the subscriber's account.
38 */
Simon Hunt09a32db2015-05-21 15:00:42 -070039public class CordModelCache extends JsonFactory {
40
41 private static final String BUNDLE = "bundle";
42 private static final String USERS = "users";
43
Simon Hunta29c87b2015-05-21 09:56:19 -070044
Simon Hunt41b943e2015-05-21 13:52:01 -070045 // faked for the demo
46 private static final int SUBSCRIBER_ID = 92;
47 private static final String MAC_1 = "010203040506";
48 private static final String MAC_2 = "010203040507";
49 private static final String MAC_3 = "010203040508";
50 private static final String MAC_4 = "010203040509";
51
52 private Bundle currentBundle;
53 private final List<SubscriberUser> users;
54
55 /**
56 * Constructs a model cache, initializing it with basic bundle.
57 */
Simon Hunt09a32db2015-05-21 15:00:42 -070058 CordModelCache() {
Simon Hunt41b943e2015-05-21 13:52:01 -070059 currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE);
60 users = new ArrayList<SubscriberUser>();
61 initUsers();
62 }
63
64 /**
65 * Used to initialize users for the demo. These are currently fake.
66 */
67 public void initUsers() {
Simon Hunt6c2555b2015-05-21 18:17:56 -070068 users.add(createUser(1, "Mom's MacBook", MAC_1));
69 users.add(createUser(2, "Dad's iPad", MAC_2));
70 users.add(createUser(3, "Dick's laptop", MAC_3));
71 users.add(createUser(4, "Jane's laptop", MAC_4));
Simon Hunt41b943e2015-05-21 13:52:01 -070072 }
73
Simon Hunt6c2555b2015-05-21 18:17:56 -070074 private SubscriberUser createUser(int uid, String name, String mac) {
75 SubscriberUser user = new SubscriberUser(uid, name, mac);
76 for (XosFunction f: currentBundle.functions()) {
77 user.setMemento(f.descriptor(), f.createMemento());
78 }
79 return user;
80 }
81
82
Simon Hunt41b943e2015-05-21 13:52:01 -070083 /**
84 * Returns the currently selected bundle.
85 *
86 * @return current bundle
87 */
88 public Bundle getCurrentBundle() {
89 return currentBundle;
90 }
91
92 /**
93 * Sets a new bundle.
94 *
95 * @param bundleId bundle identifier
96 * @throws IllegalArgumentException if bundle ID is unknown
97 */
98 public void setCurrentBundle(String bundleId) {
Simon Hunt6c2555b2015-05-21 18:17:56 -070099 BundleDescriptor bd = BundleFactory.bundleFromId(bundleId);
100 currentBundle = new Bundle(bd);
101 // update the user mementos
102 for (SubscriberUser user: users) {
103 user.clearMementos();
104 for (XosFunction f: currentBundle.functions()) {
105 user.setMemento(f.descriptor(), f.createMemento());
106 }
107 }
108
109 // TODO: tell XOS which functions are enabled / disabled
Simon Hunt41b943e2015-05-21 13:52:01 -0700110 }
111
Simon Hunt6c2555b2015-05-21 18:17:56 -0700112
Simon Hunt41b943e2015-05-21 13:52:01 -0700113 /**
114 * Returns the list of current users for this subscriber account.
115 *
116 * @return the list of users
117 */
118 public List<SubscriberUser> getUsers() {
119 return ImmutableList.copyOf(users);
120 }
Simon Hunt09a32db2015-05-21 15:00:42 -0700121
Simon Hunt6c2555b2015-05-21 18:17:56 -0700122 /**
123 * Applies a function parameter change for a user.
124 *
125 * @param userId user identifier
126 * @param funcId function identifier
127 * @param param function parameter to change
128 * @param value new value for function parameter
129 */
130 public void applyPerUserParam(String userId, String funcId,
131 String param, String value) {
132 // FIXME: this is not right yet...
133 int uid = Integer.parseInt(userId);
134 XosFunctionDescriptor xfd =
135 XosFunctionDescriptor.valueOf(funcId.toUpperCase());
136 XosFunctionFactory.apply(xfd, uid, param, value);
137 }
138
139 // =============
140
Simon Hunt09a32db2015-05-21 15:00:42 -0700141 private ArrayNode userJsonArray() {
142 ArrayNode userList = arrayNode();
143 for (SubscriberUser user: users) {
144 userList.add(UserFactory.toObjectNode(user));
145 }
146 return userList;
147 }
148
149 // ============= generate JSON for GUI rest calls..
150
151 /**
152 * Returns the dashboard page data as JSON.
153 *
154 * @return dashboard page JSON data
155 */
156 public String jsonDashboard() {
157 ObjectNode root = objectNode();
158 root.put(BUNDLE, currentBundle.descriptor().displayName());
159 root.set(USERS, userJsonArray());
160 return root.toString();
161 }
162
163 /**
164 * Returns the bundle page data as JSON.
165 *
166 * @return bundle page JSON data
167 */
168 public String jsonBundle() {
169 return BundleFactory.toJson(currentBundle);
170 }
171
172 /**
173 * Returns the users page data as JSON.
174 *
175 * @return users page JSON data
176 */
177 public String jsonUsers() {
178 ObjectNode root = objectNode();
179 root.set(USERS, userJsonArray());
180 return root.toString();
181 }
182
183 /**
184 * Singleton instance.
185 */
186 public static final CordModelCache INSTANCE = new CordModelCache();
Simon Hunta29c87b2015-05-21 09:56:19 -0700187}