Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package org.onosproject.cord.gui; |
| 19 | |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
| 23 | import org.onosproject.cord.gui.model.Bundle; |
| 24 | import org.onosproject.cord.gui.model.BundleDescriptor; |
| 25 | import org.onosproject.cord.gui.model.BundleFactory; |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 26 | import org.onosproject.cord.gui.model.JsonFactory; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 27 | import org.onosproject.cord.gui.model.SubscriberUser; |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 28 | import org.onosproject.cord.gui.model.UserFactory; |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 29 | import org.onosproject.cord.gui.model.XosFunction; |
| 30 | import org.onosproject.cord.gui.model.XosFunctionDescriptor; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 31 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 32 | import java.util.List; |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 33 | import java.util.Map; |
| 34 | import java.util.TreeMap; |
| 35 | |
| 36 | import static com.google.common.base.Preconditions.checkNotNull; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 37 | |
Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 38 | /** |
| 39 | * In memory cache of the model of the subscriber's account. |
| 40 | */ |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 41 | public class CordModelCache extends JsonFactory { |
| 42 | |
| 43 | private static final String BUNDLE = "bundle"; |
| 44 | private static final String USERS = "users"; |
| 45 | |
Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 46 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 47 | // 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 Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 55 | |
| 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 Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Constructs a model cache, initializing it with basic bundle. |
| 62 | */ |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 63 | CordModelCache() { |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 64 | currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 65 | initUsers(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Used to initialize users for the demo. These are currently fake. |
| 70 | */ |
| 71 | public void initUsers() { |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 72 | 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 Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 78 | 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 Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 86 | /** |
| 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 Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 102 | BundleDescriptor bd = BundleFactory.bundleFromId(bundleId); |
| 103 | currentBundle = new Bundle(bd); |
| 104 | // update the user mementos |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 105 | for (SubscriberUser user: userMap.values()) { |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 106 | user.clearMementos(); |
| 107 | for (XosFunction f: currentBundle.functions()) { |
| 108 | user.setMemento(f.descriptor(), f.createMemento()); |
| 109 | } |
| 110 | } |
| 111 | |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 112 | XosManager.INSTANCE.setNewBundle(SUBSCRIBER_ID, currentBundle); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 115 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 116 | /** |
| 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 Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 122 | return ImmutableList.copyOf(userMap.values()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 123 | } |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 124 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 125 | /** |
| 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 Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 135 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 136 | int uid = Integer.parseInt(userId); |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 137 | SubscriberUser user = userMap.get(uid); |
| 138 | checkNotNull(user, "unknown user id: " + uid); |
| 139 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 140 | XosFunctionDescriptor xfd = |
| 141 | XosFunctionDescriptor.valueOf(funcId.toUpperCase()); |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 142 | |
| 143 | XosFunction func = currentBundle.findFunction(xfd); |
| 144 | checkNotNull(func, "function not part of bundle: " + funcId); |
| 145 | |
| 146 | func.applyParam(user, param, value); |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 147 | XosManager.INSTANCE.apply(SUBSCRIBER_ID, func, user); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // ============= |
| 151 | |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 152 | private ArrayNode userJsonArray() { |
| 153 | ArrayNode userList = arrayNode(); |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 154 | for (SubscriberUser user: userMap.values()) { |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 155 | 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 Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 198 | } |