Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.ui.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onlab.osgi.ServiceDirectory; |
| 22 | import org.onlab.packet.IpAddress; |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 23 | import org.onlab.util.DefaultHashMap; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 24 | import org.onosproject.cluster.ClusterEvent; |
| 25 | import org.onosproject.cluster.ClusterService; |
| 26 | import org.onosproject.cluster.ControllerNode; |
| 27 | import org.onosproject.cluster.NodeId; |
| 28 | import org.onosproject.core.CoreService; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 29 | import org.onosproject.incubator.net.PortStatisticsService; |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 30 | import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint; |
| 31 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 32 | import org.onosproject.incubator.net.tunnel.TunnelService; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 33 | import org.onosproject.mastership.MastershipService; |
| 34 | import org.onosproject.net.Annotated; |
| 35 | import org.onosproject.net.AnnotationKeys; |
| 36 | import org.onosproject.net.Annotations; |
| 37 | import org.onosproject.net.ConnectPoint; |
| 38 | import org.onosproject.net.DefaultEdgeLink; |
| 39 | import org.onosproject.net.Device; |
| 40 | import org.onosproject.net.DeviceId; |
| 41 | import org.onosproject.net.EdgeLink; |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 42 | import org.onosproject.net.ElementId; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 43 | import org.onosproject.net.Host; |
| 44 | import org.onosproject.net.HostId; |
| 45 | import org.onosproject.net.HostLocation; |
| 46 | import org.onosproject.net.Link; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 47 | import org.onosproject.net.PortNumber; |
| 48 | import org.onosproject.net.device.DeviceEvent; |
| 49 | import org.onosproject.net.device.DeviceService; |
| 50 | import org.onosproject.net.flow.FlowEntry; |
| 51 | import org.onosproject.net.flow.FlowRuleService; |
| 52 | import org.onosproject.net.flow.TrafficTreatment; |
| 53 | import org.onosproject.net.flow.instructions.Instruction; |
| 54 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 55 | import org.onosproject.net.host.HostEvent; |
| 56 | import org.onosproject.net.host.HostService; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 57 | import org.onosproject.net.intent.IntentService; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 58 | import org.onosproject.net.link.LinkEvent; |
| 59 | import org.onosproject.net.link.LinkService; |
| 60 | import org.onosproject.net.provider.ProviderId; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 61 | import org.onosproject.net.statistic.StatisticService; |
| 62 | import org.onosproject.net.topology.Topology; |
| 63 | import org.onosproject.net.topology.TopologyService; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 64 | import org.onosproject.ui.JsonUtils; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 65 | import org.onosproject.ui.UiConnection; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 66 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 67 | import org.onosproject.ui.impl.topo.util.ServicesBundle; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 68 | import org.onosproject.ui.topo.PropertyPanel; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 69 | import org.slf4j.Logger; |
| 70 | import org.slf4j.LoggerFactory; |
| 71 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 72 | import java.util.ArrayList; |
| 73 | import java.util.Collection; |
| 74 | import java.util.Collections; |
| 75 | import java.util.HashMap; |
| 76 | import java.util.HashSet; |
| 77 | import java.util.Iterator; |
| 78 | import java.util.List; |
| 79 | import java.util.Map; |
chengfan | c553c95 | 2016-07-22 15:48:23 +0800 | [diff] [blame] | 80 | import java.util.Optional; |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 81 | import java.util.Set; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 82 | import java.util.concurrent.ConcurrentHashMap; |
| 83 | |
| 84 | import static com.google.common.base.Preconditions.checkNotNull; |
| 85 | import static com.google.common.base.Strings.isNullOrEmpty; |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 86 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 87 | import static org.onosproject.net.PortNumber.portNumber; |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 88 | import static org.onosproject.ui.topo.TopoConstants.CoreButtons; |
| 89 | import static org.onosproject.ui.topo.TopoConstants.Properties; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 90 | import static org.onosproject.ui.topo.TopoUtils.compactLinkString; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Facility for creating messages bound for the topology viewer. |
| 94 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 95 | public abstract class TopologyViewMessageHandlerBase extends UiMessageHandler { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 96 | |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 97 | private static final String NO_GEO_VALUE = "0.0"; |
| 98 | |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 99 | // 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 Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 126 | protected static final Logger log = |
| 127 | LoggerFactory.getLogger(TopologyViewMessageHandlerBase.class); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 128 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 129 | private static final ProviderId PID = |
| 130 | new ProviderId("core", "org.onosproject.core", true); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 131 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 132 | protected static final String SHOW_HIGHLIGHTS = "showHighlights"; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 133 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 134 | 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 Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 142 | protected StatisticService flowStatsService; |
| 143 | protected PortStatisticsService portStatsService; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 144 | protected TopologyService topologyService; |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 145 | protected TunnelService tunnelService; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 146 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 147 | protected ServicesBundle servicesBundle; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 148 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 149 | 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 Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 155 | * 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 Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 174 | flowStatsService = directory.get(StatisticService.class); |
| 175 | portStatsService = directory.get(PortStatisticsService.class); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 176 | topologyService = directory.get(TopologyService.class); |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 177 | tunnelService = directory.get(TunnelService.class); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 178 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 179 | servicesBundle = new ServicesBundle(intentService, deviceService, |
| 180 | hostService, linkService, |
| 181 | flowService, |
| 182 | flowStatsService, portStatsService); |
| 183 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 184 | String ver = directory.get(CoreService.class).version().toString(); |
| 185 | version = ver.replace(".SNAPSHOT", "*").replaceFirst("~.*$", ""); |
| 186 | } |
| 187 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 188 | // 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 Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 196 | ObjectNode props = objectNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 197 | 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 Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 222 | ObjectNode payload = objectNode() |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 223 | .put("severity", severity) |
| 224 | .put("message", message); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 225 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 226 | return JsonUtils.envelope("message", id, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 229 | // Produces a cluster instance message to the client. |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 230 | protected ObjectNode instanceMessage(ClusterEvent event, String msgType) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 231 | ControllerNode node = event.subject(); |
| 232 | int switchCount = mastershipService.getDevicesOf(node.id()).size(); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 233 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 234 | .put("id", node.id().toString()) |
| 235 | .put("ip", node.ip().toString()) |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 236 | .put("online", clusterService.getState(node.id()).isActive()) |
Thomas Vachuska | fba7f3d | 2016-03-23 15:46:25 -0700 | [diff] [blame] | 237 | .put("ready", clusterService.getState(node.id()).isReady()) |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 238 | .put("uiAttached", node.equals(clusterService.getLocalNode())) |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 239 | .put("switches", switchCount); |
| 240 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 241 | ArrayNode labels = arrayNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 242 | 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 Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 249 | String type = msgType != null ? msgType : CLUSTER_EVENT.get(event.type()); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 250 | return JsonUtils.envelope(type, 0, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | // Produces a device event message to the client. |
| 254 | protected ObjectNode deviceMessage(DeviceEvent event) { |
| 255 | Device device = event.subject(); |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 256 | String uiType = device.annotations().value(AnnotationKeys.UI_TYPE); |
| 257 | String devType = uiType != null ? uiType : |
| 258 | device.type().toString().toLowerCase(); |
| 259 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 260 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 261 | .put("id", device.id().toString()) |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 262 | .put("type", devType) |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 263 | .put("online", deviceService.isAvailable(device.id())) |
| 264 | .put("master", master(device.id())); |
| 265 | |
| 266 | // Generate labels: id, chassis id, no-label, optional-name |
| 267 | String name = device.annotations().value(AnnotationKeys.NAME); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 268 | ArrayNode labels = arrayNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 269 | labels.add(""); |
| 270 | labels.add(isNullOrEmpty(name) ? device.id().toString() : name); |
| 271 | labels.add(device.id().toString()); |
| 272 | |
| 273 | // Add labels, props and stuff the payload into envelope. |
| 274 | payload.set("labels", labels); |
| 275 | payload.set("props", props(device.annotations())); |
| 276 | addGeoLocation(device, payload); |
| 277 | addMetaUi(device.id().toString(), payload); |
| 278 | |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 279 | String type = DEVICE_EVENT.get(event.type()); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 280 | return JsonUtils.envelope(type, 0, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | // Produces a link event message to the client. |
| 284 | protected ObjectNode linkMessage(LinkEvent event) { |
| 285 | Link link = event.subject(); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 286 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 287 | .put("id", compactLinkString(link)) |
| 288 | .put("type", link.type().toString().toLowerCase()) |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 289 | .put("expected", link.isExpected()) |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 290 | .put("online", link.state() == Link.State.ACTIVE) |
| 291 | .put("linkWidth", 1.2) |
| 292 | .put("src", link.src().deviceId().toString()) |
| 293 | .put("srcPort", link.src().port().toString()) |
| 294 | .put("dst", link.dst().deviceId().toString()) |
| 295 | .put("dstPort", link.dst().port().toString()); |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 296 | String type = LINK_EVENT.get(event.type()); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 297 | return JsonUtils.envelope(type, 0, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // Produces a host event message to the client. |
| 301 | protected ObjectNode hostMessage(HostEvent event) { |
| 302 | Host host = event.subject(); |
Charles Chan | 33f28a9 | 2015-11-13 13:12:38 -0800 | [diff] [blame] | 303 | Host prevHost = event.prevSubject(); |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 304 | String hostType = host.annotations().value(AnnotationKeys.UI_TYPE); |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 305 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 306 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 307 | .put("id", host.id().toString()) |
| 308 | .put("type", isNullOrEmpty(hostType) ? "endstation" : hostType) |
| 309 | .put("ingress", compactLinkString(edgeLink(host, true))) |
| 310 | .put("egress", compactLinkString(edgeLink(host, false))); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 311 | payload.set("cp", hostConnect(host.location())); |
Charles Chan | 33f28a9 | 2015-11-13 13:12:38 -0800 | [diff] [blame] | 312 | if (prevHost != null && prevHost.location() != null) { |
| 313 | payload.set("prevCp", hostConnect(prevHost.location())); |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 314 | } |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 315 | payload.set("labels", labels(ip(host.ipAddresses()), |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 316 | host.mac().toString())); |
| 317 | payload.set("props", props(host.annotations())); |
| 318 | addGeoLocation(host, payload); |
| 319 | addMetaUi(host.id().toString(), payload); |
| 320 | |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 321 | String type = HOST_EVENT.get(event.type()); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 322 | return JsonUtils.envelope(type, 0, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | // Encodes the specified host location into a JSON object. |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 326 | private ObjectNode hostConnect(HostLocation location) { |
| 327 | return objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 328 | .put("device", location.deviceId().toString()) |
| 329 | .put("port", location.port().toLong()); |
| 330 | } |
| 331 | |
| 332 | // Encodes the specified list of labels a JSON array. |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 333 | private ArrayNode labels(String... labels) { |
| 334 | ArrayNode json = arrayNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 335 | for (String label : labels) { |
| 336 | json.add(label); |
| 337 | } |
| 338 | return json; |
| 339 | } |
| 340 | |
| 341 | // Returns the name of the master node for the specified device id. |
| 342 | private String master(DeviceId deviceId) { |
| 343 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 344 | return master != null ? master.toString() : ""; |
| 345 | } |
| 346 | |
| 347 | // Generates an edge link from the specified host location. |
| 348 | private EdgeLink edgeLink(Host host, boolean ingress) { |
| 349 | return new DefaultEdgeLink(PID, new ConnectPoint(host.id(), portNumber(0)), |
| 350 | host.location(), ingress); |
| 351 | } |
| 352 | |
| 353 | // Adds meta UI information for the specified object. |
| 354 | private void addMetaUi(String id, ObjectNode payload) { |
| 355 | ObjectNode meta = metaUi.get(id); |
| 356 | if (meta != null) { |
| 357 | payload.set("metaUi", meta); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | // Adds a geo location JSON to the specified payload object. |
| 362 | private void addGeoLocation(Annotated annotated, ObjectNode payload) { |
| 363 | Annotations annotations = annotated.annotations(); |
| 364 | if (annotations == null) { |
| 365 | return; |
| 366 | } |
| 367 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 368 | String slng = annotations.value(AnnotationKeys.LONGITUDE); |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 369 | String slat = annotations.value(AnnotationKeys.LATITUDE); |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 370 | boolean validLng = slng != null && !slng.equals(NO_GEO_VALUE); |
| 371 | boolean validLat = slat != null && !slat.equals(NO_GEO_VALUE); |
| 372 | if (validLat && validLng) { |
| 373 | try { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 374 | double lng = Double.parseDouble(slng); |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 375 | double lat = Double.parseDouble(slat); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 376 | ObjectNode loc = objectNode() |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 377 | .put("type", "lnglat") |
| 378 | .put("lng", lng) |
| 379 | .put("lat", lat); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 380 | payload.set("location", loc); |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 381 | } catch (NumberFormatException e) { |
| 382 | log.warn("Invalid geo data: longitude={}, latitude={}", slng, slat); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 383 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
| 387 | // Updates meta UI information for the specified object. |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 388 | protected void updateMetaUi(ObjectNode payload) { |
| 389 | metaUi.put(JsonUtils.string(payload, "id"), |
| 390 | JsonUtils.node(payload, "memento")); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 393 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 394 | // ----------------------------------------------------------------------- |
| 395 | // Create models of the data to return, that overlays can adjust / augment |
| 396 | |
| 397 | // Returns property panel model for summary response. |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 398 | protected PropertyPanel summmaryMessage(long sid) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 399 | Topology topology = topologyService.currentTopology(); |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 400 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 401 | return new PropertyPanel("ONOS Summary", "node") |
Thomas Vachuska | e50b621 | 2015-12-02 08:00:09 -0800 | [diff] [blame] | 402 | .addProp(Properties.VERSION, version) |
| 403 | .addSeparator() |
chengfan | c553c95 | 2016-07-22 15:48:23 +0800 | [diff] [blame] | 404 | .addProp(Properties.DEVICES, deviceService.getDeviceCount()) |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 405 | .addProp(Properties.LINKS, topology.linkCount()) |
| 406 | .addProp(Properties.HOSTS, hostService.getHostCount()) |
| 407 | .addProp(Properties.TOPOLOGY_SSCS, topology.clusterCount()) |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 408 | .addSeparator() |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 409 | .addProp(Properties.INTENTS, intentService.getIntentCount()) |
| 410 | .addProp(Properties.TUNNELS, tunnelService.tunnelCount()) |
Thomas Vachuska | e50b621 | 2015-12-02 08:00:09 -0800 | [diff] [blame] | 411 | .addProp(Properties.FLOWS, flowService.getFlowRuleCount()); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 414 | // Returns property panel model for device details response. |
| 415 | protected PropertyPanel deviceDetails(DeviceId deviceId, long sid) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 416 | Device device = deviceService.getDevice(deviceId); |
| 417 | Annotations annot = device.annotations(); |
| 418 | String name = annot.value(AnnotationKeys.NAME); |
| 419 | int portCount = deviceService.getPorts(deviceId).size(); |
| 420 | int flowCount = getFlowCount(deviceId); |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 421 | int tunnelCount = getTunnelCount(deviceId); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 422 | |
| 423 | String title = isNullOrEmpty(name) ? deviceId.toString() : name; |
| 424 | String typeId = device.type().toString().toLowerCase(); |
| 425 | |
| 426 | PropertyPanel pp = new PropertyPanel(title, typeId) |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 427 | .id(deviceId.toString()) |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 428 | |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 429 | .addProp(Properties.URI, deviceId.toString()) |
| 430 | .addProp(Properties.VENDOR, device.manufacturer()) |
| 431 | .addProp(Properties.HW_VERSION, device.hwVersion()) |
| 432 | .addProp(Properties.SW_VERSION, device.swVersion()) |
| 433 | .addProp(Properties.SERIAL_NUMBER, device.serialNumber()) |
| 434 | .addProp(Properties.PROTOCOL, annot.value(AnnotationKeys.PROTOCOL)) |
| 435 | .addSeparator() |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 436 | |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 437 | .addProp(Properties.LATITUDE, annot.value(AnnotationKeys.LATITUDE)) |
| 438 | .addProp(Properties.LONGITUDE, annot.value(AnnotationKeys.LONGITUDE)) |
| 439 | .addSeparator() |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 440 | |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 441 | .addProp(Properties.PORTS, portCount) |
| 442 | .addProp(Properties.FLOWS, flowCount) |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 443 | .addProp(Properties.TUNNELS, tunnelCount) |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 444 | |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 445 | .addButton(CoreButtons.SHOW_DEVICE_VIEW) |
| 446 | .addButton(CoreButtons.SHOW_FLOW_VIEW) |
| 447 | .addButton(CoreButtons.SHOW_PORT_VIEW) |
Jian Li | 79f6732 | 2016-01-06 18:22:37 -0800 | [diff] [blame] | 448 | .addButton(CoreButtons.SHOW_GROUP_VIEW) |
| 449 | .addButton(CoreButtons.SHOW_METER_VIEW); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 450 | |
| 451 | return pp; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | protected int getFlowCount(DeviceId deviceId) { |
| 455 | int count = 0; |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 456 | for (FlowEntry flowEntry : flowService.getFlowEntries(deviceId)) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 457 | count++; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 458 | } |
| 459 | return count; |
| 460 | } |
| 461 | |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 462 | protected int getTunnelCount(DeviceId deviceId) { |
| 463 | int count = 0; |
| 464 | Collection<Tunnel> tunnels = tunnelService.queryAllTunnels(); |
| 465 | for (Tunnel tunnel : tunnels) { |
chengfan | c553c95 | 2016-07-22 15:48:23 +0800 | [diff] [blame] | 466 | //Only OpticalTunnelEndPoint has a device |
| 467 | if (!(tunnel.src() instanceof OpticalTunnelEndPoint) || |
| 468 | !(tunnel.dst() instanceof OpticalTunnelEndPoint)) { |
| 469 | continue; |
| 470 | } |
| 471 | |
| 472 | Optional<ElementId> srcElementId = ((OpticalTunnelEndPoint) tunnel.src()).elementId(); |
| 473 | Optional<ElementId> dstElementId = ((OpticalTunnelEndPoint) tunnel.dst()).elementId(); |
| 474 | if (!srcElementId.isPresent() || !dstElementId.isPresent()) { |
| 475 | continue; |
| 476 | } |
| 477 | DeviceId srcDeviceId = (DeviceId) srcElementId.get(); |
| 478 | DeviceId dstDeviceId = (DeviceId) dstElementId.get(); |
| 479 | if (srcDeviceId.equals(deviceId) || dstDeviceId.equals(deviceId)) { |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 480 | count++; |
| 481 | } |
| 482 | } |
| 483 | return count; |
| 484 | } |
| 485 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 486 | // Counts all flow entries that egress on the links of the given device. |
| 487 | private Map<Link, Integer> getLinkFlowCounts(DeviceId deviceId) { |
| 488 | // get the flows for the device |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 489 | List<FlowEntry> entries = new ArrayList<>(); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 490 | for (FlowEntry flowEntry : flowService.getFlowEntries(deviceId)) { |
| 491 | entries.add(flowEntry); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 494 | // get egress links from device, and include edge links |
| 495 | Set<Link> links = new HashSet<>(linkService.getDeviceEgressLinks(deviceId)); |
| 496 | Set<Host> hosts = hostService.getConnectedHosts(deviceId); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 497 | if (hosts != null) { |
| 498 | for (Host host : hosts) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 499 | links.add(createEdgeLink(host, false)); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 503 | // compile flow counts per link |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 504 | Map<Link, Integer> counts = new HashMap<>(); |
| 505 | for (Link link : links) { |
| 506 | counts.put(link, getEgressFlows(link, entries)); |
| 507 | } |
| 508 | return counts; |
| 509 | } |
| 510 | |
| 511 | // Counts all entries that egress on the link source port. |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 512 | private int getEgressFlows(Link link, List<FlowEntry> entries) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 513 | int count = 0; |
| 514 | PortNumber out = link.src().port(); |
| 515 | for (FlowEntry entry : entries) { |
| 516 | TrafficTreatment treatment = entry.treatment(); |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 517 | for (Instruction instruction : treatment.allInstructions()) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 518 | if (instruction.type() == Instruction.Type.OUTPUT && |
| 519 | ((OutputInstruction) instruction).port().equals(out)) { |
| 520 | count++; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | return count; |
| 525 | } |
| 526 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 527 | // Returns host details response. |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 528 | protected PropertyPanel hostDetails(HostId hostId, long sid) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 529 | Host host = hostService.getHost(hostId); |
| 530 | Annotations annot = host.annotations(); |
| 531 | String type = annot.value(AnnotationKeys.TYPE); |
| 532 | String name = annot.value(AnnotationKeys.NAME); |
| 533 | String vlan = host.vlan().toString(); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 534 | |
| 535 | String title = isNullOrEmpty(name) ? hostId.toString() : name; |
| 536 | String typeId = isNullOrEmpty(type) ? "endstation" : type; |
| 537 | |
| 538 | PropertyPanel pp = new PropertyPanel(title, typeId) |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 539 | .id(hostId.toString()) |
| 540 | .addProp(Properties.MAC, host.mac()) |
| 541 | .addProp(Properties.IP, host.ipAddresses(), "[\\[\\]]") |
| 542 | .addProp(Properties.VLAN, vlan.equals("-1") ? "none" : vlan) |
| 543 | .addSeparator() |
| 544 | .addProp(Properties.LATITUDE, annot.value(AnnotationKeys.LATITUDE)) |
| 545 | .addProp(Properties.LONGITUDE, annot.value(AnnotationKeys.LONGITUDE)); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 546 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 547 | // Potentially add button descriptors here |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 548 | return pp; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 551 | } |