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