Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.ui.impl.topo; |
| 18 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.JsonNode; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 22 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 23 | import org.onlab.osgi.ServiceDirectory; |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 25 | import org.onosproject.cluster.ClusterService; |
| 26 | import org.onosproject.cluster.NodeId; |
| 27 | import org.onosproject.incubator.net.PortStatisticsService; |
| 28 | import org.onosproject.incubator.net.tunnel.TunnelService; |
| 29 | import org.onosproject.mastership.MastershipService; |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 30 | import org.onosproject.net.Annotated; |
| 31 | import org.onosproject.net.Annotations; |
| 32 | import org.onosproject.net.Device; |
Steven Burrows | ad75aa2 | 2016-12-14 17:17:24 -0500 | [diff] [blame^] | 33 | import org.onosproject.net.DeviceId; |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 34 | import org.onosproject.net.Host; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 35 | import org.onosproject.net.device.DeviceService; |
| 36 | import org.onosproject.net.flow.FlowRuleService; |
| 37 | import org.onosproject.net.host.HostService; |
| 38 | import org.onosproject.net.intent.IntentService; |
| 39 | import org.onosproject.net.link.LinkService; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 40 | import org.onosproject.net.region.Region; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 41 | import org.onosproject.net.statistic.StatisticService; |
| 42 | import org.onosproject.net.topology.TopologyService; |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 43 | import org.onosproject.ui.JsonUtils; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 44 | import org.onosproject.ui.model.topo.UiClusterMember; |
| 45 | import org.onosproject.ui.model.topo.UiDevice; |
| 46 | import org.onosproject.ui.model.topo.UiHost; |
| 47 | import org.onosproject.ui.model.topo.UiLink; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 48 | import org.onosproject.ui.model.topo.UiNode; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 49 | import org.onosproject.ui.model.topo.UiRegion; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 50 | import org.onosproject.ui.model.topo.UiSynthLink; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 51 | import org.onosproject.ui.model.topo.UiTopoLayout; |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 52 | import org.slf4j.Logger; |
| 53 | import org.slf4j.LoggerFactory; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 54 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 55 | import java.util.ArrayList; |
| 56 | import java.util.HashMap; |
| 57 | import java.util.HashSet; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 58 | import java.util.List; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 59 | import java.util.Map; |
| 60 | import java.util.Set; |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 61 | import java.util.concurrent.ConcurrentHashMap; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 62 | |
| 63 | import static com.google.common.base.Preconditions.checkNotNull; |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 64 | import static org.onosproject.net.AnnotationKeys.LATITUDE; |
| 65 | import static org.onosproject.net.AnnotationKeys.LONGITUDE; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 66 | import static org.onosproject.ui.model.topo.UiNode.LAYER_DEFAULT; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Facility for creating JSON messages to send to the topology view in the |
| 70 | * Web client. |
| 71 | */ |
| 72 | class Topo2Jsonifier { |
| 73 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 74 | private static final String E_DEF_NOT_LAST = |
| 75 | "UiNode.LAYER_DEFAULT not last in layer list"; |
| 76 | private static final String E_UNKNOWN_UI_NODE = |
| 77 | "Unknown subclass of UiNode: "; |
| 78 | |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 79 | private static final String REGION = "region"; |
| 80 | private static final String DEVICE = "device"; |
| 81 | private static final String HOST = "host"; |
| 82 | |
| 83 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 84 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 85 | private final ObjectMapper mapper = new ObjectMapper(); |
| 86 | |
| 87 | private ServiceDirectory directory; |
| 88 | private ClusterService clusterService; |
| 89 | private DeviceService deviceService; |
| 90 | private LinkService linkService; |
| 91 | private HostService hostService; |
| 92 | private MastershipService mastershipService; |
| 93 | private IntentService intentService; |
| 94 | private FlowRuleService flowService; |
| 95 | private StatisticService flowStatsService; |
| 96 | private PortStatisticsService portStatsService; |
| 97 | private TopologyService topologyService; |
| 98 | private TunnelService tunnelService; |
| 99 | |
| 100 | |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 101 | // NOTE: we'll stick this here for now, but maybe there is a better home? |
| 102 | // (this is not distributed across the cluster) |
| 103 | private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>(); |
| 104 | |
| 105 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 106 | /** |
| 107 | * Creates an instance with a reference to the services directory, so that |
| 108 | * additional information about network elements may be looked up on |
| 109 | * on the fly. |
| 110 | * |
| 111 | * @param directory service directory |
| 112 | */ |
| 113 | Topo2Jsonifier(ServiceDirectory directory) { |
| 114 | this.directory = checkNotNull(directory, "Directory cannot be null"); |
| 115 | |
| 116 | clusterService = directory.get(ClusterService.class); |
| 117 | deviceService = directory.get(DeviceService.class); |
| 118 | linkService = directory.get(LinkService.class); |
| 119 | hostService = directory.get(HostService.class); |
| 120 | mastershipService = directory.get(MastershipService.class); |
| 121 | intentService = directory.get(IntentService.class); |
| 122 | flowService = directory.get(FlowRuleService.class); |
| 123 | flowStatsService = directory.get(StatisticService.class); |
| 124 | portStatsService = directory.get(PortStatisticsService.class); |
| 125 | topologyService = directory.get(TopologyService.class); |
| 126 | tunnelService = directory.get(TunnelService.class); |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 127 | } |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 128 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 129 | // for unit testing |
| 130 | Topo2Jsonifier() { |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | private ObjectNode objectNode() { |
| 134 | return mapper.createObjectNode(); |
| 135 | } |
| 136 | |
| 137 | private ArrayNode arrayNode() { |
| 138 | return mapper.createArrayNode(); |
| 139 | } |
| 140 | |
| 141 | private String nullIsEmpty(Object o) { |
| 142 | return o == null ? "" : o.toString(); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | /** |
| 147 | * Returns a JSON representation of the cluster members (ONOS instances). |
| 148 | * |
| 149 | * @param instances the instance model objects |
| 150 | * @return a JSON representation of the data |
| 151 | */ |
| 152 | ObjectNode instances(List<UiClusterMember> instances) { |
| 153 | NodeId local = clusterService.getLocalNode().id(); |
| 154 | ObjectNode payload = objectNode(); |
| 155 | |
| 156 | ArrayNode members = arrayNode(); |
| 157 | payload.set("members", members); |
| 158 | for (UiClusterMember member : instances) { |
| 159 | members.add(json(member, member.id().equals(local))); |
| 160 | } |
| 161 | |
| 162 | return payload; |
| 163 | } |
| 164 | |
| 165 | private ObjectNode json(UiClusterMember member, boolean isUiAttached) { |
Steven Burrows | ad75aa2 | 2016-12-14 17:17:24 -0500 | [diff] [blame^] | 166 | int switchCount = mastershipService.getDevicesOf(member.id()).size(); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 167 | return objectNode() |
| 168 | .put("id", member.id().toString()) |
| 169 | .put("ip", member.ip().toString()) |
| 170 | .put("online", member.isOnline()) |
| 171 | .put("ready", member.isReady()) |
| 172 | .put("uiAttached", isUiAttached) |
Steven Burrows | ad75aa2 | 2016-12-14 17:17:24 -0500 | [diff] [blame^] | 173 | .put("switches", switchCount); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Returns a JSON representation of the layout to use for displaying in |
Simon Hunt | f836a87 | 2016-08-10 17:37:36 -0700 | [diff] [blame] | 178 | * the topology view. The identifiers and names of regions from the |
| 179 | * current to the root is included, so that the bread-crumb widget can |
| 180 | * be rendered. |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 181 | * |
| 182 | * @param layout the layout to transform |
Simon Hunt | f836a87 | 2016-08-10 17:37:36 -0700 | [diff] [blame] | 183 | * @param crumbs list of layouts in bread-crumb order |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 184 | * @return a JSON representation of the data |
| 185 | */ |
Simon Hunt | f836a87 | 2016-08-10 17:37:36 -0700 | [diff] [blame] | 186 | ObjectNode layout(UiTopoLayout layout, List<UiTopoLayout> crumbs) { |
| 187 | ObjectNode result = objectNode() |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 188 | .put("id", layout.id().toString()) |
| 189 | .put("parent", nullIsEmpty(layout.parent())) |
| 190 | .put("region", nullIsEmpty(layout.regionId())) |
Simon Hunt | f836a87 | 2016-08-10 17:37:36 -0700 | [diff] [blame] | 191 | .put("regionName", UiRegion.safeName(layout.region())); |
| 192 | addCrumbs(result, crumbs); |
| 193 | return result; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Simon Hunt | f836a87 | 2016-08-10 17:37:36 -0700 | [diff] [blame] | 196 | private void addCrumbs(ObjectNode result, List<UiTopoLayout> crumbs) { |
| 197 | ArrayNode trail = arrayNode(); |
| 198 | crumbs.forEach(c -> { |
| 199 | ObjectNode n = objectNode() |
| 200 | .put("id", c.regionId().toString()) |
| 201 | .put("name", UiRegion.safeName(c.region())); |
| 202 | trail.add(n); |
| 203 | }); |
| 204 | result.set("crumbs", trail); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Returns a JSON representation of the region to display in the topology |
| 209 | * view. |
| 210 | * |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 211 | * @param region the region to transform to JSON |
| 212 | * @param subRegions the subregions within this region |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 213 | * @param links the links within this region |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 214 | * @return a JSON representation of the data |
| 215 | */ |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 216 | ObjectNode region(UiRegion region, Set<UiRegion> subRegions, |
| 217 | List<UiSynthLink> links) { |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 218 | ObjectNode payload = objectNode(); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 219 | if (region == null) { |
| 220 | payload.put("note", "no-region"); |
| 221 | return payload; |
| 222 | } |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 223 | payload.put("id", region.idAsString()); |
Simon Hunt | cd508a6 | 2016-10-27 12:47:24 -0700 | [diff] [blame] | 224 | payload.set("subregions", jsonSubRegions(subRegions)); |
| 225 | payload.set("links", jsonLinks(links)); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 226 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 227 | List<String> layerTags = region.layerOrder(); |
| 228 | List<Set<UiNode>> splitDevices = splitByLayer(layerTags, region.devices()); |
| 229 | List<Set<UiNode>> splitHosts = splitByLayer(layerTags, region.hosts()); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 230 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 231 | payload.set("devices", jsonGrouped(splitDevices)); |
| 232 | payload.set("hosts", jsonGrouped(splitHosts)); |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 233 | payload.set("layerOrder", jsonStrings(layerTags)); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 234 | |
| 235 | return payload; |
| 236 | } |
| 237 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 238 | private ArrayNode jsonSubRegions(Set<UiRegion> subregions) { |
| 239 | ArrayNode kids = arrayNode(); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 240 | subregions.forEach(s -> kids.add(jsonClosedRegion(s))); |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 241 | return kids; |
| 242 | } |
| 243 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 244 | private JsonNode jsonLinks(List<UiSynthLink> links) { |
| 245 | ArrayNode synthLinks = arrayNode(); |
| 246 | links.forEach(l -> synthLinks.add(json(l))); |
| 247 | return synthLinks; |
| 248 | } |
| 249 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 250 | private ArrayNode jsonStrings(List<String> strings) { |
| 251 | ArrayNode array = arrayNode(); |
| 252 | strings.forEach(array::add); |
| 253 | return array; |
| 254 | } |
| 255 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 256 | private ArrayNode jsonGrouped(List<Set<UiNode>> groupedNodes) { |
| 257 | ArrayNode result = arrayNode(); |
| 258 | groupedNodes.forEach(g -> { |
| 259 | ArrayNode subset = arrayNode(); |
| 260 | g.forEach(n -> subset.add(json(n))); |
| 261 | result.add(subset); |
| 262 | }); |
| 263 | return result; |
| 264 | } |
| 265 | |
Steven Burrows | ad75aa2 | 2016-12-14 17:17:24 -0500 | [diff] [blame^] | 266 | // Returns the name of the master node for the specified device id. |
| 267 | private String master(DeviceId deviceId) { |
| 268 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 269 | return master != null ? master.toString() : ""; |
| 270 | } |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 271 | |
| 272 | private ObjectNode json(UiNode node) { |
| 273 | if (node instanceof UiRegion) { |
| 274 | return jsonClosedRegion((UiRegion) node); |
| 275 | } |
| 276 | if (node instanceof UiDevice) { |
| 277 | return json((UiDevice) node); |
| 278 | } |
| 279 | if (node instanceof UiHost) { |
| 280 | return json((UiHost) node); |
| 281 | } |
| 282 | throw new IllegalStateException(E_UNKNOWN_UI_NODE + node.getClass()); |
| 283 | } |
| 284 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 285 | private ObjectNode json(UiDevice device) { |
| 286 | ObjectNode node = objectNode() |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 287 | .put("id", device.idAsString()) |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 288 | .put("nodeType", DEVICE) |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 289 | .put("type", device.type()) |
Simon Hunt | 3d71252 | 2016-08-11 11:20:44 -0700 | [diff] [blame] | 290 | .put("online", deviceService.isAvailable(device.id())) |
Steven Burrows | ad75aa2 | 2016-12-14 17:17:24 -0500 | [diff] [blame^] | 291 | .put("master", master(device.id())) |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 292 | .put("layer", device.layer()); |
| 293 | |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 294 | Device d = device.backingDevice(); |
| 295 | |
| 296 | addProps(node, d); |
| 297 | addGeoLocation(node, d); |
| 298 | addMetaUi(node, device.idAsString()); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 299 | |
| 300 | return node; |
| 301 | } |
| 302 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 303 | private void addProps(ObjectNode node, Annotated a) { |
| 304 | Annotations annot = a.annotations(); |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 305 | ObjectNode props = objectNode(); |
| 306 | if (annot != null) { |
| 307 | annot.keys().forEach(k -> props.put(k, annot.value(k))); |
| 308 | } |
| 309 | node.set("props", props); |
| 310 | } |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 311 | |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 312 | private void addMetaUi(ObjectNode node, String metaInstanceId) { |
| 313 | ObjectNode meta = metaUi.get(metaInstanceId); |
| 314 | if (meta != null) { |
| 315 | node.set("metaUi", meta); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | private void addGeoLocation(ObjectNode node, Annotated a) { |
| 320 | List<String> lngLat = getAnnotValues(a, LONGITUDE, LATITUDE); |
| 321 | if (lngLat != null) { |
| 322 | try { |
| 323 | double lng = Double.parseDouble(lngLat.get(0)); |
| 324 | double lat = Double.parseDouble(lngLat.get(1)); |
| 325 | ObjectNode loc = objectNode() |
| 326 | .put("type", "lnglat") |
| 327 | .put("lng", lng) |
| 328 | .put("lat", lat); |
| 329 | node.set("location", loc); |
| 330 | |
| 331 | } catch (NumberFormatException e) { |
| 332 | log.warn("Invalid geo data: longitude={}, latitude={}", |
| 333 | lngLat.get(0), lngLat.get(1)); |
| 334 | } |
| 335 | } else { |
| 336 | log.debug("No geo lng/lat for {}", a); |
| 337 | } |
| 338 | } |
| 339 | |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 340 | private void addIps(ObjectNode node, Host h) { |
| 341 | Set<IpAddress> ips = h.ipAddresses(); |
| 342 | |
| 343 | ArrayNode a = arrayNode(); |
| 344 | for (IpAddress ip : ips) { |
| 345 | a.add(ip.toString()); |
| 346 | } |
| 347 | |
| 348 | node.set("ips", a); |
| 349 | } |
| 350 | |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 351 | // return list of string values from annotated instance, for given keys |
| 352 | // return null if any keys are not present |
| 353 | List<String> getAnnotValues(Annotated a, String... annotKeys) { |
| 354 | List<String> result = new ArrayList<>(annotKeys.length); |
| 355 | for (String k : annotKeys) { |
| 356 | String v = a.annotations().value(k); |
| 357 | if (v == null) { |
| 358 | return null; |
| 359 | } |
| 360 | result.add(v); |
| 361 | } |
| 362 | return result; |
| 363 | } |
| 364 | |
| 365 | // derive JSON object from annotations |
| 366 | private ObjectNode props(Annotations annotations) { |
| 367 | ObjectNode p = objectNode(); |
| 368 | if (annotations != null) { |
| 369 | annotations.keys().forEach(k -> p.put(k, annotations.value(k))); |
| 370 | } |
| 371 | return p; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | private ObjectNode json(UiHost host) { |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 375 | ObjectNode node = objectNode() |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 376 | .put("id", host.idAsString()) |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 377 | .put("nodeType", HOST) |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 378 | .put("layer", host.layer()); |
| 379 | // TODO: complete host details |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 380 | Host h = host.backingHost(); |
| 381 | |
| 382 | addIps(node, h); |
| 383 | addGeoLocation(node, h); |
| 384 | addMetaUi(node, host.idAsString()); |
| 385 | |
| 386 | return node; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 389 | private ObjectNode json(UiSynthLink sLink) { |
| 390 | UiLink uLink = sLink.link(); |
Simon Hunt | 3d71252 | 2016-08-11 11:20:44 -0700 | [diff] [blame] | 391 | ObjectNode data = objectNode() |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 392 | .put("id", uLink.idAsString()) |
| 393 | .put("epA", uLink.endPointA()) |
| 394 | .put("epB", uLink.endPointB()) |
| 395 | .put("type", uLink.type()); |
Simon Hunt | 3d71252 | 2016-08-11 11:20:44 -0700 | [diff] [blame] | 396 | String pA = uLink.endPortA(); |
| 397 | String pB = uLink.endPortB(); |
| 398 | if (pA != null) { |
| 399 | data.put("portA", pA); |
| 400 | } |
| 401 | if (pB != null) { |
| 402 | data.put("portB", pB); |
| 403 | } |
| 404 | return data; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 408 | private ObjectNode jsonClosedRegion(UiRegion region) { |
Steven Burrows | 482d950 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 409 | ObjectNode node = objectNode() |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 410 | .put("id", region.idAsString()) |
Simon Hunt | f836a87 | 2016-08-10 17:37:36 -0700 | [diff] [blame] | 411 | .put("name", region.name()) |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 412 | .put("nodeType", REGION) |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 413 | .put("nDevs", region.deviceCount()) |
| 414 | .put("nHosts", region.hostCount()); |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 415 | |
| 416 | Region r = region.backingRegion(); |
| 417 | addGeoLocation(node, r); |
| 418 | addProps(node, r); |
Steven Burrows | 482d950 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 419 | |
| 420 | addMetaUi(node, region.idAsString()); |
| 421 | return node; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 424 | /** |
| 425 | * Returns a JSON array representation of a set of regions/devices. Note |
| 426 | * that the information is sufficient for showing regions as nodes. |
| 427 | * |
| 428 | * @param nodes the nodes |
| 429 | * @return a JSON representation of the nodes |
| 430 | */ |
| 431 | public ArrayNode closedNodes(Set<UiNode> nodes) { |
| 432 | ArrayNode array = arrayNode(); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 433 | for (UiNode node : nodes) { |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 434 | if (node instanceof UiRegion) { |
| 435 | array.add(jsonClosedRegion((UiRegion) node)); |
| 436 | } else if (node instanceof UiDevice) { |
| 437 | array.add(json((UiDevice) node)); |
| 438 | } else { |
| 439 | log.warn("Unexpected node instance: {}", node.getClass()); |
| 440 | } |
| 441 | } |
| 442 | return array; |
| 443 | } |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 444 | |
| 445 | /** |
| 446 | * Returns a JSON array representation of a list of regions. Note that the |
| 447 | * information about each region is limited to what needs to be used to |
| 448 | * show the regions as nodes on the view. |
| 449 | * |
| 450 | * @param regions the regions |
| 451 | * @return a JSON representation of the minimal region information |
| 452 | */ |
| 453 | public ArrayNode closedRegions(Set<UiRegion> regions) { |
| 454 | ArrayNode array = arrayNode(); |
| 455 | for (UiRegion r : regions) { |
| 456 | array.add(jsonClosedRegion(r)); |
| 457 | } |
| 458 | return array; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Returns a JSON array representation of a list of devices. |
| 463 | * |
| 464 | * @param devices the devices |
| 465 | * @return a JSON representation of the devices |
| 466 | */ |
| 467 | public ArrayNode devices(Set<UiDevice> devices) { |
| 468 | ArrayNode array = arrayNode(); |
| 469 | for (UiDevice device : devices) { |
| 470 | array.add(json(device)); |
| 471 | } |
| 472 | return array; |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Returns a JSON array representation of a list of hosts. |
| 477 | * |
| 478 | * @param hosts the hosts |
| 479 | * @return a JSON representation of the hosts |
| 480 | */ |
| 481 | public ArrayNode hosts(Set<UiHost> hosts) { |
| 482 | ArrayNode array = arrayNode(); |
| 483 | for (UiHost host : hosts) { |
| 484 | array.add(json(host)); |
| 485 | } |
| 486 | return array; |
| 487 | } |
| 488 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 489 | // package-private for unit testing |
| 490 | List<Set<UiNode>> splitByLayer(List<String> layerTags, |
| 491 | Set<? extends UiNode> nodes) { |
| 492 | final int nLayers = layerTags.size(); |
| 493 | if (!layerTags.get(nLayers - 1).equals(LAYER_DEFAULT)) { |
| 494 | throw new IllegalArgumentException(E_DEF_NOT_LAST); |
| 495 | } |
| 496 | |
| 497 | List<Set<UiNode>> splitList = new ArrayList<>(layerTags.size()); |
| 498 | Map<String, Set<UiNode>> byLayer = new HashMap<>(layerTags.size()); |
| 499 | |
| 500 | for (String tag : layerTags) { |
| 501 | Set<UiNode> set = new HashSet<>(); |
| 502 | byLayer.put(tag, set); |
| 503 | splitList.add(set); |
| 504 | } |
| 505 | |
| 506 | for (UiNode n : nodes) { |
| 507 | String which = n.layer(); |
| 508 | if (!layerTags.contains(which)) { |
| 509 | which = LAYER_DEFAULT; |
| 510 | } |
| 511 | byLayer.get(which).add(n); |
| 512 | } |
| 513 | |
| 514 | return splitList; |
| 515 | } |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 516 | |
| 517 | /** |
| 518 | * Stores the memento for an element. |
| 519 | * This method assumes the payload has an id String, memento ObjectNode |
| 520 | * |
| 521 | * @param payload event payload |
| 522 | */ |
| 523 | void updateMeta(ObjectNode payload) { |
| 524 | |
| 525 | String id = JsonUtils.string(payload, "id"); |
| 526 | metaUi.put(id, JsonUtils.node(payload, "memento")); |
| 527 | |
| 528 | log.debug("Storing metadata for {}", id); |
| 529 | } |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 530 | } |