blob: 991b72b8c5eb8ace9185da3cbebda88a98978e28 [file] [log] [blame]
Simon Hunt41b943e2015-05-21 13:52:01 -07001package org.onosproject.cord.gui.model;
2
Simon Hunt41b943e2015-05-21 13:52:01 -07003import com.fasterxml.jackson.databind.node.ArrayNode;
4import com.fasterxml.jackson.databind.node.ObjectNode;
5
6import java.util.HashMap;
7import java.util.Map;
8
Simon Hunt6c2555b2015-05-21 18:17:56 -07009import static org.onosproject.cord.gui.model.XosFunctionDescriptor.URL_FILTER;
10
Simon Hunt41b943e2015-05-21 13:52:01 -070011/**
12 * Utility factory for operating on XOS functions.
13 */
14public class XosFunctionFactory extends JsonFactory {
15
Simon Hunt41b943e2015-05-21 13:52:01 -070016 private static final String PARAMS = "params";
Simon Hunt41b943e2015-05-21 13:52:01 -070017 private static final String LEVEL = "level";
18 private static final String LEVELS = "levels";
19
Simon Hunt6c2555b2015-05-21 18:17:56 -070020 private static final UrlFilterFunction.Level DEFAULT_FILTER_LEVEL =
21 UrlFilterFunction.Level.PG;
Simon Hunt41b943e2015-05-21 13:52:01 -070022
23
24 // no instantiation
25 private XosFunctionFactory() {}
26
27 /**
28 * Produces the JSON representation of the given XOS function descriptor.
29 *
30 * @param xfd function descriptor
31 * @return JSON encoding
32 */
33 public static ObjectNode toObjectNode(XosFunctionDescriptor xfd) {
34 ObjectNode root = objectNode()
35 .put(ID, xfd.id())
36 .put(NAME, xfd.displayName())
37 .put(DESC, xfd.description());
38 root.set(PARAMS, paramsForXfd(xfd));
39 return root;
40 }
41
Simon Hunt6c2555b2015-05-21 18:17:56 -070042 private static ObjectNode paramsForXfd(XosFunctionDescriptor xfd) {
43 ParamsFactory psf = PARAM_MAP.get(xfd);
Simon Hunt41b943e2015-05-21 13:52:01 -070044 if (psf == null) {
Simon Hunt6c2555b2015-05-21 18:17:56 -070045 psf = DEF_PARAMS_FACTORY;
Simon Hunt41b943e2015-05-21 13:52:01 -070046 }
47 return psf.params();
48 }
49
Simon Hunt41b943e2015-05-21 13:52:01 -070050
Simon Hunt6c2555b2015-05-21 18:17:56 -070051 // ==== handling different parameter structures...
52 private static final Map<XosFunctionDescriptor, ParamsFactory>
53 PARAM_MAP = new HashMap<XosFunctionDescriptor, ParamsFactory>();
54
55 private static final ParamsFactory DEF_PARAMS_FACTORY = new ParamsFactory();
Simon Hunt41b943e2015-05-21 13:52:01 -070056 static {
Simon Hunt6c2555b2015-05-21 18:17:56 -070057 PARAM_MAP.put(URL_FILTER, new UrlFilterParamsFactory());
Simon Hunt41b943e2015-05-21 13:52:01 -070058 }
59
Simon Hunt6c2555b2015-05-21 18:17:56 -070060 /**
Simon Hunt6c2555b2015-05-21 18:17:56 -070061 * Creates an object node representation of the profile for the
62 * specified user.
63 *
64 * @param user the user
65 * @return object node profile
66 */
67 public static ObjectNode profileForUser(SubscriberUser user) {
68 ObjectNode root = objectNode();
69 for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) {
70 XosFunction.Memento mem = user.getMemento(xfd);
71 if (mem != null) {
72 root.set(xfd.id(), mem.toObjectNode());
73 }
74 }
75 return root;
76 }
77
78
79 // ===================================================================
80 // === factories for creating parameter structures, both default
81 // and from a memento...
82
Simon Hunt41b943e2015-05-21 13:52:01 -070083 // private parameter structure creator
Simon Hunt6c2555b2015-05-21 18:17:56 -070084 static class ParamsFactory {
Simon Hunt41b943e2015-05-21 13:52:01 -070085 ObjectNode params() {
86 return objectNode();
87 }
88 }
89
Simon Hunt6c2555b2015-05-21 18:17:56 -070090 static class UrlFilterParamsFactory extends ParamsFactory {
Simon Hunt41b943e2015-05-21 13:52:01 -070091 @Override
92 ObjectNode params() {
93 ObjectNode result = objectNode();
Simon Hunt6c2555b2015-05-21 18:17:56 -070094 result.put(LEVEL, DEFAULT_FILTER_LEVEL.name());
Simon Hunt41b943e2015-05-21 13:52:01 -070095 ArrayNode levels = arrayNode();
Simon Hunt6c2555b2015-05-21 18:17:56 -070096 for (UrlFilterFunction.Level lvl: UrlFilterFunction.Level.values()) {
97 levels.add(lvl.name());
Simon Hunt41b943e2015-05-21 13:52:01 -070098 }
99 result.set(LEVELS, levels);
100 return result;
101 }
102 }
103}