blob: 653b0f9e6f92d5af07b624b0daa1727dc3a7e644 [file] [log] [blame]
Simon Huntd3ceffa2015-08-25 12:44:35 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Huntd3ceffa2015-08-25 12:44:35 -07003 *
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 Huntd3ceffa2015-08-25 12:44:35 -070015 */
16
Simon Hunt52560662015-08-27 22:46:44 -070017package org.onosproject.ui.topo;
Simon Huntd3ceffa2015-08-25 12:44:35 -070018
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Hunt52560662015-08-27 22:46:44 -070022
23import static org.onosproject.ui.JsonUtils.envelope;
Simon Huntd3ceffa2015-08-25 12:44:35 -070024
25/**
26 * JSON utilities for the Topology View.
27 */
28public final class TopoJson {
Simon Hunt743a8492015-08-25 16:18:19 -070029 // package-private for unit test access
Simon Hunt52560662015-08-27 22:46:44 -070030 static final String SHOW_HIGHLIGHTS = "showHighlights";
Simon Hunt8e258112017-05-05 13:19:04 -070031 static final String TOPO2_HIGHLIGHTS = "topo2Highlights";
Simon Hunt52560662015-08-27 22:46:44 -070032
Simon Hunt743a8492015-08-25 16:18:19 -070033 static final String DEVICES = "devices";
34 static final String HOSTS = "hosts";
35 static final String LINKS = "links";
36 static final String SUBDUE = "subdue";
Andrea Campanella2dc91dc2015-12-07 12:17:02 -080037 static final String DELAY = "delay";
Simon Huntd3ceffa2015-08-25 12:44:35 -070038
Simon Hunt743a8492015-08-25 16:18:19 -070039 static final String ID = "id";
40 static final String LABEL = "label";
41 static final String CSS = "css";
Simon Hunt85897572015-10-20 16:29:51 -070042 static final String BADGE = "badge";
Simon Hunte9343f32015-10-21 18:07:46 -070043 static final String STATUS = "status";
44 static final String TXT = "txt";
45 static final String GID = "gid";
Simon Hunt85897572015-10-20 16:29:51 -070046 static final String MSG = "msg";
Simon Huntd3ceffa2015-08-25 12:44:35 -070047
Simon Hunt743a8492015-08-25 16:18:19 -070048 static final String TITLE = "title";
49 static final String TYPE = "type";
50 static final String PROP_ORDER = "propOrder";
51 static final String PROPS = "props";
52 static final String BUTTONS = "buttons";
Simon Huntd3ceffa2015-08-25 12:44:35 -070053
54
55 private static final ObjectMapper MAPPER = new ObjectMapper();
56
57 private static ObjectNode objectNode() {
58 return MAPPER.createObjectNode();
59 }
60
61 private static ArrayNode arrayNode() {
62 return MAPPER.createArrayNode();
63 }
64
65 // non-instantiable
66 private TopoJson() { }
67
68 /**
Simon Hunt52560662015-08-27 22:46:44 -070069 * Returns a formatted message ready to send to the topology view
70 * to render highlights.
71 *
72 * @param highlights highlights model to transform
73 * @return fully formatted "show highlights" message
74 */
75 public static ObjectNode highlightsMessage(Highlights highlights) {
76 return envelope(SHOW_HIGHLIGHTS, json(highlights));
77 }
78
79 /**
Simon Hunt8e258112017-05-05 13:19:04 -070080 * Returns a formatted message ready to send to the topology-2 view
81 * to render highlights.
82 *
83 * @param highlights highlights model to transform
84 * @return fully formatted "show highlights" message
85 */
86 public static ObjectNode topo2HighlightsMessage(Highlights highlights) {
87 return envelope(TOPO2_HIGHLIGHTS, json(highlights));
88 }
89
90 /**
Simon Huntd3ceffa2015-08-25 12:44:35 -070091 * Transforms the given highlights model into a JSON message payload.
92 *
93 * @param highlights the model to transform
94 * @return JSON payload
95 */
96 public static ObjectNode json(Highlights highlights) {
97 ObjectNode payload = objectNode();
98
99 ArrayNode devices = arrayNode();
100 ArrayNode hosts = arrayNode();
101 ArrayNode links = arrayNode();
102
103 payload.set(DEVICES, devices);
104 payload.set(HOSTS, hosts);
105 payload.set(LINKS, links);
106
107 highlights.devices().forEach(dh -> devices.add(json(dh)));
108 highlights.hosts().forEach(hh -> hosts.add(json(hh)));
109 highlights.links().forEach(lh -> links.add(json(lh)));
110
Simon Hunt743a8492015-08-25 16:18:19 -0700111 Highlights.Amount toSubdue = highlights.subdueLevel();
112 if (!toSubdue.equals(Highlights.Amount.ZERO)) {
113 payload.put(SUBDUE, toSubdue.toString());
114 }
Andrea Campanella2dc91dc2015-12-07 12:17:02 -0800115 int delay = highlights.delayMs();
116 if (delay > 0) {
117 payload.put(DELAY, delay);
118 }
Simon Huntd3ceffa2015-08-25 12:44:35 -0700119 return payload;
120 }
121
Simon Hunte9343f32015-10-21 18:07:46 -0700122 private static ObjectNode json(NodeBadge b) {
123 ObjectNode n = objectNode()
124 .put(STATUS, b.status().code())
125 .put(b.isGlyph() ? GID : TXT, b.text());
126 if (b.message() != null) {
127 n.put(MSG, b.message());
128 }
129 return n;
130 }
131
Simon Huntd3ceffa2015-08-25 12:44:35 -0700132 private static ObjectNode json(DeviceHighlight dh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700133 ObjectNode n = objectNode()
Simon Hunt94f7dae2015-08-26 17:40:59 -0700134 .put(ID, dh.elementId());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700135 if (dh.subdued()) {
136 n.put(SUBDUE, true);
137 }
Simon Hunt85897572015-10-20 16:29:51 -0700138 NodeBadge badge = dh.badge();
139 if (badge != null) {
Simon Hunte9343f32015-10-21 18:07:46 -0700140 n.set(BADGE, json(badge));
Simon Hunt85897572015-10-20 16:29:51 -0700141 }
Simon Hunt5b3ff902015-08-27 09:46:27 -0700142 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700143 }
144
145 private static ObjectNode json(HostHighlight hh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700146 ObjectNode n = objectNode()
Simon Hunt94f7dae2015-08-26 17:40:59 -0700147 .put(ID, hh.elementId());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700148 if (hh.subdued()) {
149 n.put(SUBDUE, true);
150 }
Andrea Campanella52125412015-12-03 14:50:40 -0800151 NodeBadge badge = hh.badge();
152 if (badge != null) {
153 n.set(BADGE, json(badge));
154 }
Simon Hunt5b3ff902015-08-27 09:46:27 -0700155 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700156 }
157
158 private static ObjectNode json(LinkHighlight lh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700159 ObjectNode n = objectNode()
Simon Huntd3ceffa2015-08-25 12:44:35 -0700160 .put(ID, lh.elementId())
161 .put(LABEL, lh.label())
162 .put(CSS, lh.cssClasses());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700163 if (lh.subdued()) {
164 n.put(SUBDUE, true);
165 }
166 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700167 }
168
169 /**
170 * Translates the given property panel into JSON, for returning
171 * to the client.
172 *
173 * @param pp the property panel model
174 * @return JSON payload
175 */
176 public static ObjectNode json(PropertyPanel pp) {
177 ObjectNode result = objectNode()
178 .put(TITLE, pp.title())
179 .put(TYPE, pp.typeId())
180 .put(ID, pp.id());
181
182 ObjectNode pnode = objectNode();
183 ArrayNode porder = arrayNode();
184 for (PropertyPanel.Prop p : pp.properties()) {
185 porder.add(p.key());
186 pnode.put(p.key(), p.value());
187 }
188 result.set(PROP_ORDER, porder);
189 result.set(PROPS, pnode);
190
191 ArrayNode buttons = arrayNode();
192 for (ButtonId b : pp.buttons()) {
193 buttons.add(b.id());
194 }
195 result.set(BUTTONS, buttons);
196 return result;
197 }
198
199}