Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 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 Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 16 | package org.onosproject.cord.gui.model; |
| 17 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | |
| 21 | import java.util.HashMap; |
| 22 | import java.util.Map; |
| 23 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 24 | import static org.onosproject.cord.gui.model.XosFunctionDescriptor.URL_FILTER; |
| 25 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Utility factory for operating on XOS functions. |
| 28 | */ |
| 29 | public class XosFunctionFactory extends JsonFactory { |
| 30 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 31 | private static final String PARAMS = "params"; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 32 | private static final String LEVEL = "level"; |
| 33 | private static final String LEVELS = "levels"; |
| 34 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 35 | |
| 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 Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 54 | private static ObjectNode paramsForXfd(XosFunctionDescriptor xfd) { |
| 55 | ParamsFactory psf = PARAM_MAP.get(xfd); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 56 | if (psf == null) { |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 57 | psf = DEF_PARAMS_FACTORY; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 58 | } |
| 59 | return psf.params(); |
| 60 | } |
| 61 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 62 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 63 | // ==== 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 Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 68 | static { |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 69 | PARAM_MAP.put(URL_FILTER, new UrlFilterParamsFactory()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 72 | /** |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 73 | * 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 Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 95 | // private parameter structure creator |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 96 | static class ParamsFactory { |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 97 | ObjectNode params() { |
| 98 | return objectNode(); |
| 99 | } |
| 100 | } |
| 101 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 102 | static class UrlFilterParamsFactory extends ParamsFactory { |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 103 | @Override |
| 104 | ObjectNode params() { |
| 105 | ObjectNode result = objectNode(); |
Simon Hunt | ab5232e | 2015-05-26 16:59:46 -0700 | [diff] [blame] | 106 | result.put(LEVEL, UrlFilterFunction.DEFAULT_LEVEL.name()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 107 | ArrayNode levels = arrayNode(); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 108 | for (UrlFilterFunction.Level lvl: UrlFilterFunction.Level.values()) { |
| 109 | levels.add(lvl.name()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 110 | } |
| 111 | result.set(LEVELS, levels); |
| 112 | return result; |
| 113 | } |
| 114 | } |
| 115 | } |