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; |
| 31 | import org.onosproject.cord.gui.model.XosFunctionFactory; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 32 | |
| 33 | import java.util.ArrayList; |
| 34 | import java.util.List; |
| 35 | |
Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 36 | /** |
| 37 | * In memory cache of the model of the subscriber's account. |
| 38 | */ |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 39 | public class CordModelCache extends JsonFactory { |
| 40 | |
| 41 | private static final String BUNDLE = "bundle"; |
| 42 | private static final String USERS = "users"; |
| 43 | |
Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 44 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 45 | // 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 Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 58 | CordModelCache() { |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 59 | 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 Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame^] | 68 | 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 Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame^] | 74 | 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 Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 83 | /** |
| 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 Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame^] | 99 | 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 Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame^] | 112 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 113 | /** |
| 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 Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 121 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame^] | 122 | /** |
| 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 Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 141 | 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 Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 187 | } |