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