blob: e47e68fda0bd8ef5834d94e26ad5501e4164d670 [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 Hunt41b943e2015-05-21 13:52:01 -070029
30import java.util.ArrayList;
31import java.util.List;
32
Simon Hunta29c87b2015-05-21 09:56:19 -070033/**
34 * In memory cache of the model of the subscriber's account.
35 */
Simon Hunt09a32db2015-05-21 15:00:42 -070036public class CordModelCache extends JsonFactory {
37
38 private static final String BUNDLE = "bundle";
39 private static final String USERS = "users";
40
Simon Hunta29c87b2015-05-21 09:56:19 -070041
Simon Hunt41b943e2015-05-21 13:52:01 -070042 // 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 Hunt09a32db2015-05-21 15:00:42 -070055 CordModelCache() {
Simon Hunt41b943e2015-05-21 13:52:01 -070056 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 Hunt09a32db2015-05-21 15:00:42 -070099
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 Hunta29c87b2015-05-21 09:56:19 -0700146}