blob: 13e131de215d3418d35eec5f30d2182e33c56c13 [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";
Simon Hunt10618f62017-06-15 19:30:52 -070050 static final String NAV_PATH = "navPath";
Simon Hunt743a8492015-08-25 16:18:19 -070051 static final String PROP_ORDER = "propOrder";
52 static final String PROPS = "props";
53 static final String BUTTONS = "buttons";
Simon Huntd3ceffa2015-08-25 12:44:35 -070054
55
56 private static final ObjectMapper MAPPER = new ObjectMapper();
57
58 private static ObjectNode objectNode() {
59 return MAPPER.createObjectNode();
60 }
61
62 private static ArrayNode arrayNode() {
63 return MAPPER.createArrayNode();
64 }
65
66 // non-instantiable
Simon Hunt10618f62017-06-15 19:30:52 -070067 private TopoJson() {
68 }
Simon Huntd3ceffa2015-08-25 12:44:35 -070069
70 /**
Simon Hunt52560662015-08-27 22:46:44 -070071 * Returns a formatted message ready to send to the topology view
72 * to render highlights.
73 *
74 * @param highlights highlights model to transform
75 * @return fully formatted "show highlights" message
76 */
77 public static ObjectNode highlightsMessage(Highlights highlights) {
78 return envelope(SHOW_HIGHLIGHTS, json(highlights));
79 }
80
81 /**
Simon Hunt8e258112017-05-05 13:19:04 -070082 * Returns a formatted message ready to send to the topology-2 view
83 * to render highlights.
84 *
85 * @param highlights highlights model to transform
86 * @return fully formatted "show highlights" message
87 */
88 public static ObjectNode topo2HighlightsMessage(Highlights highlights) {
89 return envelope(TOPO2_HIGHLIGHTS, json(highlights));
90 }
91
92 /**
Simon Huntd3ceffa2015-08-25 12:44:35 -070093 * Transforms the given highlights model into a JSON message payload.
94 *
95 * @param highlights the model to transform
96 * @return JSON payload
97 */
98 public static ObjectNode json(Highlights highlights) {
99 ObjectNode payload = objectNode();
100
101 ArrayNode devices = arrayNode();
102 ArrayNode hosts = arrayNode();
103 ArrayNode links = arrayNode();
104
105 payload.set(DEVICES, devices);
106 payload.set(HOSTS, hosts);
107 payload.set(LINKS, links);
108
109 highlights.devices().forEach(dh -> devices.add(json(dh)));
110 highlights.hosts().forEach(hh -> hosts.add(json(hh)));
111 highlights.links().forEach(lh -> links.add(json(lh)));
112
Simon Hunt743a8492015-08-25 16:18:19 -0700113 Highlights.Amount toSubdue = highlights.subdueLevel();
114 if (!toSubdue.equals(Highlights.Amount.ZERO)) {
115 payload.put(SUBDUE, toSubdue.toString());
116 }
Andrea Campanella2dc91dc2015-12-07 12:17:02 -0800117 int delay = highlights.delayMs();
118 if (delay > 0) {
119 payload.put(DELAY, delay);
120 }
Simon Huntd3ceffa2015-08-25 12:44:35 -0700121 return payload;
122 }
123
Simon Hunte9343f32015-10-21 18:07:46 -0700124 private static ObjectNode json(NodeBadge b) {
125 ObjectNode n = objectNode()
126 .put(STATUS, b.status().code())
127 .put(b.isGlyph() ? GID : TXT, b.text());
128 if (b.message() != null) {
129 n.put(MSG, b.message());
130 }
131 return n;
132 }
133
Simon Huntd3ceffa2015-08-25 12:44:35 -0700134 private static ObjectNode json(DeviceHighlight dh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700135 ObjectNode n = objectNode()
Simon Hunt94f7dae2015-08-26 17:40:59 -0700136 .put(ID, dh.elementId());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700137 if (dh.subdued()) {
138 n.put(SUBDUE, true);
139 }
Simon Hunt85897572015-10-20 16:29:51 -0700140 NodeBadge badge = dh.badge();
141 if (badge != null) {
Simon Hunte9343f32015-10-21 18:07:46 -0700142 n.set(BADGE, json(badge));
Simon Hunt85897572015-10-20 16:29:51 -0700143 }
Simon Hunt5b3ff902015-08-27 09:46:27 -0700144 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700145 }
146
147 private static ObjectNode json(HostHighlight hh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700148 ObjectNode n = objectNode()
Simon Hunt94f7dae2015-08-26 17:40:59 -0700149 .put(ID, hh.elementId());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700150 if (hh.subdued()) {
151 n.put(SUBDUE, true);
152 }
Andrea Campanella52125412015-12-03 14:50:40 -0800153 NodeBadge badge = hh.badge();
154 if (badge != null) {
155 n.set(BADGE, json(badge));
156 }
Simon Hunt5b3ff902015-08-27 09:46:27 -0700157 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700158 }
159
160 private static ObjectNode json(LinkHighlight lh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700161 ObjectNode n = objectNode()
Simon Huntd3ceffa2015-08-25 12:44:35 -0700162 .put(ID, lh.elementId())
163 .put(LABEL, lh.label())
164 .put(CSS, lh.cssClasses());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700165 if (lh.subdued()) {
166 n.put(SUBDUE, true);
167 }
168 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700169 }
170
171 /**
172 * Translates the given property panel into JSON, for returning
173 * to the client.
174 *
175 * @param pp the property panel model
176 * @return JSON payload
177 */
178 public static ObjectNode json(PropertyPanel pp) {
179 ObjectNode result = objectNode()
180 .put(TITLE, pp.title())
181 .put(TYPE, pp.typeId())
182 .put(ID, pp.id());
183
Simon Hunt10618f62017-06-15 19:30:52 -0700184 if (pp.navPath() != null) {
185 result.put(NAV_PATH, pp.navPath());
186 }
187
Simon Huntd3ceffa2015-08-25 12:44:35 -0700188 ObjectNode pnode = objectNode();
189 ArrayNode porder = arrayNode();
190 for (PropertyPanel.Prop p : pp.properties()) {
191 porder.add(p.key());
192 pnode.put(p.key(), p.value());
193 }
194 result.set(PROP_ORDER, porder);
195 result.set(PROPS, pnode);
196
197 ArrayNode buttons = arrayNode();
198 for (ButtonId b : pp.buttons()) {
199 buttons.add(b.id());
200 }
201 result.set(BUTTONS, buttons);
202 return result;
203 }
204
205}