Simon Hunt

CORD Subscriber GUI - Plumbed through the parameter change per function per user.

Change-Id: I9b8eb677f606fd75f70366cec7f5b4993d188ab1
...@@ -28,10 +28,12 @@ import org.onosproject.cord.gui.model.SubscriberUser; ...@@ -28,10 +28,12 @@ import org.onosproject.cord.gui.model.SubscriberUser;
28 import org.onosproject.cord.gui.model.UserFactory; 28 import org.onosproject.cord.gui.model.UserFactory;
29 import org.onosproject.cord.gui.model.XosFunction; 29 import org.onosproject.cord.gui.model.XosFunction;
30 import org.onosproject.cord.gui.model.XosFunctionDescriptor; 30 import org.onosproject.cord.gui.model.XosFunctionDescriptor;
31 -import org.onosproject.cord.gui.model.XosFunctionFactory;
32 31
33 -import java.util.ArrayList;
34 import java.util.List; 32 import java.util.List;
33 +import java.util.Map;
34 +import java.util.TreeMap;
35 +
36 +import static com.google.common.base.Preconditions.checkNotNull;
35 37
36 /** 38 /**
37 * In memory cache of the model of the subscriber's account. 39 * In memory cache of the model of the subscriber's account.
...@@ -50,14 +52,16 @@ public class CordModelCache extends JsonFactory { ...@@ -50,14 +52,16 @@ public class CordModelCache extends JsonFactory {
50 private static final String MAC_4 = "010203040509"; 52 private static final String MAC_4 = "010203040509";
51 53
52 private Bundle currentBundle; 54 private Bundle currentBundle;
53 - private final List<SubscriberUser> users; 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>();
54 59
55 /** 60 /**
56 * Constructs a model cache, initializing it with basic bundle. 61 * Constructs a model cache, initializing it with basic bundle.
57 */ 62 */
58 CordModelCache() { 63 CordModelCache() {
59 currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE); 64 currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE);
60 - users = new ArrayList<SubscriberUser>();
61 initUsers(); 65 initUsers();
62 } 66 }
63 67
...@@ -65,10 +69,10 @@ public class CordModelCache extends JsonFactory { ...@@ -65,10 +69,10 @@ public class CordModelCache extends JsonFactory {
65 * Used to initialize users for the demo. These are currently fake. 69 * Used to initialize users for the demo. These are currently fake.
66 */ 70 */
67 public void initUsers() { 71 public void initUsers() {
68 - users.add(createUser(1, "Mom's MacBook", MAC_1)); 72 + userMap.put(1, createUser(1, "Mom's MacBook", MAC_1));
69 - users.add(createUser(2, "Dad's iPad", MAC_2)); 73 + userMap.put(2, createUser(2, "Dad's iPad", MAC_2));
70 - users.add(createUser(3, "Dick's laptop", MAC_3)); 74 + userMap.put(3, createUser(3, "Dick's laptop", MAC_3));
71 - users.add(createUser(4, "Jane's laptop", MAC_4)); 75 + userMap.put(4, createUser(4, "Jane's laptop", MAC_4));
72 } 76 }
73 77
74 private SubscriberUser createUser(int uid, String name, String mac) { 78 private SubscriberUser createUser(int uid, String name, String mac) {
...@@ -79,7 +83,6 @@ public class CordModelCache extends JsonFactory { ...@@ -79,7 +83,6 @@ public class CordModelCache extends JsonFactory {
79 return user; 83 return user;
80 } 84 }
81 85
82 -
83 /** 86 /**
84 * Returns the currently selected bundle. 87 * Returns the currently selected bundle.
85 * 88 *
...@@ -99,7 +102,7 @@ public class CordModelCache extends JsonFactory { ...@@ -99,7 +102,7 @@ public class CordModelCache extends JsonFactory {
99 BundleDescriptor bd = BundleFactory.bundleFromId(bundleId); 102 BundleDescriptor bd = BundleFactory.bundleFromId(bundleId);
100 currentBundle = new Bundle(bd); 103 currentBundle = new Bundle(bd);
101 // update the user mementos 104 // update the user mementos
102 - for (SubscriberUser user: users) { 105 + for (SubscriberUser user: userMap.values()) {
103 user.clearMementos(); 106 user.clearMementos();
104 for (XosFunction f: currentBundle.functions()) { 107 for (XosFunction f: currentBundle.functions()) {
105 user.setMemento(f.descriptor(), f.createMemento()); 108 user.setMemento(f.descriptor(), f.createMemento());
...@@ -116,7 +119,7 @@ public class CordModelCache extends JsonFactory { ...@@ -116,7 +119,7 @@ public class CordModelCache extends JsonFactory {
116 * @return the list of users 119 * @return the list of users
117 */ 120 */
118 public List<SubscriberUser> getUsers() { 121 public List<SubscriberUser> getUsers() {
119 - return ImmutableList.copyOf(users); 122 + return ImmutableList.copyOf(userMap.values());
120 } 123 }
121 124
122 /** 125 /**
...@@ -129,18 +132,25 @@ public class CordModelCache extends JsonFactory { ...@@ -129,18 +132,25 @@ public class CordModelCache extends JsonFactory {
129 */ 132 */
130 public void applyPerUserParam(String userId, String funcId, 133 public void applyPerUserParam(String userId, String funcId,
131 String param, String value) { 134 String param, String value) {
132 - // FIXME: this is not right yet... 135 +
133 int uid = Integer.parseInt(userId); 136 int uid = Integer.parseInt(userId);
137 + SubscriberUser user = userMap.get(uid);
138 + checkNotNull(user, "unknown user id: " + uid);
139 +
134 XosFunctionDescriptor xfd = 140 XosFunctionDescriptor xfd =
135 XosFunctionDescriptor.valueOf(funcId.toUpperCase()); 141 XosFunctionDescriptor.valueOf(funcId.toUpperCase());
136 - XosFunctionFactory.apply(xfd, uid, param, value); 142 +
143 + XosFunction func = currentBundle.findFunction(xfd);
144 + checkNotNull(func, "function not part of bundle: " + funcId);
145 +
146 + func.applyParam(user, param, value);
137 } 147 }
138 148
139 // ============= 149 // =============
140 150
141 private ArrayNode userJsonArray() { 151 private ArrayNode userJsonArray() {
142 ArrayNode userList = arrayNode(); 152 ArrayNode userList = arrayNode();
143 - for (SubscriberUser user: users) { 153 + for (SubscriberUser user: userMap.values()) {
144 userList.add(UserFactory.toObjectNode(user)); 154 userList.add(UserFactory.toObjectNode(user));
145 } 155 }
146 return userList; 156 return userList;
......
...@@ -19,7 +19,8 @@ package org.onosproject.cord.gui.model; ...@@ -19,7 +19,8 @@ package org.onosproject.cord.gui.model;
19 19
20 import com.google.common.collect.ImmutableSet; 20 import com.google.common.collect.ImmutableSet;
21 21
22 -import java.util.HashSet; 22 +import java.util.HashMap;
23 +import java.util.Map;
23 import java.util.Set; 24 import java.util.Set;
24 25
25 /** 26 /**
...@@ -27,7 +28,8 @@ import java.util.Set; ...@@ -27,7 +28,8 @@ import java.util.Set;
27 */ 28 */
28 public class Bundle { 29 public class Bundle {
29 private final BundleDescriptor bundleDescriptor; 30 private final BundleDescriptor bundleDescriptor;
30 - private final Set<XosFunction> functions; 31 + private final Map<XosFunctionDescriptor, XosFunction> functionMap =
32 + new HashMap<XosFunctionDescriptor, XosFunction>();
31 33
32 /** 34 /**
33 * Constructs a new bundle instance. 35 * Constructs a new bundle instance.
...@@ -36,7 +38,7 @@ public class Bundle { ...@@ -36,7 +38,7 @@ public class Bundle {
36 */ 38 */
37 public Bundle(BundleDescriptor bundleDescriptor) { 39 public Bundle(BundleDescriptor bundleDescriptor) {
38 this.bundleDescriptor = bundleDescriptor; 40 this.bundleDescriptor = bundleDescriptor;
39 - this.functions = initFunctions(); 41 + initFunctions();
40 } 42 }
41 43
42 /** 44 /**
...@@ -54,20 +56,16 @@ public class Bundle { ...@@ -54,20 +56,16 @@ public class Bundle {
54 * @return the functions 56 * @return the functions
55 */ 57 */
56 public Set<XosFunction> functions() { 58 public Set<XosFunction> functions() {
57 - return ImmutableSet.copyOf(functions); 59 + return ImmutableSet.copyOf(functionMap.values());
58 } 60 }
59 61
60 /** 62 /**
61 * Creates an initial set of function instances. 63 * Creates an initial set of function instances.
62 - *
63 - * @return initial function instances
64 */ 64 */
65 - private Set<XosFunction> initFunctions() { 65 + private void initFunctions() {
66 - Set<XosFunction> funcs = new HashSet<XosFunction>();
67 for (XosFunctionDescriptor xfd: bundleDescriptor.functions()) { 66 for (XosFunctionDescriptor xfd: bundleDescriptor.functions()) {
68 - funcs.add(createFunction(xfd)); 67 + functionMap.put(xfd, createFunction(xfd));
69 } 68 }
70 - return funcs;
71 } 69 }
72 70
73 private XosFunction createFunction(XosFunctionDescriptor xfd) { 71 private XosFunction createFunction(XosFunctionDescriptor xfd) {
...@@ -83,4 +81,15 @@ public class Bundle { ...@@ -83,4 +81,15 @@ public class Bundle {
83 } 81 }
84 return func; 82 return func;
85 } 83 }
84 +
85 + /**
86 + * Returns the function instance for the specified descriptor, or returns
87 + * null if function is not part of this bundle.
88 + *
89 + * @param xfd function descrriptor
90 + * @return function instance
91 + */
92 + public XosFunction findFunction(XosFunctionDescriptor xfd) {
93 + return functionMap.get(xfd);
94 + }
86 } 95 }
......
...@@ -19,6 +19,8 @@ package org.onosproject.cord.gui.model; ...@@ -19,6 +19,8 @@ package org.onosproject.cord.gui.model;
19 19
20 import com.fasterxml.jackson.databind.node.ObjectNode; 20 import com.fasterxml.jackson.databind.node.ObjectNode;
21 21
22 +import static com.google.common.base.Preconditions.checkNotNull;
23 +
22 /** 24 /**
23 * Specialization of XosFunction for URL filtering. 25 * Specialization of XosFunction for URL filtering.
24 */ 26 */
...@@ -41,6 +43,18 @@ public class UrlFilterFunction extends DefaultXosFunction { ...@@ -41,6 +43,18 @@ public class UrlFilterFunction extends DefaultXosFunction {
41 } 43 }
42 44
43 @Override 45 @Override
46 + public void applyParam(SubscriberUser user, String param, String value) {
47 + Memento memo = user.getMemento(descriptor());
48 + checkNotNull(memo, "missing memento for " + descriptor());
49 + UrlFilterMemento ufMemo = (UrlFilterMemento) memo;
50 +
51 + if (LEVEL.equals(param)) {
52 + Level newLevel = Level.valueOf(value.toUpperCase());
53 + ufMemo.setLevel(newLevel);
54 + }
55 + }
56 +
57 + @Override
44 public Memento createMemento() { 58 public Memento createMemento() {
45 return new UrlFilterMemento(); 59 return new UrlFilterMemento();
46 } 60 }
......
...@@ -58,20 +58,6 @@ public class XosFunctionFactory extends JsonFactory { ...@@ -58,20 +58,6 @@ public class XosFunctionFactory extends JsonFactory {
58 } 58 }
59 59
60 /** 60 /**
61 - * Applies a parameter change for the given function, in the context of
62 - * the specified user.
63 - *
64 - * @param xfd function context
65 - * @param userId user identifier
66 - * @param param parameter name
67 - * @param value value to apply
68 - */
69 - public static void apply(XosFunctionDescriptor xfd, int userId,
70 - String param, String value) {
71 - // TODO:
72 - }
73 -
74 - /**
75 * Creates an object node representation of the profile for the 61 * Creates an object node representation of the profile for the
76 * specified user. 62 * specified user.
77 * 63 *
......
...@@ -91,8 +91,36 @@ public class CoreModelCacheTest { ...@@ -91,8 +91,36 @@ public class CoreModelCacheTest {
91 assertTrue("bad users family json", sameJson(USERS_FAMILY, json)); 91 assertTrue("bad users family json", sameJson(USERS_FAMILY, json));
92 } 92 }
93 93
94 + @Test
95 + public void setNewLevel() {
96 + cache.setCurrentBundle("family");
97 + JsonNode node = fromString(cache.jsonUsers());
98 + assertEquals("wrong level", "PG", getMomsLevel(node));
99 +
100 + cache.applyPerUserParam("1", "url_filter", "level", "R");
101 +
102 + node = fromString(cache.jsonUsers());
103 + assertEquals("wrong level", "R", getMomsLevel(node));
104 + }
105 +
106 + private String getMomsLevel(JsonNode node) {
107 + JsonNode mom = node.get("users").elements().next();
108 + assertEquals("wrong ID", 1, mom.get("id").asInt());
109 + return mom.get("profile").get("url_filter").get("level").asText();
110 + }
111 +
112 +
94 // ============= 113 // =============
95 114
115 + private JsonNode fromString(String s) {
116 + try {
117 + return MAPPER.readTree(s);
118 + } catch (IOException e) {
119 + System.out.println("Exception: " + e);
120 + }
121 + return null;
122 + }
123 +
96 private boolean sameJson(String s1, String s2) { 124 private boolean sameJson(String s1, String s2) {
97 try { 125 try {
98 JsonNode tree1 = MAPPER.readTree(s1); 126 JsonNode tree1 = MAPPER.readTree(s1);
......