blob: dffabceb41ea75798fa8b72f23524e8e7e2389b1 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -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 */
Simon Hunt41b943e2015-05-21 13:52:01 -070016package org.onosproject.cord.gui.model;
17
Simon Hunt41b943e2015-05-21 13:52:01 -070018import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20
21import java.util.HashMap;
22import java.util.Map;
23
Simon Hunt6c2555b2015-05-21 18:17:56 -070024import static org.onosproject.cord.gui.model.XosFunctionDescriptor.URL_FILTER;
25
Simon Hunt41b943e2015-05-21 13:52:01 -070026/**
27 * Utility factory for operating on XOS functions.
28 */
29public class XosFunctionFactory extends JsonFactory {
30
Simon Hunt41b943e2015-05-21 13:52:01 -070031 private static final String PARAMS = "params";
Simon Hunt41b943e2015-05-21 13:52:01 -070032 private static final String LEVEL = "level";
33 private static final String LEVELS = "levels";
34
Simon Hunt41b943e2015-05-21 13:52:01 -070035
36 // no instantiation
37 private XosFunctionFactory() {}
38
39 /**
40 * Produces the JSON representation of the given XOS function descriptor.
41 *
42 * @param xfd function descriptor
43 * @return JSON encoding
44 */
45 public static ObjectNode toObjectNode(XosFunctionDescriptor xfd) {
46 ObjectNode root = objectNode()
47 .put(ID, xfd.id())
48 .put(NAME, xfd.displayName())
49 .put(DESC, xfd.description());
50 root.set(PARAMS, paramsForXfd(xfd));
51 return root;
52 }
53
Simon Hunt6c2555b2015-05-21 18:17:56 -070054 private static ObjectNode paramsForXfd(XosFunctionDescriptor xfd) {
55 ParamsFactory psf = PARAM_MAP.get(xfd);
Simon Hunt41b943e2015-05-21 13:52:01 -070056 if (psf == null) {
Simon Hunt6c2555b2015-05-21 18:17:56 -070057 psf = DEF_PARAMS_FACTORY;
Simon Hunt41b943e2015-05-21 13:52:01 -070058 }
59 return psf.params();
60 }
61
Simon Hunt41b943e2015-05-21 13:52:01 -070062
Simon Hunt6c2555b2015-05-21 18:17:56 -070063 // ==== handling different parameter structures...
64 private static final Map<XosFunctionDescriptor, ParamsFactory>
65 PARAM_MAP = new HashMap<XosFunctionDescriptor, ParamsFactory>();
66
67 private static final ParamsFactory DEF_PARAMS_FACTORY = new ParamsFactory();
Simon Hunt41b943e2015-05-21 13:52:01 -070068 static {
Simon Hunt6c2555b2015-05-21 18:17:56 -070069 PARAM_MAP.put(URL_FILTER, new UrlFilterParamsFactory());
Simon Hunt41b943e2015-05-21 13:52:01 -070070 }
71
Simon Hunt6c2555b2015-05-21 18:17:56 -070072 /**
Simon Hunt6c2555b2015-05-21 18:17:56 -070073 * Creates an object node representation of the profile for the
74 * specified user.
75 *
76 * @param user the user
77 * @return object node profile
78 */
79 public static ObjectNode profileForUser(SubscriberUser user) {
80 ObjectNode root = objectNode();
81 for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) {
82 XosFunction.Memento mem = user.getMemento(xfd);
83 if (mem != null) {
84 root.set(xfd.id(), mem.toObjectNode());
85 }
86 }
87 return root;
88 }
89
90
91 // ===================================================================
92 // === factories for creating parameter structures, both default
93 // and from a memento...
94
Simon Hunt41b943e2015-05-21 13:52:01 -070095 // private parameter structure creator
Simon Hunt6c2555b2015-05-21 18:17:56 -070096 static class ParamsFactory {
Simon Hunt41b943e2015-05-21 13:52:01 -070097 ObjectNode params() {
98 return objectNode();
99 }
100 }
101
Simon Hunt6c2555b2015-05-21 18:17:56 -0700102 static class UrlFilterParamsFactory extends ParamsFactory {
Simon Hunt41b943e2015-05-21 13:52:01 -0700103 @Override
104 ObjectNode params() {
105 ObjectNode result = objectNode();
Simon Huntab5232e2015-05-26 16:59:46 -0700106 result.put(LEVEL, UrlFilterFunction.DEFAULT_LEVEL.name());
Simon Hunt41b943e2015-05-21 13:52:01 -0700107 ArrayNode levels = arrayNode();
Simon Hunt6c2555b2015-05-21 18:17:56 -0700108 for (UrlFilterFunction.Level lvl: UrlFilterFunction.Level.values()) {
109 levels.add(lvl.name());
Simon Hunt41b943e2015-05-21 13:52:01 -0700110 }
111 result.set(LEVELS, levels);
112 return result;
113 }
114 }
115}