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