Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 1 | /* |
| 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 | * |
| 16 | */ |
| 17 | |
| 18 | package org.onosproject.ui.impl.topo; |
| 19 | |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 22 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 23 | import org.onosproject.ui.topo.ButtonId; |
| 24 | import org.onosproject.ui.topo.DeviceHighlight; |
| 25 | import org.onosproject.ui.topo.Highlights; |
| 26 | import org.onosproject.ui.topo.HostHighlight; |
| 27 | import org.onosproject.ui.topo.LinkHighlight; |
| 28 | import org.onosproject.ui.topo.PropertyPanel; |
| 29 | |
| 30 | /** |
| 31 | * JSON utilities for the Topology View. |
| 32 | */ |
| 33 | public final class TopoJson { |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 34 | // package-private for unit test access |
| 35 | static final String DEVICES = "devices"; |
| 36 | static final String HOSTS = "hosts"; |
| 37 | static final String LINKS = "links"; |
| 38 | static final String SUBDUE = "subdue"; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 39 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 40 | static final String ID = "id"; |
| 41 | static final String LABEL = "label"; |
| 42 | static final String CSS = "css"; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 43 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 44 | static final String TITLE = "title"; |
| 45 | static final String TYPE = "type"; |
| 46 | static final String PROP_ORDER = "propOrder"; |
| 47 | static final String PROPS = "props"; |
| 48 | static final String BUTTONS = "buttons"; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 49 | |
| 50 | |
| 51 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 52 | |
| 53 | private static ObjectNode objectNode() { |
| 54 | return MAPPER.createObjectNode(); |
| 55 | } |
| 56 | |
| 57 | private static ArrayNode arrayNode() { |
| 58 | return MAPPER.createArrayNode(); |
| 59 | } |
| 60 | |
| 61 | // non-instantiable |
| 62 | private TopoJson() { } |
| 63 | |
| 64 | /** |
| 65 | * Transforms the given highlights model into a JSON message payload. |
| 66 | * |
| 67 | * @param highlights the model to transform |
| 68 | * @return JSON payload |
| 69 | */ |
| 70 | public static ObjectNode json(Highlights highlights) { |
| 71 | ObjectNode payload = objectNode(); |
| 72 | |
| 73 | ArrayNode devices = arrayNode(); |
| 74 | ArrayNode hosts = arrayNode(); |
| 75 | ArrayNode links = arrayNode(); |
| 76 | |
| 77 | payload.set(DEVICES, devices); |
| 78 | payload.set(HOSTS, hosts); |
| 79 | payload.set(LINKS, links); |
| 80 | |
| 81 | highlights.devices().forEach(dh -> devices.add(json(dh))); |
| 82 | highlights.hosts().forEach(hh -> hosts.add(json(hh))); |
| 83 | highlights.links().forEach(lh -> links.add(json(lh))); |
| 84 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 85 | Highlights.Amount toSubdue = highlights.subdueLevel(); |
| 86 | if (!toSubdue.equals(Highlights.Amount.ZERO)) { |
| 87 | payload.put(SUBDUE, toSubdue.toString()); |
| 88 | } |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 89 | return payload; |
| 90 | } |
| 91 | |
| 92 | private static ObjectNode json(DeviceHighlight dh) { |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 93 | return objectNode() |
| 94 | .put(ID, dh.elementId()); |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | private static ObjectNode json(HostHighlight hh) { |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 98 | return objectNode() |
| 99 | .put(ID, hh.elementId()); |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | private static ObjectNode json(LinkHighlight lh) { |
| 103 | return objectNode() |
| 104 | .put(ID, lh.elementId()) |
| 105 | .put(LABEL, lh.label()) |
| 106 | .put(CSS, lh.cssClasses()); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Translates the given property panel into JSON, for returning |
| 111 | * to the client. |
| 112 | * |
| 113 | * @param pp the property panel model |
| 114 | * @return JSON payload |
| 115 | */ |
| 116 | public static ObjectNode json(PropertyPanel pp) { |
| 117 | ObjectNode result = objectNode() |
| 118 | .put(TITLE, pp.title()) |
| 119 | .put(TYPE, pp.typeId()) |
| 120 | .put(ID, pp.id()); |
| 121 | |
| 122 | ObjectNode pnode = objectNode(); |
| 123 | ArrayNode porder = arrayNode(); |
| 124 | for (PropertyPanel.Prop p : pp.properties()) { |
| 125 | porder.add(p.key()); |
| 126 | pnode.put(p.key(), p.value()); |
| 127 | } |
| 128 | result.set(PROP_ORDER, porder); |
| 129 | result.set(PROPS, pnode); |
| 130 | |
| 131 | ArrayNode buttons = arrayNode(); |
| 132 | for (ButtonId b : pp.buttons()) { |
| 133 | buttons.add(b.id()); |
| 134 | } |
| 135 | result.set(BUTTONS, buttons); |
| 136 | return result; |
| 137 | } |
| 138 | |
| 139 | } |