Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 1 | package org.onosproject.cord.gui.model; |
| 2 | |
| 3 | import com.fasterxml.jackson.databind.JsonNode; |
| 4 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 5 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 6 | |
| 7 | import java.util.HashMap; |
| 8 | import java.util.Map; |
| 9 | |
| 10 | /** |
| 11 | * Utility factory for operating on XOS functions. |
| 12 | */ |
| 13 | public class XosFunctionFactory extends JsonFactory { |
| 14 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 15 | private static final String PARAMS = "params"; |
| 16 | |
| 17 | private static final String LEVEL = "level"; |
| 18 | private static final String LEVELS = "levels"; |
| 19 | |
| 20 | |
| 21 | // URL Filtering Levels... |
| 22 | private static final String PG = "PG"; |
| 23 | private static final String PG13 = "PG-13"; |
| 24 | private static final String R = "R"; |
| 25 | |
| 26 | private static final String[] FILTER_LEVELS = { PG, PG13, R }; |
| 27 | private static final String DEFAULT_FILTER_LEVEL = PG; |
| 28 | |
| 29 | |
| 30 | // no instantiation |
| 31 | private XosFunctionFactory() {} |
| 32 | |
| 33 | /** |
| 34 | * Produces the JSON representation of the given XOS function descriptor. |
| 35 | * |
| 36 | * @param xfd function descriptor |
| 37 | * @return JSON encoding |
| 38 | */ |
| 39 | public static ObjectNode toObjectNode(XosFunctionDescriptor xfd) { |
| 40 | ObjectNode root = objectNode() |
| 41 | .put(ID, xfd.id()) |
| 42 | .put(NAME, xfd.displayName()) |
| 43 | .put(DESC, xfd.description()); |
| 44 | root.set(PARAMS, paramsForXfd(xfd)); |
| 45 | return root; |
| 46 | } |
| 47 | |
| 48 | private static JsonNode paramsForXfd(XosFunctionDescriptor xfd) { |
| 49 | ParamStructFactory psf = PARAM_MAP.get(xfd); |
| 50 | if (psf == null) { |
| 51 | psf = DEF_PARAMS; |
| 52 | } |
| 53 | return psf.params(); |
| 54 | } |
| 55 | |
| 56 | // ==== handling different parameter structures... |
| 57 | private static final Map<XosFunctionDescriptor, ParamStructFactory> |
| 58 | PARAM_MAP = new HashMap<XosFunctionDescriptor, ParamStructFactory>(); |
| 59 | |
| 60 | private static final ParamStructFactory DEF_PARAMS = new ParamStructFactory(); |
| 61 | static { |
| 62 | PARAM_MAP.put(XosFunctionDescriptor.URL_FILTER, new UrlFilterParams()); |
| 63 | } |
| 64 | |
| 65 | // private parameter structure creator |
| 66 | static class ParamStructFactory { |
| 67 | ObjectNode params() { |
| 68 | return objectNode(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static class UrlFilterParams extends ParamStructFactory { |
| 73 | @Override |
| 74 | ObjectNode params() { |
| 75 | ObjectNode result = objectNode(); |
| 76 | result.put(LEVEL, DEFAULT_FILTER_LEVEL); |
| 77 | ArrayNode levels = arrayNode(); |
| 78 | for (String lvl: FILTER_LEVELS) { |
| 79 | levels.add(lvl); |
| 80 | } |
| 81 | result.set(LEVELS, levels); |
| 82 | return result; |
| 83 | } |
| 84 | } |
| 85 | } |