blob: 840e89f3406f7b9e36f869b22c59b81275b62d81 [file] [log] [blame]
Thomas Vachuska329af532015-03-10 02:08:33 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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;
23import org.onosproject.cluster.ClusterEvent;
24import org.onosproject.cluster.ClusterService;
25import org.onosproject.cluster.ControllerNode;
26import org.onosproject.cluster.NodeId;
27import org.onosproject.core.CoreService;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070028import org.onosproject.incubator.net.PortStatisticsService;
cheng fan35dc0f22015-06-10 06:02:47 +080029import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
30import org.onosproject.incubator.net.tunnel.Tunnel;
31import org.onosproject.incubator.net.tunnel.TunnelService;
Thomas Vachuska329af532015-03-10 02:08:33 -070032import org.onosproject.mastership.MastershipService;
33import org.onosproject.net.Annotated;
34import org.onosproject.net.AnnotationKeys;
35import org.onosproject.net.Annotations;
36import org.onosproject.net.ConnectPoint;
37import org.onosproject.net.DefaultEdgeLink;
38import org.onosproject.net.Device;
39import org.onosproject.net.DeviceId;
40import org.onosproject.net.EdgeLink;
41import org.onosproject.net.Host;
42import org.onosproject.net.HostId;
43import org.onosproject.net.HostLocation;
44import org.onosproject.net.Link;
Thomas Vachuska329af532015-03-10 02:08:33 -070045import org.onosproject.net.PortNumber;
46import org.onosproject.net.device.DeviceEvent;
47import org.onosproject.net.device.DeviceService;
48import org.onosproject.net.flow.FlowEntry;
49import org.onosproject.net.flow.FlowRuleService;
50import org.onosproject.net.flow.TrafficTreatment;
51import org.onosproject.net.flow.instructions.Instruction;
52import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
53import org.onosproject.net.host.HostEvent;
54import org.onosproject.net.host.HostService;
Thomas Vachuska329af532015-03-10 02:08:33 -070055import org.onosproject.net.intent.IntentService;
Thomas Vachuska329af532015-03-10 02:08:33 -070056import org.onosproject.net.link.LinkEvent;
57import org.onosproject.net.link.LinkService;
58import org.onosproject.net.provider.ProviderId;
Thomas Vachuska329af532015-03-10 02:08:33 -070059import org.onosproject.net.statistic.StatisticService;
60import org.onosproject.net.topology.Topology;
61import org.onosproject.net.topology.TopologyService;
Simon Huntd2747a02015-04-30 22:41:16 -070062import org.onosproject.ui.JsonUtils;
Thomas Vachuska329af532015-03-10 02:08:33 -070063import org.onosproject.ui.UiConnection;
Simon Hunta0ddb022015-05-01 09:53:01 -070064import org.onosproject.ui.UiMessageHandler;
Simon Hunta17fa672015-08-19 18:42:22 -070065import org.onosproject.ui.impl.topo.ServicesBundle;
Simon Hunt0af1ec32015-07-24 12:17:55 -070066import org.onosproject.ui.topo.PropertyPanel;
Thomas Vachuska329af532015-03-10 02:08:33 -070067import org.slf4j.Logger;
68import org.slf4j.LoggerFactory;
69
Thomas Vachuska329af532015-03-10 02:08:33 -070070import java.util.ArrayList;
71import java.util.Collection;
72import java.util.Collections;
73import java.util.HashMap;
74import java.util.HashSet;
75import java.util.Iterator;
76import java.util.List;
77import java.util.Map;
78import java.util.Set;
79import java.util.concurrent.ConcurrentHashMap;
80
81import static com.google.common.base.Preconditions.checkNotNull;
82import static com.google.common.base.Strings.isNullOrEmpty;
83import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_ADDED;
84import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_REMOVED;
85import static org.onosproject.cluster.ControllerNode.State.ACTIVE;
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;
88import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED;
89import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED;
90import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
91import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED;
92import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
93import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED;
Simon Hunt3a0598f2015-08-04 19:59:04 -070094import static org.onosproject.ui.topo.TopoConstants.CoreButtons;
95import static org.onosproject.ui.topo.TopoConstants.Properties;
Simon Huntd3ceffa2015-08-25 12:44:35 -070096import static org.onosproject.ui.topo.TopoUtils.compactLinkString;
Thomas Vachuska329af532015-03-10 02:08:33 -070097
98/**
99 * Facility for creating messages bound for the topology viewer.
100 */
Simon Hunta0ddb022015-05-01 09:53:01 -0700101public abstract class TopologyViewMessageHandlerBase extends UiMessageHandler {
Thomas Vachuska329af532015-03-10 02:08:33 -0700102
Simon Huntd2747a02015-04-30 22:41:16 -0700103 protected static final Logger log =
104 LoggerFactory.getLogger(TopologyViewMessageHandlerBase.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700105
Simon Huntd2747a02015-04-30 22:41:16 -0700106 private static final ProviderId PID =
107 new ProviderId("core", "org.onosproject.core", true);
Thomas Vachuska329af532015-03-10 02:08:33 -0700108
Simon Hunta17fa672015-08-19 18:42:22 -0700109 protected static final String SHOW_HIGHLIGHTS = "showHighlights";
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700110
Thomas Vachuska329af532015-03-10 02:08:33 -0700111 protected ServiceDirectory directory;
112 protected ClusterService clusterService;
113 protected DeviceService deviceService;
114 protected LinkService linkService;
115 protected HostService hostService;
116 protected MastershipService mastershipService;
117 protected IntentService intentService;
118 protected FlowRuleService flowService;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700119 protected StatisticService flowStatsService;
120 protected PortStatisticsService portStatsService;
Thomas Vachuska329af532015-03-10 02:08:33 -0700121 protected TopologyService topologyService;
cheng fan35dc0f22015-06-10 06:02:47 +0800122 protected TunnelService tunnelService;
Thomas Vachuska329af532015-03-10 02:08:33 -0700123
Simon Hunta17fa672015-08-19 18:42:22 -0700124 protected ServicesBundle servicesBundle;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700125
Thomas Vachuska329af532015-03-10 02:08:33 -0700126 private String version;
127
128 // TODO: extract into an external & durable state; good enough for now and demo
129 private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>();
130
131 /**
Thomas Vachuska329af532015-03-10 02:08:33 -0700132 * Returns read-only view of the meta-ui information.
133 *
134 * @return map of id to meta-ui mementos
135 */
136 static Map<String, ObjectNode> getMetaUi() {
137 return Collections.unmodifiableMap(metaUi);
138 }
139
140 @Override
141 public void init(UiConnection connection, ServiceDirectory directory) {
142 super.init(connection, directory);
143 this.directory = checkNotNull(directory, "Directory cannot be null");
144 clusterService = directory.get(ClusterService.class);
145 deviceService = directory.get(DeviceService.class);
146 linkService = directory.get(LinkService.class);
147 hostService = directory.get(HostService.class);
148 mastershipService = directory.get(MastershipService.class);
149 intentService = directory.get(IntentService.class);
150 flowService = directory.get(FlowRuleService.class);
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700151 flowStatsService = directory.get(StatisticService.class);
152 portStatsService = directory.get(PortStatisticsService.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700153 topologyService = directory.get(TopologyService.class);
cheng fan35dc0f22015-06-10 06:02:47 +0800154 tunnelService = directory.get(TunnelService.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700155
Simon Hunta17fa672015-08-19 18:42:22 -0700156 servicesBundle = new ServicesBundle(intentService, deviceService,
157 hostService, linkService,
158 flowService,
159 flowStatsService, portStatsService);
160
Thomas Vachuska329af532015-03-10 02:08:33 -0700161 String ver = directory.get(CoreService.class).version().toString();
162 version = ver.replace(".SNAPSHOT", "*").replaceFirst("~.*$", "");
163 }
164
Thomas Vachuska329af532015-03-10 02:08:33 -0700165 // Returns the specified set of IP addresses as a string.
166 private String ip(Set<IpAddress> ipAddresses) {
167 Iterator<IpAddress> it = ipAddresses.iterator();
168 return it.hasNext() ? it.next().toString() : "unknown";
169 }
170
171 // Produces JSON structure from annotations.
172 private JsonNode props(Annotations annotations) {
Simon Huntda580882015-05-12 20:58:18 -0700173 ObjectNode props = objectNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700174 if (annotations != null) {
175 for (String key : annotations.keys()) {
176 props.put(key, annotations.value(key));
177 }
178 }
179 return props;
180 }
181
182 // Produces an informational log message event bound to the client.
183 protected ObjectNode info(long id, String message) {
184 return message("info", id, message);
185 }
186
187 // Produces a warning log message event bound to the client.
188 protected ObjectNode warning(long id, String message) {
189 return message("warning", id, message);
190 }
191
192 // Produces an error log message event bound to the client.
193 protected ObjectNode error(long id, String message) {
194 return message("error", id, message);
195 }
196
197 // Produces a log message event bound to the client.
198 private ObjectNode message(String severity, long id, String message) {
Simon Huntda580882015-05-12 20:58:18 -0700199 ObjectNode payload = objectNode()
Simon Huntd2747a02015-04-30 22:41:16 -0700200 .put("severity", severity)
201 .put("message", message);
Thomas Vachuska329af532015-03-10 02:08:33 -0700202
Simon Huntd2747a02015-04-30 22:41:16 -0700203 return JsonUtils.envelope("message", id, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700204 }
205
Thomas Vachuska329af532015-03-10 02:08:33 -0700206 // Produces a cluster instance message to the client.
207 protected ObjectNode instanceMessage(ClusterEvent event, String messageType) {
208 ControllerNode node = event.subject();
209 int switchCount = mastershipService.getDevicesOf(node.id()).size();
Simon Huntda580882015-05-12 20:58:18 -0700210 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700211 .put("id", node.id().toString())
212 .put("ip", node.ip().toString())
213 .put("online", clusterService.getState(node.id()) == ACTIVE)
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700214 .put("uiAttached", node.equals(clusterService.getLocalNode()))
Thomas Vachuska329af532015-03-10 02:08:33 -0700215 .put("switches", switchCount);
216
Simon Huntda580882015-05-12 20:58:18 -0700217 ArrayNode labels = arrayNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700218 labels.add(node.id().toString());
219 labels.add(node.ip().toString());
220
221 // Add labels, props and stuff the payload into envelope.
222 payload.set("labels", labels);
223 addMetaUi(node.id().toString(), payload);
224
225 String type = messageType != null ? messageType :
226 ((event.type() == INSTANCE_ADDED) ? "addInstance" :
227 ((event.type() == INSTANCE_REMOVED ? "removeInstance" :
228 "addInstance")));
Simon Huntd2747a02015-04-30 22:41:16 -0700229 return JsonUtils.envelope(type, 0, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700230 }
231
232 // Produces a device event message to the client.
233 protected ObjectNode deviceMessage(DeviceEvent event) {
234 Device device = event.subject();
Simon Huntda580882015-05-12 20:58:18 -0700235 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700236 .put("id", device.id().toString())
237 .put("type", device.type().toString().toLowerCase())
238 .put("online", deviceService.isAvailable(device.id()))
239 .put("master", master(device.id()));
240
241 // Generate labels: id, chassis id, no-label, optional-name
242 String name = device.annotations().value(AnnotationKeys.NAME);
Simon Huntda580882015-05-12 20:58:18 -0700243 ArrayNode labels = arrayNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700244 labels.add("");
245 labels.add(isNullOrEmpty(name) ? device.id().toString() : name);
246 labels.add(device.id().toString());
247
248 // Add labels, props and stuff the payload into envelope.
249 payload.set("labels", labels);
250 payload.set("props", props(device.annotations()));
251 addGeoLocation(device, payload);
252 addMetaUi(device.id().toString(), payload);
253
254 String type = (event.type() == DEVICE_ADDED) ? "addDevice" :
255 ((event.type() == DEVICE_REMOVED) ? "removeDevice" : "updateDevice");
Simon Huntd2747a02015-04-30 22:41:16 -0700256 return JsonUtils.envelope(type, 0, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700257 }
258
259 // Produces a link event message to the client.
260 protected ObjectNode linkMessage(LinkEvent event) {
261 Link link = event.subject();
Simon Huntda580882015-05-12 20:58:18 -0700262 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700263 .put("id", compactLinkString(link))
264 .put("type", link.type().toString().toLowerCase())
265 .put("online", link.state() == Link.State.ACTIVE)
266 .put("linkWidth", 1.2)
267 .put("src", link.src().deviceId().toString())
268 .put("srcPort", link.src().port().toString())
269 .put("dst", link.dst().deviceId().toString())
270 .put("dstPort", link.dst().port().toString());
271 String type = (event.type() == LINK_ADDED) ? "addLink" :
272 ((event.type() == LINK_REMOVED) ? "removeLink" : "updateLink");
Simon Huntd2747a02015-04-30 22:41:16 -0700273 return JsonUtils.envelope(type, 0, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700274 }
275
276 // Produces a host event message to the client.
277 protected ObjectNode hostMessage(HostEvent event) {
278 Host host = event.subject();
279 String hostType = host.annotations().value(AnnotationKeys.TYPE);
Simon Huntda580882015-05-12 20:58:18 -0700280 ObjectNode payload = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700281 .put("id", host.id().toString())
282 .put("type", isNullOrEmpty(hostType) ? "endstation" : hostType)
283 .put("ingress", compactLinkString(edgeLink(host, true)))
284 .put("egress", compactLinkString(edgeLink(host, false)));
Simon Huntda580882015-05-12 20:58:18 -0700285 payload.set("cp", hostConnect(host.location()));
286 payload.set("labels", labels(ip(host.ipAddresses()),
Thomas Vachuska329af532015-03-10 02:08:33 -0700287 host.mac().toString()));
288 payload.set("props", props(host.annotations()));
289 addGeoLocation(host, payload);
290 addMetaUi(host.id().toString(), payload);
291
292 String type = (event.type() == HOST_ADDED) ? "addHost" :
293 ((event.type() == HOST_REMOVED) ? "removeHost" : "updateHost");
Simon Huntd2747a02015-04-30 22:41:16 -0700294 return JsonUtils.envelope(type, 0, payload);
Thomas Vachuska329af532015-03-10 02:08:33 -0700295 }
296
297 // Encodes the specified host location into a JSON object.
Simon Huntda580882015-05-12 20:58:18 -0700298 private ObjectNode hostConnect(HostLocation location) {
299 return objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700300 .put("device", location.deviceId().toString())
301 .put("port", location.port().toLong());
302 }
303
304 // Encodes the specified list of labels a JSON array.
Simon Huntda580882015-05-12 20:58:18 -0700305 private ArrayNode labels(String... labels) {
306 ArrayNode json = arrayNode();
Thomas Vachuska329af532015-03-10 02:08:33 -0700307 for (String label : labels) {
308 json.add(label);
309 }
310 return json;
311 }
312
313 // Returns the name of the master node for the specified device id.
314 private String master(DeviceId deviceId) {
315 NodeId master = mastershipService.getMasterFor(deviceId);
316 return master != null ? master.toString() : "";
317 }
318
319 // Generates an edge link from the specified host location.
320 private EdgeLink edgeLink(Host host, boolean ingress) {
321 return new DefaultEdgeLink(PID, new ConnectPoint(host.id(), portNumber(0)),
322 host.location(), ingress);
323 }
324
325 // Adds meta UI information for the specified object.
326 private void addMetaUi(String id, ObjectNode payload) {
327 ObjectNode meta = metaUi.get(id);
328 if (meta != null) {
329 payload.set("metaUi", meta);
330 }
331 }
332
333 // Adds a geo location JSON to the specified payload object.
334 private void addGeoLocation(Annotated annotated, ObjectNode payload) {
335 Annotations annotations = annotated.annotations();
336 if (annotations == null) {
337 return;
338 }
339
340 String slat = annotations.value(AnnotationKeys.LATITUDE);
341 String slng = annotations.value(AnnotationKeys.LONGITUDE);
342 try {
343 if (slat != null && slng != null && !slat.isEmpty() && !slng.isEmpty()) {
344 double lat = Double.parseDouble(slat);
345 double lng = Double.parseDouble(slng);
Simon Huntda580882015-05-12 20:58:18 -0700346 ObjectNode loc = objectNode()
Thomas Vachuska329af532015-03-10 02:08:33 -0700347 .put("type", "latlng").put("lat", lat).put("lng", lng);
348 payload.set("location", loc);
349 }
350 } catch (NumberFormatException e) {
351 log.warn("Invalid geo data latitude={}; longiture={}", slat, slng);
352 }
353 }
354
355 // Updates meta UI information for the specified object.
Simon Huntd2747a02015-04-30 22:41:16 -0700356 protected void updateMetaUi(ObjectNode payload) {
357 metaUi.put(JsonUtils.string(payload, "id"),
358 JsonUtils.node(payload, "memento"));
Thomas Vachuska329af532015-03-10 02:08:33 -0700359 }
360
Simon Hunta17fa672015-08-19 18:42:22 -0700361
Simon Huntb745ca62015-07-28 15:37:11 -0700362 // -----------------------------------------------------------------------
363 // Create models of the data to return, that overlays can adjust / augment
364
365 // Returns property panel model for summary response.
Simon Hunt0af1ec32015-07-24 12:17:55 -0700366 protected PropertyPanel summmaryMessage(long sid) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700367 Topology topology = topologyService.currentTopology();
Simon Hunt0af1ec32015-07-24 12:17:55 -0700368
Simon Hunt00a27ff2015-07-28 18:53:40 -0700369 return new PropertyPanel("ONOS Summary", "node")
Simon Huntfb940112015-07-29 18:36:35 -0700370 .addProp(Properties.DEVICES, topology.deviceCount())
371 .addProp(Properties.LINKS, topology.linkCount())
372 .addProp(Properties.HOSTS, hostService.getHostCount())
373 .addProp(Properties.TOPOLOGY_SSCS, topology.clusterCount())
Simon Hunt00a27ff2015-07-28 18:53:40 -0700374 .addSeparator()
Simon Huntfb940112015-07-29 18:36:35 -0700375 .addProp(Properties.INTENTS, intentService.getIntentCount())
376 .addProp(Properties.TUNNELS, tunnelService.tunnelCount())
377 .addProp(Properties.FLOWS, flowService.getFlowRuleCount())
378 .addProp(Properties.VERSION, version);
Thomas Vachuska329af532015-03-10 02:08:33 -0700379 }
380
Simon Huntb745ca62015-07-28 15:37:11 -0700381 // Returns property panel model for device details response.
382 protected PropertyPanel deviceDetails(DeviceId deviceId, long sid) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700383 Device device = deviceService.getDevice(deviceId);
384 Annotations annot = device.annotations();
385 String name = annot.value(AnnotationKeys.NAME);
386 int portCount = deviceService.getPorts(deviceId).size();
387 int flowCount = getFlowCount(deviceId);
cheng fan35dc0f22015-06-10 06:02:47 +0800388 int tunnelCount = getTunnelCount(deviceId);
Simon Huntb745ca62015-07-28 15:37:11 -0700389
390 String title = isNullOrEmpty(name) ? deviceId.toString() : name;
391 String typeId = device.type().toString().toLowerCase();
392
393 PropertyPanel pp = new PropertyPanel(title, typeId)
Simon Huntfb940112015-07-29 18:36:35 -0700394 .id(deviceId.toString())
Simon Hunt3a0598f2015-08-04 19:59:04 -0700395
Simon Huntfb940112015-07-29 18:36:35 -0700396 .addProp(Properties.URI, deviceId.toString())
397 .addProp(Properties.VENDOR, device.manufacturer())
398 .addProp(Properties.HW_VERSION, device.hwVersion())
399 .addProp(Properties.SW_VERSION, device.swVersion())
400 .addProp(Properties.SERIAL_NUMBER, device.serialNumber())
401 .addProp(Properties.PROTOCOL, annot.value(AnnotationKeys.PROTOCOL))
402 .addSeparator()
Simon Hunt3a0598f2015-08-04 19:59:04 -0700403
Simon Huntfb940112015-07-29 18:36:35 -0700404 .addProp(Properties.LATITUDE, annot.value(AnnotationKeys.LATITUDE))
405 .addProp(Properties.LONGITUDE, annot.value(AnnotationKeys.LONGITUDE))
406 .addSeparator()
Simon Hunt3a0598f2015-08-04 19:59:04 -0700407
Simon Huntfb940112015-07-29 18:36:35 -0700408 .addProp(Properties.PORTS, portCount)
409 .addProp(Properties.FLOWS, flowCount)
Simon Hunt3a0598f2015-08-04 19:59:04 -0700410 .addProp(Properties.TUNNELS, tunnelCount)
Simon Hunt00a27ff2015-07-28 18:53:40 -0700411
Simon Hunt3a0598f2015-08-04 19:59:04 -0700412 .addButton(CoreButtons.SHOW_DEVICE_VIEW)
413 .addButton(CoreButtons.SHOW_FLOW_VIEW)
414 .addButton(CoreButtons.SHOW_PORT_VIEW)
415 .addButton(CoreButtons.SHOW_GROUP_VIEW);
Simon Huntb745ca62015-07-28 15:37:11 -0700416
417 return pp;
Thomas Vachuska329af532015-03-10 02:08:33 -0700418 }
419
420 protected int getFlowCount(DeviceId deviceId) {
421 int count = 0;
Simon Huntb745ca62015-07-28 15:37:11 -0700422 for (FlowEntry flowEntry : flowService.getFlowEntries(deviceId)) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700423 count++;
Thomas Vachuska329af532015-03-10 02:08:33 -0700424 }
425 return count;
426 }
427
cheng fan35dc0f22015-06-10 06:02:47 +0800428 protected int getTunnelCount(DeviceId deviceId) {
429 int count = 0;
430 Collection<Tunnel> tunnels = tunnelService.queryAllTunnels();
431 for (Tunnel tunnel : tunnels) {
432 OpticalTunnelEndPoint src = (OpticalTunnelEndPoint) tunnel.src();
433 OpticalTunnelEndPoint dst = (OpticalTunnelEndPoint) tunnel.dst();
434 DeviceId srcDevice = (DeviceId) src.elementId().get();
435 DeviceId dstDevice = (DeviceId) dst.elementId().get();
Simon Huntb745ca62015-07-28 15:37:11 -0700436 if (srcDevice.toString().equals(deviceId.toString()) ||
437 dstDevice.toString().equals(deviceId.toString())) {
cheng fan35dc0f22015-06-10 06:02:47 +0800438 count++;
439 }
440 }
441 return count;
442 }
443
Simon Hunta17fa672015-08-19 18:42:22 -0700444 // Counts all flow entries that egress on the links of the given device.
445 private Map<Link, Integer> getLinkFlowCounts(DeviceId deviceId) {
446 // get the flows for the device
Thomas Vachuska329af532015-03-10 02:08:33 -0700447 List<FlowEntry> entries = new ArrayList<>();
Simon Huntb745ca62015-07-28 15:37:11 -0700448 for (FlowEntry flowEntry : flowService.getFlowEntries(deviceId)) {
449 entries.add(flowEntry);
Thomas Vachuska329af532015-03-10 02:08:33 -0700450 }
451
Simon Hunta17fa672015-08-19 18:42:22 -0700452 // get egress links from device, and include edge links
453 Set<Link> links = new HashSet<>(linkService.getDeviceEgressLinks(deviceId));
454 Set<Host> hosts = hostService.getConnectedHosts(deviceId);
Thomas Vachuska329af532015-03-10 02:08:33 -0700455 if (hosts != null) {
456 for (Host host : hosts) {
Simon Hunta17fa672015-08-19 18:42:22 -0700457 links.add(createEdgeLink(host, false));
Thomas Vachuska329af532015-03-10 02:08:33 -0700458 }
459 }
460
Simon Hunta17fa672015-08-19 18:42:22 -0700461 // compile flow counts per link
Thomas Vachuska329af532015-03-10 02:08:33 -0700462 Map<Link, Integer> counts = new HashMap<>();
463 for (Link link : links) {
464 counts.put(link, getEgressFlows(link, entries));
465 }
466 return counts;
467 }
468
469 // Counts all entries that egress on the link source port.
Simon Hunta17fa672015-08-19 18:42:22 -0700470 private int getEgressFlows(Link link, List<FlowEntry> entries) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700471 int count = 0;
472 PortNumber out = link.src().port();
473 for (FlowEntry entry : entries) {
474 TrafficTreatment treatment = entry.treatment();
Ray Milkey42507352015-03-20 15:16:10 -0700475 for (Instruction instruction : treatment.allInstructions()) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700476 if (instruction.type() == Instruction.Type.OUTPUT &&
477 ((OutputInstruction) instruction).port().equals(out)) {
478 count++;
479 }
480 }
481 }
482 return count;
483 }
484
Thomas Vachuska329af532015-03-10 02:08:33 -0700485 // Returns host details response.
Simon Huntb745ca62015-07-28 15:37:11 -0700486 protected PropertyPanel hostDetails(HostId hostId, long sid) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700487 Host host = hostService.getHost(hostId);
488 Annotations annot = host.annotations();
489 String type = annot.value(AnnotationKeys.TYPE);
490 String name = annot.value(AnnotationKeys.NAME);
491 String vlan = host.vlan().toString();
Simon Huntb745ca62015-07-28 15:37:11 -0700492
493 String title = isNullOrEmpty(name) ? hostId.toString() : name;
494 String typeId = isNullOrEmpty(type) ? "endstation" : type;
495
496 PropertyPanel pp = new PropertyPanel(title, typeId)
Simon Huntfb940112015-07-29 18:36:35 -0700497 .id(hostId.toString())
498 .addProp(Properties.MAC, host.mac())
499 .addProp(Properties.IP, host.ipAddresses(), "[\\[\\]]")
500 .addProp(Properties.VLAN, vlan.equals("-1") ? "none" : vlan)
501 .addSeparator()
502 .addProp(Properties.LATITUDE, annot.value(AnnotationKeys.LATITUDE))
503 .addProp(Properties.LONGITUDE, annot.value(AnnotationKeys.LONGITUDE));
Simon Huntb745ca62015-07-28 15:37:11 -0700504
Simon Hunta17fa672015-08-19 18:42:22 -0700505 // Potentially add button descriptors here
Simon Huntb745ca62015-07-28 15:37:11 -0700506 return pp;
Thomas Vachuska329af532015-03-10 02:08:33 -0700507 }
508
Thomas Vachuska329af532015-03-10 02:08:33 -0700509}