blob: d9edbd753d1e1c218a14dbbd48306e92e9ed8a86 [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";
31
Simon Hunt743a8492015-08-25 16:18:19 -070032 static final String DEVICES = "devices";
33 static final String HOSTS = "hosts";
34 static final String LINKS = "links";
35 static final String SUBDUE = "subdue";
Andrea Campanella2dc91dc2015-12-07 12:17:02 -080036 static final String DELAY = "delay";
Simon Huntd3ceffa2015-08-25 12:44:35 -070037
Simon Hunt743a8492015-08-25 16:18:19 -070038 static final String ID = "id";
39 static final String LABEL = "label";
40 static final String CSS = "css";
Simon Hunt85897572015-10-20 16:29:51 -070041 static final String BADGE = "badge";
Simon Hunte9343f32015-10-21 18:07:46 -070042 static final String STATUS = "status";
43 static final String TXT = "txt";
44 static final String GID = "gid";
Simon Hunt85897572015-10-20 16:29:51 -070045 static final String MSG = "msg";
Simon Huntd3ceffa2015-08-25 12:44:35 -070046
Simon Hunt743a8492015-08-25 16:18:19 -070047 static final String TITLE = "title";
48 static final String TYPE = "type";
49 static final String PROP_ORDER = "propOrder";
50 static final String PROPS = "props";
51 static final String BUTTONS = "buttons";
Simon Huntd3ceffa2015-08-25 12:44:35 -070052
53
54 private static final ObjectMapper MAPPER = new ObjectMapper();
55
56 private static ObjectNode objectNode() {
57 return MAPPER.createObjectNode();
58 }
59
60 private static ArrayNode arrayNode() {
61 return MAPPER.createArrayNode();
62 }
63
64 // non-instantiable
65 private TopoJson() { }
66
67 /**
Simon Hunt52560662015-08-27 22:46:44 -070068 * Returns a formatted message ready to send to the topology view
69 * to render highlights.
70 *
71 * @param highlights highlights model to transform
72 * @return fully formatted "show highlights" message
73 */
74 public static ObjectNode highlightsMessage(Highlights highlights) {
75 return envelope(SHOW_HIGHLIGHTS, json(highlights));
76 }
77
78 /**
Simon Huntd3ceffa2015-08-25 12:44:35 -070079 * Transforms the given highlights model into a JSON message payload.
80 *
81 * @param highlights the model to transform
82 * @return JSON payload
83 */
84 public static ObjectNode json(Highlights highlights) {
85 ObjectNode payload = objectNode();
86
87 ArrayNode devices = arrayNode();
88 ArrayNode hosts = arrayNode();
89 ArrayNode links = arrayNode();
90
91 payload.set(DEVICES, devices);
92 payload.set(HOSTS, hosts);
93 payload.set(LINKS, links);
94
95 highlights.devices().forEach(dh -> devices.add(json(dh)));
96 highlights.hosts().forEach(hh -> hosts.add(json(hh)));
97 highlights.links().forEach(lh -> links.add(json(lh)));
98
Simon Hunt743a8492015-08-25 16:18:19 -070099 Highlights.Amount toSubdue = highlights.subdueLevel();
100 if (!toSubdue.equals(Highlights.Amount.ZERO)) {
101 payload.put(SUBDUE, toSubdue.toString());
102 }
Andrea Campanella2dc91dc2015-12-07 12:17:02 -0800103 int delay = highlights.delayMs();
104 if (delay > 0) {
105 payload.put(DELAY, delay);
106 }
Simon Huntd3ceffa2015-08-25 12:44:35 -0700107 return payload;
108 }
109
Simon Hunte9343f32015-10-21 18:07:46 -0700110 private static ObjectNode json(NodeBadge b) {
111 ObjectNode n = objectNode()
112 .put(STATUS, b.status().code())
113 .put(b.isGlyph() ? GID : TXT, b.text());
114 if (b.message() != null) {
115 n.put(MSG, b.message());
116 }
117 return n;
118 }
119
Simon Huntd3ceffa2015-08-25 12:44:35 -0700120 private static ObjectNode json(DeviceHighlight dh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700121 ObjectNode n = objectNode()
Simon Hunt94f7dae2015-08-26 17:40:59 -0700122 .put(ID, dh.elementId());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700123 if (dh.subdued()) {
124 n.put(SUBDUE, true);
125 }
Simon Hunt85897572015-10-20 16:29:51 -0700126 NodeBadge badge = dh.badge();
127 if (badge != null) {
Simon Hunte9343f32015-10-21 18:07:46 -0700128 n.set(BADGE, json(badge));
Simon Hunt85897572015-10-20 16:29:51 -0700129 }
Simon Hunt5b3ff902015-08-27 09:46:27 -0700130 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700131 }
132
133 private static ObjectNode json(HostHighlight hh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700134 ObjectNode n = objectNode()
Simon Hunt94f7dae2015-08-26 17:40:59 -0700135 .put(ID, hh.elementId());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700136 if (hh.subdued()) {
137 n.put(SUBDUE, true);
138 }
Andrea Campanella52125412015-12-03 14:50:40 -0800139 NodeBadge badge = hh.badge();
140 if (badge != null) {
141 n.set(BADGE, json(badge));
142 }
Simon Hunt5b3ff902015-08-27 09:46:27 -0700143 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700144 }
145
146 private static ObjectNode json(LinkHighlight lh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700147 ObjectNode n = objectNode()
Simon Huntd3ceffa2015-08-25 12:44:35 -0700148 .put(ID, lh.elementId())
149 .put(LABEL, lh.label())
150 .put(CSS, lh.cssClasses());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700151 if (lh.subdued()) {
152 n.put(SUBDUE, true);
153 }
154 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700155 }
156
157 /**
158 * Translates the given property panel into JSON, for returning
159 * to the client.
160 *
161 * @param pp the property panel model
162 * @return JSON payload
163 */
164 public static ObjectNode json(PropertyPanel pp) {
165 ObjectNode result = objectNode()
166 .put(TITLE, pp.title())
167 .put(TYPE, pp.typeId())
168 .put(ID, pp.id());
169
170 ObjectNode pnode = objectNode();
171 ArrayNode porder = arrayNode();
172 for (PropertyPanel.Prop p : pp.properties()) {
173 porder.add(p.key());
174 pnode.put(p.key(), p.value());
175 }
176 result.set(PROP_ORDER, porder);
177 result.set(PROPS, pnode);
178
179 ArrayNode buttons = arrayNode();
180 for (ButtonId b : pp.buttons()) {
181 buttons.add(b.id());
182 }
183 result.set(BUTTONS, buttons);
184 return result;
185 }
186
187}