1 package org.onosproject.cord.gui.model;
3 import com.fasterxml.jackson.databind.node.ArrayNode;
4 import com.fasterxml.jackson.databind.node.ObjectNode;
6 import java.util.HashMap;
9 import static org.onosproject.cord.gui.model.XosFunctionDescriptor.URL_FILTER;
12 * Utility factory for operating on XOS functions.
14 public class XosFunctionFactory extends JsonFactory {
16 private static final String PARAMS = "params";
17 private static final String LEVEL = "level";
18 private static final String LEVELS = "levels";
22 private XosFunctionFactory() {}
25 * Produces the JSON representation of the given XOS function descriptor.
27 * @param xfd function descriptor
28 * @return JSON encoding
30 public static ObjectNode toObjectNode(XosFunctionDescriptor xfd) {
31 ObjectNode root = objectNode()
33 .put(NAME, xfd.displayName())
34 .put(DESC, xfd.description());
35 root.set(PARAMS, paramsForXfd(xfd));
39 private static ObjectNode paramsForXfd(XosFunctionDescriptor xfd) {
40 ParamsFactory psf = PARAM_MAP.get(xfd);
42 psf = DEF_PARAMS_FACTORY;
48 // ==== handling different parameter structures...
49 private static final Map<XosFunctionDescriptor, ParamsFactory>
50 PARAM_MAP = new HashMap<XosFunctionDescriptor, ParamsFactory>();
52 private static final ParamsFactory DEF_PARAMS_FACTORY = new ParamsFactory();
54 PARAM_MAP.put(URL_FILTER, new UrlFilterParamsFactory());
58 * Creates an object node representation of the profile for the
61 * @param user the user
62 * @return object node profile
64 public static ObjectNode profileForUser(SubscriberUser user) {
65 ObjectNode root = objectNode();
66 for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) {
67 XosFunction.Memento mem = user.getMemento(xfd);
69 root.set(xfd.id(), mem.toObjectNode());
76 // ===================================================================
77 // === factories for creating parameter structures, both default
78 // and from a memento...
80 // private parameter structure creator
81 static class ParamsFactory {
87 static class UrlFilterParamsFactory extends ParamsFactory {
90 ObjectNode result = objectNode();
91 result.put(LEVEL, UrlFilterFunction.DEFAULT_LEVEL.name());
92 ArrayNode levels = arrayNode();
93 for (UrlFilterFunction.Level lvl: UrlFilterFunction.Level.values()) {
94 levels.add(lvl.name());
96 result.set(LEVELS, levels);