blob: 36118abb26b56827e51457c7e36b6444a12985b0 [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
Simon Huntc13082f2016-08-03 21:20:23 -070019import com.fasterxml.jackson.databind.JsonNode;
Simon Huntd5b96732016-07-08 13:22:27 -070020import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ArrayNode;
22import com.fasterxml.jackson.databind.node.ObjectNode;
23import org.onlab.osgi.ServiceDirectory;
24import org.onosproject.cluster.ClusterService;
25import org.onosproject.cluster.NodeId;
26import org.onosproject.incubator.net.PortStatisticsService;
27import org.onosproject.incubator.net.tunnel.TunnelService;
28import org.onosproject.mastership.MastershipService;
Simon Hunt6a8cb4f2016-08-09 15:08:57 -070029import org.onosproject.net.Annotated;
30import org.onosproject.net.Annotations;
31import org.onosproject.net.Device;
Simon Huntd5b96732016-07-08 13:22:27 -070032import org.onosproject.net.device.DeviceService;
33import org.onosproject.net.flow.FlowRuleService;
34import org.onosproject.net.host.HostService;
35import org.onosproject.net.intent.IntentService;
36import org.onosproject.net.link.LinkService;
Simon Huntd5b96732016-07-08 13:22:27 -070037import org.onosproject.net.statistic.StatisticService;
38import org.onosproject.net.topology.TopologyService;
Steven Burrowse7cc3082016-09-27 11:24:58 -070039import org.onosproject.ui.JsonUtils;
Simon Huntd5b96732016-07-08 13:22:27 -070040import org.onosproject.ui.model.topo.UiClusterMember;
41import org.onosproject.ui.model.topo.UiDevice;
42import org.onosproject.ui.model.topo.UiHost;
43import org.onosproject.ui.model.topo.UiLink;
Simon Hunt977aa052016-07-20 17:08:29 -070044import org.onosproject.ui.model.topo.UiNode;
Simon Huntd5b96732016-07-08 13:22:27 -070045import org.onosproject.ui.model.topo.UiRegion;
Simon Huntc13082f2016-08-03 21:20:23 -070046import org.onosproject.ui.model.topo.UiSynthLink;
Simon Huntd5b96732016-07-08 13:22:27 -070047import org.onosproject.ui.model.topo.UiTopoLayout;
Simon Hunt98189192016-07-29 19:02:27 -070048import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
Simon Huntd5b96732016-07-08 13:22:27 -070050
Simon Hunt977aa052016-07-20 17:08:29 -070051import java.util.ArrayList;
52import java.util.HashMap;
53import java.util.HashSet;
Simon Huntd5b96732016-07-08 13:22:27 -070054import java.util.List;
Simon Hunt977aa052016-07-20 17:08:29 -070055import java.util.Map;
56import java.util.Set;
Simon Hunt6a8cb4f2016-08-09 15:08:57 -070057import java.util.concurrent.ConcurrentHashMap;
Simon Huntd5b96732016-07-08 13:22:27 -070058
59import static com.google.common.base.Preconditions.checkNotNull;
Simon Hunt6a8cb4f2016-08-09 15:08:57 -070060import static org.onosproject.net.AnnotationKeys.LATITUDE;
61import static org.onosproject.net.AnnotationKeys.LONGITUDE;
Simon Hunt977aa052016-07-20 17:08:29 -070062import static org.onosproject.ui.model.topo.UiNode.LAYER_DEFAULT;
Simon Huntd5b96732016-07-08 13:22:27 -070063
64/**
65 * Facility for creating JSON messages to send to the topology view in the
66 * Web client.
67 */
68class Topo2Jsonifier {
69
Simon Hunt977aa052016-07-20 17:08:29 -070070 private static final String E_DEF_NOT_LAST =
71 "UiNode.LAYER_DEFAULT not last in layer list";
72 private static final String E_UNKNOWN_UI_NODE =
73 "Unknown subclass of UiNode: ";
74
Simon Hunt98189192016-07-29 19:02:27 -070075 private static final String REGION = "region";
76 private static final String DEVICE = "device";
77 private static final String HOST = "host";
78
79 private final Logger log = LoggerFactory.getLogger(getClass());
80
Simon Huntd5b96732016-07-08 13:22:27 -070081 private final ObjectMapper mapper = new ObjectMapper();
82
83 private ServiceDirectory directory;
84 private ClusterService clusterService;
85 private DeviceService deviceService;
86 private LinkService linkService;
87 private HostService hostService;
88 private MastershipService mastershipService;
89 private IntentService intentService;
90 private FlowRuleService flowService;
91 private StatisticService flowStatsService;
92 private PortStatisticsService portStatsService;
93 private TopologyService topologyService;
94 private TunnelService tunnelService;
95
96
Simon Hunt6a8cb4f2016-08-09 15:08:57 -070097 // NOTE: we'll stick this here for now, but maybe there is a better home?
98 // (this is not distributed across the cluster)
99 private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>();
100
101
Simon Huntd5b96732016-07-08 13:22:27 -0700102 /**
103 * Creates an instance with a reference to the services directory, so that
104 * additional information about network elements may be looked up on
105 * on the fly.
106 *
107 * @param directory service directory
108 */
109 Topo2Jsonifier(ServiceDirectory directory) {
110 this.directory = checkNotNull(directory, "Directory cannot be null");
111
112 clusterService = directory.get(ClusterService.class);
113 deviceService = directory.get(DeviceService.class);
114 linkService = directory.get(LinkService.class);
115 hostService = directory.get(HostService.class);
116 mastershipService = directory.get(MastershipService.class);
117 intentService = directory.get(IntentService.class);
118 flowService = directory.get(FlowRuleService.class);
119 flowStatsService = directory.get(StatisticService.class);
120 portStatsService = directory.get(PortStatisticsService.class);
121 topologyService = directory.get(TopologyService.class);
122 tunnelService = directory.get(TunnelService.class);
Simon Hunt977aa052016-07-20 17:08:29 -0700123 }
Simon Huntd5b96732016-07-08 13:22:27 -0700124
Simon Hunt977aa052016-07-20 17:08:29 -0700125 // for unit testing
126 Topo2Jsonifier() {
Simon Huntd5b96732016-07-08 13:22:27 -0700127 }
128
129 private ObjectNode objectNode() {
130 return mapper.createObjectNode();
131 }
132
133 private ArrayNode arrayNode() {
134 return mapper.createArrayNode();
135 }
136
137 private String nullIsEmpty(Object o) {
138 return o == null ? "" : o.toString();
139 }
140
141
142 /**
143 * Returns a JSON representation of the cluster members (ONOS instances).
144 *
145 * @param instances the instance model objects
146 * @return a JSON representation of the data
147 */
148 ObjectNode instances(List<UiClusterMember> instances) {
149 NodeId local = clusterService.getLocalNode().id();
150 ObjectNode payload = objectNode();
151
152 ArrayNode members = arrayNode();
153 payload.set("members", members);
154 for (UiClusterMember member : instances) {
155 members.add(json(member, member.id().equals(local)));
156 }
157
158 return payload;
159 }
160
161 private ObjectNode json(UiClusterMember member, boolean isUiAttached) {
162 return objectNode()
163 .put("id", member.id().toString())
164 .put("ip", member.ip().toString())
165 .put("online", member.isOnline())
166 .put("ready", member.isReady())
167 .put("uiAttached", isUiAttached)
168 .put("switches", member.deviceCount());
169 }
170
171 /**
172 * Returns a JSON representation of the layout to use for displaying in
Simon Huntf836a872016-08-10 17:37:36 -0700173 * the topology view. The identifiers and names of regions from the
174 * current to the root is included, so that the bread-crumb widget can
175 * be rendered.
Simon Huntd5b96732016-07-08 13:22:27 -0700176 *
177 * @param layout the layout to transform
Simon Huntf836a872016-08-10 17:37:36 -0700178 * @param crumbs list of layouts in bread-crumb order
Simon Huntd5b96732016-07-08 13:22:27 -0700179 * @return a JSON representation of the data
180 */
Simon Huntf836a872016-08-10 17:37:36 -0700181 ObjectNode layout(UiTopoLayout layout, List<UiTopoLayout> crumbs) {
182 ObjectNode result = objectNode()
Simon Huntd5b96732016-07-08 13:22:27 -0700183 .put("id", layout.id().toString())
184 .put("parent", nullIsEmpty(layout.parent()))
185 .put("region", nullIsEmpty(layout.regionId()))
Simon Huntf836a872016-08-10 17:37:36 -0700186 .put("regionName", UiRegion.safeName(layout.region()));
187 addCrumbs(result, crumbs);
188 return result;
Simon Huntd5b96732016-07-08 13:22:27 -0700189 }
190
Simon Huntf836a872016-08-10 17:37:36 -0700191 private void addCrumbs(ObjectNode result, List<UiTopoLayout> crumbs) {
192 ArrayNode trail = arrayNode();
193 crumbs.forEach(c -> {
194 ObjectNode n = objectNode()
195 .put("id", c.regionId().toString())
196 .put("name", UiRegion.safeName(c.region()));
197 trail.add(n);
198 });
199 result.set("crumbs", trail);
Simon Huntd5b96732016-07-08 13:22:27 -0700200 }
201
202 /**
203 * Returns a JSON representation of the region to display in the topology
204 * view.
205 *
Simon Hunt977aa052016-07-20 17:08:29 -0700206 * @param region the region to transform to JSON
207 * @param subRegions the subregions within this region
Simon Huntc13082f2016-08-03 21:20:23 -0700208 * @param links the links within this region
Simon Huntd5b96732016-07-08 13:22:27 -0700209 * @return a JSON representation of the data
210 */
Simon Huntc13082f2016-08-03 21:20:23 -0700211 ObjectNode region(UiRegion region, Set<UiRegion> subRegions,
212 List<UiSynthLink> links) {
Simon Huntd5b96732016-07-08 13:22:27 -0700213 ObjectNode payload = objectNode();
Simon Huntd5b96732016-07-08 13:22:27 -0700214 if (region == null) {
215 payload.put("note", "no-region");
216 return payload;
217 }
Simon Hunt977aa052016-07-20 17:08:29 -0700218 payload.put("id", region.idAsString());
Simon Huntcd508a62016-10-27 12:47:24 -0700219 payload.set("subregions", jsonSubRegions(subRegions));
220 payload.set("links", jsonLinks(links));
Simon Huntc13082f2016-08-03 21:20:23 -0700221
Simon Hunt977aa052016-07-20 17:08:29 -0700222 List<String> layerTags = region.layerOrder();
223 List<Set<UiNode>> splitDevices = splitByLayer(layerTags, region.devices());
224 List<Set<UiNode>> splitHosts = splitByLayer(layerTags, region.hosts());
Simon Huntd5b96732016-07-08 13:22:27 -0700225
Simon Hunt977aa052016-07-20 17:08:29 -0700226 payload.set("devices", jsonGrouped(splitDevices));
227 payload.set("hosts", jsonGrouped(splitHosts));
Simon Hunt977aa052016-07-20 17:08:29 -0700228 payload.set("layerOrder", jsonStrings(layerTags));
Simon Huntd5b96732016-07-08 13:22:27 -0700229
230 return payload;
231 }
232
Simon Hunt977aa052016-07-20 17:08:29 -0700233 private ArrayNode jsonSubRegions(Set<UiRegion> subregions) {
234 ArrayNode kids = arrayNode();
Simon Huntc13082f2016-08-03 21:20:23 -0700235 subregions.forEach(s -> kids.add(jsonClosedRegion(s)));
Simon Hunt977aa052016-07-20 17:08:29 -0700236 return kids;
237 }
238
Simon Huntc13082f2016-08-03 21:20:23 -0700239 private JsonNode jsonLinks(List<UiSynthLink> links) {
240 ArrayNode synthLinks = arrayNode();
241 links.forEach(l -> synthLinks.add(json(l)));
242 return synthLinks;
243 }
244
Simon Hunt977aa052016-07-20 17:08:29 -0700245 private ArrayNode jsonStrings(List<String> strings) {
246 ArrayNode array = arrayNode();
247 strings.forEach(array::add);
248 return array;
249 }
250
Simon Hunt977aa052016-07-20 17:08:29 -0700251 private ArrayNode jsonGrouped(List<Set<UiNode>> groupedNodes) {
252 ArrayNode result = arrayNode();
253 groupedNodes.forEach(g -> {
254 ArrayNode subset = arrayNode();
255 g.forEach(n -> subset.add(json(n)));
256 result.add(subset);
257 });
258 return result;
259 }
260
Simon Hunt977aa052016-07-20 17:08:29 -0700261
262 private ObjectNode json(UiNode node) {
263 if (node instanceof UiRegion) {
264 return jsonClosedRegion((UiRegion) node);
265 }
266 if (node instanceof UiDevice) {
267 return json((UiDevice) node);
268 }
269 if (node instanceof UiHost) {
270 return json((UiHost) node);
271 }
272 throw new IllegalStateException(E_UNKNOWN_UI_NODE + node.getClass());
273 }
274
Simon Huntd5b96732016-07-08 13:22:27 -0700275 private ObjectNode json(UiDevice device) {
276 ObjectNode node = objectNode()
Simon Hunt977aa052016-07-20 17:08:29 -0700277 .put("id", device.idAsString())
Simon Hunt98189192016-07-29 19:02:27 -0700278 .put("nodeType", DEVICE)
Simon Huntd5b96732016-07-08 13:22:27 -0700279 .put("type", device.type())
Simon Hunt3d712522016-08-11 11:20:44 -0700280 .put("online", deviceService.isAvailable(device.id()))
Simon Huntb1ce2602016-07-23 14:04:31 -0700281 .put("master", nullIsEmpty(device.master()))
Simon Huntd5b96732016-07-08 13:22:27 -0700282 .put("layer", device.layer());
283
Simon Hunt6a8cb4f2016-08-09 15:08:57 -0700284 Device d = device.backingDevice();
285
286 addProps(node, d);
287 addGeoLocation(node, d);
288 addMetaUi(node, device.idAsString());
Simon Huntd5b96732016-07-08 13:22:27 -0700289
290 return node;
291 }
292
Simon Hunt6a8cb4f2016-08-09 15:08:57 -0700293 private void addProps(ObjectNode node, Device dev) {
294 Annotations annot = dev.annotations();
295 ObjectNode props = objectNode();
296 if (annot != null) {
297 annot.keys().forEach(k -> props.put(k, annot.value(k)));
298 }
299 node.set("props", props);
300 }
Simon Huntd5b96732016-07-08 13:22:27 -0700301
Simon Hunt6a8cb4f2016-08-09 15:08:57 -0700302 private void addMetaUi(ObjectNode node, String metaInstanceId) {
303 ObjectNode meta = metaUi.get(metaInstanceId);
304 if (meta != null) {
305 node.set("metaUi", meta);
306 }
307 }
308
309 private void addGeoLocation(ObjectNode node, Annotated a) {
310 List<String> lngLat = getAnnotValues(a, LONGITUDE, LATITUDE);
311 if (lngLat != null) {
312 try {
313 double lng = Double.parseDouble(lngLat.get(0));
314 double lat = Double.parseDouble(lngLat.get(1));
315 ObjectNode loc = objectNode()
316 .put("type", "lnglat")
317 .put("lng", lng)
318 .put("lat", lat);
319 node.set("location", loc);
320
321 } catch (NumberFormatException e) {
322 log.warn("Invalid geo data: longitude={}, latitude={}",
323 lngLat.get(0), lngLat.get(1));
324 }
325 } else {
326 log.debug("No geo lng/lat for {}", a);
327 }
328 }
329
330 // return list of string values from annotated instance, for given keys
331 // return null if any keys are not present
332 List<String> getAnnotValues(Annotated a, String... annotKeys) {
333 List<String> result = new ArrayList<>(annotKeys.length);
334 for (String k : annotKeys) {
335 String v = a.annotations().value(k);
336 if (v == null) {
337 return null;
338 }
339 result.add(v);
340 }
341 return result;
342 }
343
344 // derive JSON object from annotations
345 private ObjectNode props(Annotations annotations) {
346 ObjectNode p = objectNode();
347 if (annotations != null) {
348 annotations.keys().forEach(k -> p.put(k, annotations.value(k)));
349 }
350 return p;
Simon Huntd5b96732016-07-08 13:22:27 -0700351 }
352
353 private ObjectNode json(UiHost host) {
354 return objectNode()
Simon Hunt977aa052016-07-20 17:08:29 -0700355 .put("id", host.idAsString())
Simon Hunt98189192016-07-29 19:02:27 -0700356 .put("nodeType", HOST)
Simon Huntd5b96732016-07-08 13:22:27 -0700357 .put("layer", host.layer());
358 // TODO: complete host details
359 }
360
Simon Huntc13082f2016-08-03 21:20:23 -0700361 private ObjectNode json(UiSynthLink sLink) {
362 UiLink uLink = sLink.link();
Simon Hunt3d712522016-08-11 11:20:44 -0700363 ObjectNode data = objectNode()
Simon Huntc13082f2016-08-03 21:20:23 -0700364 .put("id", uLink.idAsString())
365 .put("epA", uLink.endPointA())
366 .put("epB", uLink.endPointB())
367 .put("type", uLink.type());
Simon Hunt3d712522016-08-11 11:20:44 -0700368 String pA = uLink.endPortA();
369 String pB = uLink.endPortB();
370 if (pA != null) {
371 data.put("portA", pA);
372 }
373 if (pB != null) {
374 data.put("portB", pB);
375 }
376 return data;
Simon Huntd5b96732016-07-08 13:22:27 -0700377 }
378
379
Simon Hunt977aa052016-07-20 17:08:29 -0700380 private ObjectNode jsonClosedRegion(UiRegion region) {
Steven Burrows482d9502016-09-27 11:24:58 -0700381 ObjectNode node = objectNode()
Simon Huntb1ce2602016-07-23 14:04:31 -0700382 .put("id", region.idAsString())
Simon Huntf836a872016-08-10 17:37:36 -0700383 .put("name", region.name())
Simon Hunt98189192016-07-29 19:02:27 -0700384 .put("nodeType", REGION)
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500385 .put("nDevs", region.deviceCount())
386 .put("nHosts", region.hostCount());
Simon Hunt977aa052016-07-20 17:08:29 -0700387 // TODO: complete closed-region details
Steven Burrows482d9502016-09-27 11:24:58 -0700388
389 addMetaUi(node, region.idAsString());
390 return node;
Simon Hunt977aa052016-07-20 17:08:29 -0700391 }
392
Simon Hunt98189192016-07-29 19:02:27 -0700393 /**
394 * Returns a JSON array representation of a set of regions/devices. Note
395 * that the information is sufficient for showing regions as nodes.
396 *
397 * @param nodes the nodes
398 * @return a JSON representation of the nodes
399 */
400 public ArrayNode closedNodes(Set<UiNode> nodes) {
401 ArrayNode array = arrayNode();
Simon Huntc13082f2016-08-03 21:20:23 -0700402 for (UiNode node : nodes) {
Simon Hunt98189192016-07-29 19:02:27 -0700403 if (node instanceof UiRegion) {
404 array.add(jsonClosedRegion((UiRegion) node));
405 } else if (node instanceof UiDevice) {
406 array.add(json((UiDevice) node));
407 } else {
408 log.warn("Unexpected node instance: {}", node.getClass());
409 }
410 }
411 return array;
412 }
Simon Hunt977aa052016-07-20 17:08:29 -0700413
414 /**
415 * Returns a JSON array representation of a list of regions. Note that the
416 * information about each region is limited to what needs to be used to
417 * show the regions as nodes on the view.
418 *
419 * @param regions the regions
420 * @return a JSON representation of the minimal region information
421 */
422 public ArrayNode closedRegions(Set<UiRegion> regions) {
423 ArrayNode array = arrayNode();
424 for (UiRegion r : regions) {
425 array.add(jsonClosedRegion(r));
426 }
427 return array;
428 }
429
430 /**
431 * Returns a JSON array representation of a list of devices.
432 *
433 * @param devices the devices
434 * @return a JSON representation of the devices
435 */
436 public ArrayNode devices(Set<UiDevice> devices) {
437 ArrayNode array = arrayNode();
438 for (UiDevice device : devices) {
439 array.add(json(device));
440 }
441 return array;
442 }
443
444 /**
445 * Returns a JSON array representation of a list of hosts.
446 *
447 * @param hosts the hosts
448 * @return a JSON representation of the hosts
449 */
450 public ArrayNode hosts(Set<UiHost> hosts) {
451 ArrayNode array = arrayNode();
452 for (UiHost host : hosts) {
453 array.add(json(host));
454 }
455 return array;
456 }
457
Simon Hunt977aa052016-07-20 17:08:29 -0700458 // package-private for unit testing
459 List<Set<UiNode>> splitByLayer(List<String> layerTags,
460 Set<? extends UiNode> nodes) {
461 final int nLayers = layerTags.size();
462 if (!layerTags.get(nLayers - 1).equals(LAYER_DEFAULT)) {
463 throw new IllegalArgumentException(E_DEF_NOT_LAST);
464 }
465
466 List<Set<UiNode>> splitList = new ArrayList<>(layerTags.size());
467 Map<String, Set<UiNode>> byLayer = new HashMap<>(layerTags.size());
468
469 for (String tag : layerTags) {
470 Set<UiNode> set = new HashSet<>();
471 byLayer.put(tag, set);
472 splitList.add(set);
473 }
474
475 for (UiNode n : nodes) {
476 String which = n.layer();
477 if (!layerTags.contains(which)) {
478 which = LAYER_DEFAULT;
479 }
480 byLayer.get(which).add(n);
481 }
482
483 return splitList;
484 }
Steven Burrowse7cc3082016-09-27 11:24:58 -0700485
486 /**
487 * Stores the memento for an element.
488 * This method assumes the payload has an id String, memento ObjectNode
489 *
490 * @param payload event payload
491 */
492 void updateMeta(ObjectNode payload) {
493
494 String id = JsonUtils.string(payload, "id");
495 metaUi.put(id, JsonUtils.node(payload, "memento"));
496
497 log.debug("Storing metadata for {}", id);
498 }
Simon Huntd5b96732016-07-08 13:22:27 -0700499}