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 | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 29 | |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
| 32 | |
Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 33 | /** |
| 34 | * In memory cache of the model of the subscriber's account. |
| 35 | */ |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame^] | 36 | public class CordModelCache extends JsonFactory { |
| 37 | |
| 38 | private static final String BUNDLE = "bundle"; |
| 39 | private static final String USERS = "users"; |
| 40 | |
Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 41 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 42 | // faked for the demo |
| 43 | private static final int SUBSCRIBER_ID = 92; |
| 44 | private static final String MAC_1 = "010203040506"; |
| 45 | private static final String MAC_2 = "010203040507"; |
| 46 | private static final String MAC_3 = "010203040508"; |
| 47 | private static final String MAC_4 = "010203040509"; |
| 48 | |
| 49 | private Bundle currentBundle; |
| 50 | private final List<SubscriberUser> users; |
| 51 | |
| 52 | /** |
| 53 | * Constructs a model cache, initializing it with basic bundle. |
| 54 | */ |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame^] | 55 | CordModelCache() { |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 56 | currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE); |
| 57 | users = new ArrayList<SubscriberUser>(); |
| 58 | initUsers(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Used to initialize users for the demo. These are currently fake. |
| 63 | */ |
| 64 | public void initUsers() { |
| 65 | users.add(new SubscriberUser(1, "Mom's MacBook", MAC_1)); |
| 66 | users.add(new SubscriberUser(2, "Dad's iPad", MAC_2)); |
| 67 | users.add(new SubscriberUser(3, "Dick's laptop", MAC_3)); |
| 68 | users.add(new SubscriberUser(4, "Jane's laptop", MAC_4)); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns the currently selected bundle. |
| 73 | * |
| 74 | * @return current bundle |
| 75 | */ |
| 76 | public Bundle getCurrentBundle() { |
| 77 | return currentBundle; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Sets a new bundle. |
| 82 | * |
| 83 | * @param bundleId bundle identifier |
| 84 | * @throws IllegalArgumentException if bundle ID is unknown |
| 85 | */ |
| 86 | public void setCurrentBundle(String bundleId) { |
| 87 | BundleDescriptor bdesc = BundleFactory.bundleFromId(bundleId); |
| 88 | currentBundle = new Bundle(bdesc); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns the list of current users for this subscriber account. |
| 93 | * |
| 94 | * @return the list of users |
| 95 | */ |
| 96 | public List<SubscriberUser> getUsers() { |
| 97 | return ImmutableList.copyOf(users); |
| 98 | } |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame^] | 99 | |
| 100 | private ArrayNode userJsonArray() { |
| 101 | ArrayNode userList = arrayNode(); |
| 102 | for (SubscriberUser user: users) { |
| 103 | userList.add(UserFactory.toObjectNode(user)); |
| 104 | } |
| 105 | return userList; |
| 106 | } |
| 107 | |
| 108 | // ============= generate JSON for GUI rest calls.. |
| 109 | |
| 110 | /** |
| 111 | * Returns the dashboard page data as JSON. |
| 112 | * |
| 113 | * @return dashboard page JSON data |
| 114 | */ |
| 115 | public String jsonDashboard() { |
| 116 | ObjectNode root = objectNode(); |
| 117 | root.put(BUNDLE, currentBundle.descriptor().displayName()); |
| 118 | root.set(USERS, userJsonArray()); |
| 119 | return root.toString(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Returns the bundle page data as JSON. |
| 124 | * |
| 125 | * @return bundle page JSON data |
| 126 | */ |
| 127 | public String jsonBundle() { |
| 128 | return BundleFactory.toJson(currentBundle); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Returns the users page data as JSON. |
| 133 | * |
| 134 | * @return users page JSON data |
| 135 | */ |
| 136 | public String jsonUsers() { |
| 137 | ObjectNode root = objectNode(); |
| 138 | root.set(USERS, userJsonArray()); |
| 139 | return root.toString(); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Singleton instance. |
| 144 | */ |
| 145 | public static final CordModelCache INSTANCE = new CordModelCache(); |
Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 146 | } |