blob: 985cb17e13ddb613060fefe2c758eccb557b6fc5 [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;
25import org.onosproject.cluster.ClusterService;
26import org.onosproject.cluster.ControllerNode;
27import org.onosproject.cluster.NodeId;
28import org.onosproject.core.CoreService;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070029import org.onosproject.incubator.net.PortStatisticsService;
cheng fan35dc0f22015-06-10 06:02:47 +080030import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
31import org.onosproject.incubator.net.tunnel.Tunnel;
32import org.onosproject.incubator.net.tunnel.TunnelService;
Thomas Vachuska329af532015-03-10 02:08:33 -070033import org.onosproject.mastership.MastershipService;
34import org.onosproject.net.Annotated;
35import org.onosproject.net.AnnotationKeys;
36import org.onosproject.net.Annotations;
37import org.onosproject.net.ConnectPoint;
38import org.onosproject.net.DefaultEdgeLink;
39import org.onosproject.net.Device;
40import org.onosproject.net.DeviceId;
41import org.onosproject.net.EdgeLink;
Simon Huntf4fd2a22016-08-10 15:41:09 -070042import org.onosproject.net.ElementId;
Thomas Vachuska329af532015-03-10 02:08:33 -070043import org.onosproject.net.Host;
44import org.onosproject.net.HostId;
45import org.onosproject.net.HostLocation;
46import org.onosproject.net.Link;
Thomas Vachuska329af532015-03-10 02:08:33 -070047import org.onosproject.net.PortNumber;
48import org.onosproject.net.device.DeviceEvent;
49import org.onosproject.net.device.DeviceService;
50import org.onosproject.net.flow.FlowEntry;
51import org.onosproject.net.flow.FlowRuleService;
52import org.onosproject.net.flow.TrafficTreatment;
53import org.onosproject.net.flow.instructions.Instruction;
54import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
55import org.onosproject.net.host.HostEvent;
56import org.onosproject.net.host.HostService;
Thomas Vachuska329af532015-03-10 02:08:33 -070057import org.onosproject.net.intent.IntentService;
Thomas Vachuska329af532015-03-10 02:08:33 -070058import org.onosproject.net.link.LinkEvent;
59import org.onosproject.net.link.LinkService;
60import org.onosproject.net.provider.ProviderId;
Thomas Vachuska329af532015-03-10 02:08:33 -070061import org.onosproject.net.statistic.StatisticService;
62import org.onosproject.net.topology.Topology;
63import org.onosproject.net.topology.TopologyService;
Simon Huntd2747a02015-04-30 22:41:16 -070064import org.onosproject.ui.JsonUtils;
Thomas Vachuska329af532015-03-10 02:08:33 -070065import org.onosproject.ui.UiConnection;
Simon Hunta0ddb022015-05-01 09:53:01 -070066import org.onosproject.ui.UiMessageHandler;
Simon Hunted804d52016-03-30 09:51:40 -070067import org.onosproject.ui.impl.topo.util.ServicesBundle;
Simon Hunt0af1ec32015-07-24 12:17:55 -070068import org.onosproject.ui.topo.PropertyPanel;
Thomas Vachuska329af532015-03-10 02:08:33 -070069import org.slf4j.Logger;
70import org.slf4j.LoggerFactory;
71
Thomas Vachuska329af532015-03-10 02:08:33 -070072import java.util.ArrayList;
73import java.util.Collection;
74import java.util.Collections;
75import java.util.HashMap;
76import java.util.HashSet;
77import java.util.Iterator;
78import java.util.List;
79import java.util.Map;
chengfanc553c952016-07-22 15:48:23 +080080import java.util.Optional;
Simon Huntf4fd2a22016-08-10 15:41:09 -070081import java.util.Set;
Thomas Vachuska329af532015-03-10 02:08:33 -070082import java.util.concurrent.ConcurrentHashMap;
83
84import static com.google.common.base.Preconditions.checkNotNull;
85import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuska204cb6c2015-06-04 00:03:06 -070086import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Thomas Vachuska329af532015-03-10 02:08:33 -070087import static org.onosproject.net.PortNumber.portNumber;
Simon Hunt3a0598f2015-08-04 19:59:04 -070088import static org.onosproject.ui.topo.TopoConstants.CoreButtons;
89import static org.onosproject.ui.topo.TopoConstants.Properties;
Simon Huntd3ceffa2015-08-25 12:44:35 -070090import static org.onosproject.ui.topo.TopoUtils.compactLinkString;
Thomas Vachuska329af532015-03-10 02:08:33 -070091
92/**
93 * Facility for creating messages bound for the topology viewer.
94 */
Simon Hunta0ddb022015-05-01 09:53:01 -070095public abstract class TopologyViewMessageHandlerBase extends UiMessageHandler {
Thomas Vachuska329af532015-03-10 02:08:33 -070096
Simon Huntf4fd2a22016-08-10 15:41:09 -070097 private static final String NO_GEO_VALUE = "0.0";
98
Simon Hunt95d56fd2015-11-12 11:06:44 -080099 // default to an "add" event...
100 private static final DefaultHashMap<ClusterEvent.Type, String> CLUSTER_EVENT =
101 new DefaultHashMap<>("addInstance");
102
103 // default to an "update" event...
104 private static final DefaultHashMap<DeviceEvent.Type, String> DEVICE_EVENT =
105 new DefaultHashMap<>("updateDevice");
106 private static final DefaultHashMap<LinkEvent.Type, String> LINK_EVENT =
107 new DefaultHashMap<>("updateLink");
108 private static final DefaultHashMap<HostEvent.Type, String> HOST_EVENT =
109 new DefaultHashMap<>("updateHost");
110
111 // but call out specific events that we care to differentiate...
112 static {
113 CLUSTER_EVENT.put(ClusterEvent.Type.INSTANCE_REMOVED, "removeInstance");
114
115 DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_ADDED, "addDevice");
116 DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_REMOVED, "removeDevice");
117
118 LINK_EVENT.put(LinkEvent.Type.LINK_ADDED, "addLink");
119 LINK_EVENT.put(LinkEvent.Type.LINK_REMOVED, "removeLink");
120
121 HOST_EVENT.put(HostEvent.Type.HOST_ADDED, "addHost");
122 HOST_EVENT.put(HostEvent.Type.HOST_REMOVED, "removeHost");
123 HOST_EVENT.put(HostEvent.Type.HOST_MOVED, "moveHost");
124 }
125
Simon Huntd2747a02015-04-30 22:41:16 -0700126 protected static final Logger log =
127 LoggerFactory.getLogger(TopologyViewMessageHandlerBase.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700128
Simon Huntd2747a02015-04-30 22:41:16 -0700129 private static final ProviderId PID =
130 new ProviderId("core", "org.onosproject.core", true);
Thomas Vachuska329af532015-03-10 02:08:33 -0700131
Simon Hunta17fa672015-08-19 18:42:22 -0700132 protected static final String SHOW_HIGHLIGHTS = "showHighlights";
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700133
Thomas Vachuska329af532015-03-10 02:08:33 -0700134 protected ServiceDirectory directory;
135 protected ClusterService clusterService;
136 protected DeviceService deviceService;
137 protected LinkService linkService;
138 protected HostService hostService;
139 protected MastershipService mastershipService;
140 protected IntentService intentService;
141 protected FlowRuleService flowService;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700142 protected StatisticService flowStatsService;
143 protected PortStatisticsService portStatsService;
Thomas Vachuska329af532015-03-10 02:08:33 -0700144 protected TopologyService topologyService;
cheng fan35dc0f22015-06-10 06:02:47 +0800145 protected TunnelService tunnelService;
Thomas Vachuska329af532015-03-10 02:08:33 -0700146
Simon Hunta17fa672015-08-19 18:42:22 -0700147 protected ServicesBundle servicesBundle;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700148
Thomas Vachuska329af532015-03-10 02:08:33 -0700149 private String version;
150
151 // TODO: extract into an external & durable state; good enough for now and demo
152 private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>();
153
154 /**
Thomas Vachuska329af532015-03-10 02:08:33 -0700155 * Returns read-only view of the meta-ui information.
156 *
157 * @return map of id to meta-ui mementos
158 */
159 static Map<String, ObjectNode> getMetaUi() {
160 return Collections.unmodifiableMap(metaUi);
161 }
162
163 @Override
164 public void init(UiConnection connection, ServiceDirectory directory) {
165 super.init(connection, directory);
166 this.directory = checkNotNull(directory, "Directory cannot be null");
167 clusterService = directory.get(ClusterService.class);
168 deviceService = directory.get(DeviceService.class);
169 linkService = directory.get(LinkService.class);
170 hostService = directory.get(HostService.class);
171 mastershipService = directory.get(MastershipService.class);
172 intentService = directory.get(IntentService.class);
173 flowService = directory.get(FlowRuleService.class);
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700174 flowStatsService = directory.get(StatisticService.class);
175 portStatsService = directory.get(PortStatisticsService.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700176 topologyService = directory.get(TopologyService.class);
cheng fan35dc0f22015-06-10 06:02:47 +0800177 tunnelService = directory.get(TunnelService.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700178
Simon Hunta17fa672015-08-19 18:42:22 -0700179 servicesBundle = new ServicesBundle(intentService, deviceService,
180 hostService, linkService,
181 flowService,
182 flowStatsService, portStatsService);
183
Thomas Vachuska329af532015-03-10 02:08:33 -0700184 String ver = directory.get(CoreService.class).version().toString();
185 version = ver.replace(".SNAPSHOT", "*").replaceFirst("~.*$", "");
186 }
187
Thomas Vachuska329af532015-03-10 02:08:33 -0700188 // Returns the specified set of IP addresses as a string.
189 private String ip(Set<IpAddress> ipAddresses) {
190 Iterator<IpAddress> it = ipAddresses.iterator();
191 return it.hasNext() ? it.next().toString() : "unknown";
192 }
193
194 // Produces JSON structure from annotations.
195 private JsonNode props(Annotations annotations) {
Simon Huntda580882015-05-12 20:58:18 -0700196 ObjectNode props = objectNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700197 if (annotations != null) {
198 for (String key : annotations.keys()) {
199 props.put(key, annotations.value(key));
200 }
201 }
202 return props;
203 }
204
205 // Produces an informational log message event bound to the client.
206 protected ObjectNode info(long id, String message) {
207 return message("info", id, message);
208 }
209
210 // Produces a warning log message event bound to the client.
211 protected ObjectNode warning(long id, String message) {
212 return message("warning", id, message);
213 }
214
215 // Produces an error log message event bound to the client.
216 protected ObjectNode error(long id, String message) {
217 return message("error", id, message);
218 }
219
220 // Produces a log message event bound to the client.
221 private ObjectNode message(String severity, long id, String message) {
Simon Huntda580882015-05-12 20:58:18 -0700222 ObjectNode payload = objectNode()
Simon Huntd2747a02015-04-30 22:41:16 -0700223 .put("severity", severity)
224 .put("message", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700225
Simon Huntd2747a02015-04-30 22:41:16 -0700226 return JsonUtils.envelope("message", id, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700227 }
228
Thomas Vachuska329af532015-03-10 02:08:33 -0700229 // Produces a cluster instance message to the client.
Simon Hunt95d56fd2015-11-12 11:06:44 -0800230 protected ObjectNode instanceMessage(ClusterEvent event, String msgType) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700231 ControllerNode node = event.subject();
232 int switchCount = mastershipService.getDevicesOf(node.id()).size();
Simon Huntda580882015-05-12 20:58:18 -0700233 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700234 .put("id", node.id().toString())
235 .put("ip", node.ip().toString())
Thomas Vachuska7a8de842016-03-07 20:56:35 -0800236 .put("online", clusterService.getState(node.id()).isActive())
Thomas Vachuskafba7f3d2016-03-23 15:46:25 -0700237 .put("ready", clusterService.getState(node.id()).isReady())
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700238 .put("uiAttached", node.equals(clusterService.getLocalNode()))
Thomas Vachuska329af532015-03-10 02:08:33 -0700239 .put("switches", switchCount);
240
Simon Huntda580882015-05-12 20:58:18 -0700241 ArrayNode labels = arrayNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700242 labels.add(node.id().toString());
243 labels.add(node.ip().toString());
244
245 // Add labels, props and stuff the payload into envelope.
246 payload.set("labels", labels);
247 addMetaUi(node.id().toString(), payload);
248
Simon Hunt95d56fd2015-11-12 11:06:44 -0800249 String type = msgType != null ? msgType : CLUSTER_EVENT.get(event.type());
Simon Huntd2747a02015-04-30 22:41:16 -0700250 return JsonUtils.envelope(type, 0, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700251 }
252
253 // Produces a device event message to the client.
254 protected ObjectNode deviceMessage(DeviceEvent event) {
255 Device device = event.subject();
Simon Huntda580882015-05-12 20:58:18 -0700256 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700257 .put("id", device.id().toString())
258 .put("type", device.type().toString().toLowerCase())
259 .put("online", deviceService.isAvailable(device.id()))
260 .put("master", master(device.id()));
261
262 // Generate labels: id, chassis id, no-label, optional-name
263 String name = device.annotations().value(AnnotationKeys.NAME);
Simon Huntda580882015-05-12 20:58:18 -0700264 ArrayNode labels = arrayNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700265 labels.add("");
266 labels.add(isNullOrEmpty(name) ? device.id().toString() : name);
267 labels.add(device.id().toString());
268
269 // Add labels, props and stuff the payload into envelope.
270 payload.set("labels", labels);
271 payload.set("props", props(device.annotations()));
272 addGeoLocation(device, payload);
273 addMetaUi(device.id().toString(), payload);
274
Simon Hunt95d56fd2015-11-12 11:06:44 -0800275 String type = DEVICE_EVENT.get(event.type());
Simon Huntd2747a02015-04-30 22:41:16 -0700276 return JsonUtils.envelope(type, 0, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700277 }
278
279 // Produces a link event message to the client.
280 protected ObjectNode linkMessage(LinkEvent event) {
281 Link link = event.subject();
Simon Huntda580882015-05-12 20:58:18 -0700282 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700283 .put("id", compactLinkString(link))
284 .put("type", link.type().toString().toLowerCase())
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800285 .put("expected", link.isExpected())
Thomas Vachuska329af532015-03-10 02:08:33 -0700286 .put("online", link.state() == Link.State.ACTIVE)
287 .put("linkWidth", 1.2)
288 .put("src", link.src().deviceId().toString())
289 .put("srcPort", link.src().port().toString())
290 .put("dst", link.dst().deviceId().toString())
291 .put("dstPort", link.dst().port().toString());
Simon Hunt95d56fd2015-11-12 11:06:44 -0800292 String type = LINK_EVENT.get(event.type());
Simon Huntd2747a02015-04-30 22:41:16 -0700293 return JsonUtils.envelope(type, 0, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700294 }
295
296 // Produces a host event message to the client.
297 protected ObjectNode hostMessage(HostEvent event) {
298 Host host = event.subject();
Charles Chan33f28a92015-11-13 13:12:38 -0800299 Host prevHost = event.prevSubject();
Thomas Vachuska329af532015-03-10 02:08:33 -0700300 String hostType = host.annotations().value(AnnotationKeys.TYPE);
Simon Hunt95d56fd2015-11-12 11:06:44 -0800301
Simon Huntda580882015-05-12 20:58:18 -0700302 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700303 .put("id", host.id().toString())
304 .put("type", isNullOrEmpty(hostType) ? "endstation" : hostType)
305 .put("ingress", compactLinkString(edgeLink(host, true)))
306 .put("egress", compactLinkString(edgeLink(host, false)));
Simon Huntda580882015-05-12 20:58:18 -0700307 payload.set("cp", hostConnect(host.location()));
Charles Chan33f28a92015-11-13 13:12:38 -0800308 if (prevHost != null && prevHost.location() != null) {
309 payload.set("prevCp", hostConnect(prevHost.location()));
Simon Hunt95d56fd2015-11-12 11:06:44 -0800310 }
Simon Huntda580882015-05-12 20:58:18 -0700311 payload.set("labels", labels(ip(host.ipAddresses()),
Thomas Vachuska329af532015-03-10 02:08:33 -0700312 host.mac().toString()));
313 payload.set("props", props(host.annotations()));
314 addGeoLocation(host, payload);
315 addMetaUi(host.id().toString(), payload);
316
Simon Hunt95d56fd2015-11-12 11:06:44 -0800317 String type = HOST_EVENT.get(event.type());
Simon Huntd2747a02015-04-30 22:41:16 -0700318 return JsonUtils.envelope(type, 0, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700319 }
320
321 // Encodes the specified host location into a JSON object.
Simon Huntda580882015-05-12 20:58:18 -0700322 private ObjectNode hostConnect(HostLocation location) {
323 return objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700324 .put("device", location.deviceId().toString())
325 .put("port", location.port().toLong());
326 }
327
328 // Encodes the specified list of labels a JSON array.
Simon Huntda580882015-05-12 20:58:18 -0700329 private ArrayNode labels(String... labels) {
330 ArrayNode json = arrayNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700331 for (String label : labels) {
332 json.add(label);
333 }
334 return json;
335 }
336
337 // Returns the name of the master node for the specified device id.
338 private String master(DeviceId deviceId) {
339 NodeId master = mastershipService.getMasterFor(deviceId);
340 return master != null ? master.toString() : "";
341 }
342
343 // Generates an edge link from the specified host location.
344 private EdgeLink edgeLink(Host host, boolean ingress) {
345 return new DefaultEdgeLink(PID, new ConnectPoint(host.id(), portNumber(0)),
346 host.location(), ingress);
347 }
348
349 // Adds meta UI information for the specified object.
350 private void addMetaUi(String id, ObjectNode payload) {
351 ObjectNode meta = metaUi.get(id);
352 if (meta != null) {
353 payload.set("metaUi", meta);
354 }
355 }
356
357 // Adds a geo location JSON to the specified payload object.
358 private void addGeoLocation(Annotated annotated, ObjectNode payload) {
359 Annotations annotations = annotated.annotations();
360 if (annotations == null) {
361 return;
362 }
363
Thomas Vachuska329af532015-03-10 02:08:33 -0700364 String slng = annotations.value(AnnotationKeys.LONGITUDE);
Simon Huntfd7106c2016-02-09 15:05:26 -0800365 String slat = annotations.value(AnnotationKeys.LATITUDE);
Simon Huntf4fd2a22016-08-10 15:41:09 -0700366 boolean validLng = slng != null && !slng.equals(NO_GEO_VALUE);
367 boolean validLat = slat != null && !slat.equals(NO_GEO_VALUE);
368 if (validLat && validLng) {
369 try {
Thomas Vachuska329af532015-03-10 02:08:33 -0700370 double lng = Double.parseDouble(slng);
Simon Huntfd7106c2016-02-09 15:05:26 -0800371 double lat = Double.parseDouble(slat);
Simon Huntda580882015-05-12 20:58:18 -0700372 ObjectNode loc = objectNode()
Simon Huntfd7106c2016-02-09 15:05:26 -0800373 .put("type", "lnglat")
374 .put("lng", lng)
375 .put("lat", lat);
Thomas Vachuska329af532015-03-10 02:08:33 -0700376 payload.set("location", loc);
Simon Huntf4fd2a22016-08-10 15:41:09 -0700377 } catch (NumberFormatException e) {
378 log.warn("Invalid geo data: longitude={}, latitude={}", slng, slat);
Thomas Vachuska329af532015-03-10 02:08:33 -0700379 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700380 }
381 }
382
383 // Updates meta UI information for the specified object.
Simon Huntd2747a02015-04-30 22:41:16 -0700384 protected void updateMetaUi(ObjectNode payload) {
385 metaUi.put(JsonUtils.string(payload, "id"),
386 JsonUtils.node(payload, "memento"));
Thomas Vachuska329af532015-03-10 02:08:33 -0700387 }
388
Simon Hunta17fa672015-08-19 18:42:22 -0700389
Simon Huntb745ca62015-07-28 15:37:11 -0700390 // -----------------------------------------------------------------------
391 // Create models of the data to return, that overlays can adjust / augment
392
393 // Returns property panel model for summary response.
Simon Hunt0af1ec32015-07-24 12:17:55 -0700394 protected PropertyPanel summmaryMessage(long sid) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700395 Topology topology = topologyService.currentTopology();
Simon Hunt0af1ec32015-07-24 12:17:55 -0700396
Simon Hunt00a27ff2015-07-28 18:53:40 -0700397 return new PropertyPanel("ONOS Summary", "node")
Thomas Vachuskae50b6212015-12-02 08:00:09 -0800398 .addProp(Properties.VERSION, version)
399 .addSeparator()
chengfanc553c952016-07-22 15:48:23 +0800400 .addProp(Properties.DEVICES, deviceService.getDeviceCount())
Simon Huntfb940112015-07-29 18:36:35 -0700401 .addProp(Properties.LINKS, topology.linkCount())
402 .addProp(Properties.HOSTS, hostService.getHostCount())
403 .addProp(Properties.TOPOLOGY_SSCS, topology.clusterCount())
Simon Hunt00a27ff2015-07-28 18:53:40 -0700404 .addSeparator()
Simon Huntfb940112015-07-29 18:36:35 -0700405 .addProp(Properties.INTENTS, intentService.getIntentCount())
406 .addProp(Properties.TUNNELS, tunnelService.tunnelCount())
Thomas Vachuskae50b6212015-12-02 08:00:09 -0800407 .addProp(Properties.FLOWS, flowService.getFlowRuleCount());
Thomas Vachuska329af532015-03-10 02:08:33 -0700408 }
409
Simon Huntb745ca62015-07-28 15:37:11 -0700410 // Returns property panel model for device details response.
411 protected PropertyPanel deviceDetails(DeviceId deviceId, long sid) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700412 Device device = deviceService.getDevice(deviceId);
413 Annotations annot = device.annotations();
414 String name = annot.value(AnnotationKeys.NAME);
415 int portCount = deviceService.getPorts(deviceId).size();
416 int flowCount = getFlowCount(deviceId);
cheng fan35dc0f22015-06-10 06:02:47 +0800417 int tunnelCount = getTunnelCount(deviceId);
Simon Huntb745ca62015-07-28 15:37:11 -0700418
419 String title = isNullOrEmpty(name) ? deviceId.toString() : name;
420 String typeId = device.type().toString().toLowerCase();
421
422 PropertyPanel pp = new PropertyPanel(title, typeId)
Simon Huntfb940112015-07-29 18:36:35 -0700423 .id(deviceId.toString())
Simon Hunt3a0598f2015-08-04 19:59:04 -0700424
Simon Huntfb940112015-07-29 18:36:35 -0700425 .addProp(Properties.URI, deviceId.toString())
426 .addProp(Properties.VENDOR, device.manufacturer())
427 .addProp(Properties.HW_VERSION, device.hwVersion())
428 .addProp(Properties.SW_VERSION, device.swVersion())
429 .addProp(Properties.SERIAL_NUMBER, device.serialNumber())
430 .addProp(Properties.PROTOCOL, annot.value(AnnotationKeys.PROTOCOL))
431 .addSeparator()
Simon Hunt3a0598f2015-08-04 19:59:04 -0700432
Simon Huntfb940112015-07-29 18:36:35 -0700433 .addProp(Properties.LATITUDE, annot.value(AnnotationKeys.LATITUDE))
434 .addProp(Properties.LONGITUDE, annot.value(AnnotationKeys.LONGITUDE))
435 .addSeparator()
Simon Hunt3a0598f2015-08-04 19:59:04 -0700436
Simon Huntfb940112015-07-29 18:36:35 -0700437 .addProp(Properties.PORTS, portCount)
438 .addProp(Properties.FLOWS, flowCount)
Simon Hunt3a0598f2015-08-04 19:59:04 -0700439 .addProp(Properties.TUNNELS, tunnelCount)
Simon Hunt00a27ff2015-07-28 18:53:40 -0700440
Simon Hunt3a0598f2015-08-04 19:59:04 -0700441 .addButton(CoreButtons.SHOW_DEVICE_VIEW)
442 .addButton(CoreButtons.SHOW_FLOW_VIEW)
443 .addButton(CoreButtons.SHOW_PORT_VIEW)
Jian Li79f67322016-01-06 18:22:37 -0800444 .addButton(CoreButtons.SHOW_GROUP_VIEW)
445 .addButton(CoreButtons.SHOW_METER_VIEW);
Simon Huntb745ca62015-07-28 15:37:11 -0700446
447 return pp;
Thomas Vachuska329af532015-03-10 02:08:33 -0700448 }
449
450 protected int getFlowCount(DeviceId deviceId) {
451 int count = 0;
Simon Huntb745ca62015-07-28 15:37:11 -0700452 for (FlowEntry flowEntry : flowService.getFlowEntries(deviceId)) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700453 count++;
Thomas Vachuska329af532015-03-10 02:08:33 -0700454 }
455 return count;
456 }
457
cheng fan35dc0f22015-06-10 06:02:47 +0800458 protected int getTunnelCount(DeviceId deviceId) {
459 int count = 0;
460 Collection<Tunnel> tunnels = tunnelService.queryAllTunnels();
461 for (Tunnel tunnel : tunnels) {
chengfanc553c952016-07-22 15:48:23 +0800462 //Only OpticalTunnelEndPoint has a device
463 if (!(tunnel.src() instanceof OpticalTunnelEndPoint) ||
464 !(tunnel.dst() instanceof OpticalTunnelEndPoint)) {
465 continue;
466 }
467
468 Optional<ElementId> srcElementId = ((OpticalTunnelEndPoint) tunnel.src()).elementId();
469 Optional<ElementId> dstElementId = ((OpticalTunnelEndPoint) tunnel.dst()).elementId();
470 if (!srcElementId.isPresent() || !dstElementId.isPresent()) {
471 continue;
472 }
473 DeviceId srcDeviceId = (DeviceId) srcElementId.get();
474 DeviceId dstDeviceId = (DeviceId) dstElementId.get();
475 if (srcDeviceId.equals(deviceId) || dstDeviceId.equals(deviceId)) {
cheng fan35dc0f22015-06-10 06:02:47 +0800476 count++;
477 }
478 }
479 return count;
480 }
481
Simon Hunta17fa672015-08-19 18:42:22 -0700482 // Counts all flow entries that egress on the links of the given device.
483 private Map<Link, Integer> getLinkFlowCounts(DeviceId deviceId) {
484 // get the flows for the device
Thomas Vachuska329af532015-03-10 02:08:33 -0700485 List<FlowEntry> entries = new ArrayList<>();
Simon Huntb745ca62015-07-28 15:37:11 -0700486 for (FlowEntry flowEntry : flowService.getFlowEntries(deviceId)) {
487 entries.add(flowEntry);
Thomas Vachuska329af532015-03-10 02:08:33 -0700488 }
489
Simon Hunta17fa672015-08-19 18:42:22 -0700490 // get egress links from device, and include edge links
491 Set<Link> links = new HashSet<>(linkService.getDeviceEgressLinks(deviceId));
492 Set<Host> hosts = hostService.getConnectedHosts(deviceId);
Thomas Vachuska329af532015-03-10 02:08:33 -0700493 if (hosts != null) {
494 for (Host host : hosts) {
Simon Hunta17fa672015-08-19 18:42:22 -0700495 links.add(createEdgeLink(host, false));
Thomas Vachuska329af532015-03-10 02:08:33 -0700496 }
497 }
498
Simon Hunta17fa672015-08-19 18:42:22 -0700499 // compile flow counts per link
Thomas Vachuska329af532015-03-10 02:08:33 -0700500 Map<Link, Integer> counts = new HashMap<>();
501 for (Link link : links) {
502 counts.put(link, getEgressFlows(link, entries));
503 }
504 return counts;
505 }
506
507 // Counts all entries that egress on the link source port.
Simon Hunta17fa672015-08-19 18:42:22 -0700508 private int getEgressFlows(Link link, List<FlowEntry> entries) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700509 int count = 0;
510 PortNumber out = link.src().port();
511 for (FlowEntry entry : entries) {
512 TrafficTreatment treatment = entry.treatment();
Ray Milkey42507352015-03-20 15:16:10 -0700513 for (Instruction instruction : treatment.allInstructions()) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700514 if (instruction.type() == Instruction.Type.OUTPUT &&
515 ((OutputInstruction) instruction).port().equals(out)) {
516 count++;
517 }
518 }
519 }
520 return count;
521 }
522
Thomas Vachuska329af532015-03-10 02:08:33 -0700523 // Returns host details response.
Simon Huntb745ca62015-07-28 15:37:11 -0700524 protected PropertyPanel hostDetails(HostId hostId, long sid) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700525 Host host = hostService.getHost(hostId);
526 Annotations annot = host.annotations();
527 String type = annot.value(AnnotationKeys.TYPE);
528 String name = annot.value(AnnotationKeys.NAME);
529 String vlan = host.vlan().toString();
Simon Huntb745ca62015-07-28 15:37:11 -0700530
531 String title = isNullOrEmpty(name) ? hostId.toString() : name;
532 String typeId = isNullOrEmpty(type) ? "endstation" : type;
533
534 PropertyPanel pp = new PropertyPanel(title, typeId)
Simon Huntfb940112015-07-29 18:36:35 -0700535 .id(hostId.toString())
536 .addProp(Properties.MAC, host.mac())
537 .addProp(Properties.IP, host.ipAddresses(), "[\\[\\]]")
538 .addProp(Properties.VLAN, vlan.equals("-1") ? "none" : vlan)
539 .addSeparator()
540 .addProp(Properties.LATITUDE, annot.value(AnnotationKeys.LATITUDE))
541 .addProp(Properties.LONGITUDE, annot.value(AnnotationKeys.LONGITUDE));
Simon Huntb745ca62015-07-28 15:37:11 -0700542
Simon Hunta17fa672015-08-19 18:42:22 -0700543 // Potentially add button descriptors here
Simon Huntb745ca62015-07-28 15:37:11 -0700544 return pp;
Thomas Vachuska329af532015-03-10 02:08:33 -0700545 }
546
Thomas Vachuska329af532015-03-10 02:08:33 -0700547}