blob: 02df84cf6f2850cb01893f669fae5fc55e8c75f7 [file] [log] [blame]
Simon Huntd5b96732016-07-08 13:22:27 -07001/*
2 * Copyright 2016-present 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
17package org.onosproject.ui.impl.topo;
18
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import org.onlab.osgi.ServiceDirectory;
23import org.onosproject.cluster.ClusterService;
24import org.onosproject.cluster.NodeId;
25import org.onosproject.incubator.net.PortStatisticsService;
26import org.onosproject.incubator.net.tunnel.TunnelService;
27import org.onosproject.mastership.MastershipService;
28import org.onosproject.net.device.DeviceService;
29import org.onosproject.net.flow.FlowRuleService;
30import org.onosproject.net.host.HostService;
31import org.onosproject.net.intent.IntentService;
32import org.onosproject.net.link.LinkService;
33import org.onosproject.net.region.Region;
34import org.onosproject.net.statistic.StatisticService;
35import org.onosproject.net.topology.TopologyService;
36import org.onosproject.ui.model.topo.UiClusterMember;
37import org.onosproject.ui.model.topo.UiDevice;
38import org.onosproject.ui.model.topo.UiHost;
39import org.onosproject.ui.model.topo.UiLink;
Simon Hunt977aa052016-07-20 17:08:29 -070040import org.onosproject.ui.model.topo.UiNode;
Simon Huntd5b96732016-07-08 13:22:27 -070041import org.onosproject.ui.model.topo.UiRegion;
42import org.onosproject.ui.model.topo.UiTopoLayout;
43
Simon Hunt977aa052016-07-20 17:08:29 -070044import java.util.ArrayList;
45import java.util.HashMap;
46import java.util.HashSet;
Simon Huntd5b96732016-07-08 13:22:27 -070047import java.util.List;
Simon Hunt977aa052016-07-20 17:08:29 -070048import java.util.Map;
49import java.util.Set;
Simon Huntd5b96732016-07-08 13:22:27 -070050
51import static com.google.common.base.Preconditions.checkNotNull;
Simon Hunt977aa052016-07-20 17:08:29 -070052import static org.onosproject.ui.model.topo.UiNode.LAYER_DEFAULT;
Simon Huntd5b96732016-07-08 13:22:27 -070053
54/**
55 * Facility for creating JSON messages to send to the topology view in the
56 * Web client.
57 */
58class Topo2Jsonifier {
59
Simon Hunt977aa052016-07-20 17:08:29 -070060 private static final String E_DEF_NOT_LAST =
61 "UiNode.LAYER_DEFAULT not last in layer list";
62 private static final String E_UNKNOWN_UI_NODE =
63 "Unknown subclass of UiNode: ";
64
Simon Huntd5b96732016-07-08 13:22:27 -070065 private final ObjectMapper mapper = new ObjectMapper();
66
67 private ServiceDirectory directory;
68 private ClusterService clusterService;
69 private DeviceService deviceService;
70 private LinkService linkService;
71 private HostService hostService;
72 private MastershipService mastershipService;
73 private IntentService intentService;
74 private FlowRuleService flowService;
75 private StatisticService flowStatsService;
76 private PortStatisticsService portStatsService;
77 private TopologyService topologyService;
78 private TunnelService tunnelService;
79
80
81 /**
82 * Creates an instance with a reference to the services directory, so that
83 * additional information about network elements may be looked up on
84 * on the fly.
85 *
86 * @param directory service directory
87 */
88 Topo2Jsonifier(ServiceDirectory directory) {
89 this.directory = checkNotNull(directory, "Directory cannot be null");
90
91 clusterService = directory.get(ClusterService.class);
92 deviceService = directory.get(DeviceService.class);
93 linkService = directory.get(LinkService.class);
94 hostService = directory.get(HostService.class);
95 mastershipService = directory.get(MastershipService.class);
96 intentService = directory.get(IntentService.class);
97 flowService = directory.get(FlowRuleService.class);
98 flowStatsService = directory.get(StatisticService.class);
99 portStatsService = directory.get(PortStatisticsService.class);
100 topologyService = directory.get(TopologyService.class);
101 tunnelService = directory.get(TunnelService.class);
Simon Hunt977aa052016-07-20 17:08:29 -0700102 }
Simon Huntd5b96732016-07-08 13:22:27 -0700103
Simon Hunt977aa052016-07-20 17:08:29 -0700104 // for unit testing
105 Topo2Jsonifier() {
Simon Huntd5b96732016-07-08 13:22:27 -0700106 }
107
108 private ObjectNode objectNode() {
109 return mapper.createObjectNode();
110 }
111
112 private ArrayNode arrayNode() {
113 return mapper.createArrayNode();
114 }
115
116 private String nullIsEmpty(Object o) {
117 return o == null ? "" : o.toString();
118 }
119
120
121 /**
122 * Returns a JSON representation of the cluster members (ONOS instances).
123 *
124 * @param instances the instance model objects
125 * @return a JSON representation of the data
126 */
127 ObjectNode instances(List<UiClusterMember> instances) {
128 NodeId local = clusterService.getLocalNode().id();
129 ObjectNode payload = objectNode();
130
131 ArrayNode members = arrayNode();
132 payload.set("members", members);
133 for (UiClusterMember member : instances) {
134 members.add(json(member, member.id().equals(local)));
135 }
136
137 return payload;
138 }
139
140 private ObjectNode json(UiClusterMember member, boolean isUiAttached) {
141 return objectNode()
142 .put("id", member.id().toString())
143 .put("ip", member.ip().toString())
144 .put("online", member.isOnline())
145 .put("ready", member.isReady())
146 .put("uiAttached", isUiAttached)
147 .put("switches", member.deviceCount());
148 }
149
150 /**
151 * Returns a JSON representation of the layout to use for displaying in
152 * the topology view.
153 *
154 * @param layout the layout to transform
155 * @return a JSON representation of the data
156 */
157 ObjectNode layout(UiTopoLayout layout) {
158 return objectNode()
159 .put("id", layout.id().toString())
160 .put("parent", nullIsEmpty(layout.parent()))
161 .put("region", nullIsEmpty(layout.regionId()))
162 .put("regionName", regionName(layout.region()));
163 }
164
165 private String regionName(Region region) {
166 return region == null ? "" : region.name();
167 }
168
169 /**
170 * Returns a JSON representation of the region to display in the topology
171 * view.
172 *
Simon Hunt977aa052016-07-20 17:08:29 -0700173 * @param region the region to transform to JSON
174 * @param subRegions the subregions within this region
Simon Huntd5b96732016-07-08 13:22:27 -0700175 * @return a JSON representation of the data
176 */
Simon Hunt977aa052016-07-20 17:08:29 -0700177 ObjectNode region(UiRegion region, Set<UiRegion> subRegions) {
Simon Huntd5b96732016-07-08 13:22:27 -0700178 ObjectNode payload = objectNode();
Simon Huntd5b96732016-07-08 13:22:27 -0700179 if (region == null) {
180 payload.put("note", "no-region");
181 return payload;
182 }
Simon Hunt977aa052016-07-20 17:08:29 -0700183 payload.put("id", region.idAsString());
Simon Huntb1ce2602016-07-23 14:04:31 -0700184 if (subRegions != null) {
185 payload.set("subregions", jsonSubRegions(subRegions));
186 }
Simon Huntd5b96732016-07-08 13:22:27 -0700187
Simon Hunt977aa052016-07-20 17:08:29 -0700188 List<String> layerTags = region.layerOrder();
189 List<Set<UiNode>> splitDevices = splitByLayer(layerTags, region.devices());
190 List<Set<UiNode>> splitHosts = splitByLayer(layerTags, region.hosts());
191 Set<UiLink> links = region.links();
Simon Huntd5b96732016-07-08 13:22:27 -0700192
Simon Hunt977aa052016-07-20 17:08:29 -0700193 payload.set("devices", jsonGrouped(splitDevices));
194 payload.set("hosts", jsonGrouped(splitHosts));
195 payload.set("links", jsonLinks(links));
196 payload.set("layerOrder", jsonStrings(layerTags));
Simon Huntd5b96732016-07-08 13:22:27 -0700197
198 return payload;
199 }
200
Simon Hunt977aa052016-07-20 17:08:29 -0700201 private ArrayNode jsonSubRegions(Set<UiRegion> subregions) {
202 ArrayNode kids = arrayNode();
203 if (subregions != null) {
204 subregions.forEach(s -> kids.add(jsonClosedRegion(s)));
205 }
206 return kids;
207 }
208
209 private ArrayNode jsonStrings(List<String> strings) {
210 ArrayNode array = arrayNode();
211 strings.forEach(array::add);
212 return array;
213 }
214
215 private ArrayNode jsonLinks(Set<UiLink> links) {
216 ArrayNode result = arrayNode();
217 links.forEach(lnk -> result.add(json(lnk)));
218 return result;
219 }
220
221 private ArrayNode jsonGrouped(List<Set<UiNode>> groupedNodes) {
222 ArrayNode result = arrayNode();
223 groupedNodes.forEach(g -> {
224 ArrayNode subset = arrayNode();
225 g.forEach(n -> subset.add(json(n)));
226 result.add(subset);
227 });
228 return result;
229 }
230
Simon Hunt977aa052016-07-20 17:08:29 -0700231
232 private ObjectNode json(UiNode node) {
233 if (node instanceof UiRegion) {
234 return jsonClosedRegion((UiRegion) node);
235 }
236 if (node instanceof UiDevice) {
237 return json((UiDevice) node);
238 }
239 if (node instanceof UiHost) {
240 return json((UiHost) node);
241 }
242 throw new IllegalStateException(E_UNKNOWN_UI_NODE + node.getClass());
243 }
244
Simon Huntd5b96732016-07-08 13:22:27 -0700245 private ObjectNode json(UiDevice device) {
246 ObjectNode node = objectNode()
Simon Hunt977aa052016-07-20 17:08:29 -0700247 .put("id", device.idAsString())
Simon Huntd5b96732016-07-08 13:22:27 -0700248 .put("type", device.type())
249 .put("online", device.isOnline())
Simon Huntb1ce2602016-07-23 14:04:31 -0700250 .put("master", nullIsEmpty(device.master()))
Simon Huntd5b96732016-07-08 13:22:27 -0700251 .put("layer", device.layer());
252
253 // TODO: complete device details
254// addLabels(node, device);
255// addProps(node, device);
256// addGeoLocation(node, device);
257// addMetaUi(node, device);
258
259 return node;
260 }
261
262 private void addLabels(ObjectNode node, UiDevice device) {
263
264 }
265
266 private ObjectNode json(UiHost host) {
267 return objectNode()
Simon Hunt977aa052016-07-20 17:08:29 -0700268 .put("id", host.idAsString())
Simon Huntd5b96732016-07-08 13:22:27 -0700269 .put("layer", host.layer());
270 // TODO: complete host details
271 }
272
273
274 private ObjectNode json(UiLink link) {
275 return objectNode()
Simon Hunt977aa052016-07-20 17:08:29 -0700276 .put("id", link.idAsString());
Simon Huntd5b96732016-07-08 13:22:27 -0700277 // TODO: complete link details
278 }
279
280
Simon Hunt977aa052016-07-20 17:08:29 -0700281 private ObjectNode jsonClosedRegion(UiRegion region) {
282 return objectNode()
Simon Huntb1ce2602016-07-23 14:04:31 -0700283 .put("id", region.idAsString())
284 .put("nDevs", region.deviceCount());
Simon Hunt977aa052016-07-20 17:08:29 -0700285 // TODO: complete closed-region details
286 }
287
288
289 /**
290 * Returns a JSON array representation of a list of regions. Note that the
291 * information about each region is limited to what needs to be used to
292 * show the regions as nodes on the view.
293 *
294 * @param regions the regions
295 * @return a JSON representation of the minimal region information
296 */
297 public ArrayNode closedRegions(Set<UiRegion> regions) {
298 ArrayNode array = arrayNode();
299 for (UiRegion r : regions) {
300 array.add(jsonClosedRegion(r));
301 }
302 return array;
303 }
304
305 /**
306 * Returns a JSON array representation of a list of devices.
307 *
308 * @param devices the devices
309 * @return a JSON representation of the devices
310 */
311 public ArrayNode devices(Set<UiDevice> devices) {
312 ArrayNode array = arrayNode();
313 for (UiDevice device : devices) {
314 array.add(json(device));
315 }
316 return array;
317 }
318
319 /**
320 * Returns a JSON array representation of a list of hosts.
321 *
322 * @param hosts the hosts
323 * @return a JSON representation of the hosts
324 */
325 public ArrayNode hosts(Set<UiHost> hosts) {
326 ArrayNode array = arrayNode();
327 for (UiHost host : hosts) {
328 array.add(json(host));
329 }
330 return array;
331 }
332
333 /**
334 * Returns a JSON array representation of a list of links.
335 *
336 * @param links the links
337 * @return a JSON representation of the links
338 */
339 public ArrayNode links(Set<UiLink> links) {
340 ArrayNode array = arrayNode();
341 for (UiLink link : links) {
342 array.add(json(link));
343 }
344 return array;
345 }
346
347 // package-private for unit testing
348 List<Set<UiNode>> splitByLayer(List<String> layerTags,
349 Set<? extends UiNode> nodes) {
350 final int nLayers = layerTags.size();
351 if (!layerTags.get(nLayers - 1).equals(LAYER_DEFAULT)) {
352 throw new IllegalArgumentException(E_DEF_NOT_LAST);
353 }
354
355 List<Set<UiNode>> splitList = new ArrayList<>(layerTags.size());
356 Map<String, Set<UiNode>> byLayer = new HashMap<>(layerTags.size());
357
358 for (String tag : layerTags) {
359 Set<UiNode> set = new HashSet<>();
360 byLayer.put(tag, set);
361 splitList.add(set);
362 }
363
364 for (UiNode n : nodes) {
365 String which = n.layer();
366 if (!layerTags.contains(which)) {
367 which = LAYER_DEFAULT;
368 }
369 byLayer.get(which).add(n);
370 }
371
372 return splitList;
373 }
Simon Huntd5b96732016-07-08 13:22:27 -0700374}