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; |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 23 | import com.google.common.base.Strings; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 24 | import org.onlab.osgi.ServiceDirectory; |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 25 | import org.onlab.packet.IpAddress; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 26 | import org.onosproject.cluster.ClusterService; |
| 27 | import org.onosproject.cluster.NodeId; |
| 28 | import org.onosproject.incubator.net.PortStatisticsService; |
| 29 | import org.onosproject.incubator.net.tunnel.TunnelService; |
| 30 | import org.onosproject.mastership.MastershipService; |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 31 | import org.onosproject.net.Annotated; |
| 32 | import org.onosproject.net.Annotations; |
| 33 | import org.onosproject.net.Device; |
Steven Burrows | ad75aa2 | 2016-12-14 17:17:24 -0500 | [diff] [blame] | 34 | import org.onosproject.net.DeviceId; |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 35 | import org.onosproject.net.Host; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 36 | import org.onosproject.net.device.DeviceService; |
| 37 | import org.onosproject.net.flow.FlowRuleService; |
| 38 | import org.onosproject.net.host.HostService; |
| 39 | import org.onosproject.net.intent.IntentService; |
| 40 | import org.onosproject.net.link.LinkService; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 41 | import org.onosproject.net.region.Region; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 42 | import org.onosproject.net.statistic.StatisticService; |
| 43 | import org.onosproject.net.topology.TopologyService; |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 44 | import org.onosproject.ui.JsonUtils; |
Steven Burrows | 86b74fc | 2017-02-22 00:15:16 +0000 | [diff] [blame] | 45 | import org.onosproject.ui.UiExtensionService; |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 46 | import org.onosproject.ui.UiPreferencesService; |
Steven Burrows | 86b74fc | 2017-02-22 00:15:16 +0000 | [diff] [blame] | 47 | import org.onosproject.ui.UiTopoMap; |
| 48 | import org.onosproject.ui.UiTopoMapFactory; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 49 | import org.onosproject.ui.model.topo.UiClusterMember; |
| 50 | import org.onosproject.ui.model.topo.UiDevice; |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 51 | import org.onosproject.ui.model.topo.UiElement; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 52 | import org.onosproject.ui.model.topo.UiHost; |
| 53 | import org.onosproject.ui.model.topo.UiLink; |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame^] | 54 | import org.onosproject.ui.model.topo.UiModelEvent; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 55 | import org.onosproject.ui.model.topo.UiNode; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 56 | import org.onosproject.ui.model.topo.UiRegion; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 57 | import org.onosproject.ui.model.topo.UiSynthLink; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 58 | import org.onosproject.ui.model.topo.UiTopoLayout; |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 59 | import org.onosproject.ui.topo.LayoutLocation; |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 60 | import org.slf4j.Logger; |
| 61 | import org.slf4j.LoggerFactory; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 62 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 63 | import java.util.ArrayList; |
| 64 | import java.util.HashMap; |
| 65 | import java.util.HashSet; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 66 | import java.util.List; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 67 | import java.util.Map; |
| 68 | import java.util.Set; |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 69 | import java.util.concurrent.ConcurrentHashMap; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 70 | |
| 71 | import static com.google.common.base.Preconditions.checkNotNull; |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 72 | import static org.onosproject.net.AnnotationKeys.GRID_X; |
| 73 | import static org.onosproject.net.AnnotationKeys.GRID_Y; |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 74 | import static org.onosproject.net.AnnotationKeys.LATITUDE; |
| 75 | import static org.onosproject.net.AnnotationKeys.LONGITUDE; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 76 | import static org.onosproject.ui.model.topo.UiNode.LAYER_DEFAULT; |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 77 | import static org.onosproject.ui.topo.LayoutLocation.fromCompactListString; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Facility for creating JSON messages to send to the topology view in the |
| 81 | * Web client. |
| 82 | */ |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 83 | public class Topo2Jsonifier { |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 84 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 85 | private static final String E_DEF_NOT_LAST = |
| 86 | "UiNode.LAYER_DEFAULT not last in layer list"; |
| 87 | private static final String E_UNKNOWN_UI_NODE = |
| 88 | "Unknown subclass of UiNode: "; |
| 89 | |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 90 | private static final String CONTEXT_KEY_DELIM = "_"; |
| 91 | private static final String NO_CONTEXT = ""; |
Simon Hunt | f0c6f54 | 2017-03-22 18:31:18 -0700 | [diff] [blame] | 92 | private static final String ZOOM_KEY = "layoutZoom"; |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 93 | |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 94 | private static final String REGION = "region"; |
| 95 | private static final String DEVICE = "device"; |
| 96 | private static final String HOST = "host"; |
Steven Burrows | 512b627 | 2016-12-19 14:09:45 -0500 | [diff] [blame] | 97 | private static final String TYPE = "type"; |
| 98 | private static final String SUBJECT = "subject"; |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 99 | private static final String DATA = "data"; |
| 100 | private static final String MEMO = "memo"; |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 101 | |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 102 | private static final String GEO = "geo"; |
| 103 | private static final String GRID = "grid"; |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 104 | private static final String PEER_LOCATIONS = "peerLocations"; |
Simon Hunt | f27a929 | 2017-05-04 17:36:26 -0700 | [diff] [blame] | 105 | private static final String LOCATION = "location"; |
| 106 | private static final String LOC_TYPE = "locType"; |
| 107 | private static final String LAT_OR_Y = "latOrY"; |
| 108 | private static final String LONG_OR_X = "longOrX"; |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 109 | |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 110 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 111 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 112 | private final ObjectMapper mapper = new ObjectMapper(); |
| 113 | |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 114 | // preferences are stored per user name... |
| 115 | private final String userName; |
| 116 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 117 | private ServiceDirectory directory; |
| 118 | private ClusterService clusterService; |
| 119 | private DeviceService deviceService; |
| 120 | private LinkService linkService; |
| 121 | private HostService hostService; |
| 122 | private MastershipService mastershipService; |
| 123 | private IntentService intentService; |
| 124 | private FlowRuleService flowService; |
| 125 | private StatisticService flowStatsService; |
| 126 | private PortStatisticsService portStatsService; |
| 127 | private TopologyService topologyService; |
| 128 | private TunnelService tunnelService; |
Steven Burrows | 86b74fc | 2017-02-22 00:15:16 +0000 | [diff] [blame] | 129 | private UiExtensionService uiextService; |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 130 | private UiPreferencesService prefService; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 131 | |
| 132 | |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 133 | // NOTE: we'll stick this here for now, but maybe there is a better home? |
| 134 | // (this is not distributed across the cluster) |
| 135 | private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>(); |
| 136 | |
| 137 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 138 | /** |
| 139 | * Creates an instance with a reference to the services directory, so that |
| 140 | * additional information about network elements may be looked up on |
| 141 | * on the fly. |
| 142 | * |
| 143 | * @param directory service directory |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 144 | * @param userName logged in user name |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 145 | */ |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 146 | public Topo2Jsonifier(ServiceDirectory directory, String userName) { |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 147 | this.directory = checkNotNull(directory, "Directory cannot be null"); |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 148 | this.userName = checkNotNull(userName, "User name cannot be null"); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 149 | |
| 150 | clusterService = directory.get(ClusterService.class); |
| 151 | deviceService = directory.get(DeviceService.class); |
| 152 | linkService = directory.get(LinkService.class); |
| 153 | hostService = directory.get(HostService.class); |
| 154 | mastershipService = directory.get(MastershipService.class); |
| 155 | intentService = directory.get(IntentService.class); |
| 156 | flowService = directory.get(FlowRuleService.class); |
| 157 | flowStatsService = directory.get(StatisticService.class); |
| 158 | portStatsService = directory.get(PortStatisticsService.class); |
| 159 | topologyService = directory.get(TopologyService.class); |
| 160 | tunnelService = directory.get(TunnelService.class); |
Steven Burrows | 86b74fc | 2017-02-22 00:15:16 +0000 | [diff] [blame] | 161 | uiextService = directory.get(UiExtensionService.class); |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 162 | prefService = directory.get(UiPreferencesService.class); |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 163 | } |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 164 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 165 | // for unit testing |
| 166 | Topo2Jsonifier() { |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 167 | userName = "(unit-test)"; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | private ObjectNode objectNode() { |
| 171 | return mapper.createObjectNode(); |
| 172 | } |
| 173 | |
| 174 | private ArrayNode arrayNode() { |
| 175 | return mapper.createArrayNode(); |
| 176 | } |
| 177 | |
| 178 | private String nullIsEmpty(Object o) { |
| 179 | return o == null ? "" : o.toString(); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /** |
| 184 | * Returns a JSON representation of the cluster members (ONOS instances). |
| 185 | * |
| 186 | * @param instances the instance model objects |
| 187 | * @return a JSON representation of the data |
| 188 | */ |
| 189 | ObjectNode instances(List<UiClusterMember> instances) { |
| 190 | NodeId local = clusterService.getLocalNode().id(); |
| 191 | ObjectNode payload = objectNode(); |
| 192 | |
| 193 | ArrayNode members = arrayNode(); |
| 194 | payload.set("members", members); |
| 195 | for (UiClusterMember member : instances) { |
| 196 | members.add(json(member, member.id().equals(local))); |
| 197 | } |
| 198 | |
| 199 | return payload; |
| 200 | } |
| 201 | |
| 202 | private ObjectNode json(UiClusterMember member, boolean isUiAttached) { |
Steven Burrows | ad75aa2 | 2016-12-14 17:17:24 -0500 | [diff] [blame] | 203 | int switchCount = mastershipService.getDevicesOf(member.id()).size(); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 204 | return objectNode() |
| 205 | .put("id", member.id().toString()) |
| 206 | .put("ip", member.ip().toString()) |
| 207 | .put("online", member.isOnline()) |
| 208 | .put("ready", member.isReady()) |
| 209 | .put("uiAttached", isUiAttached) |
Steven Burrows | ad75aa2 | 2016-12-14 17:17:24 -0500 | [diff] [blame] | 210 | .put("switches", switchCount); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Returns a JSON representation of the layout to use for displaying in |
Simon Hunt | f836a87 | 2016-08-10 17:37:36 -0700 | [diff] [blame] | 215 | * the topology view. The identifiers and names of regions from the |
| 216 | * current to the root is included, so that the bread-crumb widget can |
| 217 | * be rendered. |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 218 | * |
| 219 | * @param layout the layout to transform |
Simon Hunt | f836a87 | 2016-08-10 17:37:36 -0700 | [diff] [blame] | 220 | * @param crumbs list of layouts in bread-crumb order |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 221 | * @return a JSON representation of the data |
| 222 | */ |
Simon Hunt | f7e7d4a | 2017-03-24 15:22:20 -0700 | [diff] [blame] | 223 | ObjectNode layout(UiTopoLayout layout, List<UiTopoLayout> crumbs) { |
Simon Hunt | f836a87 | 2016-08-10 17:37:36 -0700 | [diff] [blame] | 224 | ObjectNode result = objectNode() |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 225 | .put("id", layout.id().toString()) |
| 226 | .put("parent", nullIsEmpty(layout.parent())) |
| 227 | .put("region", nullIsEmpty(layout.regionId())) |
Simon Hunt | f836a87 | 2016-08-10 17:37:36 -0700 | [diff] [blame] | 228 | .put("regionName", UiRegion.safeName(layout.region())); |
| 229 | addCrumbs(result, crumbs); |
Simon Hunt | f7e7d4a | 2017-03-24 15:22:20 -0700 | [diff] [blame] | 230 | addBgRef(result, layout); |
Simon Hunt | f836a87 | 2016-08-10 17:37:36 -0700 | [diff] [blame] | 231 | return result; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Simon Hunt | f7e7d4a | 2017-03-24 15:22:20 -0700 | [diff] [blame] | 234 | private void addBgRef(ObjectNode result, UiTopoLayout layout) { |
Steven Burrows | 86b74fc | 2017-02-22 00:15:16 +0000 | [diff] [blame] | 235 | String mapId = layout.geomap(); |
| 236 | String sprId = layout.sprites(); |
| 237 | |
| 238 | if (mapId != null) { |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 239 | result.put("bgType", GEO).put("bgId", mapId); |
Steven Burrows | 86b74fc | 2017-02-22 00:15:16 +0000 | [diff] [blame] | 240 | addMapParameters(result, mapId); |
| 241 | } else if (sprId != null) { |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 242 | result.put("bgType", GRID).put("bgId", sprId); |
Steven Burrows | 86b74fc | 2017-02-22 00:15:16 +0000 | [diff] [blame] | 243 | } |
Simon Hunt | f0c6f54 | 2017-03-22 18:31:18 -0700 | [diff] [blame] | 244 | |
Simon Hunt | f7e7d4a | 2017-03-24 15:22:20 -0700 | [diff] [blame] | 245 | attachZoomData(result, layout); |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Simon Hunt | f7e7d4a | 2017-03-24 15:22:20 -0700 | [diff] [blame] | 248 | private void attachZoomData(ObjectNode result, UiTopoLayout layout) { |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 249 | |
Simon Hunt | f0c6f54 | 2017-03-22 18:31:18 -0700 | [diff] [blame] | 250 | ObjectNode zoomData = objectNode(); |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 251 | |
Simon Hunt | f0c6f54 | 2017-03-22 18:31:18 -0700 | [diff] [blame] | 252 | // first, set configured scale and offset |
| 253 | addCfgZoomData(zoomData, layout); |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 254 | |
Simon Hunt | f0c6f54 | 2017-03-22 18:31:18 -0700 | [diff] [blame] | 255 | // next, retrieve user-set zoom data, if we have it |
Simon Hunt | f7e7d4a | 2017-03-24 15:22:20 -0700 | [diff] [blame] | 256 | String rid = layout.regionId().toString(); |
Simon Hunt | f0c6f54 | 2017-03-22 18:31:18 -0700 | [diff] [blame] | 257 | ObjectNode userZoom = metaUi.get(contextKey(rid, ZOOM_KEY)); |
| 258 | if (userZoom != null) { |
| 259 | zoomData.set("usr", userZoom); |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 260 | } |
Simon Hunt | f0c6f54 | 2017-03-22 18:31:18 -0700 | [diff] [blame] | 261 | result.set("bgZoom", zoomData); |
| 262 | } |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 263 | |
Simon Hunt | f0c6f54 | 2017-03-22 18:31:18 -0700 | [diff] [blame] | 264 | private void addCfgZoomData(ObjectNode data, UiTopoLayout layout) { |
| 265 | ObjectNode zoom = objectNode(); |
| 266 | zoom.put("scale", layout.scale()); |
| 267 | zoom.put("offsetX", layout.offsetX()); |
| 268 | zoom.put("offsetY", layout.offsetY()); |
| 269 | data.set("cfg", zoom); |
Steven Burrows | 86b74fc | 2017-02-22 00:15:16 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | private void addMapParameters(ObjectNode result, String mapId) { |
| 273 | |
| 274 | // TODO: This ought to be written more efficiently. |
| 275 | |
| 276 | // ALSO: Should retrieving a UiTopoMap by ID be something that |
| 277 | // the UiExtensionService provides, along with other |
| 278 | // useful lookups? |
| 279 | // |
| 280 | // Or should it remain very basic / general? |
| 281 | // |
| 282 | // return uiextService.getTopoMap(String mapId); |
| 283 | |
| 284 | final UiTopoMap[] map = {null}; |
| 285 | |
| 286 | uiextService.getExtensions().forEach(ext -> { |
| 287 | UiTopoMapFactory factory = ext.topoMapFactory(); |
| 288 | |
| 289 | // TODO: use .stream().filter(...) here |
| 290 | if (map[0] == null && factory != null) { |
| 291 | List<UiTopoMap> topoMaps = factory.geoMaps(); |
| 292 | |
| 293 | topoMaps.forEach(m -> { |
| 294 | if (map[0] == null && m.id().equals(mapId)) { |
| 295 | map[0] = m; |
| 296 | } |
| 297 | }); |
| 298 | } |
| 299 | }); |
| 300 | |
| 301 | UiTopoMap m = map[0]; |
| 302 | if (m != null) { |
| 303 | result.put("bgDesc", m.description()) |
| 304 | .put("bgFilePath", m.filePath()) |
| 305 | .put("bgDefaultScale", m.scale()); |
| 306 | } else { |
| 307 | result.put("bgWarn", "no map registered with id: " + mapId); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
Simon Hunt | f836a87 | 2016-08-10 17:37:36 -0700 | [diff] [blame] | 311 | private void addCrumbs(ObjectNode result, List<UiTopoLayout> crumbs) { |
| 312 | ArrayNode trail = arrayNode(); |
| 313 | crumbs.forEach(c -> { |
| 314 | ObjectNode n = objectNode() |
| 315 | .put("id", c.regionId().toString()) |
| 316 | .put("name", UiRegion.safeName(c.region())); |
| 317 | trail.add(n); |
| 318 | }); |
| 319 | result.set("crumbs", trail); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Returns a JSON representation of the region to display in the topology |
| 324 | * view. |
| 325 | * |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 326 | * @param region the region to transform to JSON |
| 327 | * @param subRegions the subregions within this region |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 328 | * @param links the links within this region |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 329 | * @return a JSON representation of the data |
| 330 | */ |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 331 | ObjectNode region(UiRegion region, Set<UiRegion> subRegions, |
| 332 | List<UiSynthLink> links) { |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 333 | ObjectNode payload = objectNode(); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 334 | if (region == null) { |
| 335 | payload.put("note", "no-region"); |
| 336 | return payload; |
| 337 | } |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 338 | |
| 339 | String ridStr = region.idAsString(); |
| 340 | |
| 341 | payload.put("id", ridStr); |
| 342 | payload.set("subregions", jsonSubRegions(ridStr, subRegions)); |
Simon Hunt | cd508a6 | 2016-10-27 12:47:24 -0700 | [diff] [blame] | 343 | payload.set("links", jsonLinks(links)); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 344 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 345 | List<String> layerTags = region.layerOrder(); |
| 346 | List<Set<UiNode>> splitDevices = splitByLayer(layerTags, region.devices()); |
| 347 | List<Set<UiNode>> splitHosts = splitByLayer(layerTags, region.hosts()); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 348 | |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 349 | payload.set("devices", jsonGrouped(ridStr, splitDevices)); |
| 350 | payload.set("hosts", jsonGrouped(ridStr, splitHosts)); |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 351 | payload.set("layerOrder", jsonStrings(layerTags)); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 352 | |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 353 | if (!region.isRoot()) { |
| 354 | addPeerLocations(payload, region.backingRegion()); |
| 355 | } |
| 356 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 357 | return payload; |
| 358 | } |
| 359 | |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 360 | private ArrayNode jsonSubRegions(String ridStr, Set<UiRegion> subregions) { |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 361 | ArrayNode kids = arrayNode(); |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 362 | subregions.forEach(s -> kids.add(jsonClosedRegion(ridStr, s))); |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 363 | return kids; |
| 364 | } |
| 365 | |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame^] | 366 | protected JsonNode jsonLinks(List<UiSynthLink> links) { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 367 | ArrayNode synthLinks = arrayNode(); |
| 368 | links.forEach(l -> synthLinks.add(json(l))); |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame^] | 369 | |
| 370 | // TODO: implement the following........ |
| 371 | // collateSynthLinks(synthLinks, links); |
| 372 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 373 | return synthLinks; |
| 374 | } |
| 375 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 376 | private ArrayNode jsonStrings(List<String> strings) { |
| 377 | ArrayNode array = arrayNode(); |
| 378 | strings.forEach(array::add); |
| 379 | return array; |
| 380 | } |
| 381 | |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 382 | private ArrayNode jsonGrouped(String ridStr, List<Set<UiNode>> groupedNodes) { |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 383 | ArrayNode result = arrayNode(); |
| 384 | groupedNodes.forEach(g -> { |
| 385 | ArrayNode subset = arrayNode(); |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 386 | g.forEach(n -> subset.add(json(ridStr, n))); |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 387 | result.add(subset); |
| 388 | }); |
| 389 | return result; |
| 390 | } |
| 391 | |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 392 | /** |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 393 | * Creates a JSON representation of a UI element. |
| 394 | * |
| 395 | * @param element the source element |
| 396 | * @return a JSON representation of that element |
| 397 | */ |
| 398 | public ObjectNode jsonUiElement(UiElement element) { |
| 399 | if (element instanceof UiNode) { |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 400 | return json(NO_CONTEXT, (UiNode) element); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 401 | } |
| 402 | if (element instanceof UiLink) { |
| 403 | return json((UiLink) element); |
| 404 | } |
| 405 | |
| 406 | // TODO: UiClusterMember |
| 407 | |
| 408 | // Unrecognized UiElement class |
| 409 | return objectNode() |
| 410 | .put("warning", "unknown UiElement... cannot encode") |
| 411 | .put("javaclass", element.getClass().toString()); |
| 412 | } |
| 413 | |
| 414 | /** |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 415 | * Creates a JSON representation of a UI model event. |
| 416 | * |
| 417 | * @param modelEvent the source model event |
| 418 | * @return a JSON representation of that event |
| 419 | */ |
| 420 | public ObjectNode jsonEvent(UiModelEvent modelEvent) { |
Steven Burrows | 512b627 | 2016-12-19 14:09:45 -0500 | [diff] [blame] | 421 | ObjectNode payload = objectNode(); |
| 422 | payload.put(TYPE, enumToString(modelEvent.type())); |
| 423 | payload.put(SUBJECT, modelEvent.subject().idAsString()); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 424 | payload.set(DATA, modelEvent.data()); |
| 425 | payload.put(MEMO, modelEvent.memo()); |
Steven Burrows | 512b627 | 2016-12-19 14:09:45 -0500 | [diff] [blame] | 426 | return payload; |
| 427 | } |
| 428 | |
| 429 | // TODO: Investigate why we can't do this inline |
| 430 | private String enumToString(Enum<?> e) { |
| 431 | return e.toString(); |
| 432 | } |
| 433 | |
Steven Burrows | ad75aa2 | 2016-12-14 17:17:24 -0500 | [diff] [blame] | 434 | // Returns the name of the master node for the specified device id. |
| 435 | private String master(DeviceId deviceId) { |
| 436 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 437 | return master != null ? master.toString() : ""; |
| 438 | } |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 439 | |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 440 | private ObjectNode json(String ridStr, UiNode node) { |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 441 | if (node instanceof UiRegion) { |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 442 | return jsonClosedRegion(ridStr, (UiRegion) node); |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 443 | } |
| 444 | if (node instanceof UiDevice) { |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 445 | return json(ridStr, (UiDevice) node); |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 446 | } |
| 447 | if (node instanceof UiHost) { |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 448 | return json(ridStr, (UiHost) node); |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 449 | } |
| 450 | throw new IllegalStateException(E_UNKNOWN_UI_NODE + node.getClass()); |
| 451 | } |
| 452 | |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 453 | private ObjectNode json(String ridStr, UiDevice device) { |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 454 | ObjectNode node = objectNode() |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 455 | .put("id", device.idAsString()) |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 456 | .put("nodeType", DEVICE) |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 457 | .put("type", device.type()) |
Simon Hunt | 3d71252 | 2016-08-11 11:20:44 -0700 | [diff] [blame] | 458 | .put("online", deviceService.isAvailable(device.id())) |
Steven Burrows | ad75aa2 | 2016-12-14 17:17:24 -0500 | [diff] [blame] | 459 | .put("master", master(device.id())) |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 460 | .put("layer", device.layer()); |
| 461 | |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 462 | Device d = device.backingDevice(); |
| 463 | |
| 464 | addProps(node, d); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 465 | addGeoGridLocation(node, d); |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 466 | addMetaUi(node, ridStr, device.idAsString()); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 467 | |
| 468 | return node; |
| 469 | } |
| 470 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 471 | private void addProps(ObjectNode node, Annotated a) { |
| 472 | Annotations annot = a.annotations(); |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 473 | ObjectNode props = objectNode(); |
| 474 | if (annot != null) { |
| 475 | annot.keys().forEach(k -> props.put(k, annot.value(k))); |
| 476 | } |
| 477 | node.set("props", props); |
| 478 | } |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 479 | |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 480 | private void addMetaUi(ObjectNode node, String ridStr, String metaInstanceId) { |
| 481 | String key = contextKey(ridStr, metaInstanceId); |
| 482 | ObjectNode meta = metaUi.get(key); |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 483 | if (meta != null) { |
| 484 | node.set("metaUi", meta); |
| 485 | } |
| 486 | } |
| 487 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 488 | private void addGeoGridLocation(ObjectNode node, Annotated a) { |
Simon Hunt | f27a929 | 2017-05-04 17:36:26 -0700 | [diff] [blame] | 489 | List<String> latLongData = getAnnotValues(a, LATITUDE, LONGITUDE); |
| 490 | List<String> gridYXdata = getAnnotValues(a, GRID_Y, GRID_X); |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 491 | |
Simon Hunt | f27a929 | 2017-05-04 17:36:26 -0700 | [diff] [blame] | 492 | if (latLongData != null) { |
| 493 | attachLocation(node, GEO, latLongData); |
| 494 | } else if (gridYXdata != null) { |
| 495 | attachLocation(node, GRID, gridYXdata); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
| 499 | private void attachLocation(ObjectNode node, String locType, |
Simon Hunt | f27a929 | 2017-05-04 17:36:26 -0700 | [diff] [blame] | 500 | List<String> values) { |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 501 | try { |
Simon Hunt | f27a929 | 2017-05-04 17:36:26 -0700 | [diff] [blame] | 502 | double latOrY = Double.parseDouble(values.get(0)); |
| 503 | double longOrX = Double.parseDouble(values.get(1)); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 504 | ObjectNode loc = objectNode() |
Simon Hunt | f27a929 | 2017-05-04 17:36:26 -0700 | [diff] [blame] | 505 | .put(LOC_TYPE, locType) |
| 506 | .put(LAT_OR_Y, latOrY) |
| 507 | .put(LONG_OR_X, longOrX); |
| 508 | node.set(LOCATION, loc); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 509 | |
| 510 | } catch (NumberFormatException e) { |
Simon Hunt | f27a929 | 2017-05-04 17:36:26 -0700 | [diff] [blame] | 511 | log.warn("Invalid {} data: lat/Y={}, long/X={}", |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame^] | 512 | locType, values.get(0), values.get(1)); |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 516 | private void addPeerLocations(ObjectNode node, Region r) { |
| 517 | String compact = r.annotations().value(PEER_LOCATIONS); |
| 518 | if (!Strings.isNullOrEmpty(compact)) { |
| 519 | List<LayoutLocation> locs = fromCompactListString(compact); |
| 520 | |
| 521 | ObjectNode o = objectNode(); |
| 522 | for (LayoutLocation ll : locs) { |
| 523 | ObjectNode lnode = objectNode() |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame^] | 524 | .put(LOC_TYPE, ll.locType().toString()) |
| 525 | .put(LAT_OR_Y, ll.latOrY()) |
| 526 | .put(LONG_OR_X, ll.longOrX()); |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 527 | o.set(ll.id(), lnode); |
| 528 | } |
| 529 | |
| 530 | node.set(PEER_LOCATIONS, o); |
| 531 | } |
| 532 | } |
| 533 | |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 534 | private void addIps(ObjectNode node, Host h) { |
| 535 | Set<IpAddress> ips = h.ipAddresses(); |
| 536 | |
| 537 | ArrayNode a = arrayNode(); |
| 538 | for (IpAddress ip : ips) { |
| 539 | a.add(ip.toString()); |
| 540 | } |
| 541 | |
| 542 | node.set("ips", a); |
| 543 | } |
| 544 | |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 545 | // return list of string values from annotated instance, for given keys |
| 546 | // return null if any keys are not present |
| 547 | List<String> getAnnotValues(Annotated a, String... annotKeys) { |
| 548 | List<String> result = new ArrayList<>(annotKeys.length); |
| 549 | for (String k : annotKeys) { |
| 550 | String v = a.annotations().value(k); |
| 551 | if (v == null) { |
| 552 | return null; |
| 553 | } |
| 554 | result.add(v); |
| 555 | } |
| 556 | return result; |
| 557 | } |
| 558 | |
| 559 | // derive JSON object from annotations |
| 560 | private ObjectNode props(Annotations annotations) { |
| 561 | ObjectNode p = objectNode(); |
| 562 | if (annotations != null) { |
| 563 | annotations.keys().forEach(k -> p.put(k, annotations.value(k))); |
| 564 | } |
| 565 | return p; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 568 | private ObjectNode json(String ridStr, UiHost host) { |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 569 | ObjectNode node = objectNode() |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 570 | .put("id", host.idAsString()) |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 571 | .put("nodeType", HOST) |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 572 | .put("layer", host.layer()); |
| 573 | // TODO: complete host details |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 574 | Host h = host.backingHost(); |
| 575 | |
| 576 | addIps(node, h); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 577 | addProps(node, h); |
| 578 | addGeoGridLocation(node, h); |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 579 | addMetaUi(node, ridStr, host.idAsString()); |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 580 | |
| 581 | return node; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame^] | 584 | // TODO: add method to JSONify an aggregated collection of synth links |
| 585 | |
| 586 | private void collateSynthLinks(ArrayNode array, |
| 587 | List<UiSynthLink> links) { |
| 588 | // TODO - combine via link id |
| 589 | } |
| 590 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 591 | private ObjectNode json(UiSynthLink sLink) { |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 592 | return json(sLink.link()); |
| 593 | } |
| 594 | |
| 595 | private ObjectNode json(UiLink link) { |
Simon Hunt | 3d71252 | 2016-08-11 11:20:44 -0700 | [diff] [blame] | 596 | ObjectNode data = objectNode() |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 597 | .put("id", link.idAsString()) |
| 598 | .put("epA", link.endPointA()) |
| 599 | .put("epB", link.endPointB()) |
| 600 | .put("type", link.type()); |
| 601 | String pA = link.endPortA(); |
| 602 | String pB = link.endPortB(); |
Simon Hunt | 3d71252 | 2016-08-11 11:20:44 -0700 | [diff] [blame] | 603 | if (pA != null) { |
| 604 | data.put("portA", pA); |
| 605 | } |
| 606 | if (pB != null) { |
| 607 | data.put("portB", pB); |
| 608 | } |
| 609 | return data; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 613 | private ObjectNode jsonClosedRegion(String ridStr, UiRegion region) { |
Steven Burrows | 482d950 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 614 | ObjectNode node = objectNode() |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 615 | .put("id", region.idAsString()) |
Simon Hunt | f836a87 | 2016-08-10 17:37:36 -0700 | [diff] [blame] | 616 | .put("name", region.name()) |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 617 | .put("nodeType", REGION) |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 618 | .put("nDevs", region.deviceCount()) |
| 619 | .put("nHosts", region.hostCount()); |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 620 | // TODO: device and host counts should take into account any nested |
| 621 | // subregions. i.e. should be the sum of all devices/hosts in |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 622 | // all descendant subregions. |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 623 | |
| 624 | Region r = region.backingRegion(); |
Simon Hunt | 0ee20bf | 2017-05-10 19:59:17 -0700 | [diff] [blame] | 625 | if (r != null) { |
| 626 | // add data injected via network configuration script |
| 627 | addGeoGridLocation(node, r); |
| 628 | addProps(node, r); |
| 629 | } |
Steven Burrows | 482d950 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 630 | |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 631 | // this may contain location data, as dragged by user |
| 632 | // (which should take precedence, over configured data) |
| 633 | addMetaUi(node, ridStr, region.idAsString()); |
Steven Burrows | 482d950 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 634 | return node; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 637 | /** |
| 638 | * Returns a JSON array representation of a set of regions/devices. Note |
| 639 | * that the information is sufficient for showing regions as nodes. |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 640 | * THe region ID string defines the context (which region) the node is |
| 641 | * being displayed in. |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 642 | * |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 643 | * @param ridStr region-id string |
Simon Hunt | f0c6f54 | 2017-03-22 18:31:18 -0700 | [diff] [blame] | 644 | * @param nodes the nodes |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 645 | * @return a JSON representation of the nodes |
| 646 | */ |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 647 | public ArrayNode closedNodes(String ridStr, Set<UiNode> nodes) { |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 648 | ArrayNode array = arrayNode(); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 649 | for (UiNode node : nodes) { |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 650 | if (node instanceof UiRegion) { |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 651 | array.add(jsonClosedRegion(ridStr, (UiRegion) node)); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 652 | } else if (node instanceof UiDevice) { |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 653 | array.add(json(ridStr, (UiDevice) node)); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 654 | } else { |
| 655 | log.warn("Unexpected node instance: {}", node.getClass()); |
| 656 | } |
| 657 | } |
| 658 | return array; |
| 659 | } |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 660 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 661 | // package-private for unit testing |
| 662 | List<Set<UiNode>> splitByLayer(List<String> layerTags, |
| 663 | Set<? extends UiNode> nodes) { |
| 664 | final int nLayers = layerTags.size(); |
| 665 | if (!layerTags.get(nLayers - 1).equals(LAYER_DEFAULT)) { |
| 666 | throw new IllegalArgumentException(E_DEF_NOT_LAST); |
| 667 | } |
| 668 | |
| 669 | List<Set<UiNode>> splitList = new ArrayList<>(layerTags.size()); |
| 670 | Map<String, Set<UiNode>> byLayer = new HashMap<>(layerTags.size()); |
| 671 | |
| 672 | for (String tag : layerTags) { |
| 673 | Set<UiNode> set = new HashSet<>(); |
| 674 | byLayer.put(tag, set); |
| 675 | splitList.add(set); |
| 676 | } |
| 677 | |
| 678 | for (UiNode n : nodes) { |
| 679 | String which = n.layer(); |
| 680 | if (!layerTags.contains(which)) { |
| 681 | which = LAYER_DEFAULT; |
| 682 | } |
| 683 | byLayer.get(which).add(n); |
| 684 | } |
| 685 | |
| 686 | return splitList; |
| 687 | } |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 688 | |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 689 | |
| 690 | private String contextKey(String context, String key) { |
| 691 | return context + CONTEXT_KEY_DELIM + key; |
| 692 | } |
| 693 | |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 694 | /** |
| 695 | * Stores the memento for an element. |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 696 | * This method assumes the payload has an id String, memento ObjectNode. |
| 697 | * The region-id string is used as a context within which to store the |
| 698 | * memento. |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 699 | * |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 700 | * @param ridStr region ID string |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 701 | * @param payload event payload |
| 702 | */ |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 703 | void updateMeta(String ridStr, ObjectNode payload) { |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 704 | |
| 705 | String id = JsonUtils.string(payload, "id"); |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 706 | String key = contextKey(ridStr, id); |
| 707 | metaUi.put(key, JsonUtils.node(payload, "memento")); |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 708 | |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 709 | log.debug("Storing metadata for {}", key); |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 710 | } |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 711 | } |