blob: d4990f3b07daf2ba6819112947b1ea74f11d6558 [file] [log] [blame]
Simon Huntd3ceffa2015-08-25 12:44:35 -07001/*
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 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";
Simon Huntd3ceffa2015-08-25 12:44:35 -070036
Simon Hunt743a8492015-08-25 16:18:19 -070037 static final String ID = "id";
38 static final String LABEL = "label";
39 static final String CSS = "css";
Simon Hunt85897572015-10-20 16:29:51 -070040 static final String BADGE = "badge";
41 static final String MSG = "msg";
Simon Huntd3ceffa2015-08-25 12:44:35 -070042
Simon Hunt743a8492015-08-25 16:18:19 -070043 static final String TITLE = "title";
44 static final String TYPE = "type";
45 static final String PROP_ORDER = "propOrder";
46 static final String PROPS = "props";
47 static final String BUTTONS = "buttons";
Simon Huntd3ceffa2015-08-25 12:44:35 -070048
49
50 private static final ObjectMapper MAPPER = new ObjectMapper();
51
52 private static ObjectNode objectNode() {
53 return MAPPER.createObjectNode();
54 }
55
56 private static ArrayNode arrayNode() {
57 return MAPPER.createArrayNode();
58 }
59
60 // non-instantiable
61 private TopoJson() { }
62
63 /**
Simon Hunt52560662015-08-27 22:46:44 -070064 * Returns a formatted message ready to send to the topology view
65 * to render highlights.
66 *
67 * @param highlights highlights model to transform
68 * @return fully formatted "show highlights" message
69 */
70 public static ObjectNode highlightsMessage(Highlights highlights) {
71 return envelope(SHOW_HIGHLIGHTS, json(highlights));
72 }
73
74 /**
Simon Huntd3ceffa2015-08-25 12:44:35 -070075 * Transforms the given highlights model into a JSON message payload.
76 *
77 * @param highlights the model to transform
78 * @return JSON payload
79 */
80 public static ObjectNode json(Highlights highlights) {
81 ObjectNode payload = objectNode();
82
83 ArrayNode devices = arrayNode();
84 ArrayNode hosts = arrayNode();
85 ArrayNode links = arrayNode();
86
87 payload.set(DEVICES, devices);
88 payload.set(HOSTS, hosts);
89 payload.set(LINKS, links);
90
91 highlights.devices().forEach(dh -> devices.add(json(dh)));
92 highlights.hosts().forEach(hh -> hosts.add(json(hh)));
93 highlights.links().forEach(lh -> links.add(json(lh)));
94
Simon Hunt743a8492015-08-25 16:18:19 -070095 Highlights.Amount toSubdue = highlights.subdueLevel();
96 if (!toSubdue.equals(Highlights.Amount.ZERO)) {
97 payload.put(SUBDUE, toSubdue.toString());
98 }
Simon Huntd3ceffa2015-08-25 12:44:35 -070099 return payload;
100 }
101
102 private static ObjectNode json(DeviceHighlight dh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700103 ObjectNode n = objectNode()
Simon Hunt94f7dae2015-08-26 17:40:59 -0700104 .put(ID, dh.elementId());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700105 if (dh.subdued()) {
106 n.put(SUBDUE, true);
107 }
Simon Hunt85897572015-10-20 16:29:51 -0700108 NodeBadge badge = dh.badge();
109 if (badge != null) {
110 ObjectNode b = objectNode()
111 .put(TYPE, badge.type().code())
112 .put(MSG, badge.message());
113 n.set(BADGE, b);
114 }
Simon Hunt5b3ff902015-08-27 09:46:27 -0700115 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700116 }
117
118 private static ObjectNode json(HostHighlight hh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700119 ObjectNode n = objectNode()
Simon Hunt94f7dae2015-08-26 17:40:59 -0700120 .put(ID, hh.elementId());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700121 if (hh.subdued()) {
122 n.put(SUBDUE, true);
123 }
124 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700125 }
126
127 private static ObjectNode json(LinkHighlight lh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700128 ObjectNode n = objectNode()
Simon Huntd3ceffa2015-08-25 12:44:35 -0700129 .put(ID, lh.elementId())
130 .put(LABEL, lh.label())
131 .put(CSS, lh.cssClasses());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700132 if (lh.subdued()) {
133 n.put(SUBDUE, true);
134 }
135 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700136 }
137
138 /**
139 * Translates the given property panel into JSON, for returning
140 * to the client.
141 *
142 * @param pp the property panel model
143 * @return JSON payload
144 */
145 public static ObjectNode json(PropertyPanel pp) {
146 ObjectNode result = objectNode()
147 .put(TITLE, pp.title())
148 .put(TYPE, pp.typeId())
149 .put(ID, pp.id());
150
151 ObjectNode pnode = objectNode();
152 ArrayNode porder = arrayNode();
153 for (PropertyPanel.Prop p : pp.properties()) {
154 porder.add(p.key());
155 pnode.put(p.key(), p.value());
156 }
157 result.set(PROP_ORDER, porder);
158 result.set(PROPS, pnode);
159
160 ArrayNode buttons = arrayNode();
161 for (ButtonId b : pp.buttons()) {
162 buttons.add(b.id());
163 }
164 result.set(BUTTONS, buttons);
165 return result;
166 }
167
168}