blob: 8df031691209b5a9010b64f856fc9b053ba3b667 [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 Huntd3ceffa2015-08-25 12:44:35 -070040
Simon Hunt743a8492015-08-25 16:18:19 -070041 static final String TITLE = "title";
42 static final String TYPE = "type";
43 static final String PROP_ORDER = "propOrder";
44 static final String PROPS = "props";
45 static final String BUTTONS = "buttons";
Simon Huntd3ceffa2015-08-25 12:44:35 -070046
47
48 private static final ObjectMapper MAPPER = new ObjectMapper();
49
50 private static ObjectNode objectNode() {
51 return MAPPER.createObjectNode();
52 }
53
54 private static ArrayNode arrayNode() {
55 return MAPPER.createArrayNode();
56 }
57
58 // non-instantiable
59 private TopoJson() { }
60
61 /**
Simon Hunt52560662015-08-27 22:46:44 -070062 * Returns a formatted message ready to send to the topology view
63 * to render highlights.
64 *
65 * @param highlights highlights model to transform
66 * @return fully formatted "show highlights" message
67 */
68 public static ObjectNode highlightsMessage(Highlights highlights) {
69 return envelope(SHOW_HIGHLIGHTS, json(highlights));
70 }
71
72 /**
Simon Huntd3ceffa2015-08-25 12:44:35 -070073 * Transforms the given highlights model into a JSON message payload.
74 *
75 * @param highlights the model to transform
76 * @return JSON payload
77 */
78 public static ObjectNode json(Highlights highlights) {
79 ObjectNode payload = objectNode();
80
81 ArrayNode devices = arrayNode();
82 ArrayNode hosts = arrayNode();
83 ArrayNode links = arrayNode();
84
85 payload.set(DEVICES, devices);
86 payload.set(HOSTS, hosts);
87 payload.set(LINKS, links);
88
89 highlights.devices().forEach(dh -> devices.add(json(dh)));
90 highlights.hosts().forEach(hh -> hosts.add(json(hh)));
91 highlights.links().forEach(lh -> links.add(json(lh)));
92
Simon Hunt743a8492015-08-25 16:18:19 -070093 Highlights.Amount toSubdue = highlights.subdueLevel();
94 if (!toSubdue.equals(Highlights.Amount.ZERO)) {
95 payload.put(SUBDUE, toSubdue.toString());
96 }
Simon Huntd3ceffa2015-08-25 12:44:35 -070097 return payload;
98 }
99
100 private static ObjectNode json(DeviceHighlight dh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700101 ObjectNode n = objectNode()
Simon Hunt94f7dae2015-08-26 17:40:59 -0700102 .put(ID, dh.elementId());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700103 if (dh.subdued()) {
104 n.put(SUBDUE, true);
105 }
106 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700107 }
108
109 private static ObjectNode json(HostHighlight hh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700110 ObjectNode n = objectNode()
Simon Hunt94f7dae2015-08-26 17:40:59 -0700111 .put(ID, hh.elementId());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700112 if (hh.subdued()) {
113 n.put(SUBDUE, true);
114 }
115 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700116 }
117
118 private static ObjectNode json(LinkHighlight lh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700119 ObjectNode n = objectNode()
Simon Huntd3ceffa2015-08-25 12:44:35 -0700120 .put(ID, lh.elementId())
121 .put(LABEL, lh.label())
122 .put(CSS, lh.cssClasses());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700123 if (lh.subdued()) {
124 n.put(SUBDUE, true);
125 }
126 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700127 }
128
129 /**
130 * Translates the given property panel into JSON, for returning
131 * to the client.
132 *
133 * @param pp the property panel model
134 * @return JSON payload
135 */
136 public static ObjectNode json(PropertyPanel pp) {
137 ObjectNode result = objectNode()
138 .put(TITLE, pp.title())
139 .put(TYPE, pp.typeId())
140 .put(ID, pp.id());
141
142 ObjectNode pnode = objectNode();
143 ArrayNode porder = arrayNode();
144 for (PropertyPanel.Prop p : pp.properties()) {
145 porder.add(p.key());
146 pnode.put(p.key(), p.value());
147 }
148 result.set(PROP_ORDER, porder);
149 result.set(PROPS, pnode);
150
151 ArrayNode buttons = arrayNode();
152 for (ButtonId b : pp.buttons()) {
153 buttons.add(b.id());
154 }
155 result.set(BUTTONS, buttons);
156 return result;
157 }
158
159}