blob: efe69f5f91ac0fb5fdaf8e914355ba171d54dcbe [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";
Simon Hunte9343f32015-10-21 18:07:46 -070041 static final String STATUS = "status";
42 static final String TXT = "txt";
43 static final String GID = "gid";
Simon Hunt85897572015-10-20 16:29:51 -070044 static final String MSG = "msg";
Simon Huntd3ceffa2015-08-25 12:44:35 -070045
Simon Hunt743a8492015-08-25 16:18:19 -070046 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 Huntd3ceffa2015-08-25 12:44:35 -070051
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 Hunt52560662015-08-27 22:46:44 -070067 * 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 Huntd3ceffa2015-08-25 12:44:35 -070078 * 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 Hunt743a8492015-08-25 16:18:19 -070098 Highlights.Amount toSubdue = highlights.subdueLevel();
99 if (!toSubdue.equals(Highlights.Amount.ZERO)) {
100 payload.put(SUBDUE, toSubdue.toString());
101 }
Simon Huntd3ceffa2015-08-25 12:44:35 -0700102 return payload;
103 }
104
Simon Hunte9343f32015-10-21 18:07:46 -0700105 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 Huntd3ceffa2015-08-25 12:44:35 -0700115 private static ObjectNode json(DeviceHighlight dh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700116 ObjectNode n = objectNode()
Simon Hunt94f7dae2015-08-26 17:40:59 -0700117 .put(ID, dh.elementId());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700118 if (dh.subdued()) {
119 n.put(SUBDUE, true);
120 }
Simon Hunt85897572015-10-20 16:29:51 -0700121 NodeBadge badge = dh.badge();
122 if (badge != null) {
Simon Hunte9343f32015-10-21 18:07:46 -0700123 n.set(BADGE, json(badge));
Simon Hunt85897572015-10-20 16:29:51 -0700124 }
Simon Hunt5b3ff902015-08-27 09:46:27 -0700125 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700126 }
127
128 private static ObjectNode json(HostHighlight hh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700129 ObjectNode n = objectNode()
Simon Hunt94f7dae2015-08-26 17:40:59 -0700130 .put(ID, hh.elementId());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700131 if (hh.subdued()) {
132 n.put(SUBDUE, true);
133 }
Andrea Campanella52125412015-12-03 14:50:40 -0800134 NodeBadge badge = hh.badge();
135 if (badge != null) {
136 n.set(BADGE, json(badge));
137 }
Simon Hunt5b3ff902015-08-27 09:46:27 -0700138 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700139 }
140
141 private static ObjectNode json(LinkHighlight lh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700142 ObjectNode n = objectNode()
Simon Huntd3ceffa2015-08-25 12:44:35 -0700143 .put(ID, lh.elementId())
144 .put(LABEL, lh.label())
145 .put(CSS, lh.cssClasses());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700146 if (lh.subdued()) {
147 n.put(SUBDUE, true);
148 }
149 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700150 }
151
152 /**
153 * Translates the given property panel into JSON, for returning
154 * to the client.
155 *
156 * @param pp the property panel model
157 * @return JSON payload
158 */
159 public static ObjectNode json(PropertyPanel pp) {
160 ObjectNode result = objectNode()
161 .put(TITLE, pp.title())
162 .put(TYPE, pp.typeId())
163 .put(ID, pp.id());
164
165 ObjectNode pnode = objectNode();
166 ArrayNode porder = arrayNode();
167 for (PropertyPanel.Prop p : pp.properties()) {
168 porder.add(p.key());
169 pnode.put(p.key(), p.value());
170 }
171 result.set(PROP_ORDER, porder);
172 result.set(PROPS, pnode);
173
174 ArrayNode buttons = arrayNode();
175 for (ButtonId b : pp.buttons()) {
176 buttons.add(b.id());
177 }
178 result.set(BUTTONS, buttons);
179 return result;
180 }
181
182}