blob: 60fa17aa1d6577a311f9b0bd534e851ad53ae1eb [file] [log] [blame]
Thomas Vachuska329af532015-03-10 02:08:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska329af532015-03-10 02:08:33 -07003 *
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 */
16package org.onosproject.ui.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
Thomas Vachuska329af532015-03-10 02:08:33 -070019import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Hunt879ce452017-08-10 23:32:00 -070021import com.google.common.collect.ImmutableSet;
Thomas Vachuska329af532015-03-10 02:08:33 -070022import org.onlab.osgi.ServiceDirectory;
23import org.onlab.packet.IpAddress;
Simon Hunta58d8942017-08-11 12:51:14 -070024import org.onlab.packet.VlanId;
Simon Hunt95d56fd2015-11-12 11:06:44 -080025import org.onlab.util.DefaultHashMap;
Thomas Vachuska329af532015-03-10 02:08:33 -070026import org.onosproject.cluster.ClusterEvent;
Thomas Vachuska329af532015-03-10 02:08:33 -070027import org.onosproject.cluster.ControllerNode;
28import org.onosproject.cluster.NodeId;
29import org.onosproject.core.CoreService;
cheng fan35dc0f22015-06-10 06:02:47 +080030import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
31import org.onosproject.incubator.net.tunnel.Tunnel;
Thomas Vachuska329af532015-03-10 02:08:33 -070032import org.onosproject.net.Annotated;
33import org.onosproject.net.AnnotationKeys;
34import org.onosproject.net.Annotations;
35import org.onosproject.net.ConnectPoint;
36import org.onosproject.net.DefaultEdgeLink;
37import org.onosproject.net.Device;
38import org.onosproject.net.DeviceId;
39import org.onosproject.net.EdgeLink;
Simon Huntf4fd2a22016-08-10 15:41:09 -070040import org.onosproject.net.ElementId;
Thomas Vachuska329af532015-03-10 02:08:33 -070041import org.onosproject.net.Host;
42import org.onosproject.net.HostId;
43import org.onosproject.net.HostLocation;
44import org.onosproject.net.Link;
Thomas Vachuska329af532015-03-10 02:08:33 -070045import org.onosproject.net.device.DeviceEvent;
Thomas Vachuska329af532015-03-10 02:08:33 -070046import org.onosproject.net.host.HostEvent;
Thomas Vachuska329af532015-03-10 02:08:33 -070047import org.onosproject.net.link.LinkEvent;
Thomas Vachuska329af532015-03-10 02:08:33 -070048import org.onosproject.net.provider.ProviderId;
Thomas Vachuska329af532015-03-10 02:08:33 -070049import org.onosproject.net.topology.Topology;
Simon Huntd2747a02015-04-30 22:41:16 -070050import org.onosproject.ui.JsonUtils;
Thomas Vachuska329af532015-03-10 02:08:33 -070051import org.onosproject.ui.UiConnection;
Simon Hunta0ddb022015-05-01 09:53:01 -070052import org.onosproject.ui.UiMessageHandler;
Simon Hunted804d52016-03-30 09:51:40 -070053import org.onosproject.ui.impl.topo.util.ServicesBundle;
Simon Hunt879ce452017-08-10 23:32:00 -070054import org.onosproject.ui.lion.LionBundle;
Simon Hunt0af1ec32015-07-24 12:17:55 -070055import org.onosproject.ui.topo.PropertyPanel;
Thomas Vachuska329af532015-03-10 02:08:33 -070056import org.slf4j.Logger;
57import org.slf4j.LoggerFactory;
58
Thomas Vachuska329af532015-03-10 02:08:33 -070059import java.util.Collection;
60import java.util.Collections;
Thomas Vachuska329af532015-03-10 02:08:33 -070061import java.util.Iterator;
Thomas Vachuska329af532015-03-10 02:08:33 -070062import java.util.Map;
Thomas Vachuskac616e172018-04-17 16:57:12 -070063import java.util.Objects;
chengfanc553c952016-07-22 15:48:23 +080064import java.util.Optional;
Simon Huntf4fd2a22016-08-10 15:41:09 -070065import java.util.Set;
Thomas Vachuska329af532015-03-10 02:08:33 -070066import java.util.concurrent.ConcurrentHashMap;
67
Thomas Vachuska329af532015-03-10 02:08:33 -070068import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuska329af532015-03-10 02:08:33 -070069import static org.onosproject.net.PortNumber.portNumber;
Thomas Vachuskac616e172018-04-17 16:57:12 -070070import static org.onosproject.net.config.basics.BasicElementConfig.LOC_TYPE_GEO;
71import static org.onosproject.net.config.basics.BasicElementConfig.LOC_TYPE_GRID;
Simon Hunt3a0598f2015-08-04 19:59:04 -070072import static org.onosproject.ui.topo.TopoConstants.CoreButtons;
Simon Hunt879ce452017-08-10 23:32:00 -070073import static org.onosproject.ui.topo.TopoConstants.Properties.DEVICES;
74import static org.onosproject.ui.topo.TopoConstants.Properties.FLOWS;
Simon Hunta58d8942017-08-11 12:51:14 -070075import static org.onosproject.ui.topo.TopoConstants.Properties.GRID_X;
76import static org.onosproject.ui.topo.TopoConstants.Properties.GRID_Y;
Simon Hunt879ce452017-08-10 23:32:00 -070077import static org.onosproject.ui.topo.TopoConstants.Properties.HOSTS;
Simon Hunta58d8942017-08-11 12:51:14 -070078import static org.onosproject.ui.topo.TopoConstants.Properties.HW_VERSION;
Simon Hunt879ce452017-08-10 23:32:00 -070079import static org.onosproject.ui.topo.TopoConstants.Properties.INTENTS;
Simon Hunta58d8942017-08-11 12:51:14 -070080import static org.onosproject.ui.topo.TopoConstants.Properties.IP;
81import static org.onosproject.ui.topo.TopoConstants.Properties.LATITUDE;
Simon Hunt879ce452017-08-10 23:32:00 -070082import static org.onosproject.ui.topo.TopoConstants.Properties.LINKS;
Simon Hunta58d8942017-08-11 12:51:14 -070083import static org.onosproject.ui.topo.TopoConstants.Properties.LONGITUDE;
84import static org.onosproject.ui.topo.TopoConstants.Properties.MAC;
85import static org.onosproject.ui.topo.TopoConstants.Properties.PORTS;
86import static org.onosproject.ui.topo.TopoConstants.Properties.PROTOCOL;
87import static org.onosproject.ui.topo.TopoConstants.Properties.SERIAL_NUMBER;
88import static org.onosproject.ui.topo.TopoConstants.Properties.SW_VERSION;
Simon Hunt879ce452017-08-10 23:32:00 -070089import static org.onosproject.ui.topo.TopoConstants.Properties.TOPOLOGY_SSCS;
90import static org.onosproject.ui.topo.TopoConstants.Properties.TUNNELS;
Simon Hunta58d8942017-08-11 12:51:14 -070091import static org.onosproject.ui.topo.TopoConstants.Properties.URI;
92import static org.onosproject.ui.topo.TopoConstants.Properties.VENDOR;
Simon Hunt879ce452017-08-10 23:32:00 -070093import static org.onosproject.ui.topo.TopoConstants.Properties.VERSION;
Simon Hunta58d8942017-08-11 12:51:14 -070094import static org.onosproject.ui.topo.TopoConstants.Properties.VLAN;
95import static org.onosproject.ui.topo.TopoConstants.Properties.VLAN_NONE;
Simon Huntd3ceffa2015-08-25 12:44:35 -070096import static org.onosproject.ui.topo.TopoUtils.compactLinkString;
Thomas Vachuska329af532015-03-10 02:08:33 -070097
98/**
99 * Facility for creating messages bound for the topology viewer.
100 */
Simon Hunta0ddb022015-05-01 09:53:01 -0700101public abstract class TopologyViewMessageHandlerBase extends UiMessageHandler {
Thomas Vachuska329af532015-03-10 02:08:33 -0700102
Simon Huntf4fd2a22016-08-10 15:41:09 -0700103 private static final String NO_GEO_VALUE = "0.0";
Simon Hunt10618f62017-06-15 19:30:52 -0700104 private static final String DASH = "-";
Simon Hunta58d8942017-08-11 12:51:14 -0700105 private static final String SLASH = " / ";
Simon Hunt10618f62017-06-15 19:30:52 -0700106
107 // nav paths are the view names for hot-link navigation from topo view...
108 private static final String DEVICE_NAV_PATH = "device";
109 private static final String HOST_NAV_PATH = "host";
Simon Huntf4fd2a22016-08-10 15:41:09 -0700110
Simon Hunta58d8942017-08-11 12:51:14 -0700111 // link panel label keys
112 private static final String LPL_FRIENDLY = "lp_label_friendly";
113 private static final String LPL_A_TYPE = "lp_label_a_type";
114 private static final String LPL_A_ID = "lp_label_a_id";
115 private static final String LPL_A_FRIENDLY = "lp_label_a_friendly";
116 private static final String LPL_A_PORT = "lp_label_a_port";
117 private static final String LPL_B_TYPE = "lp_label_b_type";
118 private static final String LPL_B_ID = "lp_label_b_id";
119 private static final String LPL_B_FRIENDLY = "lp_label_b_friendly";
120 private static final String LPL_B_PORT = "lp_label_b_port";
121 private static final String LPL_A2B = "lp_label_a2b";
122 private static final String LPL_B2A = "lp_label_b2a";
123 private static final String LPV_NO_LINK = "lp_value_no_link";
124
125 // other Lion keys
126 private static final String HOST = "host";
127 private static final String DEVICE = "device";
128 private static final String EXPECTED = "expected";
129 private static final String NOT_EXPECTED = "not_expected";
130
Simon Hunt95d56fd2015-11-12 11:06:44 -0800131 // default to an "add" event...
132 private static final DefaultHashMap<ClusterEvent.Type, String> CLUSTER_EVENT =
133 new DefaultHashMap<>("addInstance");
134
135 // default to an "update" event...
136 private static final DefaultHashMap<DeviceEvent.Type, String> DEVICE_EVENT =
137 new DefaultHashMap<>("updateDevice");
138 private static final DefaultHashMap<LinkEvent.Type, String> LINK_EVENT =
139 new DefaultHashMap<>("updateLink");
140 private static final DefaultHashMap<HostEvent.Type, String> HOST_EVENT =
141 new DefaultHashMap<>("updateHost");
142
143 // but call out specific events that we care to differentiate...
144 static {
145 CLUSTER_EVENT.put(ClusterEvent.Type.INSTANCE_REMOVED, "removeInstance");
146
147 DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_ADDED, "addDevice");
148 DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_REMOVED, "removeDevice");
149
150 LINK_EVENT.put(LinkEvent.Type.LINK_ADDED, "addLink");
151 LINK_EVENT.put(LinkEvent.Type.LINK_REMOVED, "removeLink");
152
153 HOST_EVENT.put(HostEvent.Type.HOST_ADDED, "addHost");
154 HOST_EVENT.put(HostEvent.Type.HOST_REMOVED, "removeHost");
155 HOST_EVENT.put(HostEvent.Type.HOST_MOVED, "moveHost");
156 }
157
Simon Hunta58d8942017-08-11 12:51:14 -0700158 private static final DefaultHashMap<Device.Type, String> DEVICE_GLYPHS =
159 new DefaultHashMap<>("m_unknown");
160
161 static {
162 DEVICE_GLYPHS.put(Device.Type.SWITCH, "m_switch");
163 DEVICE_GLYPHS.put(Device.Type.ROUTER, "m_router");
164 DEVICE_GLYPHS.put(Device.Type.ROADM, "m_roadm");
165 DEVICE_GLYPHS.put(Device.Type.OTN, "m_otn");
166 DEVICE_GLYPHS.put(Device.Type.ROADM_OTN, "m_roadm_otn");
167 DEVICE_GLYPHS.put(Device.Type.BALANCER, "m_balancer");
168 DEVICE_GLYPHS.put(Device.Type.IPS, "m_ips");
169 DEVICE_GLYPHS.put(Device.Type.IDS, "m_ids");
170 DEVICE_GLYPHS.put(Device.Type.CONTROLLER, "m_controller");
171 DEVICE_GLYPHS.put(Device.Type.VIRTUAL, "m_virtual");
172 DEVICE_GLYPHS.put(Device.Type.FIBER_SWITCH, "m_fiberSwitch");
173 DEVICE_GLYPHS.put(Device.Type.MICROWAVE, "m_microwave");
174 DEVICE_GLYPHS.put(Device.Type.OLT, "m_olt");
175 DEVICE_GLYPHS.put(Device.Type.ONU, "m_onu");
176 DEVICE_GLYPHS.put(Device.Type.OPTICAL_AMPLIFIER, "unknown"); // TODO glyph needed
177 DEVICE_GLYPHS.put(Device.Type.OTHER, "m_other");
178 }
179
180 private static final String DEFAULT_HOST_GLYPH = "m_endstation";
181 private static final String LINK_GLYPH = "m_ports";
182
183
Ray Milkey9c9cde42018-01-12 14:22:06 -0800184 static final Logger log =
Simon Huntd2747a02015-04-30 22:41:16 -0700185 LoggerFactory.getLogger(TopologyViewMessageHandlerBase.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700186
Simon Huntd2747a02015-04-30 22:41:16 -0700187 private static final ProviderId PID =
188 new ProviderId("core", "org.onosproject.core", true);
Thomas Vachuska329af532015-03-10 02:08:33 -0700189
Thomas Vachuska329af532015-03-10 02:08:33 -0700190 // TODO: extract into an external & durable state; good enough for now and demo
191 private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>();
192
193 /**
Thomas Vachuska329af532015-03-10 02:08:33 -0700194 * Returns read-only view of the meta-ui information.
195 *
196 * @return map of id to meta-ui mementos
197 */
198 static Map<String, ObjectNode> getMetaUi() {
199 return Collections.unmodifiableMap(metaUi);
200 }
201
Thomas Vachuskac616e172018-04-17 16:57:12 -0700202 /**
203 * Clears any meta-ui information.
204 */
205 public static void clearMetaUi() {
206 metaUi.clear();
207 }
208
Simon Hunt879ce452017-08-10 23:32:00 -0700209 private static final String LION_TOPO = "core.view.Topo";
210
211 private static final Set<String> REQ_LION_BUNDLES = ImmutableSet.of(
212 LION_TOPO
213 );
Simon Hunt1911fe42017-05-02 18:25:58 -0700214
215 protected ServicesBundle services;
216
217 private String version;
218
219
Thomas Vachuska329af532015-03-10 02:08:33 -0700220 @Override
221 public void init(UiConnection connection, ServiceDirectory directory) {
222 super.init(connection, directory);
Simon Hunt1911fe42017-05-02 18:25:58 -0700223 services = new ServicesBundle(directory);
224 setVersionString(directory);
225 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700226
Simon Hunt1911fe42017-05-02 18:25:58 -0700227 // Creates a palatable version string to display on the summary panel
228 private void setVersionString(ServiceDirectory directory) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700229 String ver = directory.get(CoreService.class).version().toString();
230 version = ver.replace(".SNAPSHOT", "*").replaceFirst("~.*$", "");
231 }
232
Simon Hunt879ce452017-08-10 23:32:00 -0700233 @Override
234 public Set<String> requiredLionBundles() {
235 return REQ_LION_BUNDLES;
236 }
237
Simon Hunt10618f62017-06-15 19:30:52 -0700238 // Returns the first of the given set of IP addresses as a string.
Thomas Vachuska329af532015-03-10 02:08:33 -0700239 private String ip(Set<IpAddress> ipAddresses) {
240 Iterator<IpAddress> it = ipAddresses.iterator();
241 return it.hasNext() ? it.next().toString() : "unknown";
242 }
243
244 // Produces JSON structure from annotations.
245 private JsonNode props(Annotations annotations) {
Simon Huntda580882015-05-12 20:58:18 -0700246 ObjectNode props = objectNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700247 if (annotations != null) {
248 for (String key : annotations.keys()) {
249 props.put(key, annotations.value(key));
250 }
251 }
252 return props;
253 }
254
255 // Produces an informational log message event bound to the client.
Simon Hunt36740d02017-06-07 11:25:51 -0700256 protected ObjectNode info(String message) {
257 return message("info", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700258 }
259
260 // Produces a warning log message event bound to the client.
Simon Hunt36740d02017-06-07 11:25:51 -0700261 protected ObjectNode warning(String message) {
262 return message("warning", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700263 }
264
265 // Produces an error log message event bound to the client.
Simon Hunt36740d02017-06-07 11:25:51 -0700266 protected ObjectNode error(String message) {
267 return message("error", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700268 }
269
270 // Produces a log message event bound to the client.
Simon Hunt36740d02017-06-07 11:25:51 -0700271 private ObjectNode message(String severity, String message) {
Simon Huntda580882015-05-12 20:58:18 -0700272 ObjectNode payload = objectNode()
Simon Huntd2747a02015-04-30 22:41:16 -0700273 .put("severity", severity)
274 .put("message", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700275
Simon Hunt36740d02017-06-07 11:25:51 -0700276 return JsonUtils.envelope("message", payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700277 }
278
Thomas Vachuska329af532015-03-10 02:08:33 -0700279 // Produces a cluster instance message to the client.
Simon Hunt95d56fd2015-11-12 11:06:44 -0800280 protected ObjectNode instanceMessage(ClusterEvent event, String msgType) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700281 ControllerNode node = event.subject();
Simon Hunt1911fe42017-05-02 18:25:58 -0700282 int switchCount = services.mastership().getDevicesOf(node.id()).size();
Simon Huntda580882015-05-12 20:58:18 -0700283 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700284 .put("id", node.id().toString())
285 .put("ip", node.ip().toString())
Simon Hunt1911fe42017-05-02 18:25:58 -0700286 .put("online", services.cluster().getState(node.id()).isActive())
287 .put("ready", services.cluster().getState(node.id()).isReady())
288 .put("uiAttached", node.equals(services.cluster().getLocalNode()))
Thomas Vachuska329af532015-03-10 02:08:33 -0700289 .put("switches", switchCount);
290
Simon Huntda580882015-05-12 20:58:18 -0700291 ArrayNode labels = arrayNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700292 labels.add(node.id().toString());
293 labels.add(node.ip().toString());
294
295 // Add labels, props and stuff the payload into envelope.
296 payload.set("labels", labels);
297 addMetaUi(node.id().toString(), payload);
298
Simon Hunt95d56fd2015-11-12 11:06:44 -0800299 String type = msgType != null ? msgType : CLUSTER_EVENT.get(event.type());
Simon Hunt36740d02017-06-07 11:25:51 -0700300 return JsonUtils.envelope(type, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700301 }
302
303 // Produces a device event message to the client.
304 protected ObjectNode deviceMessage(DeviceEvent event) {
305 Device device = event.subject();
Simon Hunt1e20dae2016-10-28 11:26:26 -0700306 String uiType = device.annotations().value(AnnotationKeys.UI_TYPE);
307 String devType = uiType != null ? uiType :
Laszlo Pappe1579fa2018-03-08 09:18:48 +0000308 services.driver().getDriver(device.id()).getProperty(AnnotationKeys.UI_TYPE);
309 if (devType == null) {
310 devType = device.type().toString().toLowerCase();
311 }
Simon Hunt10618f62017-06-15 19:30:52 -0700312 String name = device.annotations().value(AnnotationKeys.NAME);
313 name = isNullOrEmpty(name) ? device.id().toString() : name;
Simon Hunt1e20dae2016-10-28 11:26:26 -0700314
Simon Huntda580882015-05-12 20:58:18 -0700315 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700316 .put("id", device.id().toString())
Simon Hunt1e20dae2016-10-28 11:26:26 -0700317 .put("type", devType)
Simon Hunt1911fe42017-05-02 18:25:58 -0700318 .put("online", services.device().isAvailable(device.id()))
Thomas Vachuska329af532015-03-10 02:08:33 -0700319 .put("master", master(device.id()));
320
Simon Hunt10618f62017-06-15 19:30:52 -0700321 payload.set("labels", labels("", name, device.id().toString()));
Thomas Vachuska329af532015-03-10 02:08:33 -0700322 payload.set("props", props(device.annotations()));
Thomas Vachuskac616e172018-04-17 16:57:12 -0700323
324 if (!addGeoLocation(device, payload) && !addGridLocation(device, payload)) {
325 addMetaUi(device.id().toString(), payload);
326 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700327
Simon Hunt95d56fd2015-11-12 11:06:44 -0800328 String type = DEVICE_EVENT.get(event.type());
Simon Hunt36740d02017-06-07 11:25:51 -0700329 return JsonUtils.envelope(type, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700330 }
331
332 // Produces a link event message to the client.
333 protected ObjectNode linkMessage(LinkEvent event) {
334 Link link = event.subject();
Simon Huntda580882015-05-12 20:58:18 -0700335 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700336 .put("id", compactLinkString(link))
337 .put("type", link.type().toString().toLowerCase())
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800338 .put("expected", link.isExpected())
Thomas Vachuska329af532015-03-10 02:08:33 -0700339 .put("online", link.state() == Link.State.ACTIVE)
340 .put("linkWidth", 1.2)
341 .put("src", link.src().deviceId().toString())
342 .put("srcPort", link.src().port().toString())
343 .put("dst", link.dst().deviceId().toString())
344 .put("dstPort", link.dst().port().toString());
Simon Hunt95d56fd2015-11-12 11:06:44 -0800345 String type = LINK_EVENT.get(event.type());
Simon Hunt36740d02017-06-07 11:25:51 -0700346 return JsonUtils.envelope(type, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700347 }
348
349 // Produces a host event message to the client.
350 protected ObjectNode hostMessage(HostEvent event) {
351 Host host = event.subject();
Charles Chan33f28a92015-11-13 13:12:38 -0800352 Host prevHost = event.prevSubject();
Simon Hunt1e20dae2016-10-28 11:26:26 -0700353 String hostType = host.annotations().value(AnnotationKeys.UI_TYPE);
Simon Hunt10618f62017-06-15 19:30:52 -0700354 String ip = ip(host.ipAddresses());
Simon Hunt95d56fd2015-11-12 11:06:44 -0800355
Simon Huntda580882015-05-12 20:58:18 -0700356 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700357 .put("id", host.id().toString())
Simon Hunt12c79ed2017-09-12 11:58:44 -0700358 .put("type", isNullOrEmpty(hostType) ? "endstation" : hostType);
Simon Hunt10618f62017-06-15 19:30:52 -0700359
Simon Hunt12c79ed2017-09-12 11:58:44 -0700360 // set most recent connect point (and previous if we know it)
Simon Huntda580882015-05-12 20:58:18 -0700361 payload.set("cp", hostConnect(host.location()));
Charles Chan33f28a92015-11-13 13:12:38 -0800362 if (prevHost != null && prevHost.location() != null) {
363 payload.set("prevCp", hostConnect(prevHost.location()));
Simon Hunt95d56fd2015-11-12 11:06:44 -0800364 }
Simon Hunt12c79ed2017-09-12 11:58:44 -0700365
366 // set ALL connect points
367 addAllCps(host.locations(), payload);
368
Thomas Vachuska528ea952017-10-09 18:25:35 +0200369 payload.set("labels", labels(nameForHost(host), ip, host.mac().toString(), ""));
Thomas Vachuska329af532015-03-10 02:08:33 -0700370 payload.set("props", props(host.annotations()));
Thomas Vachuskac616e172018-04-17 16:57:12 -0700371
372 if (!addGeoLocation(host, payload) && !addGridLocation(host, payload)) {
373 addMetaUi(host.id().toString(), payload);
374 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700375
Simon Hunt95d56fd2015-11-12 11:06:44 -0800376 String type = HOST_EVENT.get(event.type());
Simon Hunt36740d02017-06-07 11:25:51 -0700377 return JsonUtils.envelope(type, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700378 }
379
Simon Hunt12c79ed2017-09-12 11:58:44 -0700380 private void addAllCps(Set<HostLocation> locations, ObjectNode payload) {
381 ArrayNode cps = arrayNode();
382 locations.forEach(loc -> cps.add(hostConnect(loc)));
383 payload.set("allCps", cps);
384 }
385
Thomas Vachuska329af532015-03-10 02:08:33 -0700386 // Encodes the specified host location into a JSON object.
Simon Huntda580882015-05-12 20:58:18 -0700387 private ObjectNode hostConnect(HostLocation location) {
388 return objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700389 .put("device", location.deviceId().toString())
390 .put("port", location.port().toLong());
391 }
392
393 // Encodes the specified list of labels a JSON array.
Simon Huntda580882015-05-12 20:58:18 -0700394 private ArrayNode labels(String... labels) {
395 ArrayNode json = arrayNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700396 for (String label : labels) {
397 json.add(label);
398 }
399 return json;
400 }
401
402 // Returns the name of the master node for the specified device id.
403 private String master(DeviceId deviceId) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700404 NodeId master = services.mastership().getMasterFor(deviceId);
Thomas Vachuska329af532015-03-10 02:08:33 -0700405 return master != null ? master.toString() : "";
406 }
407
408 // Generates an edge link from the specified host location.
409 private EdgeLink edgeLink(Host host, boolean ingress) {
410 return new DefaultEdgeLink(PID, new ConnectPoint(host.id(), portNumber(0)),
411 host.location(), ingress);
412 }
413
414 // Adds meta UI information for the specified object.
415 private void addMetaUi(String id, ObjectNode payload) {
416 ObjectNode meta = metaUi.get(id);
417 if (meta != null) {
418 payload.set("metaUi", meta);
419 }
420 }
421
422 // Adds a geo location JSON to the specified payload object.
Thomas Vachuskac616e172018-04-17 16:57:12 -0700423 private boolean addGeoLocation(Annotated annotated, ObjectNode payload) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700424 Annotations annotations = annotated.annotations();
Thomas Vachuskac616e172018-04-17 16:57:12 -0700425 if (annotations != null) {
426 String locType = annotations.value(AnnotationKeys.LOC_TYPE);
427 String slat = annotations.value(AnnotationKeys.LATITUDE);
428 String slng = annotations.value(AnnotationKeys.LONGITUDE);
429 boolean validLat = slat != null && !slat.equals(NO_GEO_VALUE);
430 boolean validLng = slng != null && !slng.equals(NO_GEO_VALUE);
Thomas Vachuska329af532015-03-10 02:08:33 -0700431
Thomas Vachuskac616e172018-04-17 16:57:12 -0700432 if (Objects.equals(locType, LOC_TYPE_GEO) && validLat && validLng) {
433 try {
434 double lat = Double.parseDouble(slat);
435 double lng = Double.parseDouble(slng);
436 ObjectNode loc = objectNode()
437 .put("locType", "geo")
438 .put("latOrY", lat)
439 .put("longOrX", lng);
440 payload.set("location", loc);
441 return true;
442 } catch (NumberFormatException e) {
443 log.warn("Invalid geo data: latitude={}, longitude={}", slat, slng);
444 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700445 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700446 }
Thomas Vachuskac616e172018-04-17 16:57:12 -0700447 return false;
Thomas Vachuska329af532015-03-10 02:08:33 -0700448 }
449
Thomas Vachuskac67a9912018-03-06 14:37:45 -0800450 // Adds a grid location JSON to the specified payload object.
Thomas Vachuskac616e172018-04-17 16:57:12 -0700451 private boolean addGridLocation(Annotated annotated, ObjectNode payload) {
Thomas Vachuskac67a9912018-03-06 14:37:45 -0800452 Annotations annotations = annotated.annotations();
Thomas Vachuskac616e172018-04-17 16:57:12 -0700453 if (annotations != null) {
454 String locType = annotations.value(AnnotationKeys.LOC_TYPE);
455 String xs = annotations.value(AnnotationKeys.GRID_X);
456 String ys = annotations.value(AnnotationKeys.GRID_Y);
Thomas Vachuskac67a9912018-03-06 14:37:45 -0800457
Thomas Vachuskac616e172018-04-17 16:57:12 -0700458 if (Objects.equals(locType, LOC_TYPE_GRID) && xs != null && ys != null) {
459 try {
460 double x = Double.parseDouble(xs);
461 double y = Double.parseDouble(ys);
462 ObjectNode loc = objectNode()
463 .put("locType", "grid")
464 .put("latOrY", y)
465 .put("longOrX", x);
466 payload.set("location", loc);
467 return true;
468 } catch (NumberFormatException e) {
469 log.warn("Invalid grid data: x={}, y={}", xs, ys);
470 }
Thomas Vachuskac67a9912018-03-06 14:37:45 -0800471 }
472 }
Thomas Vachuskac616e172018-04-17 16:57:12 -0700473 return false;
Thomas Vachuskac67a9912018-03-06 14:37:45 -0800474 }
475
Thomas Vachuska329af532015-03-10 02:08:33 -0700476 // Updates meta UI information for the specified object.
Simon Huntd2747a02015-04-30 22:41:16 -0700477 protected void updateMetaUi(ObjectNode payload) {
478 metaUi.put(JsonUtils.string(payload, "id"),
479 JsonUtils.node(payload, "memento"));
Thomas Vachuska329af532015-03-10 02:08:33 -0700480 }
481
Simon Hunta17fa672015-08-19 18:42:22 -0700482
Simon Huntb745ca62015-07-28 15:37:11 -0700483 // -----------------------------------------------------------------------
484 // Create models of the data to return, that overlays can adjust / augment
485
Simon Hunta58d8942017-08-11 12:51:14 -0700486 private String lookupGlyph(Device device) {
487 return DEVICE_GLYPHS.get(device.type());
488 }
489
490
Simon Huntb745ca62015-07-28 15:37:11 -0700491 // Returns property panel model for summary response.
Simon Hunt8a0429a2017-01-06 16:52:47 -0800492 protected PropertyPanel summmaryMessage() {
Simon Hunta58d8942017-08-11 12:51:14 -0700493 // chose NOT to add debug messages, since this is called every few seconds
Simon Hunt1911fe42017-05-02 18:25:58 -0700494 Topology topology = services.topology().currentTopology();
Simon Hunt879ce452017-08-10 23:32:00 -0700495 LionBundle lion = getLionBundle(LION_TOPO);
496 String panelTitle = lion.getSafe("title_panel_summary");
Simon Hunt0af1ec32015-07-24 12:17:55 -0700497
Simon Hunta58d8942017-08-11 12:51:14 -0700498 return new PropertyPanel(panelTitle, "bird")
Simon Hunt879ce452017-08-10 23:32:00 -0700499 .addProp(VERSION, lion.getSafe(VERSION), version)
Simon Hunt1911fe42017-05-02 18:25:58 -0700500 .addSeparator()
Simon Hunt879ce452017-08-10 23:32:00 -0700501 .addProp(DEVICES, lion.getSafe(DEVICES), services.device().getDeviceCount())
502 .addProp(LINKS, lion.getSafe(LINKS), topology.linkCount())
503 .addProp(HOSTS, lion.getSafe(HOSTS), services.host().getHostCount())
504 .addProp(TOPOLOGY_SSCS, lion.getSafe(TOPOLOGY_SSCS), topology.clusterCount())
Simon Hunt1911fe42017-05-02 18:25:58 -0700505 .addSeparator()
Simon Hunt879ce452017-08-10 23:32:00 -0700506 .addProp(INTENTS, lion.getSafe(INTENTS), services.intent().getIntentCount())
507 .addProp(TUNNELS, lion.getSafe(TUNNELS), services.tunnel().tunnelCount())
508 .addProp(FLOWS, lion.getSafe(FLOWS), services.flow().getFlowRuleCount());
Thomas Vachuska329af532015-03-10 02:08:33 -0700509 }
510
Simon Hunta58d8942017-08-11 12:51:14 -0700511
512 private String friendlyDevice(DeviceId deviceId) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700513 Device device = services.device().getDevice(deviceId);
Thomas Vachuska329af532015-03-10 02:08:33 -0700514 Annotations annot = device.annotations();
515 String name = annot.value(AnnotationKeys.NAME);
Simon Hunta58d8942017-08-11 12:51:14 -0700516 return isNullOrEmpty(name) ? deviceId.toString() : name;
517 }
518
519 // Generates a property panel model for device details response
520 protected PropertyPanel deviceDetails(DeviceId deviceId) {
521 log.debug("generate prop panel data for device {}", deviceId);
522 Device device = services.device().getDevice(deviceId);
523 Annotations annot = device.annotations();
524 String proto = annot.value(AnnotationKeys.PROTOCOL);
525 String title = friendlyDevice(deviceId);
526 LionBundle lion = getLionBundle(LION_TOPO);
527
528 PropertyPanel pp = new PropertyPanel(title, lookupGlyph(device))
529 .navPath(DEVICE_NAV_PATH)
530 .id(deviceId.toString());
531 addDeviceBasicProps(pp, deviceId, device, proto, lion);
532 addLocationProps(pp, annot, lion);
533 addDeviceCountStats(pp, deviceId, lion);
534 addDeviceCoreButtons(pp);
535 return pp;
536 }
537
538 private void addDeviceBasicProps(PropertyPanel pp, DeviceId deviceId,
539 Device device, String proto, LionBundle lion) {
540 pp.addProp(URI, lion.getSafe(URI), deviceId.toString())
541 .addProp(VENDOR, lion.getSafe(VENDOR), device.manufacturer())
542 .addProp(HW_VERSION, lion.getSafe(HW_VERSION), device.hwVersion())
543 .addProp(SW_VERSION, lion.getSafe(SW_VERSION), device.swVersion())
544 .addProp(SERIAL_NUMBER, lion.getSafe(SERIAL_NUMBER), device.serialNumber())
545 .addProp(PROTOCOL, lion.getSafe(PROTOCOL), proto)
546 .addSeparator();
547 }
548
549 // only add location properties if we have them
550 private void addLocationProps(PropertyPanel pp, Annotations annot,
551 LionBundle lion) {
552 String slat = annot.value(AnnotationKeys.LATITUDE);
553 String slng = annot.value(AnnotationKeys.LONGITUDE);
554 String sgrY = annot.value(AnnotationKeys.GRID_Y);
555 String sgrX = annot.value(AnnotationKeys.GRID_X);
556
557 boolean validLat = slat != null && !slat.equals(NO_GEO_VALUE);
558 boolean validLng = slng != null && !slng.equals(NO_GEO_VALUE);
559 if (validLat && validLng) {
560 pp.addProp(LATITUDE, lion.getSafe(LATITUDE), slat)
561 .addProp(LONGITUDE, lion.getSafe(LONGITUDE), slng)
562 .addSeparator();
563
564 } else if (sgrY != null && sgrX != null) {
565 pp.addProp(GRID_Y, lion.getSafe(GRID_Y), sgrY)
566 .addProp(GRID_X, lion.getSafe(GRID_X), sgrX)
567 .addSeparator();
568 }
569 // else, no location
570 }
571
572 private void addDeviceCountStats(PropertyPanel pp, DeviceId deviceId, LionBundle lion) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700573 int portCount = services.device().getPorts(deviceId).size();
Thomas Vachuska329af532015-03-10 02:08:33 -0700574 int flowCount = getFlowCount(deviceId);
cheng fan35dc0f22015-06-10 06:02:47 +0800575 int tunnelCount = getTunnelCount(deviceId);
Simon Huntb745ca62015-07-28 15:37:11 -0700576
Simon Hunta58d8942017-08-11 12:51:14 -0700577 pp.addProp(PORTS, lion.getSafe(PORTS), portCount)
578 .addProp(FLOWS, lion.getSafe(FLOWS), flowCount)
579 .addProp(TUNNELS, lion.getSafe(TUNNELS), tunnelCount);
580 }
Simon Huntb745ca62015-07-28 15:37:11 -0700581
Simon Hunta58d8942017-08-11 12:51:14 -0700582 private void addDeviceCoreButtons(PropertyPanel pp) {
583 pp.addButton(CoreButtons.SHOW_DEVICE_VIEW)
Simon Hunt1911fe42017-05-02 18:25:58 -0700584 .addButton(CoreButtons.SHOW_FLOW_VIEW)
585 .addButton(CoreButtons.SHOW_PORT_VIEW)
586 .addButton(CoreButtons.SHOW_GROUP_VIEW)
587 .addButton(CoreButtons.SHOW_METER_VIEW);
Thomas Vachuska329af532015-03-10 02:08:33 -0700588 }
589
590 protected int getFlowCount(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800591 return services.flow().getFlowRuleCount(deviceId);
Thomas Vachuska329af532015-03-10 02:08:33 -0700592 }
593
cheng fan35dc0f22015-06-10 06:02:47 +0800594 protected int getTunnelCount(DeviceId deviceId) {
595 int count = 0;
Simon Hunt1911fe42017-05-02 18:25:58 -0700596 Collection<Tunnel> tunnels = services.tunnel().queryAllTunnels();
cheng fan35dc0f22015-06-10 06:02:47 +0800597 for (Tunnel tunnel : tunnels) {
chengfanc553c952016-07-22 15:48:23 +0800598 //Only OpticalTunnelEndPoint has a device
599 if (!(tunnel.src() instanceof OpticalTunnelEndPoint) ||
600 !(tunnel.dst() instanceof OpticalTunnelEndPoint)) {
601 continue;
602 }
603
604 Optional<ElementId> srcElementId = ((OpticalTunnelEndPoint) tunnel.src()).elementId();
605 Optional<ElementId> dstElementId = ((OpticalTunnelEndPoint) tunnel.dst()).elementId();
606 if (!srcElementId.isPresent() || !dstElementId.isPresent()) {
607 continue;
608 }
609 DeviceId srcDeviceId = (DeviceId) srcElementId.get();
610 DeviceId dstDeviceId = (DeviceId) dstElementId.get();
611 if (srcDeviceId.equals(deviceId) || dstDeviceId.equals(deviceId)) {
cheng fan35dc0f22015-06-10 06:02:47 +0800612 count++;
613 }
614 }
615 return count;
616 }
617
Simon Hunt10618f62017-06-15 19:30:52 -0700618 private boolean useDefaultName(String annotName) {
619 return isNullOrEmpty(annotName) || DASH.equals(annotName);
620 }
621
622 private String nameForHost(Host host) {
623 String name = host.annotations().value(AnnotationKeys.NAME);
624 return useDefaultName(name) ? ip(host.ipAddresses()) : name;
625 }
626
Simon Hunta58d8942017-08-11 12:51:14 -0700627 private String glyphForHost(Annotations annot) {
628 String uiType = annot.value(AnnotationKeys.UI_TYPE);
629 return isNullOrEmpty(uiType) ? DEFAULT_HOST_GLYPH : uiType;
Thomas Vachuska329af532015-03-10 02:08:33 -0700630 }
631
Simon Hunta58d8942017-08-11 12:51:14 -0700632 // Generates a property panel model for a host details response
633 protected PropertyPanel hostDetails(HostId hostId) {
634 log.debug("generate prop panel data for host {}", hostId);
635 Host host = services.host().getHost(hostId);
636 Annotations annot = host.annotations();
637 String glyphId = glyphForHost(annot);
638 LionBundle lion = getLionBundle(LION_TOPO);
639
640 PropertyPanel pp = new PropertyPanel(nameForHost(host), glyphId)
641 .navPath(HOST_NAV_PATH)
642 .id(hostId.toString());
643 addHostBasicProps(pp, host, lion);
644 addLocationProps(pp, annot, lion);
645 return pp;
646 }
647
648 private void addHostBasicProps(PropertyPanel pp, Host host, LionBundle lion) {
649 pp.addProp(LPL_FRIENDLY, lion.getSafe(LPL_FRIENDLY), nameForHost(host))
650 .addProp(MAC, lion.getSafe(MAC), host.mac())
651 .addProp(IP, lion.getSafe(IP), host.ipAddresses(), "[\\[\\]]")
652 .addProp(VLAN, lion.getSafe(VLAN), displayVlan(host.vlan(), lion))
653 .addSeparator();
654 }
655
656 private String displayVlan(VlanId vlan, LionBundle lion) {
657 return VlanId.NONE.equals(vlan) ? lion.getSafe(VLAN_NONE) : vlan.toString();
658 }
659
660 // Generates a property panel model for a link details response (edge-link)
661 protected PropertyPanel edgeLinkDetails(HostId hid, ConnectPoint cp) {
662 log.debug("generate prop panel data for edgelink {} {}", hid, cp);
663 LionBundle lion = getLionBundle(LION_TOPO);
664 String title = lion.getSafe("title_edge_link");
665
666 PropertyPanel pp = new PropertyPanel(title, LINK_GLYPH);
667 addLinkHostProps(pp, hid, lion);
668 addLinkCpBProps(pp, cp, lion);
669 return pp;
670 }
671
672 // Generates a property panel model for a link details response (infra-link)
673 protected PropertyPanel infraLinkDetails(ConnectPoint cpA, ConnectPoint cpB) {
674 log.debug("generate prop panel data for infralink {} {}", cpA, cpB);
675 LionBundle lion = getLionBundle(LION_TOPO);
676 String title = lion.getSafe("title_infra_link");
677
678 PropertyPanel pp = new PropertyPanel(title, LINK_GLYPH);
679 addLinkCpAProps(pp, cpA, lion);
680 addLinkCpBProps(pp, cpB, lion);
681 addLinkBackingProps(pp, cpA, cpB, lion);
682 return pp;
683 }
684
685 private void addLinkHostProps(PropertyPanel pp, HostId hostId, LionBundle lion) {
686 Host host = services.host().getHost(hostId);
687
688 pp.addProp(LPL_A_TYPE, lion.getSafe(LPL_A_TYPE), lion.getSafe(HOST))
689 .addProp(LPL_A_ID, lion.getSafe(LPL_A_ID), hostId.toString())
690 .addProp(LPL_A_FRIENDLY, lion.getSafe(LPL_A_FRIENDLY), nameForHost(host))
691 .addSeparator();
692 }
693
694 private void addLinkCpAProps(PropertyPanel pp, ConnectPoint cp, LionBundle lion) {
695 DeviceId did = cp.deviceId();
696
697 pp.addProp(LPL_A_TYPE, lion.getSafe(LPL_A_TYPE), lion.getSafe(DEVICE))
698 .addProp(LPL_A_ID, lion.getSafe(LPL_A_ID), did.toString())
699 .addProp(LPL_A_FRIENDLY, lion.getSafe(LPL_A_FRIENDLY), friendlyDevice(did))
700 .addProp(LPL_A_PORT, lion.getSafe(LPL_A_PORT), cp.port().toLong())
701 .addSeparator();
702 }
703
704 private void addLinkCpBProps(PropertyPanel pp, ConnectPoint cp, LionBundle lion) {
705 DeviceId did = cp.deviceId();
706
707 pp.addProp(LPL_B_TYPE, lion.getSafe(LPL_B_TYPE), lion.getSafe(DEVICE))
708 .addProp(LPL_B_ID, lion.getSafe(LPL_B_ID), did.toString())
709 .addProp(LPL_B_FRIENDLY, lion.getSafe(LPL_B_FRIENDLY), friendlyDevice(did))
710 .addProp(LPL_B_PORT, lion.getSafe(LPL_B_PORT), cp.port().toLong())
711 .addSeparator();
712 }
713
714 private void addLinkBackingProps(PropertyPanel pp, ConnectPoint cpA,
715 ConnectPoint cpB, LionBundle lion) {
716 Link a2b = services.link().getLink(cpA, cpB);
717 Link b2a = services.link().getLink(cpB, cpA);
718
719 pp.addProp(LPL_A2B, lion.getSafe(LPL_A2B), linkPropString(a2b, lion))
720 .addProp(LPL_B2A, lion.getSafe(LPL_B2A), linkPropString(b2a, lion));
721 }
722
723 private String linkPropString(Link link, LionBundle lion) {
724 if (link == null) {
725 return lion.getSafe(LPV_NO_LINK);
726 }
727 return lion.getSafe(link.type()) + SLASH +
728 lion.getSafe(link.state()) + SLASH +
729 lion.getSafe(link.isExpected() ? EXPECTED : NOT_EXPECTED);
730 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700731}