blob: 49d51551594d17b101ab92910a0ba942b874ae7a [file] [log] [blame]
Thomas Vachuska329af532015-03-10 02:08:33 -07001/*
Jian Li79f67322016-01-06 18:22:37 -08002 * Copyright 2015,2016 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;
42import org.onosproject.net.Host;
43import org.onosproject.net.HostId;
44import org.onosproject.net.HostLocation;
45import org.onosproject.net.Link;
Thomas Vachuska329af532015-03-10 02:08:33 -070046import org.onosproject.net.PortNumber;
47import org.onosproject.net.device.DeviceEvent;
48import org.onosproject.net.device.DeviceService;
49import org.onosproject.net.flow.FlowEntry;
50import org.onosproject.net.flow.FlowRuleService;
51import org.onosproject.net.flow.TrafficTreatment;
52import org.onosproject.net.flow.instructions.Instruction;
53import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
54import org.onosproject.net.host.HostEvent;
55import org.onosproject.net.host.HostService;
Thomas Vachuska329af532015-03-10 02:08:33 -070056import org.onosproject.net.intent.IntentService;
Thomas Vachuska329af532015-03-10 02:08:33 -070057import org.onosproject.net.link.LinkEvent;
58import org.onosproject.net.link.LinkService;
59import org.onosproject.net.provider.ProviderId;
Thomas Vachuska329af532015-03-10 02:08:33 -070060import org.onosproject.net.statistic.StatisticService;
61import org.onosproject.net.topology.Topology;
62import org.onosproject.net.topology.TopologyService;
Simon Huntd2747a02015-04-30 22:41:16 -070063import org.onosproject.ui.JsonUtils;
Thomas Vachuska329af532015-03-10 02:08:33 -070064import org.onosproject.ui.UiConnection;
Simon Hunta0ddb022015-05-01 09:53:01 -070065import org.onosproject.ui.UiMessageHandler;
Simon Hunta17fa672015-08-19 18:42:22 -070066import org.onosproject.ui.impl.topo.ServicesBundle;
Simon Hunt0af1ec32015-07-24 12:17:55 -070067import org.onosproject.ui.topo.PropertyPanel;
Thomas Vachuska329af532015-03-10 02:08:33 -070068import org.slf4j.Logger;
69import org.slf4j.LoggerFactory;
70
Thomas Vachuska329af532015-03-10 02:08:33 -070071import java.util.ArrayList;
72import java.util.Collection;
73import java.util.Collections;
74import java.util.HashMap;
75import java.util.HashSet;
76import java.util.Iterator;
77import java.util.List;
78import java.util.Map;
79import java.util.Set;
80import java.util.concurrent.ConcurrentHashMap;
81
82import static com.google.common.base.Preconditions.checkNotNull;
83import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuska329af532015-03-10 02:08:33 -070084import static org.onosproject.cluster.ControllerNode.State.ACTIVE;
Thomas Vachuska204cb6c2015-06-04 00:03:06 -070085import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Thomas Vachuska329af532015-03-10 02:08:33 -070086import static org.onosproject.net.PortNumber.portNumber;
Simon Hunt3a0598f2015-08-04 19:59:04 -070087import static org.onosproject.ui.topo.TopoConstants.CoreButtons;
88import static org.onosproject.ui.topo.TopoConstants.Properties;
Simon Huntd3ceffa2015-08-25 12:44:35 -070089import static org.onosproject.ui.topo.TopoUtils.compactLinkString;
Thomas Vachuska329af532015-03-10 02:08:33 -070090
91/**
92 * Facility for creating messages bound for the topology viewer.
93 */
Simon Hunta0ddb022015-05-01 09:53:01 -070094public abstract class TopologyViewMessageHandlerBase extends UiMessageHandler {
Thomas Vachuska329af532015-03-10 02:08:33 -070095
Simon Hunt95d56fd2015-11-12 11:06:44 -080096 // default to an "add" event...
97 private static final DefaultHashMap<ClusterEvent.Type, String> CLUSTER_EVENT =
98 new DefaultHashMap<>("addInstance");
99
100 // default to an "update" event...
101 private static final DefaultHashMap<DeviceEvent.Type, String> DEVICE_EVENT =
102 new DefaultHashMap<>("updateDevice");
103 private static final DefaultHashMap<LinkEvent.Type, String> LINK_EVENT =
104 new DefaultHashMap<>("updateLink");
105 private static final DefaultHashMap<HostEvent.Type, String> HOST_EVENT =
106 new DefaultHashMap<>("updateHost");
107
108 // but call out specific events that we care to differentiate...
109 static {
110 CLUSTER_EVENT.put(ClusterEvent.Type.INSTANCE_REMOVED, "removeInstance");
111
112 DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_ADDED, "addDevice");
113 DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_REMOVED, "removeDevice");
114
115 LINK_EVENT.put(LinkEvent.Type.LINK_ADDED, "addLink");
116 LINK_EVENT.put(LinkEvent.Type.LINK_REMOVED, "removeLink");
117
118 HOST_EVENT.put(HostEvent.Type.HOST_ADDED, "addHost");
119 HOST_EVENT.put(HostEvent.Type.HOST_REMOVED, "removeHost");
120 HOST_EVENT.put(HostEvent.Type.HOST_MOVED, "moveHost");
121 }
122
Simon Huntd2747a02015-04-30 22:41:16 -0700123 protected static final Logger log =
124 LoggerFactory.getLogger(TopologyViewMessageHandlerBase.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700125
Simon Huntd2747a02015-04-30 22:41:16 -0700126 private static final ProviderId PID =
127 new ProviderId("core", "org.onosproject.core", true);
Thomas Vachuska329af532015-03-10 02:08:33 -0700128
Simon Hunta17fa672015-08-19 18:42:22 -0700129 protected static final String SHOW_HIGHLIGHTS = "showHighlights";
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700130
Thomas Vachuska329af532015-03-10 02:08:33 -0700131 protected ServiceDirectory directory;
132 protected ClusterService clusterService;
133 protected DeviceService deviceService;
134 protected LinkService linkService;
135 protected HostService hostService;
136 protected MastershipService mastershipService;
137 protected IntentService intentService;
138 protected FlowRuleService flowService;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700139 protected StatisticService flowStatsService;
140 protected PortStatisticsService portStatsService;
Thomas Vachuska329af532015-03-10 02:08:33 -0700141 protected TopologyService topologyService;
cheng fan35dc0f22015-06-10 06:02:47 +0800142 protected TunnelService tunnelService;
Thomas Vachuska329af532015-03-10 02:08:33 -0700143
Simon Hunta17fa672015-08-19 18:42:22 -0700144 protected ServicesBundle servicesBundle;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700145
Thomas Vachuska329af532015-03-10 02:08:33 -0700146 private String version;
147
148 // TODO: extract into an external & durable state; good enough for now and demo
149 private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>();
150
151 /**
Thomas Vachuska329af532015-03-10 02:08:33 -0700152 * Returns read-only view of the meta-ui information.
153 *
154 * @return map of id to meta-ui mementos
155 */
156 static Map<String, ObjectNode> getMetaUi() {
157 return Collections.unmodifiableMap(metaUi);
158 }
159
160 @Override
161 public void init(UiConnection connection, ServiceDirectory directory) {
162 super.init(connection, directory);
163 this.directory = checkNotNull(directory, "Directory cannot be null");
164 clusterService = directory.get(ClusterService.class);
165 deviceService = directory.get(DeviceService.class);
166 linkService = directory.get(LinkService.class);
167 hostService = directory.get(HostService.class);
168 mastershipService = directory.get(MastershipService.class);
169 intentService = directory.get(IntentService.class);
170 flowService = directory.get(FlowRuleService.class);
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700171 flowStatsService = directory.get(StatisticService.class);
172 portStatsService = directory.get(PortStatisticsService.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700173 topologyService = directory.get(TopologyService.class);
cheng fan35dc0f22015-06-10 06:02:47 +0800174 tunnelService = directory.get(TunnelService.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700175
Simon Hunta17fa672015-08-19 18:42:22 -0700176 servicesBundle = new ServicesBundle(intentService, deviceService,
177 hostService, linkService,
178 flowService,
179 flowStatsService, portStatsService);
180
Thomas Vachuska329af532015-03-10 02:08:33 -0700181 String ver = directory.get(CoreService.class).version().toString();
182 version = ver.replace(".SNAPSHOT", "*").replaceFirst("~.*$", "");
183 }
184
Thomas Vachuska329af532015-03-10 02:08:33 -0700185 // Returns the specified set of IP addresses as a string.
186 private String ip(Set<IpAddress> ipAddresses) {
187 Iterator<IpAddress> it = ipAddresses.iterator();
188 return it.hasNext() ? it.next().toString() : "unknown";
189 }
190
191 // Produces JSON structure from annotations.
192 private JsonNode props(Annotations annotations) {
Simon Huntda580882015-05-12 20:58:18 -0700193 ObjectNode props = objectNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700194 if (annotations != null) {
195 for (String key : annotations.keys()) {
196 props.put(key, annotations.value(key));
197 }
198 }
199 return props;
200 }
201
202 // Produces an informational log message event bound to the client.
203 protected ObjectNode info(long id, String message) {
204 return message("info", id, message);
205 }
206
207 // Produces a warning log message event bound to the client.
208 protected ObjectNode warning(long id, String message) {
209 return message("warning", id, message);
210 }
211
212 // Produces an error log message event bound to the client.
213 protected ObjectNode error(long id, String message) {
214 return message("error", id, message);
215 }
216
217 // Produces a log message event bound to the client.
218 private ObjectNode message(String severity, long id, String message) {
Simon Huntda580882015-05-12 20:58:18 -0700219 ObjectNode payload = objectNode()
Simon Huntd2747a02015-04-30 22:41:16 -0700220 .put("severity", severity)
221 .put("message", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700222
Simon Huntd2747a02015-04-30 22:41:16 -0700223 return JsonUtils.envelope("message", id, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700224 }
225
Thomas Vachuska329af532015-03-10 02:08:33 -0700226 // Produces a cluster instance message to the client.
Simon Hunt95d56fd2015-11-12 11:06:44 -0800227 protected ObjectNode instanceMessage(ClusterEvent event, String msgType) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700228 ControllerNode node = event.subject();
229 int switchCount = mastershipService.getDevicesOf(node.id()).size();
Simon Huntda580882015-05-12 20:58:18 -0700230 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700231 .put("id", node.id().toString())
232 .put("ip", node.ip().toString())
233 .put("online", clusterService.getState(node.id()) == ACTIVE)
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700234 .put("uiAttached", node.equals(clusterService.getLocalNode()))
Thomas Vachuska329af532015-03-10 02:08:33 -0700235 .put("switches", switchCount);
236
Simon Huntda580882015-05-12 20:58:18 -0700237 ArrayNode labels = arrayNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700238 labels.add(node.id().toString());
239 labels.add(node.ip().toString());
240
241 // Add labels, props and stuff the payload into envelope.
242 payload.set("labels", labels);
243 addMetaUi(node.id().toString(), payload);
244
Simon Hunt95d56fd2015-11-12 11:06:44 -0800245 String type = msgType != null ? msgType : CLUSTER_EVENT.get(event.type());
Simon Huntd2747a02015-04-30 22:41:16 -0700246 return JsonUtils.envelope(type, 0, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700247 }
248
249 // Produces a device event message to the client.
250 protected ObjectNode deviceMessage(DeviceEvent event) {
251 Device device = event.subject();
Simon Huntda580882015-05-12 20:58:18 -0700252 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700253 .put("id", device.id().toString())
254 .put("type", device.type().toString().toLowerCase())
255 .put("online", deviceService.isAvailable(device.id()))
256 .put("master", master(device.id()));
257
258 // Generate labels: id, chassis id, no-label, optional-name
259 String name = device.annotations().value(AnnotationKeys.NAME);
Simon Huntda580882015-05-12 20:58:18 -0700260 ArrayNode labels = arrayNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700261 labels.add("");
262 labels.add(isNullOrEmpty(name) ? device.id().toString() : name);
263 labels.add(device.id().toString());
264
265 // Add labels, props and stuff the payload into envelope.
266 payload.set("labels", labels);
267 payload.set("props", props(device.annotations()));
268 addGeoLocation(device, payload);
269 addMetaUi(device.id().toString(), payload);
270
Simon Hunt95d56fd2015-11-12 11:06:44 -0800271 String type = DEVICE_EVENT.get(event.type());
Simon Huntd2747a02015-04-30 22:41:16 -0700272 return JsonUtils.envelope(type, 0, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700273 }
274
275 // Produces a link event message to the client.
276 protected ObjectNode linkMessage(LinkEvent event) {
277 Link link = event.subject();
Simon Huntda580882015-05-12 20:58:18 -0700278 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700279 .put("id", compactLinkString(link))
280 .put("type", link.type().toString().toLowerCase())
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800281 .put("expected", link.isExpected())
Thomas Vachuska329af532015-03-10 02:08:33 -0700282 .put("online", link.state() == Link.State.ACTIVE)
283 .put("linkWidth", 1.2)
284 .put("src", link.src().deviceId().toString())
285 .put("srcPort", link.src().port().toString())
286 .put("dst", link.dst().deviceId().toString())
287 .put("dstPort", link.dst().port().toString());
Simon Hunt95d56fd2015-11-12 11:06:44 -0800288 String type = LINK_EVENT.get(event.type());
Simon Huntd2747a02015-04-30 22:41:16 -0700289 return JsonUtils.envelope(type, 0, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700290 }
291
292 // Produces a host event message to the client.
293 protected ObjectNode hostMessage(HostEvent event) {
294 Host host = event.subject();
Charles Chan33f28a92015-11-13 13:12:38 -0800295 Host prevHost = event.prevSubject();
Thomas Vachuska329af532015-03-10 02:08:33 -0700296 String hostType = host.annotations().value(AnnotationKeys.TYPE);
Simon Hunt95d56fd2015-11-12 11:06:44 -0800297
Simon Huntda580882015-05-12 20:58:18 -0700298 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700299 .put("id", host.id().toString())
300 .put("type", isNullOrEmpty(hostType) ? "endstation" : hostType)
301 .put("ingress", compactLinkString(edgeLink(host, true)))
302 .put("egress", compactLinkString(edgeLink(host, false)));
Simon Huntda580882015-05-12 20:58:18 -0700303 payload.set("cp", hostConnect(host.location()));
Charles Chan33f28a92015-11-13 13:12:38 -0800304 if (prevHost != null && prevHost.location() != null) {
305 payload.set("prevCp", hostConnect(prevHost.location()));
Simon Hunt95d56fd2015-11-12 11:06:44 -0800306 }
Simon Huntda580882015-05-12 20:58:18 -0700307 payload.set("labels", labels(ip(host.ipAddresses()),
Thomas Vachuska329af532015-03-10 02:08:33 -0700308 host.mac().toString()));
309 payload.set("props", props(host.annotations()));
310 addGeoLocation(host, payload);
311 addMetaUi(host.id().toString(), payload);
312
Simon Hunt95d56fd2015-11-12 11:06:44 -0800313 String type = HOST_EVENT.get(event.type());
Simon Huntd2747a02015-04-30 22:41:16 -0700314 return JsonUtils.envelope(type, 0, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700315 }
316
317 // Encodes the specified host location into a JSON object.
Simon Huntda580882015-05-12 20:58:18 -0700318 private ObjectNode hostConnect(HostLocation location) {
319 return objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700320 .put("device", location.deviceId().toString())
321 .put("port", location.port().toLong());
322 }
323
324 // Encodes the specified list of labels a JSON array.
Simon Huntda580882015-05-12 20:58:18 -0700325 private ArrayNode labels(String... labels) {
326 ArrayNode json = arrayNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700327 for (String label : labels) {
328 json.add(label);
329 }
330 return json;
331 }
332
333 // Returns the name of the master node for the specified device id.
334 private String master(DeviceId deviceId) {
335 NodeId master = mastershipService.getMasterFor(deviceId);
336 return master != null ? master.toString() : "";
337 }
338
339 // Generates an edge link from the specified host location.
340 private EdgeLink edgeLink(Host host, boolean ingress) {
341 return new DefaultEdgeLink(PID, new ConnectPoint(host.id(), portNumber(0)),
342 host.location(), ingress);
343 }
344
345 // Adds meta UI information for the specified object.
346 private void addMetaUi(String id, ObjectNode payload) {
347 ObjectNode meta = metaUi.get(id);
348 if (meta != null) {
349 payload.set("metaUi", meta);
350 }
351 }
352
353 // Adds a geo location JSON to the specified payload object.
354 private void addGeoLocation(Annotated annotated, ObjectNode payload) {
355 Annotations annotations = annotated.annotations();
356 if (annotations == null) {
357 return;
358 }
359
360 String slat = annotations.value(AnnotationKeys.LATITUDE);
361 String slng = annotations.value(AnnotationKeys.LONGITUDE);
362 try {
363 if (slat != null && slng != null && !slat.isEmpty() && !slng.isEmpty()) {
364 double lat = Double.parseDouble(slat);
365 double lng = Double.parseDouble(slng);
Simon Huntda580882015-05-12 20:58:18 -0700366 ObjectNode loc = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700367 .put("type", "latlng").put("lat", lat).put("lng", lng);
368 payload.set("location", loc);
369 }
370 } catch (NumberFormatException e) {
371 log.warn("Invalid geo data latitude={}; longiture={}", slat, slng);
372 }
373 }
374
375 // Updates meta UI information for the specified object.
Simon Huntd2747a02015-04-30 22:41:16 -0700376 protected void updateMetaUi(ObjectNode payload) {
377 metaUi.put(JsonUtils.string(payload, "id"),
378 JsonUtils.node(payload, "memento"));
Thomas Vachuska329af532015-03-10 02:08:33 -0700379 }
380
Simon Hunta17fa672015-08-19 18:42:22 -0700381
Simon Huntb745ca62015-07-28 15:37:11 -0700382 // -----------------------------------------------------------------------
383 // Create models of the data to return, that overlays can adjust / augment
384
385 // Returns property panel model for summary response.
Simon Hunt0af1ec32015-07-24 12:17:55 -0700386 protected PropertyPanel summmaryMessage(long sid) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700387 Topology topology = topologyService.currentTopology();
Simon Hunt0af1ec32015-07-24 12:17:55 -0700388
Simon Hunt00a27ff2015-07-28 18:53:40 -0700389 return new PropertyPanel("ONOS Summary", "node")
Thomas Vachuskae50b6212015-12-02 08:00:09 -0800390 .addProp(Properties.VERSION, version)
391 .addSeparator()
Thomas Vachuska297907a2015-12-04 16:14:27 -0800392 .addProp(Properties.DEVICES, deviceService.getDeviceCount())
Simon Huntfb940112015-07-29 18:36:35 -0700393 .addProp(Properties.LINKS, topology.linkCount())
394 .addProp(Properties.HOSTS, hostService.getHostCount())
395 .addProp(Properties.TOPOLOGY_SSCS, topology.clusterCount())
Simon Hunt00a27ff2015-07-28 18:53:40 -0700396 .addSeparator()
Simon Huntfb940112015-07-29 18:36:35 -0700397 .addProp(Properties.INTENTS, intentService.getIntentCount())
398 .addProp(Properties.TUNNELS, tunnelService.tunnelCount())
Thomas Vachuskae50b6212015-12-02 08:00:09 -0800399 .addProp(Properties.FLOWS, flowService.getFlowRuleCount());
Thomas Vachuska329af532015-03-10 02:08:33 -0700400 }
401
Simon Huntb745ca62015-07-28 15:37:11 -0700402 // Returns property panel model for device details response.
403 protected PropertyPanel deviceDetails(DeviceId deviceId, long sid) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700404 Device device = deviceService.getDevice(deviceId);
405 Annotations annot = device.annotations();
406 String name = annot.value(AnnotationKeys.NAME);
407 int portCount = deviceService.getPorts(deviceId).size();
408 int flowCount = getFlowCount(deviceId);
cheng fan35dc0f22015-06-10 06:02:47 +0800409 int tunnelCount = getTunnelCount(deviceId);
Simon Huntb745ca62015-07-28 15:37:11 -0700410
411 String title = isNullOrEmpty(name) ? deviceId.toString() : name;
412 String typeId = device.type().toString().toLowerCase();
413
414 PropertyPanel pp = new PropertyPanel(title, typeId)
Simon Huntfb940112015-07-29 18:36:35 -0700415 .id(deviceId.toString())
Simon Hunt3a0598f2015-08-04 19:59:04 -0700416
Simon Huntfb940112015-07-29 18:36:35 -0700417 .addProp(Properties.URI, deviceId.toString())
418 .addProp(Properties.VENDOR, device.manufacturer())
419 .addProp(Properties.HW_VERSION, device.hwVersion())
420 .addProp(Properties.SW_VERSION, device.swVersion())
421 .addProp(Properties.SERIAL_NUMBER, device.serialNumber())
422 .addProp(Properties.PROTOCOL, annot.value(AnnotationKeys.PROTOCOL))
423 .addSeparator()
Simon Hunt3a0598f2015-08-04 19:59:04 -0700424
Simon Huntfb940112015-07-29 18:36:35 -0700425 .addProp(Properties.LATITUDE, annot.value(AnnotationKeys.LATITUDE))
426 .addProp(Properties.LONGITUDE, annot.value(AnnotationKeys.LONGITUDE))
427 .addSeparator()
Simon Hunt3a0598f2015-08-04 19:59:04 -0700428
Simon Huntfb940112015-07-29 18:36:35 -0700429 .addProp(Properties.PORTS, portCount)
430 .addProp(Properties.FLOWS, flowCount)
Simon Hunt3a0598f2015-08-04 19:59:04 -0700431 .addProp(Properties.TUNNELS, tunnelCount)
Simon Hunt00a27ff2015-07-28 18:53:40 -0700432
Simon Hunt3a0598f2015-08-04 19:59:04 -0700433 .addButton(CoreButtons.SHOW_DEVICE_VIEW)
434 .addButton(CoreButtons.SHOW_FLOW_VIEW)
435 .addButton(CoreButtons.SHOW_PORT_VIEW)
Jian Li79f67322016-01-06 18:22:37 -0800436 .addButton(CoreButtons.SHOW_GROUP_VIEW)
437 .addButton(CoreButtons.SHOW_METER_VIEW);
Simon Huntb745ca62015-07-28 15:37:11 -0700438
439 return pp;
Thomas Vachuska329af532015-03-10 02:08:33 -0700440 }
441
442 protected int getFlowCount(DeviceId deviceId) {
443 int count = 0;
Simon Huntb745ca62015-07-28 15:37:11 -0700444 for (FlowEntry flowEntry : flowService.getFlowEntries(deviceId)) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700445 count++;
Thomas Vachuska329af532015-03-10 02:08:33 -0700446 }
447 return count;
448 }
449
cheng fan35dc0f22015-06-10 06:02:47 +0800450 protected int getTunnelCount(DeviceId deviceId) {
451 int count = 0;
452 Collection<Tunnel> tunnels = tunnelService.queryAllTunnels();
453 for (Tunnel tunnel : tunnels) {
454 OpticalTunnelEndPoint src = (OpticalTunnelEndPoint) tunnel.src();
455 OpticalTunnelEndPoint dst = (OpticalTunnelEndPoint) tunnel.dst();
456 DeviceId srcDevice = (DeviceId) src.elementId().get();
457 DeviceId dstDevice = (DeviceId) dst.elementId().get();
Simon Huntb745ca62015-07-28 15:37:11 -0700458 if (srcDevice.toString().equals(deviceId.toString()) ||
459 dstDevice.toString().equals(deviceId.toString())) {
cheng fan35dc0f22015-06-10 06:02:47 +0800460 count++;
461 }
462 }
463 return count;
464 }
465
Simon Hunta17fa672015-08-19 18:42:22 -0700466 // Counts all flow entries that egress on the links of the given device.
467 private Map<Link, Integer> getLinkFlowCounts(DeviceId deviceId) {
468 // get the flows for the device
Thomas Vachuska329af532015-03-10 02:08:33 -0700469 List<FlowEntry> entries = new ArrayList<>();
Simon Huntb745ca62015-07-28 15:37:11 -0700470 for (FlowEntry flowEntry : flowService.getFlowEntries(deviceId)) {
471 entries.add(flowEntry);
Thomas Vachuska329af532015-03-10 02:08:33 -0700472 }
473
Simon Hunta17fa672015-08-19 18:42:22 -0700474 // get egress links from device, and include edge links
475 Set<Link> links = new HashSet<>(linkService.getDeviceEgressLinks(deviceId));
476 Set<Host> hosts = hostService.getConnectedHosts(deviceId);
Thomas Vachuska329af532015-03-10 02:08:33 -0700477 if (hosts != null) {
478 for (Host host : hosts) {
Simon Hunta17fa672015-08-19 18:42:22 -0700479 links.add(createEdgeLink(host, false));
Thomas Vachuska329af532015-03-10 02:08:33 -0700480 }
481 }
482
Simon Hunta17fa672015-08-19 18:42:22 -0700483 // compile flow counts per link
Thomas Vachuska329af532015-03-10 02:08:33 -0700484 Map<Link, Integer> counts = new HashMap<>();
485 for (Link link : links) {
486 counts.put(link, getEgressFlows(link, entries));
487 }
488 return counts;
489 }
490
491 // Counts all entries that egress on the link source port.
Simon Hunta17fa672015-08-19 18:42:22 -0700492 private int getEgressFlows(Link link, List<FlowEntry> entries) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700493 int count = 0;
494 PortNumber out = link.src().port();
495 for (FlowEntry entry : entries) {
496 TrafficTreatment treatment = entry.treatment();
Ray Milkey42507352015-03-20 15:16:10 -0700497 for (Instruction instruction : treatment.allInstructions()) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700498 if (instruction.type() == Instruction.Type.OUTPUT &&
499 ((OutputInstruction) instruction).port().equals(out)) {
500 count++;
501 }
502 }
503 }
504 return count;
505 }
506
Thomas Vachuska329af532015-03-10 02:08:33 -0700507 // Returns host details response.
Simon Huntb745ca62015-07-28 15:37:11 -0700508 protected PropertyPanel hostDetails(HostId hostId, long sid) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700509 Host host = hostService.getHost(hostId);
510 Annotations annot = host.annotations();
511 String type = annot.value(AnnotationKeys.TYPE);
512 String name = annot.value(AnnotationKeys.NAME);
513 String vlan = host.vlan().toString();
Simon Huntb745ca62015-07-28 15:37:11 -0700514
515 String title = isNullOrEmpty(name) ? hostId.toString() : name;
516 String typeId = isNullOrEmpty(type) ? "endstation" : type;
517
518 PropertyPanel pp = new PropertyPanel(title, typeId)
Simon Huntfb940112015-07-29 18:36:35 -0700519 .id(hostId.toString())
520 .addProp(Properties.MAC, host.mac())
521 .addProp(Properties.IP, host.ipAddresses(), "[\\[\\]]")
522 .addProp(Properties.VLAN, vlan.equals("-1") ? "none" : vlan)
523 .addSeparator()
524 .addProp(Properties.LATITUDE, annot.value(AnnotationKeys.LATITUDE))
525 .addProp(Properties.LONGITUDE, annot.value(AnnotationKeys.LONGITUDE));
Simon Huntb745ca62015-07-28 15:37:11 -0700526
Simon Hunta17fa672015-08-19 18:42:22 -0700527 // Potentially add button descriptors here
Simon Huntb745ca62015-07-28 15:37:11 -0700528 return pp;
Thomas Vachuska329af532015-03-10 02:08:33 -0700529 }
530
Thomas Vachuska329af532015-03-10 02:08:33 -0700531}