Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -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. |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Simon Hunt | 5256066 | 2015-08-27 22:46:44 -0700 | [diff] [blame] | 17 | package org.onosproject.ui.topo; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Simon Hunt | 5256066 | 2015-08-27 22:46:44 -0700 | [diff] [blame] | 22 | |
| 23 | import static org.onosproject.ui.JsonUtils.envelope; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * JSON utilities for the Topology View. |
| 27 | */ |
| 28 | public final class TopoJson { |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 29 | // package-private for unit test access |
Simon Hunt | 5256066 | 2015-08-27 22:46:44 -0700 | [diff] [blame] | 30 | static final String SHOW_HIGHLIGHTS = "showHighlights"; |
Simon Hunt | 8e25811 | 2017-05-05 13:19:04 -0700 | [diff] [blame] | 31 | static final String TOPO2_HIGHLIGHTS = "topo2Highlights"; |
Simon Hunt | 5256066 | 2015-08-27 22:46:44 -0700 | [diff] [blame] | 32 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 33 | static final String DEVICES = "devices"; |
| 34 | static final String HOSTS = "hosts"; |
| 35 | static final String LINKS = "links"; |
| 36 | static final String SUBDUE = "subdue"; |
Andrea Campanella | 2dc91dc | 2015-12-07 12:17:02 -0800 | [diff] [blame] | 37 | static final String DELAY = "delay"; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 38 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 39 | static final String ID = "id"; |
| 40 | static final String LABEL = "label"; |
| 41 | static final String CSS = "css"; |
Simon Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame] | 42 | static final String BADGE = "badge"; |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 43 | static final String STATUS = "status"; |
| 44 | static final String TXT = "txt"; |
| 45 | static final String GID = "gid"; |
Simon Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame] | 46 | static final String MSG = "msg"; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 47 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 48 | static final String TITLE = "title"; |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 49 | static final String GLYPH_ID = "glyphId"; |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 50 | static final String NAV_PATH = "navPath"; |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 51 | static final String PROP_ORDER = "propOrder"; |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 52 | static final String PROP_LABELS = "propLabels"; |
| 53 | static final String PROP_VALUES = "propValues"; |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 54 | static final String BUTTONS = "buttons"; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 55 | |
| 56 | |
| 57 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 58 | |
| 59 | private static ObjectNode objectNode() { |
| 60 | return MAPPER.createObjectNode(); |
| 61 | } |
| 62 | |
| 63 | private static ArrayNode arrayNode() { |
| 64 | return MAPPER.createArrayNode(); |
| 65 | } |
| 66 | |
| 67 | // non-instantiable |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 68 | private TopoJson() { |
| 69 | } |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
Simon Hunt | 5256066 | 2015-08-27 22:46:44 -0700 | [diff] [blame] | 72 | * Returns a formatted message ready to send to the topology view |
| 73 | * to render highlights. |
| 74 | * |
| 75 | * @param highlights highlights model to transform |
| 76 | * @return fully formatted "show highlights" message |
| 77 | */ |
| 78 | public static ObjectNode highlightsMessage(Highlights highlights) { |
| 79 | return envelope(SHOW_HIGHLIGHTS, json(highlights)); |
| 80 | } |
| 81 | |
| 82 | /** |
Simon Hunt | 8e25811 | 2017-05-05 13:19:04 -0700 | [diff] [blame] | 83 | * Returns a formatted message ready to send to the topology-2 view |
| 84 | * to render highlights. |
| 85 | * |
| 86 | * @param highlights highlights model to transform |
| 87 | * @return fully formatted "show highlights" message |
| 88 | */ |
| 89 | public static ObjectNode topo2HighlightsMessage(Highlights highlights) { |
| 90 | return envelope(TOPO2_HIGHLIGHTS, json(highlights)); |
| 91 | } |
| 92 | |
| 93 | /** |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 94 | * Transforms the given highlights model into a JSON message payload. |
| 95 | * |
| 96 | * @param highlights the model to transform |
| 97 | * @return JSON payload |
| 98 | */ |
| 99 | public static ObjectNode json(Highlights highlights) { |
| 100 | ObjectNode payload = objectNode(); |
| 101 | |
| 102 | ArrayNode devices = arrayNode(); |
| 103 | ArrayNode hosts = arrayNode(); |
| 104 | ArrayNode links = arrayNode(); |
| 105 | |
| 106 | payload.set(DEVICES, devices); |
| 107 | payload.set(HOSTS, hosts); |
| 108 | payload.set(LINKS, links); |
| 109 | |
| 110 | highlights.devices().forEach(dh -> devices.add(json(dh))); |
| 111 | highlights.hosts().forEach(hh -> hosts.add(json(hh))); |
| 112 | highlights.links().forEach(lh -> links.add(json(lh))); |
| 113 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 114 | Highlights.Amount toSubdue = highlights.subdueLevel(); |
| 115 | if (!toSubdue.equals(Highlights.Amount.ZERO)) { |
| 116 | payload.put(SUBDUE, toSubdue.toString()); |
| 117 | } |
Andrea Campanella | 2dc91dc | 2015-12-07 12:17:02 -0800 | [diff] [blame] | 118 | int delay = highlights.delayMs(); |
| 119 | if (delay > 0) { |
| 120 | payload.put(DELAY, delay); |
| 121 | } |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 122 | return payload; |
| 123 | } |
| 124 | |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 125 | private static ObjectNode json(NodeBadge b) { |
| 126 | ObjectNode n = objectNode() |
| 127 | .put(STATUS, b.status().code()) |
| 128 | .put(b.isGlyph() ? GID : TXT, b.text()); |
| 129 | if (b.message() != null) { |
| 130 | n.put(MSG, b.message()); |
| 131 | } |
| 132 | return n; |
| 133 | } |
| 134 | |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 135 | private static ObjectNode json(DeviceHighlight dh) { |
Simon Hunt | 5b3ff90 | 2015-08-27 09:46:27 -0700 | [diff] [blame] | 136 | ObjectNode n = objectNode() |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 137 | .put(ID, dh.elementId()); |
Simon Hunt | 5b3ff90 | 2015-08-27 09:46:27 -0700 | [diff] [blame] | 138 | if (dh.subdued()) { |
| 139 | n.put(SUBDUE, true); |
| 140 | } |
Simon Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame] | 141 | NodeBadge badge = dh.badge(); |
| 142 | if (badge != null) { |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 143 | n.set(BADGE, json(badge)); |
Simon Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame] | 144 | } |
Simon Hunt | 5b3ff90 | 2015-08-27 09:46:27 -0700 | [diff] [blame] | 145 | return n; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | private static ObjectNode json(HostHighlight hh) { |
Simon Hunt | 5b3ff90 | 2015-08-27 09:46:27 -0700 | [diff] [blame] | 149 | ObjectNode n = objectNode() |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 150 | .put(ID, hh.elementId()); |
Simon Hunt | 5b3ff90 | 2015-08-27 09:46:27 -0700 | [diff] [blame] | 151 | if (hh.subdued()) { |
| 152 | n.put(SUBDUE, true); |
| 153 | } |
Andrea Campanella | 5212541 | 2015-12-03 14:50:40 -0800 | [diff] [blame] | 154 | NodeBadge badge = hh.badge(); |
| 155 | if (badge != null) { |
| 156 | n.set(BADGE, json(badge)); |
| 157 | } |
Simon Hunt | 5b3ff90 | 2015-08-27 09:46:27 -0700 | [diff] [blame] | 158 | return n; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | private static ObjectNode json(LinkHighlight lh) { |
Simon Hunt | 5b3ff90 | 2015-08-27 09:46:27 -0700 | [diff] [blame] | 162 | ObjectNode n = objectNode() |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 163 | .put(ID, lh.elementId()) |
| 164 | .put(LABEL, lh.label()) |
| 165 | .put(CSS, lh.cssClasses()); |
Simon Hunt | 5b3ff90 | 2015-08-27 09:46:27 -0700 | [diff] [blame] | 166 | if (lh.subdued()) { |
| 167 | n.put(SUBDUE, true); |
| 168 | } |
| 169 | return n; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Translates the given property panel into JSON, for returning |
| 174 | * to the client. |
| 175 | * |
| 176 | * @param pp the property panel model |
| 177 | * @return JSON payload |
| 178 | */ |
| 179 | public static ObjectNode json(PropertyPanel pp) { |
| 180 | ObjectNode result = objectNode() |
| 181 | .put(TITLE, pp.title()) |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 182 | .put(GLYPH_ID, pp.glyphId()) |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 183 | .put(ID, pp.id()); |
| 184 | |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 185 | if (pp.navPath() != null) { |
| 186 | result.put(NAV_PATH, pp.navPath()); |
| 187 | } |
| 188 | |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 189 | ObjectNode plabels = objectNode(); |
| 190 | ObjectNode pvalues = objectNode(); |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 191 | ArrayNode porder = arrayNode(); |
| 192 | for (PropertyPanel.Prop p : pp.properties()) { |
| 193 | porder.add(p.key()); |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 194 | plabels.put(p.key(), p.label()); |
| 195 | pvalues.put(p.key(), p.value()); |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 196 | } |
| 197 | result.set(PROP_ORDER, porder); |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 198 | result.set(PROP_LABELS, plabels); |
| 199 | result.set(PROP_VALUES, pvalues); |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 200 | |
| 201 | ArrayNode buttons = arrayNode(); |
| 202 | for (ButtonId b : pp.buttons()) { |
| 203 | buttons.add(b.id()); |
| 204 | } |
| 205 | result.set(BUTTONS, buttons); |
| 206 | return result; |
| 207 | } |
| 208 | |
| 209 | } |