Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 1 | package org.onosproject.cord.gui.model; |
| 2 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 3 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 4 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 5 | |
| 6 | import java.util.HashMap; |
| 7 | import java.util.Map; |
| 8 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 9 | import static org.onosproject.cord.gui.model.XosFunctionDescriptor.URL_FILTER; |
| 10 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 11 | /** |
| 12 | * Utility factory for operating on XOS functions. |
| 13 | */ |
| 14 | public class XosFunctionFactory extends JsonFactory { |
| 15 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 16 | private static final String PARAMS = "params"; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 17 | private static final String LEVEL = "level"; |
| 18 | private static final String LEVELS = "levels"; |
| 19 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 20 | |
| 21 | // no instantiation |
| 22 | private XosFunctionFactory() {} |
| 23 | |
| 24 | /** |
| 25 | * Produces the JSON representation of the given XOS function descriptor. |
| 26 | * |
| 27 | * @param xfd function descriptor |
| 28 | * @return JSON encoding |
| 29 | */ |
| 30 | public static ObjectNode toObjectNode(XosFunctionDescriptor xfd) { |
| 31 | ObjectNode root = objectNode() |
| 32 | .put(ID, xfd.id()) |
| 33 | .put(NAME, xfd.displayName()) |
| 34 | .put(DESC, xfd.description()); |
| 35 | root.set(PARAMS, paramsForXfd(xfd)); |
| 36 | return root; |
| 37 | } |
| 38 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 39 | private static ObjectNode paramsForXfd(XosFunctionDescriptor xfd) { |
| 40 | ParamsFactory psf = PARAM_MAP.get(xfd); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 41 | if (psf == null) { |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 42 | psf = DEF_PARAMS_FACTORY; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 43 | } |
| 44 | return psf.params(); |
| 45 | } |
| 46 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 47 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 48 | // ==== handling different parameter structures... |
| 49 | private static final Map<XosFunctionDescriptor, ParamsFactory> |
| 50 | PARAM_MAP = new HashMap<XosFunctionDescriptor, ParamsFactory>(); |
| 51 | |
| 52 | private static final ParamsFactory DEF_PARAMS_FACTORY = new ParamsFactory(); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 53 | static { |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 54 | PARAM_MAP.put(URL_FILTER, new UrlFilterParamsFactory()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 57 | /** |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 58 | * Creates an object node representation of the profile for the |
| 59 | * specified user. |
| 60 | * |
| 61 | * @param user the user |
| 62 | * @return object node profile |
| 63 | */ |
| 64 | public static ObjectNode profileForUser(SubscriberUser user) { |
| 65 | ObjectNode root = objectNode(); |
| 66 | for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) { |
| 67 | XosFunction.Memento mem = user.getMemento(xfd); |
| 68 | if (mem != null) { |
| 69 | root.set(xfd.id(), mem.toObjectNode()); |
| 70 | } |
| 71 | } |
| 72 | return root; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | // =================================================================== |
| 77 | // === factories for creating parameter structures, both default |
| 78 | // and from a memento... |
| 79 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 80 | // private parameter structure creator |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 81 | static class ParamsFactory { |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 82 | ObjectNode params() { |
| 83 | return objectNode(); |
| 84 | } |
| 85 | } |
| 86 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 87 | static class UrlFilterParamsFactory extends ParamsFactory { |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 88 | @Override |
| 89 | ObjectNode params() { |
| 90 | ObjectNode result = objectNode(); |
Simon Hunt | ab5232e | 2015-05-26 16:59:46 -0700 | [diff] [blame] | 91 | result.put(LEVEL, UrlFilterFunction.DEFAULT_LEVEL.name()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 92 | ArrayNode levels = arrayNode(); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 93 | for (UrlFilterFunction.Level lvl: UrlFilterFunction.Level.values()) { |
| 94 | levels.add(lvl.name()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 95 | } |
| 96 | result.set(LEVELS, levels); |
| 97 | return result; |
| 98 | } |
| 99 | } |
| 100 | } |