blob: a94068eeb769a35a0d7333c310cf243f8a4ab801 [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.
15 *
16 */
17
Simon Hunt52560662015-08-27 22:46:44 -070018package org.onosproject.ui.topo;
Simon Huntd3ceffa2015-08-25 12:44:35 -070019
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ArrayNode;
22import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Hunt52560662015-08-27 22:46:44 -070023
24import static org.onosproject.ui.JsonUtils.envelope;
Simon Huntd3ceffa2015-08-25 12:44:35 -070025
26/**
27 * JSON utilities for the Topology View.
28 */
29public final class TopoJson {
Simon Hunt743a8492015-08-25 16:18:19 -070030 // package-private for unit test access
Simon Hunt52560662015-08-27 22:46:44 -070031 static final String SHOW_HIGHLIGHTS = "showHighlights";
32
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";
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 Huntd3ceffa2015-08-25 12:44:35 -070041
Simon Hunt743a8492015-08-25 16:18:19 -070042 static final String TITLE = "title";
43 static final String TYPE = "type";
44 static final String PROP_ORDER = "propOrder";
45 static final String PROPS = "props";
46 static final String BUTTONS = "buttons";
Simon Huntd3ceffa2015-08-25 12:44:35 -070047
48
49 private static final ObjectMapper MAPPER = new ObjectMapper();
50
51 private static ObjectNode objectNode() {
52 return MAPPER.createObjectNode();
53 }
54
55 private static ArrayNode arrayNode() {
56 return MAPPER.createArrayNode();
57 }
58
59 // non-instantiable
60 private TopoJson() { }
61
62 /**
Simon Hunt52560662015-08-27 22:46:44 -070063 * Returns a formatted message ready to send to the topology view
64 * to render highlights.
65 *
66 * @param highlights highlights model to transform
67 * @return fully formatted "show highlights" message
68 */
69 public static ObjectNode highlightsMessage(Highlights highlights) {
70 return envelope(SHOW_HIGHLIGHTS, json(highlights));
71 }
72
73 /**
Simon Huntd3ceffa2015-08-25 12:44:35 -070074 * Transforms the given highlights model into a JSON message payload.
75 *
76 * @param highlights the model to transform
77 * @return JSON payload
78 */
79 public static ObjectNode json(Highlights highlights) {
80 ObjectNode payload = objectNode();
81
82 ArrayNode devices = arrayNode();
83 ArrayNode hosts = arrayNode();
84 ArrayNode links = arrayNode();
85
86 payload.set(DEVICES, devices);
87 payload.set(HOSTS, hosts);
88 payload.set(LINKS, links);
89
90 highlights.devices().forEach(dh -> devices.add(json(dh)));
91 highlights.hosts().forEach(hh -> hosts.add(json(hh)));
92 highlights.links().forEach(lh -> links.add(json(lh)));
93
Simon Hunt743a8492015-08-25 16:18:19 -070094 Highlights.Amount toSubdue = highlights.subdueLevel();
95 if (!toSubdue.equals(Highlights.Amount.ZERO)) {
96 payload.put(SUBDUE, toSubdue.toString());
97 }
Simon Huntd3ceffa2015-08-25 12:44:35 -070098 return payload;
99 }
100
101 private static ObjectNode json(DeviceHighlight dh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700102 ObjectNode n = objectNode()
Simon Hunt94f7dae2015-08-26 17:40:59 -0700103 .put(ID, dh.elementId());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700104 if (dh.subdued()) {
105 n.put(SUBDUE, true);
106 }
107 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700108 }
109
110 private static ObjectNode json(HostHighlight hh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700111 ObjectNode n = objectNode()
Simon Hunt94f7dae2015-08-26 17:40:59 -0700112 .put(ID, hh.elementId());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700113 if (hh.subdued()) {
114 n.put(SUBDUE, true);
115 }
116 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700117 }
118
119 private static ObjectNode json(LinkHighlight lh) {
Simon Hunt5b3ff902015-08-27 09:46:27 -0700120 ObjectNode n = objectNode()
Simon Huntd3ceffa2015-08-25 12:44:35 -0700121 .put(ID, lh.elementId())
122 .put(LABEL, lh.label())
123 .put(CSS, lh.cssClasses());
Simon Hunt5b3ff902015-08-27 09:46:27 -0700124 if (lh.subdued()) {
125 n.put(SUBDUE, true);
126 }
127 return n;
Simon Huntd3ceffa2015-08-25 12:44:35 -0700128 }
129
130 /**
131 * Translates the given property panel into JSON, for returning
132 * to the client.
133 *
134 * @param pp the property panel model
135 * @return JSON payload
136 */
137 public static ObjectNode json(PropertyPanel pp) {
138 ObjectNode result = objectNode()
139 .put(TITLE, pp.title())
140 .put(TYPE, pp.typeId())
141 .put(ID, pp.id());
142
143 ObjectNode pnode = objectNode();
144 ArrayNode porder = arrayNode();
145 for (PropertyPanel.Prop p : pp.properties()) {
146 porder.add(p.key());
147 pnode.put(p.key(), p.value());
148 }
149 result.set(PROP_ORDER, porder);
150 result.set(PROPS, pnode);
151
152 ArrayNode buttons = arrayNode();
153 for (ButtonId b : pp.buttons()) {
154 buttons.add(b.id());
155 }
156 result.set(BUTTONS, buttons);
157 return result;
158 }
159
160}