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