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. |
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"; |
| 31 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 32 | static final String DEVICES = "devices"; |
| 33 | static final String HOSTS = "hosts"; |
| 34 | static final String LINKS = "links"; |
| 35 | static final String SUBDUE = "subdue"; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 36 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 37 | static final String ID = "id"; |
| 38 | static final String LABEL = "label"; |
| 39 | static final String CSS = "css"; |
Simon Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame] | 40 | static final String BADGE = "badge"; |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 41 | static final String STATUS = "status"; |
| 42 | static final String TXT = "txt"; |
| 43 | static final String GID = "gid"; |
Simon Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame] | 44 | static final String MSG = "msg"; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 45 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 46 | static final String TITLE = "title"; |
| 47 | static final String TYPE = "type"; |
| 48 | static final String PROP_ORDER = "propOrder"; |
| 49 | static final String PROPS = "props"; |
| 50 | static final String BUTTONS = "buttons"; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 51 | |
| 52 | |
| 53 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 54 | |
| 55 | private static ObjectNode objectNode() { |
| 56 | return MAPPER.createObjectNode(); |
| 57 | } |
| 58 | |
| 59 | private static ArrayNode arrayNode() { |
| 60 | return MAPPER.createArrayNode(); |
| 61 | } |
| 62 | |
| 63 | // non-instantiable |
| 64 | private TopoJson() { } |
| 65 | |
| 66 | /** |
Simon Hunt | 5256066 | 2015-08-27 22:46:44 -0700 | [diff] [blame] | 67 | * Returns a formatted message ready to send to the topology view |
| 68 | * to render highlights. |
| 69 | * |
| 70 | * @param highlights highlights model to transform |
| 71 | * @return fully formatted "show highlights" message |
| 72 | */ |
| 73 | public static ObjectNode highlightsMessage(Highlights highlights) { |
| 74 | return envelope(SHOW_HIGHLIGHTS, json(highlights)); |
| 75 | } |
| 76 | |
| 77 | /** |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 78 | * Transforms the given highlights model into a JSON message payload. |
| 79 | * |
| 80 | * @param highlights the model to transform |
| 81 | * @return JSON payload |
| 82 | */ |
| 83 | public static ObjectNode json(Highlights highlights) { |
| 84 | ObjectNode payload = objectNode(); |
| 85 | |
| 86 | ArrayNode devices = arrayNode(); |
| 87 | ArrayNode hosts = arrayNode(); |
| 88 | ArrayNode links = arrayNode(); |
| 89 | |
| 90 | payload.set(DEVICES, devices); |
| 91 | payload.set(HOSTS, hosts); |
| 92 | payload.set(LINKS, links); |
| 93 | |
| 94 | highlights.devices().forEach(dh -> devices.add(json(dh))); |
| 95 | highlights.hosts().forEach(hh -> hosts.add(json(hh))); |
| 96 | highlights.links().forEach(lh -> links.add(json(lh))); |
| 97 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 98 | Highlights.Amount toSubdue = highlights.subdueLevel(); |
| 99 | if (!toSubdue.equals(Highlights.Amount.ZERO)) { |
| 100 | payload.put(SUBDUE, toSubdue.toString()); |
| 101 | } |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 102 | return payload; |
| 103 | } |
| 104 | |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 105 | private static ObjectNode json(NodeBadge b) { |
| 106 | ObjectNode n = objectNode() |
| 107 | .put(STATUS, b.status().code()) |
| 108 | .put(b.isGlyph() ? GID : TXT, b.text()); |
| 109 | if (b.message() != null) { |
| 110 | n.put(MSG, b.message()); |
| 111 | } |
| 112 | return n; |
| 113 | } |
| 114 | |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 115 | private static ObjectNode json(DeviceHighlight dh) { |
Simon Hunt | 5b3ff90 | 2015-08-27 09:46:27 -0700 | [diff] [blame] | 116 | ObjectNode n = objectNode() |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 117 | .put(ID, dh.elementId()); |
Simon Hunt | 5b3ff90 | 2015-08-27 09:46:27 -0700 | [diff] [blame] | 118 | if (dh.subdued()) { |
| 119 | n.put(SUBDUE, true); |
| 120 | } |
Simon Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame] | 121 | NodeBadge badge = dh.badge(); |
| 122 | if (badge != null) { |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 123 | n.set(BADGE, json(badge)); |
Simon Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame] | 124 | } |
Simon Hunt | 5b3ff90 | 2015-08-27 09:46:27 -0700 | [diff] [blame] | 125 | return n; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | private static ObjectNode json(HostHighlight hh) { |
Simon Hunt | 5b3ff90 | 2015-08-27 09:46:27 -0700 | [diff] [blame] | 129 | ObjectNode n = objectNode() |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 130 | .put(ID, hh.elementId()); |
Simon Hunt | 5b3ff90 | 2015-08-27 09:46:27 -0700 | [diff] [blame] | 131 | if (hh.subdued()) { |
| 132 | n.put(SUBDUE, true); |
| 133 | } |
| 134 | return n; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | private static ObjectNode json(LinkHighlight lh) { |
Simon Hunt | 5b3ff90 | 2015-08-27 09:46:27 -0700 | [diff] [blame] | 138 | ObjectNode n = objectNode() |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 139 | .put(ID, lh.elementId()) |
| 140 | .put(LABEL, lh.label()) |
| 141 | .put(CSS, lh.cssClasses()); |
Simon Hunt | 5b3ff90 | 2015-08-27 09:46:27 -0700 | [diff] [blame] | 142 | if (lh.subdued()) { |
| 143 | n.put(SUBDUE, true); |
| 144 | } |
| 145 | return n; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Translates the given property panel into JSON, for returning |
| 150 | * to the client. |
| 151 | * |
| 152 | * @param pp the property panel model |
| 153 | * @return JSON payload |
| 154 | */ |
| 155 | public static ObjectNode json(PropertyPanel pp) { |
| 156 | ObjectNode result = objectNode() |
| 157 | .put(TITLE, pp.title()) |
| 158 | .put(TYPE, pp.typeId()) |
| 159 | .put(ID, pp.id()); |
| 160 | |
| 161 | ObjectNode pnode = objectNode(); |
| 162 | ArrayNode porder = arrayNode(); |
| 163 | for (PropertyPanel.Prop p : pp.properties()) { |
| 164 | porder.add(p.key()); |
| 165 | pnode.put(p.key(), p.value()); |
| 166 | } |
| 167 | result.set(PROP_ORDER, porder); |
| 168 | result.set(PROPS, pnode); |
| 169 | |
| 170 | ArrayNode buttons = arrayNode(); |
| 171 | for (ButtonId b : pp.buttons()) { |
| 172 | buttons.add(b.id()); |
| 173 | } |
| 174 | result.set(BUTTONS, buttons); |
| 175 | return result; |
| 176 | } |
| 177 | |
| 178 | } |