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