blob: 0a12e1ca40e5c01026388e3d644bca7dd3b133bf [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
18package org.onosproject.ui.impl.topo;
19
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ArrayNode;
22import com.fasterxml.jackson.databind.node.ObjectNode;
23import org.onosproject.ui.topo.ButtonId;
24import org.onosproject.ui.topo.DeviceHighlight;
25import org.onosproject.ui.topo.Highlights;
26import org.onosproject.ui.topo.HostHighlight;
27import org.onosproject.ui.topo.LinkHighlight;
28import org.onosproject.ui.topo.PropertyPanel;
29
30/**
31 * JSON utilities for the Topology View.
32 */
33public final class TopoJson {
Simon Hunt743a8492015-08-25 16:18:19 -070034 // package-private for unit test access
35 static final String DEVICES = "devices";
36 static final String HOSTS = "hosts";
37 static final String LINKS = "links";
38 static final String SUBDUE = "subdue";
Simon Huntd3ceffa2015-08-25 12:44:35 -070039
Simon Hunt743a8492015-08-25 16:18:19 -070040 static final String ID = "id";
41 static final String LABEL = "label";
42 static final String CSS = "css";
Simon Huntd3ceffa2015-08-25 12:44:35 -070043
Simon Hunt743a8492015-08-25 16:18:19 -070044 static final String TITLE = "title";
45 static final String TYPE = "type";
46 static final String PROP_ORDER = "propOrder";
47 static final String PROPS = "props";
48 static final String BUTTONS = "buttons";
Simon Huntd3ceffa2015-08-25 12:44:35 -070049
50
51 private static final ObjectMapper MAPPER = new ObjectMapper();
52
53 private static ObjectNode objectNode() {
54 return MAPPER.createObjectNode();
55 }
56
57 private static ArrayNode arrayNode() {
58 return MAPPER.createArrayNode();
59 }
60
61 // non-instantiable
62 private TopoJson() { }
63
64 /**
65 * Transforms the given highlights model into a JSON message payload.
66 *
67 * @param highlights the model to transform
68 * @return JSON payload
69 */
70 public static ObjectNode json(Highlights highlights) {
71 ObjectNode payload = objectNode();
72
73 ArrayNode devices = arrayNode();
74 ArrayNode hosts = arrayNode();
75 ArrayNode links = arrayNode();
76
77 payload.set(DEVICES, devices);
78 payload.set(HOSTS, hosts);
79 payload.set(LINKS, links);
80
81 highlights.devices().forEach(dh -> devices.add(json(dh)));
82 highlights.hosts().forEach(hh -> hosts.add(json(hh)));
83 highlights.links().forEach(lh -> links.add(json(lh)));
84
Simon Hunt743a8492015-08-25 16:18:19 -070085 Highlights.Amount toSubdue = highlights.subdueLevel();
86 if (!toSubdue.equals(Highlights.Amount.ZERO)) {
87 payload.put(SUBDUE, toSubdue.toString());
88 }
Simon Huntd3ceffa2015-08-25 12:44:35 -070089 return payload;
90 }
91
92 private static ObjectNode json(DeviceHighlight dh) {
93 // TODO: implement this once we know what a device highlight looks like
94 return objectNode();
95 }
96
97 private static ObjectNode json(HostHighlight hh) {
98 // TODO: implement this once we know what a host highlight looks like
99 return objectNode();
100 }
101
102 private static ObjectNode json(LinkHighlight lh) {
103 return objectNode()
104 .put(ID, lh.elementId())
105 .put(LABEL, lh.label())
106 .put(CSS, lh.cssClasses());
107 }
108
109 /**
110 * Translates the given property panel into JSON, for returning
111 * to the client.
112 *
113 * @param pp the property panel model
114 * @return JSON payload
115 */
116 public static ObjectNode json(PropertyPanel pp) {
117 ObjectNode result = objectNode()
118 .put(TITLE, pp.title())
119 .put(TYPE, pp.typeId())
120 .put(ID, pp.id());
121
122 ObjectNode pnode = objectNode();
123 ArrayNode porder = arrayNode();
124 for (PropertyPanel.Prop p : pp.properties()) {
125 porder.add(p.key());
126 pnode.put(p.key(), p.value());
127 }
128 result.set(PROP_ORDER, porder);
129 result.set(PROPS, pnode);
130
131 ArrayNode buttons = arrayNode();
132 for (ButtonId b : pp.buttons()) {
133 buttons.add(b.id());
134 }
135 result.set(BUTTONS, buttons);
136 return result;
137 }
138
139}