blob: 83a23ffd3fa5ef197c1f6fd7d8989db7f15c4924 [file] [log] [blame]
Thomas Vachuska329af532015-03-10 02:08:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
21import org.onlab.osgi.ServiceDirectory;
22import org.onlab.packet.IpAddress;
Simon Hunt95d56fd2015-11-12 11:06:44 -080023import org.onlab.util.DefaultHashMap;
Thomas Vachuska329af532015-03-10 02:08:33 -070024import org.onosproject.cluster.ClusterEvent;
Thomas Vachuska329af532015-03-10 02:08:33 -070025import org.onosproject.cluster.ControllerNode;
26import org.onosproject.cluster.NodeId;
27import org.onosproject.core.CoreService;
cheng fan35dc0f22015-06-10 06:02:47 +080028import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
29import org.onosproject.incubator.net.tunnel.Tunnel;
Thomas Vachuska329af532015-03-10 02:08:33 -070030import org.onosproject.net.Annotated;
31import org.onosproject.net.AnnotationKeys;
32import org.onosproject.net.Annotations;
33import org.onosproject.net.ConnectPoint;
34import org.onosproject.net.DefaultEdgeLink;
35import org.onosproject.net.Device;
36import org.onosproject.net.DeviceId;
37import org.onosproject.net.EdgeLink;
Simon Huntf4fd2a22016-08-10 15:41:09 -070038import org.onosproject.net.ElementId;
Thomas Vachuska329af532015-03-10 02:08:33 -070039import org.onosproject.net.Host;
40import org.onosproject.net.HostId;
41import org.onosproject.net.HostLocation;
42import org.onosproject.net.Link;
Thomas Vachuska329af532015-03-10 02:08:33 -070043import org.onosproject.net.device.DeviceEvent;
Thomas Vachuska329af532015-03-10 02:08:33 -070044import org.onosproject.net.flow.FlowEntry;
Thomas Vachuska329af532015-03-10 02:08:33 -070045import org.onosproject.net.host.HostEvent;
Thomas Vachuska329af532015-03-10 02:08:33 -070046import org.onosproject.net.link.LinkEvent;
Thomas Vachuska329af532015-03-10 02:08:33 -070047import org.onosproject.net.provider.ProviderId;
Thomas Vachuska329af532015-03-10 02:08:33 -070048import org.onosproject.net.topology.Topology;
Simon Huntd2747a02015-04-30 22:41:16 -070049import org.onosproject.ui.JsonUtils;
Thomas Vachuska329af532015-03-10 02:08:33 -070050import org.onosproject.ui.UiConnection;
Simon Hunta0ddb022015-05-01 09:53:01 -070051import org.onosproject.ui.UiMessageHandler;
Simon Hunted804d52016-03-30 09:51:40 -070052import org.onosproject.ui.impl.topo.util.ServicesBundle;
Simon Hunt0af1ec32015-07-24 12:17:55 -070053import org.onosproject.ui.topo.PropertyPanel;
Thomas Vachuska329af532015-03-10 02:08:33 -070054import org.slf4j.Logger;
55import org.slf4j.LoggerFactory;
56
Thomas Vachuska329af532015-03-10 02:08:33 -070057import java.util.Collection;
58import java.util.Collections;
Thomas Vachuska329af532015-03-10 02:08:33 -070059import java.util.Iterator;
Thomas Vachuska329af532015-03-10 02:08:33 -070060import java.util.Map;
chengfanc553c952016-07-22 15:48:23 +080061import java.util.Optional;
Simon Huntf4fd2a22016-08-10 15:41:09 -070062import java.util.Set;
Thomas Vachuska329af532015-03-10 02:08:33 -070063import java.util.concurrent.ConcurrentHashMap;
64
Thomas Vachuska329af532015-03-10 02:08:33 -070065import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuska329af532015-03-10 02:08:33 -070066import static org.onosproject.net.PortNumber.portNumber;
Simon Hunt3a0598f2015-08-04 19:59:04 -070067import static org.onosproject.ui.topo.TopoConstants.CoreButtons;
68import static org.onosproject.ui.topo.TopoConstants.Properties;
Simon Huntd3ceffa2015-08-25 12:44:35 -070069import static org.onosproject.ui.topo.TopoUtils.compactLinkString;
Thomas Vachuska329af532015-03-10 02:08:33 -070070
71/**
72 * Facility for creating messages bound for the topology viewer.
73 */
Simon Hunta0ddb022015-05-01 09:53:01 -070074public abstract class TopologyViewMessageHandlerBase extends UiMessageHandler {
Thomas Vachuska329af532015-03-10 02:08:33 -070075
Simon Huntf4fd2a22016-08-10 15:41:09 -070076 private static final String NO_GEO_VALUE = "0.0";
77
Simon Hunt95d56fd2015-11-12 11:06:44 -080078 // default to an "add" event...
79 private static final DefaultHashMap<ClusterEvent.Type, String> CLUSTER_EVENT =
80 new DefaultHashMap<>("addInstance");
81
82 // default to an "update" event...
83 private static final DefaultHashMap<DeviceEvent.Type, String> DEVICE_EVENT =
84 new DefaultHashMap<>("updateDevice");
85 private static final DefaultHashMap<LinkEvent.Type, String> LINK_EVENT =
86 new DefaultHashMap<>("updateLink");
87 private static final DefaultHashMap<HostEvent.Type, String> HOST_EVENT =
88 new DefaultHashMap<>("updateHost");
89
90 // but call out specific events that we care to differentiate...
91 static {
92 CLUSTER_EVENT.put(ClusterEvent.Type.INSTANCE_REMOVED, "removeInstance");
93
94 DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_ADDED, "addDevice");
95 DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_REMOVED, "removeDevice");
96
97 LINK_EVENT.put(LinkEvent.Type.LINK_ADDED, "addLink");
98 LINK_EVENT.put(LinkEvent.Type.LINK_REMOVED, "removeLink");
99
100 HOST_EVENT.put(HostEvent.Type.HOST_ADDED, "addHost");
101 HOST_EVENT.put(HostEvent.Type.HOST_REMOVED, "removeHost");
102 HOST_EVENT.put(HostEvent.Type.HOST_MOVED, "moveHost");
103 }
104
Simon Huntd2747a02015-04-30 22:41:16 -0700105 protected static final Logger log =
106 LoggerFactory.getLogger(TopologyViewMessageHandlerBase.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700107
Simon Huntd2747a02015-04-30 22:41:16 -0700108 private static final ProviderId PID =
109 new ProviderId("core", "org.onosproject.core", true);
Thomas Vachuska329af532015-03-10 02:08:33 -0700110
Thomas Vachuska329af532015-03-10 02:08:33 -0700111 // TODO: extract into an external & durable state; good enough for now and demo
112 private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>();
113
114 /**
Thomas Vachuska329af532015-03-10 02:08:33 -0700115 * Returns read-only view of the meta-ui information.
116 *
117 * @return map of id to meta-ui mementos
118 */
119 static Map<String, ObjectNode> getMetaUi() {
120 return Collections.unmodifiableMap(metaUi);
121 }
122
Simon Hunt1911fe42017-05-02 18:25:58 -0700123
124 protected ServicesBundle services;
125
126 private String version;
127
128
Thomas Vachuska329af532015-03-10 02:08:33 -0700129 @Override
130 public void init(UiConnection connection, ServiceDirectory directory) {
131 super.init(connection, directory);
Simon Hunt1911fe42017-05-02 18:25:58 -0700132 services = new ServicesBundle(directory);
133 setVersionString(directory);
134 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700135
Simon Hunt1911fe42017-05-02 18:25:58 -0700136 // Creates a palatable version string to display on the summary panel
137 private void setVersionString(ServiceDirectory directory) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700138 String ver = directory.get(CoreService.class).version().toString();
139 version = ver.replace(".SNAPSHOT", "*").replaceFirst("~.*$", "");
140 }
141
Thomas Vachuska329af532015-03-10 02:08:33 -0700142 // Returns the specified set of IP addresses as a string.
143 private String ip(Set<IpAddress> ipAddresses) {
144 Iterator<IpAddress> it = ipAddresses.iterator();
145 return it.hasNext() ? it.next().toString() : "unknown";
146 }
147
148 // Produces JSON structure from annotations.
149 private JsonNode props(Annotations annotations) {
Simon Huntda580882015-05-12 20:58:18 -0700150 ObjectNode props = objectNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700151 if (annotations != null) {
152 for (String key : annotations.keys()) {
153 props.put(key, annotations.value(key));
154 }
155 }
156 return props;
157 }
158
159 // Produces an informational log message event bound to the client.
Simon Hunt36740d02017-06-07 11:25:51 -0700160 protected ObjectNode info(String message) {
161 return message("info", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700162 }
163
164 // Produces a warning log message event bound to the client.
Simon Hunt36740d02017-06-07 11:25:51 -0700165 protected ObjectNode warning(String message) {
166 return message("warning", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700167 }
168
169 // Produces an error log message event bound to the client.
Simon Hunt36740d02017-06-07 11:25:51 -0700170 protected ObjectNode error(String message) {
171 return message("error", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700172 }
173
174 // Produces a log message event bound to the client.
Simon Hunt36740d02017-06-07 11:25:51 -0700175 private ObjectNode message(String severity, String message) {
Simon Huntda580882015-05-12 20:58:18 -0700176 ObjectNode payload = objectNode()
Simon Huntd2747a02015-04-30 22:41:16 -0700177 .put("severity", severity)
178 .put("message", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700179
Simon Hunt36740d02017-06-07 11:25:51 -0700180 return JsonUtils.envelope("message", payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700181 }
182
Thomas Vachuska329af532015-03-10 02:08:33 -0700183 // Produces a cluster instance message to the client.
Simon Hunt95d56fd2015-11-12 11:06:44 -0800184 protected ObjectNode instanceMessage(ClusterEvent event, String msgType) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700185 ControllerNode node = event.subject();
Simon Hunt1911fe42017-05-02 18:25:58 -0700186 int switchCount = services.mastership().getDevicesOf(node.id()).size();
Simon Huntda580882015-05-12 20:58:18 -0700187 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700188 .put("id", node.id().toString())
189 .put("ip", node.ip().toString())
Simon Hunt1911fe42017-05-02 18:25:58 -0700190 .put("online", services.cluster().getState(node.id()).isActive())
191 .put("ready", services.cluster().getState(node.id()).isReady())
192 .put("uiAttached", node.equals(services.cluster().getLocalNode()))
Thomas Vachuska329af532015-03-10 02:08:33 -0700193 .put("switches", switchCount);
194
Simon Huntda580882015-05-12 20:58:18 -0700195 ArrayNode labels = arrayNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700196 labels.add(node.id().toString());
197 labels.add(node.ip().toString());
198
199 // Add labels, props and stuff the payload into envelope.
200 payload.set("labels", labels);
201 addMetaUi(node.id().toString(), payload);
202
Simon Hunt95d56fd2015-11-12 11:06:44 -0800203 String type = msgType != null ? msgType : CLUSTER_EVENT.get(event.type());
Simon Hunt36740d02017-06-07 11:25:51 -0700204 return JsonUtils.envelope(type, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700205 }
206
207 // Produces a device event message to the client.
208 protected ObjectNode deviceMessage(DeviceEvent event) {
209 Device device = event.subject();
Simon Hunt1e20dae2016-10-28 11:26:26 -0700210 String uiType = device.annotations().value(AnnotationKeys.UI_TYPE);
211 String devType = uiType != null ? uiType :
212 device.type().toString().toLowerCase();
213
Simon Huntda580882015-05-12 20:58:18 -0700214 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700215 .put("id", device.id().toString())
Simon Hunt1e20dae2016-10-28 11:26:26 -0700216 .put("type", devType)
Simon Hunt1911fe42017-05-02 18:25:58 -0700217 .put("online", services.device().isAvailable(device.id()))
Thomas Vachuska329af532015-03-10 02:08:33 -0700218 .put("master", master(device.id()));
219
220 // Generate labels: id, chassis id, no-label, optional-name
221 String name = device.annotations().value(AnnotationKeys.NAME);
Simon Huntda580882015-05-12 20:58:18 -0700222 ArrayNode labels = arrayNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700223 labels.add("");
224 labels.add(isNullOrEmpty(name) ? device.id().toString() : name);
225 labels.add(device.id().toString());
226
227 // Add labels, props and stuff the payload into envelope.
228 payload.set("labels", labels);
229 payload.set("props", props(device.annotations()));
230 addGeoLocation(device, payload);
231 addMetaUi(device.id().toString(), payload);
232
Simon Hunt95d56fd2015-11-12 11:06:44 -0800233 String type = DEVICE_EVENT.get(event.type());
Simon Hunt36740d02017-06-07 11:25:51 -0700234 return JsonUtils.envelope(type, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700235 }
236
237 // Produces a link event message to the client.
238 protected ObjectNode linkMessage(LinkEvent event) {
239 Link link = event.subject();
Simon Huntda580882015-05-12 20:58:18 -0700240 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700241 .put("id", compactLinkString(link))
242 .put("type", link.type().toString().toLowerCase())
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800243 .put("expected", link.isExpected())
Thomas Vachuska329af532015-03-10 02:08:33 -0700244 .put("online", link.state() == Link.State.ACTIVE)
245 .put("linkWidth", 1.2)
246 .put("src", link.src().deviceId().toString())
247 .put("srcPort", link.src().port().toString())
248 .put("dst", link.dst().deviceId().toString())
249 .put("dstPort", link.dst().port().toString());
Simon Hunt95d56fd2015-11-12 11:06:44 -0800250 String type = LINK_EVENT.get(event.type());
Simon Hunt36740d02017-06-07 11:25:51 -0700251 return JsonUtils.envelope(type, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700252 }
253
254 // Produces a host event message to the client.
255 protected ObjectNode hostMessage(HostEvent event) {
256 Host host = event.subject();
Charles Chan33f28a92015-11-13 13:12:38 -0800257 Host prevHost = event.prevSubject();
Simon Hunt1e20dae2016-10-28 11:26:26 -0700258 String hostType = host.annotations().value(AnnotationKeys.UI_TYPE);
Simon Hunt95d56fd2015-11-12 11:06:44 -0800259
Simon Huntda580882015-05-12 20:58:18 -0700260 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700261 .put("id", host.id().toString())
262 .put("type", isNullOrEmpty(hostType) ? "endstation" : hostType)
263 .put("ingress", compactLinkString(edgeLink(host, true)))
264 .put("egress", compactLinkString(edgeLink(host, false)));
Simon Huntda580882015-05-12 20:58:18 -0700265 payload.set("cp", hostConnect(host.location()));
Charles Chan33f28a92015-11-13 13:12:38 -0800266 if (prevHost != null && prevHost.location() != null) {
267 payload.set("prevCp", hostConnect(prevHost.location()));
Simon Hunt95d56fd2015-11-12 11:06:44 -0800268 }
Simon Huntda580882015-05-12 20:58:18 -0700269 payload.set("labels", labels(ip(host.ipAddresses()),
Thomas Vachuska329af532015-03-10 02:08:33 -0700270 host.mac().toString()));
271 payload.set("props", props(host.annotations()));
272 addGeoLocation(host, payload);
273 addMetaUi(host.id().toString(), payload);
274
Simon Hunt95d56fd2015-11-12 11:06:44 -0800275 String type = HOST_EVENT.get(event.type());
Simon Hunt36740d02017-06-07 11:25:51 -0700276 return JsonUtils.envelope(type, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700277 }
278
279 // Encodes the specified host location into a JSON object.
Simon Huntda580882015-05-12 20:58:18 -0700280 private ObjectNode hostConnect(HostLocation location) {
281 return objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700282 .put("device", location.deviceId().toString())
283 .put("port", location.port().toLong());
284 }
285
286 // Encodes the specified list of labels a JSON array.
Simon Huntda580882015-05-12 20:58:18 -0700287 private ArrayNode labels(String... labels) {
288 ArrayNode json = arrayNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700289 for (String label : labels) {
290 json.add(label);
291 }
292 return json;
293 }
294
295 // Returns the name of the master node for the specified device id.
296 private String master(DeviceId deviceId) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700297 NodeId master = services.mastership().getMasterFor(deviceId);
Thomas Vachuska329af532015-03-10 02:08:33 -0700298 return master != null ? master.toString() : "";
299 }
300
301 // Generates an edge link from the specified host location.
302 private EdgeLink edgeLink(Host host, boolean ingress) {
303 return new DefaultEdgeLink(PID, new ConnectPoint(host.id(), portNumber(0)),
304 host.location(), ingress);
305 }
306
307 // Adds meta UI information for the specified object.
308 private void addMetaUi(String id, ObjectNode payload) {
309 ObjectNode meta = metaUi.get(id);
310 if (meta != null) {
311 payload.set("metaUi", meta);
312 }
313 }
314
315 // Adds a geo location JSON to the specified payload object.
316 private void addGeoLocation(Annotated annotated, ObjectNode payload) {
317 Annotations annotations = annotated.annotations();
318 if (annotations == null) {
319 return;
320 }
321
Simon Huntfd7106c2016-02-09 15:05:26 -0800322 String slat = annotations.value(AnnotationKeys.LATITUDE);
Simon Huntf27a9292017-05-04 17:36:26 -0700323 String slng = annotations.value(AnnotationKeys.LONGITUDE);
Simon Huntf4fd2a22016-08-10 15:41:09 -0700324 boolean validLat = slat != null && !slat.equals(NO_GEO_VALUE);
Simon Huntf27a9292017-05-04 17:36:26 -0700325 boolean validLng = slng != null && !slng.equals(NO_GEO_VALUE);
Simon Huntf4fd2a22016-08-10 15:41:09 -0700326 if (validLat && validLng) {
327 try {
Simon Huntfd7106c2016-02-09 15:05:26 -0800328 double lat = Double.parseDouble(slat);
Simon Huntf27a9292017-05-04 17:36:26 -0700329 double lng = Double.parseDouble(slng);
Simon Huntda580882015-05-12 20:58:18 -0700330 ObjectNode loc = objectNode()
Simon Huntf27a9292017-05-04 17:36:26 -0700331 .put("locType", "geo")
332 .put("latOrY", lat)
333 .put("longOrX", lng);
Thomas Vachuska329af532015-03-10 02:08:33 -0700334 payload.set("location", loc);
Simon Huntf4fd2a22016-08-10 15:41:09 -0700335 } catch (NumberFormatException e) {
Simon Huntf27a9292017-05-04 17:36:26 -0700336 log.warn("Invalid geo data: latitude={}, longitude={}", slat, slng);
Thomas Vachuska329af532015-03-10 02:08:33 -0700337 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700338 }
339 }
340
341 // Updates meta UI information for the specified object.
Simon Huntd2747a02015-04-30 22:41:16 -0700342 protected void updateMetaUi(ObjectNode payload) {
343 metaUi.put(JsonUtils.string(payload, "id"),
344 JsonUtils.node(payload, "memento"));
Thomas Vachuska329af532015-03-10 02:08:33 -0700345 }
346
Simon Hunta17fa672015-08-19 18:42:22 -0700347
Simon Huntb745ca62015-07-28 15:37:11 -0700348 // -----------------------------------------------------------------------
349 // Create models of the data to return, that overlays can adjust / augment
350
351 // Returns property panel model for summary response.
Simon Hunt8a0429a2017-01-06 16:52:47 -0800352 protected PropertyPanel summmaryMessage() {
Simon Hunt1911fe42017-05-02 18:25:58 -0700353 Topology topology = services.topology().currentTopology();
Simon Hunt0af1ec32015-07-24 12:17:55 -0700354
Simon Hunt00a27ff2015-07-28 18:53:40 -0700355 return new PropertyPanel("ONOS Summary", "node")
Simon Hunt1911fe42017-05-02 18:25:58 -0700356 .addProp(Properties.VERSION, version)
357 .addSeparator()
358 .addProp(Properties.DEVICES, services.device().getDeviceCount())
359 .addProp(Properties.LINKS, topology.linkCount())
360 .addProp(Properties.HOSTS, services.host().getHostCount())
361 .addProp(Properties.TOPOLOGY_SSCS, topology.clusterCount())
362 .addSeparator()
363 .addProp(Properties.INTENTS, services.intent().getIntentCount())
364 .addProp(Properties.TUNNELS, services.tunnel().tunnelCount())
365 .addProp(Properties.FLOWS, services.flow().getFlowRuleCount());
Thomas Vachuska329af532015-03-10 02:08:33 -0700366 }
367
Simon Huntb745ca62015-07-28 15:37:11 -0700368 // Returns property panel model for device details response.
Simon Hunt8a0429a2017-01-06 16:52:47 -0800369 protected PropertyPanel deviceDetails(DeviceId deviceId) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700370 Device device = services.device().getDevice(deviceId);
Thomas Vachuska329af532015-03-10 02:08:33 -0700371 Annotations annot = device.annotations();
372 String name = annot.value(AnnotationKeys.NAME);
Simon Hunt1911fe42017-05-02 18:25:58 -0700373 int portCount = services.device().getPorts(deviceId).size();
Thomas Vachuska329af532015-03-10 02:08:33 -0700374 int flowCount = getFlowCount(deviceId);
cheng fan35dc0f22015-06-10 06:02:47 +0800375 int tunnelCount = getTunnelCount(deviceId);
Simon Huntb745ca62015-07-28 15:37:11 -0700376
377 String title = isNullOrEmpty(name) ? deviceId.toString() : name;
378 String typeId = device.type().toString().toLowerCase();
379
Simon Hunt1911fe42017-05-02 18:25:58 -0700380 return new PropertyPanel(title, typeId)
381 .id(deviceId.toString())
Simon Hunt3a0598f2015-08-04 19:59:04 -0700382
Simon Hunt1911fe42017-05-02 18:25:58 -0700383 .addProp(Properties.URI, deviceId.toString())
384 .addProp(Properties.VENDOR, device.manufacturer())
385 .addProp(Properties.HW_VERSION, device.hwVersion())
386 .addProp(Properties.SW_VERSION, device.swVersion())
387 .addProp(Properties.SERIAL_NUMBER, device.serialNumber())
388 .addProp(Properties.PROTOCOL, annot.value(AnnotationKeys.PROTOCOL))
389 .addSeparator()
Simon Hunt3a0598f2015-08-04 19:59:04 -0700390
Simon Hunt1911fe42017-05-02 18:25:58 -0700391 .addProp(Properties.LATITUDE, annot.value(AnnotationKeys.LATITUDE))
392 .addProp(Properties.LONGITUDE, annot.value(AnnotationKeys.LONGITUDE))
393 .addSeparator()
Simon Hunt3a0598f2015-08-04 19:59:04 -0700394
Simon Hunt1911fe42017-05-02 18:25:58 -0700395 .addProp(Properties.PORTS, portCount)
396 .addProp(Properties.FLOWS, flowCount)
397 .addProp(Properties.TUNNELS, tunnelCount)
Simon Hunt00a27ff2015-07-28 18:53:40 -0700398
Simon Hunt1911fe42017-05-02 18:25:58 -0700399 .addButton(CoreButtons.SHOW_DEVICE_VIEW)
400 .addButton(CoreButtons.SHOW_FLOW_VIEW)
401 .addButton(CoreButtons.SHOW_PORT_VIEW)
402 .addButton(CoreButtons.SHOW_GROUP_VIEW)
403 .addButton(CoreButtons.SHOW_METER_VIEW);
Thomas Vachuska329af532015-03-10 02:08:33 -0700404 }
405
406 protected int getFlowCount(DeviceId deviceId) {
407 int count = 0;
Simon Hunt1911fe42017-05-02 18:25:58 -0700408 for (FlowEntry flowEntry : services.flow().getFlowEntries(deviceId)) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700409 count++;
Thomas Vachuska329af532015-03-10 02:08:33 -0700410 }
411 return count;
412 }
413
cheng fan35dc0f22015-06-10 06:02:47 +0800414 protected int getTunnelCount(DeviceId deviceId) {
415 int count = 0;
Simon Hunt1911fe42017-05-02 18:25:58 -0700416 Collection<Tunnel> tunnels = services.tunnel().queryAllTunnels();
cheng fan35dc0f22015-06-10 06:02:47 +0800417 for (Tunnel tunnel : tunnels) {
chengfanc553c952016-07-22 15:48:23 +0800418 //Only OpticalTunnelEndPoint has a device
419 if (!(tunnel.src() instanceof OpticalTunnelEndPoint) ||
420 !(tunnel.dst() instanceof OpticalTunnelEndPoint)) {
421 continue;
422 }
423
424 Optional<ElementId> srcElementId = ((OpticalTunnelEndPoint) tunnel.src()).elementId();
425 Optional<ElementId> dstElementId = ((OpticalTunnelEndPoint) tunnel.dst()).elementId();
426 if (!srcElementId.isPresent() || !dstElementId.isPresent()) {
427 continue;
428 }
429 DeviceId srcDeviceId = (DeviceId) srcElementId.get();
430 DeviceId dstDeviceId = (DeviceId) dstElementId.get();
431 if (srcDeviceId.equals(deviceId) || dstDeviceId.equals(deviceId)) {
cheng fan35dc0f22015-06-10 06:02:47 +0800432 count++;
433 }
434 }
435 return count;
436 }
437
Thomas Vachuska329af532015-03-10 02:08:33 -0700438 // Returns host details response.
Simon Hunt8a0429a2017-01-06 16:52:47 -0800439 protected PropertyPanel hostDetails(HostId hostId) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700440 Host host = services.host().getHost(hostId);
Thomas Vachuska329af532015-03-10 02:08:33 -0700441 Annotations annot = host.annotations();
442 String type = annot.value(AnnotationKeys.TYPE);
443 String name = annot.value(AnnotationKeys.NAME);
444 String vlan = host.vlan().toString();
Simon Huntb745ca62015-07-28 15:37:11 -0700445
446 String title = isNullOrEmpty(name) ? hostId.toString() : name;
447 String typeId = isNullOrEmpty(type) ? "endstation" : type;
448
Simon Hunt1911fe42017-05-02 18:25:58 -0700449 return new PropertyPanel(title, typeId)
450 .id(hostId.toString())
451 .addProp(Properties.MAC, host.mac())
452 .addProp(Properties.IP, host.ipAddresses(), "[\\[\\]]")
453 .addProp(Properties.VLAN, "-1".equals(vlan) ? "none" : vlan)
454 .addSeparator()
455 .addProp(Properties.LATITUDE, annot.value(AnnotationKeys.LATITUDE))
456 .addProp(Properties.LONGITUDE, annot.value(AnnotationKeys.LONGITUDE));
Thomas Vachuska329af532015-03-10 02:08:33 -0700457 }
458
Thomas Vachuska329af532015-03-10 02:08:33 -0700459}