blob: 3ba641fa306647f884fcb6218d33096ec2e87a6b [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;
Thomas Vachuska329af532015-03-10 02:08:33 -070030import org.onosproject.net.AnnotationKeys;
31import org.onosproject.net.Annotations;
32import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.DefaultEdgeLink;
34import org.onosproject.net.Device;
35import org.onosproject.net.DeviceId;
36import org.onosproject.net.EdgeLink;
37import org.onosproject.net.Host;
38import org.onosproject.net.HostId;
39import org.onosproject.net.HostLocation;
40import org.onosproject.net.Link;
Thomas Vachuska4c47a382018-05-07 13:48:38 -070041import org.onosproject.net.config.NetworkConfigService;
42import org.onosproject.net.config.basics.BasicDeviceConfig;
43import org.onosproject.net.config.basics.BasicElementConfig;
44import org.onosproject.net.config.basics.BasicHostConfig;
Thomas Vachuska329af532015-03-10 02:08:33 -070045import org.onosproject.net.device.DeviceEvent;
Thomas Vachuska164ecf62018-05-08 17:29:55 -070046import org.onosproject.net.driver.Driver;
Thomas Vachuska329af532015-03-10 02:08:33 -070047import org.onosproject.net.host.HostEvent;
Thomas Vachuska329af532015-03-10 02:08:33 -070048import org.onosproject.net.link.LinkEvent;
Thomas Vachuska329af532015-03-10 02:08:33 -070049import org.onosproject.net.provider.ProviderId;
Thomas Vachuska329af532015-03-10 02:08:33 -070050import org.onosproject.net.topology.Topology;
Simon Huntd2747a02015-04-30 22:41:16 -070051import org.onosproject.ui.JsonUtils;
Thomas Vachuska329af532015-03-10 02:08:33 -070052import org.onosproject.ui.UiConnection;
Simon Hunta0ddb022015-05-01 09:53:01 -070053import org.onosproject.ui.UiMessageHandler;
Simon Hunted804d52016-03-30 09:51:40 -070054import org.onosproject.ui.impl.topo.util.ServicesBundle;
Simon Hunt879ce452017-08-10 23:32:00 -070055import org.onosproject.ui.lion.LionBundle;
Simon Hunt0af1ec32015-07-24 12:17:55 -070056import org.onosproject.ui.topo.PropertyPanel;
Thomas Vachuska329af532015-03-10 02:08:33 -070057import org.slf4j.Logger;
58import org.slf4j.LoggerFactory;
59
Thomas Vachuska329af532015-03-10 02:08:33 -070060import 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;
Simon Huntf4fd2a22016-08-10 15:41:09 -070064import java.util.Set;
Thomas Vachuska329af532015-03-10 02:08:33 -070065import java.util.concurrent.ConcurrentHashMap;
66
Thomas Vachuska329af532015-03-10 02:08:33 -070067import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuska164ecf62018-05-08 17:29:55 -070068import static org.onosproject.net.AnnotationKeys.DRIVER;
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;
Thomas Vachuska4c47a382018-05-07 13:48:38 -070073import static org.onosproject.ui.topo.TopoConstants.Properties.*;
Simon Huntd3ceffa2015-08-25 12:44:35 -070074import static org.onosproject.ui.topo.TopoUtils.compactLinkString;
Thomas Vachuska329af532015-03-10 02:08:33 -070075
76/**
77 * Facility for creating messages bound for the topology viewer.
78 */
Simon Hunta0ddb022015-05-01 09:53:01 -070079public abstract class TopologyViewMessageHandlerBase extends UiMessageHandler {
Thomas Vachuska329af532015-03-10 02:08:33 -070080
Simon Huntf4fd2a22016-08-10 15:41:09 -070081 private static final String NO_GEO_VALUE = "0.0";
Simon Hunt10618f62017-06-15 19:30:52 -070082 private static final String DASH = "-";
Simon Hunta58d8942017-08-11 12:51:14 -070083 private static final String SLASH = " / ";
Simon Hunt10618f62017-06-15 19:30:52 -070084
85 // nav paths are the view names for hot-link navigation from topo view...
86 private static final String DEVICE_NAV_PATH = "device";
87 private static final String HOST_NAV_PATH = "host";
Simon Huntf4fd2a22016-08-10 15:41:09 -070088
Simon Hunta58d8942017-08-11 12:51:14 -070089 // link panel label keys
90 private static final String LPL_FRIENDLY = "lp_label_friendly";
91 private static final String LPL_A_TYPE = "lp_label_a_type";
92 private static final String LPL_A_ID = "lp_label_a_id";
93 private static final String LPL_A_FRIENDLY = "lp_label_a_friendly";
94 private static final String LPL_A_PORT = "lp_label_a_port";
95 private static final String LPL_B_TYPE = "lp_label_b_type";
96 private static final String LPL_B_ID = "lp_label_b_id";
97 private static final String LPL_B_FRIENDLY = "lp_label_b_friendly";
98 private static final String LPL_B_PORT = "lp_label_b_port";
99 private static final String LPL_A2B = "lp_label_a2b";
100 private static final String LPL_B2A = "lp_label_b2a";
101 private static final String LPV_NO_LINK = "lp_value_no_link";
102
103 // other Lion keys
104 private static final String HOST = "host";
105 private static final String DEVICE = "device";
106 private static final String EXPECTED = "expected";
107 private static final String NOT_EXPECTED = "not_expected";
108
Simon Hunt95d56fd2015-11-12 11:06:44 -0800109 // default to an "add" event...
110 private static final DefaultHashMap<ClusterEvent.Type, String> CLUSTER_EVENT =
111 new DefaultHashMap<>("addInstance");
112
113 // default to an "update" event...
114 private static final DefaultHashMap<DeviceEvent.Type, String> DEVICE_EVENT =
115 new DefaultHashMap<>("updateDevice");
116 private static final DefaultHashMap<LinkEvent.Type, String> LINK_EVENT =
117 new DefaultHashMap<>("updateLink");
118 private static final DefaultHashMap<HostEvent.Type, String> HOST_EVENT =
119 new DefaultHashMap<>("updateHost");
120
121 // but call out specific events that we care to differentiate...
122 static {
123 CLUSTER_EVENT.put(ClusterEvent.Type.INSTANCE_REMOVED, "removeInstance");
124
125 DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_ADDED, "addDevice");
126 DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_REMOVED, "removeDevice");
127
128 LINK_EVENT.put(LinkEvent.Type.LINK_ADDED, "addLink");
129 LINK_EVENT.put(LinkEvent.Type.LINK_REMOVED, "removeLink");
130
131 HOST_EVENT.put(HostEvent.Type.HOST_ADDED, "addHost");
132 HOST_EVENT.put(HostEvent.Type.HOST_REMOVED, "removeHost");
133 HOST_EVENT.put(HostEvent.Type.HOST_MOVED, "moveHost");
134 }
135
Simon Hunta58d8942017-08-11 12:51:14 -0700136 private static final DefaultHashMap<Device.Type, String> DEVICE_GLYPHS =
137 new DefaultHashMap<>("m_unknown");
138
139 static {
140 DEVICE_GLYPHS.put(Device.Type.SWITCH, "m_switch");
141 DEVICE_GLYPHS.put(Device.Type.ROUTER, "m_router");
142 DEVICE_GLYPHS.put(Device.Type.ROADM, "m_roadm");
Andrea Campanellafa0f6cc2018-12-18 15:17:53 +0100143 DEVICE_GLYPHS.put(Device.Type.OLS, "m_roadm");
Simon Hunta58d8942017-08-11 12:51:14 -0700144 DEVICE_GLYPHS.put(Device.Type.OTN, "m_otn");
Andrea Campanella1c24fb92018-12-20 16:43:59 +0100145 DEVICE_GLYPHS.put(Device.Type.TERMINAL_DEVICE, "m_otn");
Simon Hunta58d8942017-08-11 12:51:14 -0700146 DEVICE_GLYPHS.put(Device.Type.ROADM_OTN, "m_roadm_otn");
147 DEVICE_GLYPHS.put(Device.Type.BALANCER, "m_balancer");
148 DEVICE_GLYPHS.put(Device.Type.IPS, "m_ips");
149 DEVICE_GLYPHS.put(Device.Type.IDS, "m_ids");
150 DEVICE_GLYPHS.put(Device.Type.CONTROLLER, "m_controller");
151 DEVICE_GLYPHS.put(Device.Type.VIRTUAL, "m_virtual");
152 DEVICE_GLYPHS.put(Device.Type.FIBER_SWITCH, "m_fiberSwitch");
153 DEVICE_GLYPHS.put(Device.Type.MICROWAVE, "m_microwave");
154 DEVICE_GLYPHS.put(Device.Type.OLT, "m_olt");
155 DEVICE_GLYPHS.put(Device.Type.ONU, "m_onu");
156 DEVICE_GLYPHS.put(Device.Type.OPTICAL_AMPLIFIER, "unknown"); // TODO glyph needed
157 DEVICE_GLYPHS.put(Device.Type.OTHER, "m_other");
Georgios Katsikas40ecef32018-07-03 11:17:44 +0200158 DEVICE_GLYPHS.put(Device.Type.SERVER, "m_endpoint");
Simon Hunta58d8942017-08-11 12:51:14 -0700159 }
160
161 private static final String DEFAULT_HOST_GLYPH = "m_endstation";
162 private static final String LINK_GLYPH = "m_ports";
163
164
Ray Milkey9c9cde42018-01-12 14:22:06 -0800165 static final Logger log =
Simon Huntd2747a02015-04-30 22:41:16 -0700166 LoggerFactory.getLogger(TopologyViewMessageHandlerBase.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700167
Simon Huntd2747a02015-04-30 22:41:16 -0700168 private static final ProviderId PID =
169 new ProviderId("core", "org.onosproject.core", true);
Thomas Vachuska329af532015-03-10 02:08:33 -0700170
Thomas Vachuska329af532015-03-10 02:08:33 -0700171 // TODO: extract into an external & durable state; good enough for now and demo
172 private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>();
173
174 /**
Thomas Vachuska329af532015-03-10 02:08:33 -0700175 * Returns read-only view of the meta-ui information.
176 *
177 * @return map of id to meta-ui mementos
178 */
179 static Map<String, ObjectNode> getMetaUi() {
180 return Collections.unmodifiableMap(metaUi);
181 }
182
Thomas Vachuskac616e172018-04-17 16:57:12 -0700183 /**
184 * Clears any meta-ui information.
185 */
186 public static void clearMetaUi() {
187 metaUi.clear();
188 }
189
Simon Hunt879ce452017-08-10 23:32:00 -0700190 private static final String LION_TOPO = "core.view.Topo";
191
192 private static final Set<String> REQ_LION_BUNDLES = ImmutableSet.of(
193 LION_TOPO
194 );
Simon Hunt1911fe42017-05-02 18:25:58 -0700195
196 protected ServicesBundle services;
197
198 private String version;
199
200
Thomas Vachuska329af532015-03-10 02:08:33 -0700201 @Override
202 public void init(UiConnection connection, ServiceDirectory directory) {
203 super.init(connection, directory);
Simon Hunt1911fe42017-05-02 18:25:58 -0700204 services = new ServicesBundle(directory);
205 setVersionString(directory);
206 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700207
Simon Hunt1911fe42017-05-02 18:25:58 -0700208 // Creates a palatable version string to display on the summary panel
209 private void setVersionString(ServiceDirectory directory) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700210 String ver = directory.get(CoreService.class).version().toString();
211 version = ver.replace(".SNAPSHOT", "*").replaceFirst("~.*$", "");
212 }
213
Simon Hunt879ce452017-08-10 23:32:00 -0700214 @Override
215 public Set<String> requiredLionBundles() {
216 return REQ_LION_BUNDLES;
217 }
218
Simon Hunt10618f62017-06-15 19:30:52 -0700219 // Returns the first of the given set of IP addresses as a string.
Thomas Vachuska329af532015-03-10 02:08:33 -0700220 private String ip(Set<IpAddress> ipAddresses) {
221 Iterator<IpAddress> it = ipAddresses.iterator();
222 return it.hasNext() ? it.next().toString() : "unknown";
223 }
224
225 // Produces JSON structure from annotations.
226 private JsonNode props(Annotations annotations) {
Simon Huntda580882015-05-12 20:58:18 -0700227 ObjectNode props = objectNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700228 if (annotations != null) {
229 for (String key : annotations.keys()) {
230 props.put(key, annotations.value(key));
231 }
232 }
233 return props;
234 }
235
236 // Produces an informational log message event bound to the client.
Simon Hunt36740d02017-06-07 11:25:51 -0700237 protected ObjectNode info(String message) {
238 return message("info", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700239 }
240
241 // Produces a warning log message event bound to the client.
Simon Hunt36740d02017-06-07 11:25:51 -0700242 protected ObjectNode warning(String message) {
243 return message("warning", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700244 }
245
246 // Produces an error log message event bound to the client.
Simon Hunt36740d02017-06-07 11:25:51 -0700247 protected ObjectNode error(String message) {
248 return message("error", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700249 }
250
251 // Produces a log message event bound to the client.
Simon Hunt36740d02017-06-07 11:25:51 -0700252 private ObjectNode message(String severity, String message) {
Simon Huntda580882015-05-12 20:58:18 -0700253 ObjectNode payload = objectNode()
Simon Huntd2747a02015-04-30 22:41:16 -0700254 .put("severity", severity)
255 .put("message", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700256
Simon Hunt36740d02017-06-07 11:25:51 -0700257 return JsonUtils.envelope("message", payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700258 }
259
Thomas Vachuska329af532015-03-10 02:08:33 -0700260 // Produces a cluster instance message to the client.
Simon Hunt95d56fd2015-11-12 11:06:44 -0800261 protected ObjectNode instanceMessage(ClusterEvent event, String msgType) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700262 ControllerNode node = event.subject();
Simon Hunt1911fe42017-05-02 18:25:58 -0700263 int switchCount = services.mastership().getDevicesOf(node.id()).size();
Simon Huntda580882015-05-12 20:58:18 -0700264 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700265 .put("id", node.id().toString())
266 .put("ip", node.ip().toString())
Simon Hunt1911fe42017-05-02 18:25:58 -0700267 .put("online", services.cluster().getState(node.id()).isActive())
268 .put("ready", services.cluster().getState(node.id()).isReady())
269 .put("uiAttached", node.equals(services.cluster().getLocalNode()))
Thomas Vachuska329af532015-03-10 02:08:33 -0700270 .put("switches", switchCount);
271
Simon Huntda580882015-05-12 20:58:18 -0700272 ArrayNode labels = arrayNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700273 labels.add(node.id().toString());
274 labels.add(node.ip().toString());
275
276 // Add labels, props and stuff the payload into envelope.
277 payload.set("labels", labels);
278 addMetaUi(node.id().toString(), payload);
279
Simon Hunt95d56fd2015-11-12 11:06:44 -0800280 String type = msgType != null ? msgType : CLUSTER_EVENT.get(event.type());
Simon Hunt36740d02017-06-07 11:25:51 -0700281 return JsonUtils.envelope(type, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700282 }
283
284 // Produces a device event message to the client.
285 protected ObjectNode deviceMessage(DeviceEvent event) {
286 Device device = event.subject();
Simon Hunt1e20dae2016-10-28 11:26:26 -0700287 String uiType = device.annotations().value(AnnotationKeys.UI_TYPE);
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700288 String driverName = device.annotations().value(DRIVER);
289 Driver driver = driverName == null ? null : services.driver().getDriver(driverName);
Simon Hunt1e20dae2016-10-28 11:26:26 -0700290 String devType = uiType != null ? uiType :
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700291 (driver != null ? driver.getProperty(AnnotationKeys.UI_TYPE) : null);
Laszlo Pappe1579fa2018-03-08 09:18:48 +0000292 if (devType == null) {
293 devType = device.type().toString().toLowerCase();
294 }
Simon Hunt10618f62017-06-15 19:30:52 -0700295 String name = device.annotations().value(AnnotationKeys.NAME);
296 name = isNullOrEmpty(name) ? device.id().toString() : name;
Simon Hunt1e20dae2016-10-28 11:26:26 -0700297
Simon Huntda580882015-05-12 20:58:18 -0700298 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700299 .put("id", device.id().toString())
Simon Hunt1e20dae2016-10-28 11:26:26 -0700300 .put("type", devType)
Simon Hunt1911fe42017-05-02 18:25:58 -0700301 .put("online", services.device().isAvailable(device.id()))
Thomas Vachuska329af532015-03-10 02:08:33 -0700302 .put("master", master(device.id()));
303
Simon Hunt10618f62017-06-15 19:30:52 -0700304 payload.set("labels", labels("", name, device.id().toString()));
Thomas Vachuska329af532015-03-10 02:08:33 -0700305 payload.set("props", props(device.annotations()));
Thomas Vachuskac616e172018-04-17 16:57:12 -0700306
Thomas Vachuska4c47a382018-05-07 13:48:38 -0700307 BasicDeviceConfig cfg = get(NetworkConfigService.class)
308 .getConfig(device.id(), BasicDeviceConfig.class);
309 if (!addLocation(cfg, payload)) {
Thomas Vachuskac616e172018-04-17 16:57:12 -0700310 addMetaUi(device.id().toString(), payload);
311 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700312
Simon Hunt95d56fd2015-11-12 11:06:44 -0800313 String type = DEVICE_EVENT.get(event.type());
Simon Hunt36740d02017-06-07 11:25:51 -0700314 return JsonUtils.envelope(type, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700315 }
316
317 // Produces a link event message to the client.
318 protected ObjectNode linkMessage(LinkEvent event) {
319 Link link = event.subject();
Simon Huntda580882015-05-12 20:58:18 -0700320 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700321 .put("id", compactLinkString(link))
322 .put("type", link.type().toString().toLowerCase())
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800323 .put("expected", link.isExpected())
Thomas Vachuska329af532015-03-10 02:08:33 -0700324 .put("online", link.state() == Link.State.ACTIVE)
325 .put("linkWidth", 1.2)
326 .put("src", link.src().deviceId().toString())
327 .put("srcPort", link.src().port().toString())
328 .put("dst", link.dst().deviceId().toString())
329 .put("dstPort", link.dst().port().toString());
Simon Hunt95d56fd2015-11-12 11:06:44 -0800330 String type = LINK_EVENT.get(event.type());
Simon Hunt36740d02017-06-07 11:25:51 -0700331 return JsonUtils.envelope(type, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700332 }
333
334 // Produces a host event message to the client.
335 protected ObjectNode hostMessage(HostEvent event) {
336 Host host = event.subject();
Charles Chan33f28a92015-11-13 13:12:38 -0800337 Host prevHost = event.prevSubject();
Simon Hunt1e20dae2016-10-28 11:26:26 -0700338 String hostType = host.annotations().value(AnnotationKeys.UI_TYPE);
Simon Hunt10618f62017-06-15 19:30:52 -0700339 String ip = ip(host.ipAddresses());
Simon Hunt95d56fd2015-11-12 11:06:44 -0800340
Simon Huntda580882015-05-12 20:58:18 -0700341 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700342 .put("id", host.id().toString())
Simon Hunt12c79ed2017-09-12 11:58:44 -0700343 .put("type", isNullOrEmpty(hostType) ? "endstation" : hostType);
Simon Hunt10618f62017-06-15 19:30:52 -0700344
Simon Hunt12c79ed2017-09-12 11:58:44 -0700345 // set most recent connect point (and previous if we know it)
Simon Huntda580882015-05-12 20:58:18 -0700346 payload.set("cp", hostConnect(host.location()));
Charles Chan33f28a92015-11-13 13:12:38 -0800347 if (prevHost != null && prevHost.location() != null) {
348 payload.set("prevCp", hostConnect(prevHost.location()));
Simon Hunt95d56fd2015-11-12 11:06:44 -0800349 }
Simon Hunt12c79ed2017-09-12 11:58:44 -0700350
351 // set ALL connect points
352 addAllCps(host.locations(), payload);
353
Thomas Vachuska528ea952017-10-09 18:25:35 +0200354 payload.set("labels", labels(nameForHost(host), ip, host.mac().toString(), ""));
Thomas Vachuska329af532015-03-10 02:08:33 -0700355 payload.set("props", props(host.annotations()));
Thomas Vachuskac616e172018-04-17 16:57:12 -0700356
Thomas Vachuska4c47a382018-05-07 13:48:38 -0700357 BasicHostConfig cfg = get(NetworkConfigService.class)
358 .getConfig(host.id(), BasicHostConfig.class);
359 if (!addLocation(cfg, payload)) {
Thomas Vachuskac616e172018-04-17 16:57:12 -0700360 addMetaUi(host.id().toString(), payload);
361 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700362
Simon Hunt95d56fd2015-11-12 11:06:44 -0800363 String type = HOST_EVENT.get(event.type());
Simon Hunt36740d02017-06-07 11:25:51 -0700364 return JsonUtils.envelope(type, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700365 }
366
Simon Hunt12c79ed2017-09-12 11:58:44 -0700367 private void addAllCps(Set<HostLocation> locations, ObjectNode payload) {
368 ArrayNode cps = arrayNode();
369 locations.forEach(loc -> cps.add(hostConnect(loc)));
370 payload.set("allCps", cps);
371 }
372
Thomas Vachuska329af532015-03-10 02:08:33 -0700373 // Encodes the specified host location into a JSON object.
Simon Huntda580882015-05-12 20:58:18 -0700374 private ObjectNode hostConnect(HostLocation location) {
375 return objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700376 .put("device", location.deviceId().toString())
377 .put("port", location.port().toLong());
378 }
379
380 // Encodes the specified list of labels a JSON array.
Simon Huntda580882015-05-12 20:58:18 -0700381 private ArrayNode labels(String... labels) {
382 ArrayNode json = arrayNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700383 for (String label : labels) {
384 json.add(label);
385 }
386 return json;
387 }
388
389 // Returns the name of the master node for the specified device id.
390 private String master(DeviceId deviceId) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700391 NodeId master = services.mastership().getMasterFor(deviceId);
Thomas Vachuska329af532015-03-10 02:08:33 -0700392 return master != null ? master.toString() : "";
393 }
394
395 // Generates an edge link from the specified host location.
396 private EdgeLink edgeLink(Host host, boolean ingress) {
397 return new DefaultEdgeLink(PID, new ConnectPoint(host.id(), portNumber(0)),
398 host.location(), ingress);
399 }
400
401 // Adds meta UI information for the specified object.
402 private void addMetaUi(String id, ObjectNode payload) {
403 ObjectNode meta = metaUi.get(id);
404 if (meta != null) {
405 payload.set("metaUi", meta);
406 }
407 }
408
Thomas Vachuska4c47a382018-05-07 13:48:38 -0700409 private boolean addLocation(BasicElementConfig cfg, ObjectNode payload) {
410 if (cfg != null) {
411 String locType = cfg.locType();
412 boolean isGeo = Objects.equals(locType, LOC_TYPE_GEO);
413 boolean isGrid = Objects.equals(locType, LOC_TYPE_GRID);
414 if (isGeo || isGrid) {
Thomas Vachuskac616e172018-04-17 16:57:12 -0700415 try {
Thomas Vachuskac616e172018-04-17 16:57:12 -0700416 ObjectNode loc = objectNode()
Thomas Vachuska4c47a382018-05-07 13:48:38 -0700417 .put("locType", locType)
418 .put("latOrY", isGeo ? cfg.latitude() : cfg.gridY())
419 .put("longOrX", isGeo ? cfg.longitude() : cfg.gridX());
Thomas Vachuskac616e172018-04-17 16:57:12 -0700420 payload.set("location", loc);
421 return true;
422 } catch (NumberFormatException e) {
Thomas Vachuska4c47a382018-05-07 13:48:38 -0700423 log.warn("Invalid location data: {}", cfg);
Thomas Vachuskac616e172018-04-17 16:57:12 -0700424 }
Thomas Vachuskac67a9912018-03-06 14:37:45 -0800425 }
426 }
Thomas Vachuskac616e172018-04-17 16:57:12 -0700427 return false;
Thomas Vachuskac67a9912018-03-06 14:37:45 -0800428 }
429
Thomas Vachuska329af532015-03-10 02:08:33 -0700430 // Updates meta UI information for the specified object.
Simon Huntd2747a02015-04-30 22:41:16 -0700431 protected void updateMetaUi(ObjectNode payload) {
432 metaUi.put(JsonUtils.string(payload, "id"),
433 JsonUtils.node(payload, "memento"));
Thomas Vachuska329af532015-03-10 02:08:33 -0700434 }
435
Simon Hunta17fa672015-08-19 18:42:22 -0700436
Simon Huntb745ca62015-07-28 15:37:11 -0700437 // -----------------------------------------------------------------------
438 // Create models of the data to return, that overlays can adjust / augment
439
Simon Hunta58d8942017-08-11 12:51:14 -0700440 private String lookupGlyph(Device device) {
Sean Condon59d31372019-02-02 20:07:00 +0000441 String uiType = device.annotations().value("uiType");
442 if (uiType != null && !uiType.equalsIgnoreCase("undefined")) {
443 return uiType;
444 } else {
445 return DEVICE_GLYPHS.get(device.type());
446 }
Simon Hunta58d8942017-08-11 12:51:14 -0700447 }
448
449
Simon Huntb745ca62015-07-28 15:37:11 -0700450 // Returns property panel model for summary response.
Simon Hunt8a0429a2017-01-06 16:52:47 -0800451 protected PropertyPanel summmaryMessage() {
Simon Hunta58d8942017-08-11 12:51:14 -0700452 // chose NOT to add debug messages, since this is called every few seconds
Simon Hunt1911fe42017-05-02 18:25:58 -0700453 Topology topology = services.topology().currentTopology();
Simon Hunt879ce452017-08-10 23:32:00 -0700454 LionBundle lion = getLionBundle(LION_TOPO);
455 String panelTitle = lion.getSafe("title_panel_summary");
Simon Hunt0af1ec32015-07-24 12:17:55 -0700456
Simon Hunta58d8942017-08-11 12:51:14 -0700457 return new PropertyPanel(panelTitle, "bird")
Simon Hunt879ce452017-08-10 23:32:00 -0700458 .addProp(VERSION, lion.getSafe(VERSION), version)
Simon Hunt1911fe42017-05-02 18:25:58 -0700459 .addSeparator()
Simon Hunt879ce452017-08-10 23:32:00 -0700460 .addProp(DEVICES, lion.getSafe(DEVICES), services.device().getDeviceCount())
461 .addProp(LINKS, lion.getSafe(LINKS), topology.linkCount())
462 .addProp(HOSTS, lion.getSafe(HOSTS), services.host().getHostCount())
463 .addProp(TOPOLOGY_SSCS, lion.getSafe(TOPOLOGY_SSCS), topology.clusterCount())
Simon Hunt1911fe42017-05-02 18:25:58 -0700464 .addSeparator()
Simon Hunt879ce452017-08-10 23:32:00 -0700465 .addProp(INTENTS, lion.getSafe(INTENTS), services.intent().getIntentCount())
Simon Hunt879ce452017-08-10 23:32:00 -0700466 .addProp(FLOWS, lion.getSafe(FLOWS), services.flow().getFlowRuleCount());
Thomas Vachuska329af532015-03-10 02:08:33 -0700467 }
468
Simon Hunta58d8942017-08-11 12:51:14 -0700469
470 private String friendlyDevice(DeviceId deviceId) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700471 Device device = services.device().getDevice(deviceId);
Thomas Vachuska329af532015-03-10 02:08:33 -0700472 Annotations annot = device.annotations();
473 String name = annot.value(AnnotationKeys.NAME);
Simon Hunta58d8942017-08-11 12:51:14 -0700474 return isNullOrEmpty(name) ? deviceId.toString() : name;
475 }
476
477 // Generates a property panel model for device details response
478 protected PropertyPanel deviceDetails(DeviceId deviceId) {
479 log.debug("generate prop panel data for device {}", deviceId);
480 Device device = services.device().getDevice(deviceId);
481 Annotations annot = device.annotations();
482 String proto = annot.value(AnnotationKeys.PROTOCOL);
483 String title = friendlyDevice(deviceId);
484 LionBundle lion = getLionBundle(LION_TOPO);
485
486 PropertyPanel pp = new PropertyPanel(title, lookupGlyph(device))
487 .navPath(DEVICE_NAV_PATH)
488 .id(deviceId.toString());
489 addDeviceBasicProps(pp, deviceId, device, proto, lion);
490 addLocationProps(pp, annot, lion);
491 addDeviceCountStats(pp, deviceId, lion);
492 addDeviceCoreButtons(pp);
493 return pp;
494 }
495
496 private void addDeviceBasicProps(PropertyPanel pp, DeviceId deviceId,
497 Device device, String proto, LionBundle lion) {
498 pp.addProp(URI, lion.getSafe(URI), deviceId.toString())
499 .addProp(VENDOR, lion.getSafe(VENDOR), device.manufacturer())
500 .addProp(HW_VERSION, lion.getSafe(HW_VERSION), device.hwVersion())
501 .addProp(SW_VERSION, lion.getSafe(SW_VERSION), device.swVersion())
502 .addProp(SERIAL_NUMBER, lion.getSafe(SERIAL_NUMBER), device.serialNumber())
503 .addProp(PROTOCOL, lion.getSafe(PROTOCOL), proto)
504 .addSeparator();
505 }
506
507 // only add location properties if we have them
508 private void addLocationProps(PropertyPanel pp, Annotations annot,
509 LionBundle lion) {
510 String slat = annot.value(AnnotationKeys.LATITUDE);
511 String slng = annot.value(AnnotationKeys.LONGITUDE);
512 String sgrY = annot.value(AnnotationKeys.GRID_Y);
513 String sgrX = annot.value(AnnotationKeys.GRID_X);
514
515 boolean validLat = slat != null && !slat.equals(NO_GEO_VALUE);
516 boolean validLng = slng != null && !slng.equals(NO_GEO_VALUE);
517 if (validLat && validLng) {
518 pp.addProp(LATITUDE, lion.getSafe(LATITUDE), slat)
519 .addProp(LONGITUDE, lion.getSafe(LONGITUDE), slng)
520 .addSeparator();
521
522 } else if (sgrY != null && sgrX != null) {
523 pp.addProp(GRID_Y, lion.getSafe(GRID_Y), sgrY)
524 .addProp(GRID_X, lion.getSafe(GRID_X), sgrX)
525 .addSeparator();
526 }
527 // else, no location
528 }
529
530 private void addDeviceCountStats(PropertyPanel pp, DeviceId deviceId, LionBundle lion) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700531 int portCount = services.device().getPorts(deviceId).size();
Thomas Vachuska329af532015-03-10 02:08:33 -0700532 int flowCount = getFlowCount(deviceId);
cheng fan35dc0f22015-06-10 06:02:47 +0800533 int tunnelCount = getTunnelCount(deviceId);
Simon Huntb745ca62015-07-28 15:37:11 -0700534
Simon Hunta58d8942017-08-11 12:51:14 -0700535 pp.addProp(PORTS, lion.getSafe(PORTS), portCount)
536 .addProp(FLOWS, lion.getSafe(FLOWS), flowCount)
537 .addProp(TUNNELS, lion.getSafe(TUNNELS), tunnelCount);
538 }
Simon Huntb745ca62015-07-28 15:37:11 -0700539
Simon Hunta58d8942017-08-11 12:51:14 -0700540 private void addDeviceCoreButtons(PropertyPanel pp) {
541 pp.addButton(CoreButtons.SHOW_DEVICE_VIEW)
Simon Hunt1911fe42017-05-02 18:25:58 -0700542 .addButton(CoreButtons.SHOW_FLOW_VIEW)
543 .addButton(CoreButtons.SHOW_PORT_VIEW)
544 .addButton(CoreButtons.SHOW_GROUP_VIEW)
545 .addButton(CoreButtons.SHOW_METER_VIEW);
Thomas Vachuska329af532015-03-10 02:08:33 -0700546 }
547
548 protected int getFlowCount(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800549 return services.flow().getFlowRuleCount(deviceId);
Thomas Vachuska329af532015-03-10 02:08:33 -0700550 }
551
Thomas Vachuska52f2cd12018-11-08 21:20:04 -0800552 @Deprecated
cheng fan35dc0f22015-06-10 06:02:47 +0800553 protected int getTunnelCount(DeviceId deviceId) {
Thomas Vachuska52f2cd12018-11-08 21:20:04 -0800554 return 0;
cheng fan35dc0f22015-06-10 06:02:47 +0800555 }
556
Simon Hunt10618f62017-06-15 19:30:52 -0700557 private boolean useDefaultName(String annotName) {
558 return isNullOrEmpty(annotName) || DASH.equals(annotName);
559 }
560
561 private String nameForHost(Host host) {
562 String name = host.annotations().value(AnnotationKeys.NAME);
563 return useDefaultName(name) ? ip(host.ipAddresses()) : name;
564 }
565
Simon Hunta58d8942017-08-11 12:51:14 -0700566 private String glyphForHost(Annotations annot) {
567 String uiType = annot.value(AnnotationKeys.UI_TYPE);
568 return isNullOrEmpty(uiType) ? DEFAULT_HOST_GLYPH : uiType;
Thomas Vachuska329af532015-03-10 02:08:33 -0700569 }
570
Simon Hunta58d8942017-08-11 12:51:14 -0700571 // Generates a property panel model for a host details response
572 protected PropertyPanel hostDetails(HostId hostId) {
573 log.debug("generate prop panel data for host {}", hostId);
574 Host host = services.host().getHost(hostId);
575 Annotations annot = host.annotations();
576 String glyphId = glyphForHost(annot);
577 LionBundle lion = getLionBundle(LION_TOPO);
578
579 PropertyPanel pp = new PropertyPanel(nameForHost(host), glyphId)
580 .navPath(HOST_NAV_PATH)
581 .id(hostId.toString());
582 addHostBasicProps(pp, host, lion);
583 addLocationProps(pp, annot, lion);
584 return pp;
585 }
586
587 private void addHostBasicProps(PropertyPanel pp, Host host, LionBundle lion) {
588 pp.addProp(LPL_FRIENDLY, lion.getSafe(LPL_FRIENDLY), nameForHost(host))
589 .addProp(MAC, lion.getSafe(MAC), host.mac())
590 .addProp(IP, lion.getSafe(IP), host.ipAddresses(), "[\\[\\]]")
591 .addProp(VLAN, lion.getSafe(VLAN), displayVlan(host.vlan(), lion))
592 .addSeparator();
593 }
594
595 private String displayVlan(VlanId vlan, LionBundle lion) {
596 return VlanId.NONE.equals(vlan) ? lion.getSafe(VLAN_NONE) : vlan.toString();
597 }
598
599 // Generates a property panel model for a link details response (edge-link)
600 protected PropertyPanel edgeLinkDetails(HostId hid, ConnectPoint cp) {
601 log.debug("generate prop panel data for edgelink {} {}", hid, cp);
602 LionBundle lion = getLionBundle(LION_TOPO);
603 String title = lion.getSafe("title_edge_link");
604
605 PropertyPanel pp = new PropertyPanel(title, LINK_GLYPH);
606 addLinkHostProps(pp, hid, lion);
607 addLinkCpBProps(pp, cp, lion);
608 return pp;
609 }
610
611 // Generates a property panel model for a link details response (infra-link)
612 protected PropertyPanel infraLinkDetails(ConnectPoint cpA, ConnectPoint cpB) {
613 log.debug("generate prop panel data for infralink {} {}", cpA, cpB);
614 LionBundle lion = getLionBundle(LION_TOPO);
615 String title = lion.getSafe("title_infra_link");
616
617 PropertyPanel pp = new PropertyPanel(title, LINK_GLYPH);
618 addLinkCpAProps(pp, cpA, lion);
619 addLinkCpBProps(pp, cpB, lion);
620 addLinkBackingProps(pp, cpA, cpB, lion);
621 return pp;
622 }
623
624 private void addLinkHostProps(PropertyPanel pp, HostId hostId, LionBundle lion) {
625 Host host = services.host().getHost(hostId);
626
627 pp.addProp(LPL_A_TYPE, lion.getSafe(LPL_A_TYPE), lion.getSafe(HOST))
628 .addProp(LPL_A_ID, lion.getSafe(LPL_A_ID), hostId.toString())
629 .addProp(LPL_A_FRIENDLY, lion.getSafe(LPL_A_FRIENDLY), nameForHost(host))
630 .addSeparator();
631 }
632
633 private void addLinkCpAProps(PropertyPanel pp, ConnectPoint cp, LionBundle lion) {
634 DeviceId did = cp.deviceId();
635
636 pp.addProp(LPL_A_TYPE, lion.getSafe(LPL_A_TYPE), lion.getSafe(DEVICE))
637 .addProp(LPL_A_ID, lion.getSafe(LPL_A_ID), did.toString())
638 .addProp(LPL_A_FRIENDLY, lion.getSafe(LPL_A_FRIENDLY), friendlyDevice(did))
639 .addProp(LPL_A_PORT, lion.getSafe(LPL_A_PORT), cp.port().toLong())
640 .addSeparator();
641 }
642
643 private void addLinkCpBProps(PropertyPanel pp, ConnectPoint cp, LionBundle lion) {
644 DeviceId did = cp.deviceId();
645
646 pp.addProp(LPL_B_TYPE, lion.getSafe(LPL_B_TYPE), lion.getSafe(DEVICE))
647 .addProp(LPL_B_ID, lion.getSafe(LPL_B_ID), did.toString())
648 .addProp(LPL_B_FRIENDLY, lion.getSafe(LPL_B_FRIENDLY), friendlyDevice(did))
649 .addProp(LPL_B_PORT, lion.getSafe(LPL_B_PORT), cp.port().toLong())
650 .addSeparator();
651 }
652
653 private void addLinkBackingProps(PropertyPanel pp, ConnectPoint cpA,
654 ConnectPoint cpB, LionBundle lion) {
655 Link a2b = services.link().getLink(cpA, cpB);
656 Link b2a = services.link().getLink(cpB, cpA);
657
658 pp.addProp(LPL_A2B, lion.getSafe(LPL_A2B), linkPropString(a2b, lion))
659 .addProp(LPL_B2A, lion.getSafe(LPL_B2A), linkPropString(b2a, lion));
660 }
661
662 private String linkPropString(Link link, LionBundle lion) {
663 if (link == null) {
664 return lion.getSafe(LPV_NO_LINK);
665 }
666 return lion.getSafe(link.type()) + SLASH +
667 lion.getSafe(link.state()) + SLASH +
668 lion.getSafe(link.isExpected() ? EXPECTED : NOT_EXPECTED);
669 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700670}