blob: 08311ed8c98b586f1df1ba5ab8968d4b3c25ad13 [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.AnnotationKeys;
33import org.onosproject.net.Annotations;
34import org.onosproject.net.ConnectPoint;
35import org.onosproject.net.DefaultEdgeLink;
36import org.onosproject.net.Device;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.EdgeLink;
Simon Huntf4fd2a22016-08-10 15:41:09 -070039import org.onosproject.net.ElementId;
Thomas Vachuska329af532015-03-10 02:08:33 -070040import org.onosproject.net.Host;
41import org.onosproject.net.HostId;
42import org.onosproject.net.HostLocation;
43import org.onosproject.net.Link;
Thomas Vachuska4c47a382018-05-07 13:48:38 -070044import org.onosproject.net.config.NetworkConfigService;
45import org.onosproject.net.config.basics.BasicDeviceConfig;
46import org.onosproject.net.config.basics.BasicElementConfig;
47import org.onosproject.net.config.basics.BasicHostConfig;
Thomas Vachuska329af532015-03-10 02:08:33 -070048import org.onosproject.net.device.DeviceEvent;
Thomas Vachuska164ecf62018-05-08 17:29:55 -070049import org.onosproject.net.driver.Driver;
Thomas Vachuska329af532015-03-10 02:08:33 -070050import org.onosproject.net.host.HostEvent;
Thomas Vachuska329af532015-03-10 02:08:33 -070051import org.onosproject.net.link.LinkEvent;
Thomas Vachuska329af532015-03-10 02:08:33 -070052import org.onosproject.net.provider.ProviderId;
Thomas Vachuska329af532015-03-10 02:08:33 -070053import org.onosproject.net.topology.Topology;
Simon Huntd2747a02015-04-30 22:41:16 -070054import org.onosproject.ui.JsonUtils;
Thomas Vachuska329af532015-03-10 02:08:33 -070055import org.onosproject.ui.UiConnection;
Simon Hunta0ddb022015-05-01 09:53:01 -070056import org.onosproject.ui.UiMessageHandler;
Simon Hunted804d52016-03-30 09:51:40 -070057import org.onosproject.ui.impl.topo.util.ServicesBundle;
Simon Hunt879ce452017-08-10 23:32:00 -070058import org.onosproject.ui.lion.LionBundle;
Simon Hunt0af1ec32015-07-24 12:17:55 -070059import org.onosproject.ui.topo.PropertyPanel;
Thomas Vachuska329af532015-03-10 02:08:33 -070060import org.slf4j.Logger;
61import org.slf4j.LoggerFactory;
62
Thomas Vachuska329af532015-03-10 02:08:33 -070063import java.util.Collection;
64import java.util.Collections;
Thomas Vachuska329af532015-03-10 02:08:33 -070065import java.util.Iterator;
Thomas Vachuska329af532015-03-10 02:08:33 -070066import java.util.Map;
Thomas Vachuskac616e172018-04-17 16:57:12 -070067import java.util.Objects;
chengfanc553c952016-07-22 15:48:23 +080068import java.util.Optional;
Simon Huntf4fd2a22016-08-10 15:41:09 -070069import java.util.Set;
Thomas Vachuska329af532015-03-10 02:08:33 -070070import java.util.concurrent.ConcurrentHashMap;
71
Thomas Vachuska329af532015-03-10 02:08:33 -070072import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuska164ecf62018-05-08 17:29:55 -070073import static org.onosproject.net.AnnotationKeys.DRIVER;
Thomas Vachuska329af532015-03-10 02:08:33 -070074import static org.onosproject.net.PortNumber.portNumber;
Thomas Vachuskac616e172018-04-17 16:57:12 -070075import static org.onosproject.net.config.basics.BasicElementConfig.LOC_TYPE_GEO;
76import static org.onosproject.net.config.basics.BasicElementConfig.LOC_TYPE_GRID;
Simon Hunt3a0598f2015-08-04 19:59:04 -070077import static org.onosproject.ui.topo.TopoConstants.CoreButtons;
Thomas Vachuska4c47a382018-05-07 13:48:38 -070078import static org.onosproject.ui.topo.TopoConstants.Properties.*;
Simon Huntd3ceffa2015-08-25 12:44:35 -070079import static org.onosproject.ui.topo.TopoUtils.compactLinkString;
Thomas Vachuska329af532015-03-10 02:08:33 -070080
81/**
82 * Facility for creating messages bound for the topology viewer.
83 */
Simon Hunta0ddb022015-05-01 09:53:01 -070084public abstract class TopologyViewMessageHandlerBase extends UiMessageHandler {
Thomas Vachuska329af532015-03-10 02:08:33 -070085
Simon Huntf4fd2a22016-08-10 15:41:09 -070086 private static final String NO_GEO_VALUE = "0.0";
Simon Hunt10618f62017-06-15 19:30:52 -070087 private static final String DASH = "-";
Simon Hunta58d8942017-08-11 12:51:14 -070088 private static final String SLASH = " / ";
Simon Hunt10618f62017-06-15 19:30:52 -070089
90 // nav paths are the view names for hot-link navigation from topo view...
91 private static final String DEVICE_NAV_PATH = "device";
92 private static final String HOST_NAV_PATH = "host";
Simon Huntf4fd2a22016-08-10 15:41:09 -070093
Simon Hunta58d8942017-08-11 12:51:14 -070094 // link panel label keys
95 private static final String LPL_FRIENDLY = "lp_label_friendly";
96 private static final String LPL_A_TYPE = "lp_label_a_type";
97 private static final String LPL_A_ID = "lp_label_a_id";
98 private static final String LPL_A_FRIENDLY = "lp_label_a_friendly";
99 private static final String LPL_A_PORT = "lp_label_a_port";
100 private static final String LPL_B_TYPE = "lp_label_b_type";
101 private static final String LPL_B_ID = "lp_label_b_id";
102 private static final String LPL_B_FRIENDLY = "lp_label_b_friendly";
103 private static final String LPL_B_PORT = "lp_label_b_port";
104 private static final String LPL_A2B = "lp_label_a2b";
105 private static final String LPL_B2A = "lp_label_b2a";
106 private static final String LPV_NO_LINK = "lp_value_no_link";
107
108 // other Lion keys
109 private static final String HOST = "host";
110 private static final String DEVICE = "device";
111 private static final String EXPECTED = "expected";
112 private static final String NOT_EXPECTED = "not_expected";
113
Simon Hunt95d56fd2015-11-12 11:06:44 -0800114 // default to an "add" event...
115 private static final DefaultHashMap<ClusterEvent.Type, String> CLUSTER_EVENT =
116 new DefaultHashMap<>("addInstance");
117
118 // default to an "update" event...
119 private static final DefaultHashMap<DeviceEvent.Type, String> DEVICE_EVENT =
120 new DefaultHashMap<>("updateDevice");
121 private static final DefaultHashMap<LinkEvent.Type, String> LINK_EVENT =
122 new DefaultHashMap<>("updateLink");
123 private static final DefaultHashMap<HostEvent.Type, String> HOST_EVENT =
124 new DefaultHashMap<>("updateHost");
125
126 // but call out specific events that we care to differentiate...
127 static {
128 CLUSTER_EVENT.put(ClusterEvent.Type.INSTANCE_REMOVED, "removeInstance");
129
130 DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_ADDED, "addDevice");
131 DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_REMOVED, "removeDevice");
132
133 LINK_EVENT.put(LinkEvent.Type.LINK_ADDED, "addLink");
134 LINK_EVENT.put(LinkEvent.Type.LINK_REMOVED, "removeLink");
135
136 HOST_EVENT.put(HostEvent.Type.HOST_ADDED, "addHost");
137 HOST_EVENT.put(HostEvent.Type.HOST_REMOVED, "removeHost");
138 HOST_EVENT.put(HostEvent.Type.HOST_MOVED, "moveHost");
139 }
140
Simon Hunta58d8942017-08-11 12:51:14 -0700141 private static final DefaultHashMap<Device.Type, String> DEVICE_GLYPHS =
142 new DefaultHashMap<>("m_unknown");
143
144 static {
145 DEVICE_GLYPHS.put(Device.Type.SWITCH, "m_switch");
146 DEVICE_GLYPHS.put(Device.Type.ROUTER, "m_router");
147 DEVICE_GLYPHS.put(Device.Type.ROADM, "m_roadm");
148 DEVICE_GLYPHS.put(Device.Type.OTN, "m_otn");
149 DEVICE_GLYPHS.put(Device.Type.ROADM_OTN, "m_roadm_otn");
150 DEVICE_GLYPHS.put(Device.Type.BALANCER, "m_balancer");
151 DEVICE_GLYPHS.put(Device.Type.IPS, "m_ips");
152 DEVICE_GLYPHS.put(Device.Type.IDS, "m_ids");
153 DEVICE_GLYPHS.put(Device.Type.CONTROLLER, "m_controller");
154 DEVICE_GLYPHS.put(Device.Type.VIRTUAL, "m_virtual");
155 DEVICE_GLYPHS.put(Device.Type.FIBER_SWITCH, "m_fiberSwitch");
156 DEVICE_GLYPHS.put(Device.Type.MICROWAVE, "m_microwave");
157 DEVICE_GLYPHS.put(Device.Type.OLT, "m_olt");
158 DEVICE_GLYPHS.put(Device.Type.ONU, "m_onu");
159 DEVICE_GLYPHS.put(Device.Type.OPTICAL_AMPLIFIER, "unknown"); // TODO glyph needed
160 DEVICE_GLYPHS.put(Device.Type.OTHER, "m_other");
Georgios Katsikas40ecef32018-07-03 11:17:44 +0200161 DEVICE_GLYPHS.put(Device.Type.SERVER, "m_endpoint");
Simon Hunta58d8942017-08-11 12:51:14 -0700162 }
163
164 private static final String DEFAULT_HOST_GLYPH = "m_endstation";
165 private static final String LINK_GLYPH = "m_ports";
166
167
Ray Milkey9c9cde42018-01-12 14:22:06 -0800168 static final Logger log =
Simon Huntd2747a02015-04-30 22:41:16 -0700169 LoggerFactory.getLogger(TopologyViewMessageHandlerBase.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700170
Simon Huntd2747a02015-04-30 22:41:16 -0700171 private static final ProviderId PID =
172 new ProviderId("core", "org.onosproject.core", true);
Thomas Vachuska329af532015-03-10 02:08:33 -0700173
Thomas Vachuska329af532015-03-10 02:08:33 -0700174 // TODO: extract into an external & durable state; good enough for now and demo
175 private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>();
176
177 /**
Thomas Vachuska329af532015-03-10 02:08:33 -0700178 * Returns read-only view of the meta-ui information.
179 *
180 * @return map of id to meta-ui mementos
181 */
182 static Map<String, ObjectNode> getMetaUi() {
183 return Collections.unmodifiableMap(metaUi);
184 }
185
Thomas Vachuskac616e172018-04-17 16:57:12 -0700186 /**
187 * Clears any meta-ui information.
188 */
189 public static void clearMetaUi() {
190 metaUi.clear();
191 }
192
Simon Hunt879ce452017-08-10 23:32:00 -0700193 private static final String LION_TOPO = "core.view.Topo";
194
195 private static final Set<String> REQ_LION_BUNDLES = ImmutableSet.of(
196 LION_TOPO
197 );
Simon Hunt1911fe42017-05-02 18:25:58 -0700198
199 protected ServicesBundle services;
200
201 private String version;
202
203
Thomas Vachuska329af532015-03-10 02:08:33 -0700204 @Override
205 public void init(UiConnection connection, ServiceDirectory directory) {
206 super.init(connection, directory);
Simon Hunt1911fe42017-05-02 18:25:58 -0700207 services = new ServicesBundle(directory);
208 setVersionString(directory);
209 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700210
Simon Hunt1911fe42017-05-02 18:25:58 -0700211 // Creates a palatable version string to display on the summary panel
212 private void setVersionString(ServiceDirectory directory) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700213 String ver = directory.get(CoreService.class).version().toString();
214 version = ver.replace(".SNAPSHOT", "*").replaceFirst("~.*$", "");
215 }
216
Simon Hunt879ce452017-08-10 23:32:00 -0700217 @Override
218 public Set<String> requiredLionBundles() {
219 return REQ_LION_BUNDLES;
220 }
221
Simon Hunt10618f62017-06-15 19:30:52 -0700222 // Returns the first of the given set of IP addresses as a string.
Thomas Vachuska329af532015-03-10 02:08:33 -0700223 private String ip(Set<IpAddress> ipAddresses) {
224 Iterator<IpAddress> it = ipAddresses.iterator();
225 return it.hasNext() ? it.next().toString() : "unknown";
226 }
227
228 // Produces JSON structure from annotations.
229 private JsonNode props(Annotations annotations) {
Simon Huntda580882015-05-12 20:58:18 -0700230 ObjectNode props = objectNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700231 if (annotations != null) {
232 for (String key : annotations.keys()) {
233 props.put(key, annotations.value(key));
234 }
235 }
236 return props;
237 }
238
239 // Produces an informational log message event bound to the client.
Simon Hunt36740d02017-06-07 11:25:51 -0700240 protected ObjectNode info(String message) {
241 return message("info", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700242 }
243
244 // Produces a warning log message event bound to the client.
Simon Hunt36740d02017-06-07 11:25:51 -0700245 protected ObjectNode warning(String message) {
246 return message("warning", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700247 }
248
249 // Produces an error log message event bound to the client.
Simon Hunt36740d02017-06-07 11:25:51 -0700250 protected ObjectNode error(String message) {
251 return message("error", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700252 }
253
254 // Produces a log message event bound to the client.
Simon Hunt36740d02017-06-07 11:25:51 -0700255 private ObjectNode message(String severity, String message) {
Simon Huntda580882015-05-12 20:58:18 -0700256 ObjectNode payload = objectNode()
Simon Huntd2747a02015-04-30 22:41:16 -0700257 .put("severity", severity)
258 .put("message", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700259
Simon Hunt36740d02017-06-07 11:25:51 -0700260 return JsonUtils.envelope("message", payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700261 }
262
Thomas Vachuska329af532015-03-10 02:08:33 -0700263 // Produces a cluster instance message to the client.
Simon Hunt95d56fd2015-11-12 11:06:44 -0800264 protected ObjectNode instanceMessage(ClusterEvent event, String msgType) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700265 ControllerNode node = event.subject();
Simon Hunt1911fe42017-05-02 18:25:58 -0700266 int switchCount = services.mastership().getDevicesOf(node.id()).size();
Simon Huntda580882015-05-12 20:58:18 -0700267 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700268 .put("id", node.id().toString())
269 .put("ip", node.ip().toString())
Simon Hunt1911fe42017-05-02 18:25:58 -0700270 .put("online", services.cluster().getState(node.id()).isActive())
271 .put("ready", services.cluster().getState(node.id()).isReady())
272 .put("uiAttached", node.equals(services.cluster().getLocalNode()))
Thomas Vachuska329af532015-03-10 02:08:33 -0700273 .put("switches", switchCount);
274
Simon Huntda580882015-05-12 20:58:18 -0700275 ArrayNode labels = arrayNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700276 labels.add(node.id().toString());
277 labels.add(node.ip().toString());
278
279 // Add labels, props and stuff the payload into envelope.
280 payload.set("labels", labels);
281 addMetaUi(node.id().toString(), payload);
282
Simon Hunt95d56fd2015-11-12 11:06:44 -0800283 String type = msgType != null ? msgType : CLUSTER_EVENT.get(event.type());
Simon Hunt36740d02017-06-07 11:25:51 -0700284 return JsonUtils.envelope(type, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700285 }
286
287 // Produces a device event message to the client.
288 protected ObjectNode deviceMessage(DeviceEvent event) {
289 Device device = event.subject();
Simon Hunt1e20dae2016-10-28 11:26:26 -0700290 String uiType = device.annotations().value(AnnotationKeys.UI_TYPE);
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700291 String driverName = device.annotations().value(DRIVER);
292 Driver driver = driverName == null ? null : services.driver().getDriver(driverName);
Simon Hunt1e20dae2016-10-28 11:26:26 -0700293 String devType = uiType != null ? uiType :
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700294 (driver != null ? driver.getProperty(AnnotationKeys.UI_TYPE) : null);
Laszlo Pappe1579fa2018-03-08 09:18:48 +0000295 if (devType == null) {
296 devType = device.type().toString().toLowerCase();
297 }
Simon Hunt10618f62017-06-15 19:30:52 -0700298 String name = device.annotations().value(AnnotationKeys.NAME);
299 name = isNullOrEmpty(name) ? device.id().toString() : name;
Simon Hunt1e20dae2016-10-28 11:26:26 -0700300
Simon Huntda580882015-05-12 20:58:18 -0700301 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700302 .put("id", device.id().toString())
Simon Hunt1e20dae2016-10-28 11:26:26 -0700303 .put("type", devType)
Simon Hunt1911fe42017-05-02 18:25:58 -0700304 .put("online", services.device().isAvailable(device.id()))
Thomas Vachuska329af532015-03-10 02:08:33 -0700305 .put("master", master(device.id()));
306
Simon Hunt10618f62017-06-15 19:30:52 -0700307 payload.set("labels", labels("", name, device.id().toString()));
Thomas Vachuska329af532015-03-10 02:08:33 -0700308 payload.set("props", props(device.annotations()));
Thomas Vachuskac616e172018-04-17 16:57:12 -0700309
Thomas Vachuska4c47a382018-05-07 13:48:38 -0700310 BasicDeviceConfig cfg = get(NetworkConfigService.class)
311 .getConfig(device.id(), BasicDeviceConfig.class);
312 if (!addLocation(cfg, payload)) {
Thomas Vachuskac616e172018-04-17 16:57:12 -0700313 addMetaUi(device.id().toString(), payload);
314 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700315
Simon Hunt95d56fd2015-11-12 11:06:44 -0800316 String type = DEVICE_EVENT.get(event.type());
Simon Hunt36740d02017-06-07 11:25:51 -0700317 return JsonUtils.envelope(type, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700318 }
319
320 // Produces a link event message to the client.
321 protected ObjectNode linkMessage(LinkEvent event) {
322 Link link = event.subject();
Simon Huntda580882015-05-12 20:58:18 -0700323 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700324 .put("id", compactLinkString(link))
325 .put("type", link.type().toString().toLowerCase())
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800326 .put("expected", link.isExpected())
Thomas Vachuska329af532015-03-10 02:08:33 -0700327 .put("online", link.state() == Link.State.ACTIVE)
328 .put("linkWidth", 1.2)
329 .put("src", link.src().deviceId().toString())
330 .put("srcPort", link.src().port().toString())
331 .put("dst", link.dst().deviceId().toString())
332 .put("dstPort", link.dst().port().toString());
Simon Hunt95d56fd2015-11-12 11:06:44 -0800333 String type = LINK_EVENT.get(event.type());
Simon Hunt36740d02017-06-07 11:25:51 -0700334 return JsonUtils.envelope(type, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700335 }
336
337 // Produces a host event message to the client.
338 protected ObjectNode hostMessage(HostEvent event) {
339 Host host = event.subject();
Charles Chan33f28a92015-11-13 13:12:38 -0800340 Host prevHost = event.prevSubject();
Simon Hunt1e20dae2016-10-28 11:26:26 -0700341 String hostType = host.annotations().value(AnnotationKeys.UI_TYPE);
Simon Hunt10618f62017-06-15 19:30:52 -0700342 String ip = ip(host.ipAddresses());
Simon Hunt95d56fd2015-11-12 11:06:44 -0800343
Simon Huntda580882015-05-12 20:58:18 -0700344 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700345 .put("id", host.id().toString())
Simon Hunt12c79ed2017-09-12 11:58:44 -0700346 .put("type", isNullOrEmpty(hostType) ? "endstation" : hostType);
Simon Hunt10618f62017-06-15 19:30:52 -0700347
Simon Hunt12c79ed2017-09-12 11:58:44 -0700348 // set most recent connect point (and previous if we know it)
Simon Huntda580882015-05-12 20:58:18 -0700349 payload.set("cp", hostConnect(host.location()));
Charles Chan33f28a92015-11-13 13:12:38 -0800350 if (prevHost != null && prevHost.location() != null) {
351 payload.set("prevCp", hostConnect(prevHost.location()));
Simon Hunt95d56fd2015-11-12 11:06:44 -0800352 }
Simon Hunt12c79ed2017-09-12 11:58:44 -0700353
354 // set ALL connect points
355 addAllCps(host.locations(), payload);
356
Thomas Vachuska528ea952017-10-09 18:25:35 +0200357 payload.set("labels", labels(nameForHost(host), ip, host.mac().toString(), ""));
Thomas Vachuska329af532015-03-10 02:08:33 -0700358 payload.set("props", props(host.annotations()));
Thomas Vachuskac616e172018-04-17 16:57:12 -0700359
Thomas Vachuska4c47a382018-05-07 13:48:38 -0700360 BasicHostConfig cfg = get(NetworkConfigService.class)
361 .getConfig(host.id(), BasicHostConfig.class);
362 if (!addLocation(cfg, payload)) {
Thomas Vachuskac616e172018-04-17 16:57:12 -0700363 addMetaUi(host.id().toString(), payload);
364 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700365
Simon Hunt95d56fd2015-11-12 11:06:44 -0800366 String type = HOST_EVENT.get(event.type());
Simon Hunt36740d02017-06-07 11:25:51 -0700367 return JsonUtils.envelope(type, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700368 }
369
Simon Hunt12c79ed2017-09-12 11:58:44 -0700370 private void addAllCps(Set<HostLocation> locations, ObjectNode payload) {
371 ArrayNode cps = arrayNode();
372 locations.forEach(loc -> cps.add(hostConnect(loc)));
373 payload.set("allCps", cps);
374 }
375
Thomas Vachuska329af532015-03-10 02:08:33 -0700376 // Encodes the specified host location into a JSON object.
Simon Huntda580882015-05-12 20:58:18 -0700377 private ObjectNode hostConnect(HostLocation location) {
378 return objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700379 .put("device", location.deviceId().toString())
380 .put("port", location.port().toLong());
381 }
382
383 // Encodes the specified list of labels a JSON array.
Simon Huntda580882015-05-12 20:58:18 -0700384 private ArrayNode labels(String... labels) {
385 ArrayNode json = arrayNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700386 for (String label : labels) {
387 json.add(label);
388 }
389 return json;
390 }
391
392 // Returns the name of the master node for the specified device id.
393 private String master(DeviceId deviceId) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700394 NodeId master = services.mastership().getMasterFor(deviceId);
Thomas Vachuska329af532015-03-10 02:08:33 -0700395 return master != null ? master.toString() : "";
396 }
397
398 // Generates an edge link from the specified host location.
399 private EdgeLink edgeLink(Host host, boolean ingress) {
400 return new DefaultEdgeLink(PID, new ConnectPoint(host.id(), portNumber(0)),
401 host.location(), ingress);
402 }
403
404 // Adds meta UI information for the specified object.
405 private void addMetaUi(String id, ObjectNode payload) {
406 ObjectNode meta = metaUi.get(id);
407 if (meta != null) {
408 payload.set("metaUi", meta);
409 }
410 }
411
Thomas Vachuska4c47a382018-05-07 13:48:38 -0700412 private boolean addLocation(BasicElementConfig cfg, ObjectNode payload) {
413 if (cfg != null) {
414 String locType = cfg.locType();
415 boolean isGeo = Objects.equals(locType, LOC_TYPE_GEO);
416 boolean isGrid = Objects.equals(locType, LOC_TYPE_GRID);
417 if (isGeo || isGrid) {
Thomas Vachuskac616e172018-04-17 16:57:12 -0700418 try {
Thomas Vachuskac616e172018-04-17 16:57:12 -0700419 ObjectNode loc = objectNode()
Thomas Vachuska4c47a382018-05-07 13:48:38 -0700420 .put("locType", locType)
421 .put("latOrY", isGeo ? cfg.latitude() : cfg.gridY())
422 .put("longOrX", isGeo ? cfg.longitude() : cfg.gridX());
Thomas Vachuskac616e172018-04-17 16:57:12 -0700423 payload.set("location", loc);
424 return true;
425 } catch (NumberFormatException e) {
Thomas Vachuska4c47a382018-05-07 13:48:38 -0700426 log.warn("Invalid location data: {}", cfg);
Thomas Vachuskac616e172018-04-17 16:57:12 -0700427 }
Thomas Vachuskac67a9912018-03-06 14:37:45 -0800428 }
429 }
Thomas Vachuskac616e172018-04-17 16:57:12 -0700430 return false;
Thomas Vachuskac67a9912018-03-06 14:37:45 -0800431 }
432
Thomas Vachuska329af532015-03-10 02:08:33 -0700433 // Updates meta UI information for the specified object.
Simon Huntd2747a02015-04-30 22:41:16 -0700434 protected void updateMetaUi(ObjectNode payload) {
435 metaUi.put(JsonUtils.string(payload, "id"),
436 JsonUtils.node(payload, "memento"));
Thomas Vachuska329af532015-03-10 02:08:33 -0700437 }
438
Simon Hunta17fa672015-08-19 18:42:22 -0700439
Simon Huntb745ca62015-07-28 15:37:11 -0700440 // -----------------------------------------------------------------------
441 // Create models of the data to return, that overlays can adjust / augment
442
Simon Hunta58d8942017-08-11 12:51:14 -0700443 private String lookupGlyph(Device device) {
444 return DEVICE_GLYPHS.get(device.type());
445 }
446
447
Simon Huntb745ca62015-07-28 15:37:11 -0700448 // Returns property panel model for summary response.
Simon Hunt8a0429a2017-01-06 16:52:47 -0800449 protected PropertyPanel summmaryMessage() {
Simon Hunta58d8942017-08-11 12:51:14 -0700450 // chose NOT to add debug messages, since this is called every few seconds
Simon Hunt1911fe42017-05-02 18:25:58 -0700451 Topology topology = services.topology().currentTopology();
Simon Hunt879ce452017-08-10 23:32:00 -0700452 LionBundle lion = getLionBundle(LION_TOPO);
453 String panelTitle = lion.getSafe("title_panel_summary");
Simon Hunt0af1ec32015-07-24 12:17:55 -0700454
Simon Hunta58d8942017-08-11 12:51:14 -0700455 return new PropertyPanel(panelTitle, "bird")
Simon Hunt879ce452017-08-10 23:32:00 -0700456 .addProp(VERSION, lion.getSafe(VERSION), version)
Simon Hunt1911fe42017-05-02 18:25:58 -0700457 .addSeparator()
Simon Hunt879ce452017-08-10 23:32:00 -0700458 .addProp(DEVICES, lion.getSafe(DEVICES), services.device().getDeviceCount())
459 .addProp(LINKS, lion.getSafe(LINKS), topology.linkCount())
460 .addProp(HOSTS, lion.getSafe(HOSTS), services.host().getHostCount())
461 .addProp(TOPOLOGY_SSCS, lion.getSafe(TOPOLOGY_SSCS), topology.clusterCount())
Simon Hunt1911fe42017-05-02 18:25:58 -0700462 .addSeparator()
Simon Hunt879ce452017-08-10 23:32:00 -0700463 .addProp(INTENTS, lion.getSafe(INTENTS), services.intent().getIntentCount())
464 .addProp(TUNNELS, lion.getSafe(TUNNELS), services.tunnel().tunnelCount())
465 .addProp(FLOWS, lion.getSafe(FLOWS), services.flow().getFlowRuleCount());
Thomas Vachuska329af532015-03-10 02:08:33 -0700466 }
467
Simon Hunta58d8942017-08-11 12:51:14 -0700468
469 private String friendlyDevice(DeviceId deviceId) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700470 Device device = services.device().getDevice(deviceId);
Thomas Vachuska329af532015-03-10 02:08:33 -0700471 Annotations annot = device.annotations();
472 String name = annot.value(AnnotationKeys.NAME);
Simon Hunta58d8942017-08-11 12:51:14 -0700473 return isNullOrEmpty(name) ? deviceId.toString() : name;
474 }
475
476 // Generates a property panel model for device details response
477 protected PropertyPanel deviceDetails(DeviceId deviceId) {
478 log.debug("generate prop panel data for device {}", deviceId);
479 Device device = services.device().getDevice(deviceId);
480 Annotations annot = device.annotations();
481 String proto = annot.value(AnnotationKeys.PROTOCOL);
482 String title = friendlyDevice(deviceId);
483 LionBundle lion = getLionBundle(LION_TOPO);
484
485 PropertyPanel pp = new PropertyPanel(title, lookupGlyph(device))
486 .navPath(DEVICE_NAV_PATH)
487 .id(deviceId.toString());
488 addDeviceBasicProps(pp, deviceId, device, proto, lion);
489 addLocationProps(pp, annot, lion);
490 addDeviceCountStats(pp, deviceId, lion);
491 addDeviceCoreButtons(pp);
492 return pp;
493 }
494
495 private void addDeviceBasicProps(PropertyPanel pp, DeviceId deviceId,
496 Device device, String proto, LionBundle lion) {
497 pp.addProp(URI, lion.getSafe(URI), deviceId.toString())
498 .addProp(VENDOR, lion.getSafe(VENDOR), device.manufacturer())
499 .addProp(HW_VERSION, lion.getSafe(HW_VERSION), device.hwVersion())
500 .addProp(SW_VERSION, lion.getSafe(SW_VERSION), device.swVersion())
501 .addProp(SERIAL_NUMBER, lion.getSafe(SERIAL_NUMBER), device.serialNumber())
502 .addProp(PROTOCOL, lion.getSafe(PROTOCOL), proto)
503 .addSeparator();
504 }
505
506 // only add location properties if we have them
507 private void addLocationProps(PropertyPanel pp, Annotations annot,
508 LionBundle lion) {
509 String slat = annot.value(AnnotationKeys.LATITUDE);
510 String slng = annot.value(AnnotationKeys.LONGITUDE);
511 String sgrY = annot.value(AnnotationKeys.GRID_Y);
512 String sgrX = annot.value(AnnotationKeys.GRID_X);
513
514 boolean validLat = slat != null && !slat.equals(NO_GEO_VALUE);
515 boolean validLng = slng != null && !slng.equals(NO_GEO_VALUE);
516 if (validLat && validLng) {
517 pp.addProp(LATITUDE, lion.getSafe(LATITUDE), slat)
518 .addProp(LONGITUDE, lion.getSafe(LONGITUDE), slng)
519 .addSeparator();
520
521 } else if (sgrY != null && sgrX != null) {
522 pp.addProp(GRID_Y, lion.getSafe(GRID_Y), sgrY)
523 .addProp(GRID_X, lion.getSafe(GRID_X), sgrX)
524 .addSeparator();
525 }
526 // else, no location
527 }
528
529 private void addDeviceCountStats(PropertyPanel pp, DeviceId deviceId, LionBundle lion) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700530 int portCount = services.device().getPorts(deviceId).size();
Thomas Vachuska329af532015-03-10 02:08:33 -0700531 int flowCount = getFlowCount(deviceId);
cheng fan35dc0f22015-06-10 06:02:47 +0800532 int tunnelCount = getTunnelCount(deviceId);
Simon Huntb745ca62015-07-28 15:37:11 -0700533
Simon Hunta58d8942017-08-11 12:51:14 -0700534 pp.addProp(PORTS, lion.getSafe(PORTS), portCount)
535 .addProp(FLOWS, lion.getSafe(FLOWS), flowCount)
536 .addProp(TUNNELS, lion.getSafe(TUNNELS), tunnelCount);
537 }
Simon Huntb745ca62015-07-28 15:37:11 -0700538
Simon Hunta58d8942017-08-11 12:51:14 -0700539 private void addDeviceCoreButtons(PropertyPanel pp) {
540 pp.addButton(CoreButtons.SHOW_DEVICE_VIEW)
Simon Hunt1911fe42017-05-02 18:25:58 -0700541 .addButton(CoreButtons.SHOW_FLOW_VIEW)
542 .addButton(CoreButtons.SHOW_PORT_VIEW)
543 .addButton(CoreButtons.SHOW_GROUP_VIEW)
544 .addButton(CoreButtons.SHOW_METER_VIEW);
Thomas Vachuska329af532015-03-10 02:08:33 -0700545 }
546
547 protected int getFlowCount(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800548 return services.flow().getFlowRuleCount(deviceId);
Thomas Vachuska329af532015-03-10 02:08:33 -0700549 }
550
cheng fan35dc0f22015-06-10 06:02:47 +0800551 protected int getTunnelCount(DeviceId deviceId) {
552 int count = 0;
Simon Hunt1911fe42017-05-02 18:25:58 -0700553 Collection<Tunnel> tunnels = services.tunnel().queryAllTunnels();
cheng fan35dc0f22015-06-10 06:02:47 +0800554 for (Tunnel tunnel : tunnels) {
chengfanc553c952016-07-22 15:48:23 +0800555 //Only OpticalTunnelEndPoint has a device
556 if (!(tunnel.src() instanceof OpticalTunnelEndPoint) ||
557 !(tunnel.dst() instanceof OpticalTunnelEndPoint)) {
558 continue;
559 }
560
561 Optional<ElementId> srcElementId = ((OpticalTunnelEndPoint) tunnel.src()).elementId();
562 Optional<ElementId> dstElementId = ((OpticalTunnelEndPoint) tunnel.dst()).elementId();
563 if (!srcElementId.isPresent() || !dstElementId.isPresent()) {
564 continue;
565 }
566 DeviceId srcDeviceId = (DeviceId) srcElementId.get();
567 DeviceId dstDeviceId = (DeviceId) dstElementId.get();
568 if (srcDeviceId.equals(deviceId) || dstDeviceId.equals(deviceId)) {
cheng fan35dc0f22015-06-10 06:02:47 +0800569 count++;
570 }
571 }
572 return count;
573 }
574
Simon Hunt10618f62017-06-15 19:30:52 -0700575 private boolean useDefaultName(String annotName) {
576 return isNullOrEmpty(annotName) || DASH.equals(annotName);
577 }
578
579 private String nameForHost(Host host) {
580 String name = host.annotations().value(AnnotationKeys.NAME);
581 return useDefaultName(name) ? ip(host.ipAddresses()) : name;
582 }
583
Simon Hunta58d8942017-08-11 12:51:14 -0700584 private String glyphForHost(Annotations annot) {
585 String uiType = annot.value(AnnotationKeys.UI_TYPE);
586 return isNullOrEmpty(uiType) ? DEFAULT_HOST_GLYPH : uiType;
Thomas Vachuska329af532015-03-10 02:08:33 -0700587 }
588
Simon Hunta58d8942017-08-11 12:51:14 -0700589 // Generates a property panel model for a host details response
590 protected PropertyPanel hostDetails(HostId hostId) {
591 log.debug("generate prop panel data for host {}", hostId);
592 Host host = services.host().getHost(hostId);
593 Annotations annot = host.annotations();
594 String glyphId = glyphForHost(annot);
595 LionBundle lion = getLionBundle(LION_TOPO);
596
597 PropertyPanel pp = new PropertyPanel(nameForHost(host), glyphId)
598 .navPath(HOST_NAV_PATH)
599 .id(hostId.toString());
600 addHostBasicProps(pp, host, lion);
601 addLocationProps(pp, annot, lion);
602 return pp;
603 }
604
605 private void addHostBasicProps(PropertyPanel pp, Host host, LionBundle lion) {
606 pp.addProp(LPL_FRIENDLY, lion.getSafe(LPL_FRIENDLY), nameForHost(host))
607 .addProp(MAC, lion.getSafe(MAC), host.mac())
608 .addProp(IP, lion.getSafe(IP), host.ipAddresses(), "[\\[\\]]")
609 .addProp(VLAN, lion.getSafe(VLAN), displayVlan(host.vlan(), lion))
610 .addSeparator();
611 }
612
613 private String displayVlan(VlanId vlan, LionBundle lion) {
614 return VlanId.NONE.equals(vlan) ? lion.getSafe(VLAN_NONE) : vlan.toString();
615 }
616
617 // Generates a property panel model for a link details response (edge-link)
618 protected PropertyPanel edgeLinkDetails(HostId hid, ConnectPoint cp) {
619 log.debug("generate prop panel data for edgelink {} {}", hid, cp);
620 LionBundle lion = getLionBundle(LION_TOPO);
621 String title = lion.getSafe("title_edge_link");
622
623 PropertyPanel pp = new PropertyPanel(title, LINK_GLYPH);
624 addLinkHostProps(pp, hid, lion);
625 addLinkCpBProps(pp, cp, lion);
626 return pp;
627 }
628
629 // Generates a property panel model for a link details response (infra-link)
630 protected PropertyPanel infraLinkDetails(ConnectPoint cpA, ConnectPoint cpB) {
631 log.debug("generate prop panel data for infralink {} {}", cpA, cpB);
632 LionBundle lion = getLionBundle(LION_TOPO);
633 String title = lion.getSafe("title_infra_link");
634
635 PropertyPanel pp = new PropertyPanel(title, LINK_GLYPH);
636 addLinkCpAProps(pp, cpA, lion);
637 addLinkCpBProps(pp, cpB, lion);
638 addLinkBackingProps(pp, cpA, cpB, lion);
639 return pp;
640 }
641
642 private void addLinkHostProps(PropertyPanel pp, HostId hostId, LionBundle lion) {
643 Host host = services.host().getHost(hostId);
644
645 pp.addProp(LPL_A_TYPE, lion.getSafe(LPL_A_TYPE), lion.getSafe(HOST))
646 .addProp(LPL_A_ID, lion.getSafe(LPL_A_ID), hostId.toString())
647 .addProp(LPL_A_FRIENDLY, lion.getSafe(LPL_A_FRIENDLY), nameForHost(host))
648 .addSeparator();
649 }
650
651 private void addLinkCpAProps(PropertyPanel pp, ConnectPoint cp, LionBundle lion) {
652 DeviceId did = cp.deviceId();
653
654 pp.addProp(LPL_A_TYPE, lion.getSafe(LPL_A_TYPE), lion.getSafe(DEVICE))
655 .addProp(LPL_A_ID, lion.getSafe(LPL_A_ID), did.toString())
656 .addProp(LPL_A_FRIENDLY, lion.getSafe(LPL_A_FRIENDLY), friendlyDevice(did))
657 .addProp(LPL_A_PORT, lion.getSafe(LPL_A_PORT), cp.port().toLong())
658 .addSeparator();
659 }
660
661 private void addLinkCpBProps(PropertyPanel pp, ConnectPoint cp, LionBundle lion) {
662 DeviceId did = cp.deviceId();
663
664 pp.addProp(LPL_B_TYPE, lion.getSafe(LPL_B_TYPE), lion.getSafe(DEVICE))
665 .addProp(LPL_B_ID, lion.getSafe(LPL_B_ID), did.toString())
666 .addProp(LPL_B_FRIENDLY, lion.getSafe(LPL_B_FRIENDLY), friendlyDevice(did))
667 .addProp(LPL_B_PORT, lion.getSafe(LPL_B_PORT), cp.port().toLong())
668 .addSeparator();
669 }
670
671 private void addLinkBackingProps(PropertyPanel pp, ConnectPoint cpA,
672 ConnectPoint cpB, LionBundle lion) {
673 Link a2b = services.link().getLink(cpA, cpB);
674 Link b2a = services.link().getLink(cpB, cpA);
675
676 pp.addProp(LPL_A2B, lion.getSafe(LPL_A2B), linkPropString(a2b, lion))
677 .addProp(LPL_B2A, lion.getSafe(LPL_B2A), linkPropString(b2a, lion));
678 }
679
680 private String linkPropString(Link link, LionBundle lion) {
681 if (link == null) {
682 return lion.getSafe(LPV_NO_LINK);
683 }
684 return lion.getSafe(link.type()) + SLASH +
685 lion.getSafe(link.state()) + SLASH +
686 lion.getSafe(link.isExpected() ? EXPECTED : NOT_EXPECTED);
687 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700688}