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.device.DeviceEvent; |
| 48 | import org.onosproject.net.device.DeviceService; |
| 49 | import org.onosproject.net.flow.FlowEntry; |
| 50 | import org.onosproject.net.flow.FlowRuleService; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 51 | import org.onosproject.net.host.HostEvent; |
| 52 | import org.onosproject.net.host.HostService; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 53 | import org.onosproject.net.intent.IntentService; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 54 | import org.onosproject.net.link.LinkEvent; |
| 55 | import org.onosproject.net.link.LinkService; |
| 56 | import org.onosproject.net.provider.ProviderId; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 57 | import org.onosproject.net.statistic.StatisticService; |
| 58 | import org.onosproject.net.topology.Topology; |
| 59 | import org.onosproject.net.topology.TopologyService; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 60 | import org.onosproject.ui.JsonUtils; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 61 | import org.onosproject.ui.UiConnection; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 62 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 63 | import org.onosproject.ui.impl.topo.util.ServicesBundle; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 64 | import org.onosproject.ui.topo.PropertyPanel; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 65 | import org.slf4j.Logger; |
| 66 | import org.slf4j.LoggerFactory; |
| 67 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 68 | import java.util.Collection; |
| 69 | import java.util.Collections; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 70 | import java.util.Iterator; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 71 | import java.util.Map; |
chengfan | c553c95 | 2016-07-22 15:48:23 +0800 | [diff] [blame] | 72 | import java.util.Optional; |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 73 | import java.util.Set; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 74 | import java.util.concurrent.ConcurrentHashMap; |
| 75 | |
| 76 | import static com.google.common.base.Preconditions.checkNotNull; |
| 77 | import static com.google.common.base.Strings.isNullOrEmpty; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 78 | import static org.onosproject.net.PortNumber.portNumber; |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 79 | import static org.onosproject.ui.topo.TopoConstants.CoreButtons; |
| 80 | import static org.onosproject.ui.topo.TopoConstants.Properties; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 81 | import static org.onosproject.ui.topo.TopoUtils.compactLinkString; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Facility for creating messages bound for the topology viewer. |
| 85 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 86 | public abstract class TopologyViewMessageHandlerBase extends UiMessageHandler { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 87 | |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 88 | private static final String NO_GEO_VALUE = "0.0"; |
| 89 | |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 90 | // default to an "add" event... |
| 91 | private static final DefaultHashMap<ClusterEvent.Type, String> CLUSTER_EVENT = |
| 92 | new DefaultHashMap<>("addInstance"); |
| 93 | |
| 94 | // default to an "update" event... |
| 95 | private static final DefaultHashMap<DeviceEvent.Type, String> DEVICE_EVENT = |
| 96 | new DefaultHashMap<>("updateDevice"); |
| 97 | private static final DefaultHashMap<LinkEvent.Type, String> LINK_EVENT = |
| 98 | new DefaultHashMap<>("updateLink"); |
| 99 | private static final DefaultHashMap<HostEvent.Type, String> HOST_EVENT = |
| 100 | new DefaultHashMap<>("updateHost"); |
| 101 | |
| 102 | // but call out specific events that we care to differentiate... |
| 103 | static { |
| 104 | CLUSTER_EVENT.put(ClusterEvent.Type.INSTANCE_REMOVED, "removeInstance"); |
| 105 | |
| 106 | DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_ADDED, "addDevice"); |
| 107 | DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_REMOVED, "removeDevice"); |
| 108 | |
| 109 | LINK_EVENT.put(LinkEvent.Type.LINK_ADDED, "addLink"); |
| 110 | LINK_EVENT.put(LinkEvent.Type.LINK_REMOVED, "removeLink"); |
| 111 | |
| 112 | HOST_EVENT.put(HostEvent.Type.HOST_ADDED, "addHost"); |
| 113 | HOST_EVENT.put(HostEvent.Type.HOST_REMOVED, "removeHost"); |
| 114 | HOST_EVENT.put(HostEvent.Type.HOST_MOVED, "moveHost"); |
| 115 | } |
| 116 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 117 | protected static final Logger log = |
| 118 | LoggerFactory.getLogger(TopologyViewMessageHandlerBase.class); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 119 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 120 | private static final ProviderId PID = |
| 121 | new ProviderId("core", "org.onosproject.core", true); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 122 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 123 | protected static final String SHOW_HIGHLIGHTS = "showHighlights"; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 124 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 125 | protected ServiceDirectory directory; |
| 126 | protected ClusterService clusterService; |
| 127 | protected DeviceService deviceService; |
| 128 | protected LinkService linkService; |
| 129 | protected HostService hostService; |
| 130 | protected MastershipService mastershipService; |
| 131 | protected IntentService intentService; |
| 132 | protected FlowRuleService flowService; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 133 | protected StatisticService flowStatsService; |
| 134 | protected PortStatisticsService portStatsService; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 135 | protected TopologyService topologyService; |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 136 | protected TunnelService tunnelService; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 137 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 138 | protected ServicesBundle servicesBundle; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 139 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 140 | private String version; |
| 141 | |
| 142 | // TODO: extract into an external & durable state; good enough for now and demo |
| 143 | private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>(); |
| 144 | |
| 145 | /** |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 146 | * Returns read-only view of the meta-ui information. |
| 147 | * |
| 148 | * @return map of id to meta-ui mementos |
| 149 | */ |
| 150 | static Map<String, ObjectNode> getMetaUi() { |
| 151 | return Collections.unmodifiableMap(metaUi); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 156 | super.init(connection, directory); |
| 157 | this.directory = checkNotNull(directory, "Directory cannot be null"); |
| 158 | clusterService = directory.get(ClusterService.class); |
| 159 | deviceService = directory.get(DeviceService.class); |
| 160 | linkService = directory.get(LinkService.class); |
| 161 | hostService = directory.get(HostService.class); |
| 162 | mastershipService = directory.get(MastershipService.class); |
| 163 | intentService = directory.get(IntentService.class); |
| 164 | flowService = directory.get(FlowRuleService.class); |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 165 | flowStatsService = directory.get(StatisticService.class); |
| 166 | portStatsService = directory.get(PortStatisticsService.class); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 167 | topologyService = directory.get(TopologyService.class); |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 168 | tunnelService = directory.get(TunnelService.class); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 169 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 170 | servicesBundle = new ServicesBundle(intentService, deviceService, |
| 171 | hostService, linkService, |
| 172 | flowService, |
| 173 | flowStatsService, portStatsService); |
| 174 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 175 | String ver = directory.get(CoreService.class).version().toString(); |
| 176 | version = ver.replace(".SNAPSHOT", "*").replaceFirst("~.*$", ""); |
| 177 | } |
| 178 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 179 | // Returns the specified set of IP addresses as a string. |
| 180 | private String ip(Set<IpAddress> ipAddresses) { |
| 181 | Iterator<IpAddress> it = ipAddresses.iterator(); |
| 182 | return it.hasNext() ? it.next().toString() : "unknown"; |
| 183 | } |
| 184 | |
| 185 | // Produces JSON structure from annotations. |
| 186 | private JsonNode props(Annotations annotations) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 187 | ObjectNode props = objectNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 188 | if (annotations != null) { |
| 189 | for (String key : annotations.keys()) { |
| 190 | props.put(key, annotations.value(key)); |
| 191 | } |
| 192 | } |
| 193 | return props; |
| 194 | } |
| 195 | |
| 196 | // Produces an informational log message event bound to the client. |
| 197 | protected ObjectNode info(long id, String message) { |
| 198 | return message("info", id, message); |
| 199 | } |
| 200 | |
| 201 | // Produces a warning log message event bound to the client. |
| 202 | protected ObjectNode warning(long id, String message) { |
| 203 | return message("warning", id, message); |
| 204 | } |
| 205 | |
| 206 | // Produces an error log message event bound to the client. |
| 207 | protected ObjectNode error(long id, String message) { |
| 208 | return message("error", id, message); |
| 209 | } |
| 210 | |
| 211 | // Produces a log message event bound to the client. |
| 212 | private ObjectNode message(String severity, long id, String message) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 213 | ObjectNode payload = objectNode() |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 214 | .put("severity", severity) |
| 215 | .put("message", message); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 216 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 217 | return JsonUtils.envelope("message", id, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 220 | // Produces a cluster instance message to the client. |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 221 | protected ObjectNode instanceMessage(ClusterEvent event, String msgType) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 222 | ControllerNode node = event.subject(); |
| 223 | int switchCount = mastershipService.getDevicesOf(node.id()).size(); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 224 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 225 | .put("id", node.id().toString()) |
| 226 | .put("ip", node.ip().toString()) |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 227 | .put("online", clusterService.getState(node.id()).isActive()) |
Thomas Vachuska | fba7f3d | 2016-03-23 15:46:25 -0700 | [diff] [blame] | 228 | .put("ready", clusterService.getState(node.id()).isReady()) |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 229 | .put("uiAttached", node.equals(clusterService.getLocalNode())) |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 230 | .put("switches", switchCount); |
| 231 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 232 | ArrayNode labels = arrayNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 233 | labels.add(node.id().toString()); |
| 234 | labels.add(node.ip().toString()); |
| 235 | |
| 236 | // Add labels, props and stuff the payload into envelope. |
| 237 | payload.set("labels", labels); |
| 238 | addMetaUi(node.id().toString(), payload); |
| 239 | |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 240 | String type = msgType != null ? msgType : CLUSTER_EVENT.get(event.type()); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 241 | return JsonUtils.envelope(type, 0, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // Produces a device event message to the client. |
| 245 | protected ObjectNode deviceMessage(DeviceEvent event) { |
| 246 | Device device = event.subject(); |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 247 | String uiType = device.annotations().value(AnnotationKeys.UI_TYPE); |
| 248 | String devType = uiType != null ? uiType : |
| 249 | device.type().toString().toLowerCase(); |
| 250 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 251 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 252 | .put("id", device.id().toString()) |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 253 | .put("type", devType) |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 254 | .put("online", deviceService.isAvailable(device.id())) |
| 255 | .put("master", master(device.id())); |
| 256 | |
| 257 | // Generate labels: id, chassis id, no-label, optional-name |
| 258 | String name = device.annotations().value(AnnotationKeys.NAME); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 259 | ArrayNode labels = arrayNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 260 | labels.add(""); |
| 261 | labels.add(isNullOrEmpty(name) ? device.id().toString() : name); |
| 262 | labels.add(device.id().toString()); |
| 263 | |
| 264 | // Add labels, props and stuff the payload into envelope. |
| 265 | payload.set("labels", labels); |
| 266 | payload.set("props", props(device.annotations())); |
| 267 | addGeoLocation(device, payload); |
| 268 | addMetaUi(device.id().toString(), payload); |
| 269 | |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 270 | String type = DEVICE_EVENT.get(event.type()); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 271 | return JsonUtils.envelope(type, 0, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // Produces a link event message to the client. |
| 275 | protected ObjectNode linkMessage(LinkEvent event) { |
| 276 | Link link = event.subject(); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 277 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 278 | .put("id", compactLinkString(link)) |
| 279 | .put("type", link.type().toString().toLowerCase()) |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 280 | .put("expected", link.isExpected()) |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 281 | .put("online", link.state() == Link.State.ACTIVE) |
| 282 | .put("linkWidth", 1.2) |
| 283 | .put("src", link.src().deviceId().toString()) |
| 284 | .put("srcPort", link.src().port().toString()) |
| 285 | .put("dst", link.dst().deviceId().toString()) |
| 286 | .put("dstPort", link.dst().port().toString()); |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 287 | String type = LINK_EVENT.get(event.type()); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 288 | return JsonUtils.envelope(type, 0, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // Produces a host event message to the client. |
| 292 | protected ObjectNode hostMessage(HostEvent event) { |
| 293 | Host host = event.subject(); |
Charles Chan | 33f28a9 | 2015-11-13 13:12:38 -0800 | [diff] [blame] | 294 | Host prevHost = event.prevSubject(); |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 295 | String hostType = host.annotations().value(AnnotationKeys.UI_TYPE); |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 296 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 297 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 298 | .put("id", host.id().toString()) |
| 299 | .put("type", isNullOrEmpty(hostType) ? "endstation" : hostType) |
| 300 | .put("ingress", compactLinkString(edgeLink(host, true))) |
| 301 | .put("egress", compactLinkString(edgeLink(host, false))); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 302 | payload.set("cp", hostConnect(host.location())); |
Charles Chan | 33f28a9 | 2015-11-13 13:12:38 -0800 | [diff] [blame] | 303 | if (prevHost != null && prevHost.location() != null) { |
| 304 | payload.set("prevCp", hostConnect(prevHost.location())); |
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 | payload.set("labels", labels(ip(host.ipAddresses()), |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 307 | host.mac().toString())); |
| 308 | payload.set("props", props(host.annotations())); |
| 309 | addGeoLocation(host, payload); |
| 310 | addMetaUi(host.id().toString(), payload); |
| 311 | |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 312 | String type = HOST_EVENT.get(event.type()); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 313 | return JsonUtils.envelope(type, 0, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // Encodes the specified host location into a JSON object. |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 317 | private ObjectNode hostConnect(HostLocation location) { |
| 318 | return objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 319 | .put("device", location.deviceId().toString()) |
| 320 | .put("port", location.port().toLong()); |
| 321 | } |
| 322 | |
| 323 | // Encodes the specified list of labels a JSON array. |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 324 | private ArrayNode labels(String... labels) { |
| 325 | ArrayNode json = arrayNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 326 | for (String label : labels) { |
| 327 | json.add(label); |
| 328 | } |
| 329 | return json; |
| 330 | } |
| 331 | |
| 332 | // Returns the name of the master node for the specified device id. |
| 333 | private String master(DeviceId deviceId) { |
| 334 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 335 | return master != null ? master.toString() : ""; |
| 336 | } |
| 337 | |
| 338 | // Generates an edge link from the specified host location. |
| 339 | private EdgeLink edgeLink(Host host, boolean ingress) { |
| 340 | return new DefaultEdgeLink(PID, new ConnectPoint(host.id(), portNumber(0)), |
| 341 | host.location(), ingress); |
| 342 | } |
| 343 | |
| 344 | // Adds meta UI information for the specified object. |
| 345 | private void addMetaUi(String id, ObjectNode payload) { |
| 346 | ObjectNode meta = metaUi.get(id); |
| 347 | if (meta != null) { |
| 348 | payload.set("metaUi", meta); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | // Adds a geo location JSON to the specified payload object. |
| 353 | private void addGeoLocation(Annotated annotated, ObjectNode payload) { |
| 354 | Annotations annotations = annotated.annotations(); |
| 355 | if (annotations == null) { |
| 356 | return; |
| 357 | } |
| 358 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 359 | String slng = annotations.value(AnnotationKeys.LONGITUDE); |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 360 | String slat = annotations.value(AnnotationKeys.LATITUDE); |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 361 | boolean validLng = slng != null && !slng.equals(NO_GEO_VALUE); |
| 362 | boolean validLat = slat != null && !slat.equals(NO_GEO_VALUE); |
| 363 | if (validLat && validLng) { |
| 364 | try { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 365 | double lng = Double.parseDouble(slng); |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 366 | double lat = Double.parseDouble(slat); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 367 | ObjectNode loc = objectNode() |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 368 | .put("type", "lnglat") |
| 369 | .put("lng", lng) |
| 370 | .put("lat", lat); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 371 | payload.set("location", loc); |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 372 | } catch (NumberFormatException e) { |
| 373 | log.warn("Invalid geo data: longitude={}, latitude={}", slng, slat); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 374 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | // Updates meta UI information for the specified object. |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 379 | protected void updateMetaUi(ObjectNode payload) { |
| 380 | metaUi.put(JsonUtils.string(payload, "id"), |
| 381 | JsonUtils.node(payload, "memento")); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 384 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 385 | // ----------------------------------------------------------------------- |
| 386 | // Create models of the data to return, that overlays can adjust / augment |
| 387 | |
| 388 | // Returns property panel model for summary response. |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 389 | protected PropertyPanel summmaryMessage() { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 390 | Topology topology = topologyService.currentTopology(); |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 391 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 392 | return new PropertyPanel("ONOS Summary", "node") |
Thomas Vachuska | e50b621 | 2015-12-02 08:00:09 -0800 | [diff] [blame] | 393 | .addProp(Properties.VERSION, version) |
| 394 | .addSeparator() |
chengfan | c553c95 | 2016-07-22 15:48:23 +0800 | [diff] [blame] | 395 | .addProp(Properties.DEVICES, deviceService.getDeviceCount()) |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 396 | .addProp(Properties.LINKS, topology.linkCount()) |
| 397 | .addProp(Properties.HOSTS, hostService.getHostCount()) |
| 398 | .addProp(Properties.TOPOLOGY_SSCS, topology.clusterCount()) |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 399 | .addSeparator() |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 400 | .addProp(Properties.INTENTS, intentService.getIntentCount()) |
| 401 | .addProp(Properties.TUNNELS, tunnelService.tunnelCount()) |
Thomas Vachuska | e50b621 | 2015-12-02 08:00:09 -0800 | [diff] [blame] | 402 | .addProp(Properties.FLOWS, flowService.getFlowRuleCount()); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 405 | // Returns property panel model for device details response. |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 406 | protected PropertyPanel deviceDetails(DeviceId deviceId) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 407 | Device device = deviceService.getDevice(deviceId); |
| 408 | Annotations annot = device.annotations(); |
| 409 | String name = annot.value(AnnotationKeys.NAME); |
| 410 | int portCount = deviceService.getPorts(deviceId).size(); |
| 411 | int flowCount = getFlowCount(deviceId); |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 412 | int tunnelCount = getTunnelCount(deviceId); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 413 | |
| 414 | String title = isNullOrEmpty(name) ? deviceId.toString() : name; |
| 415 | String typeId = device.type().toString().toLowerCase(); |
| 416 | |
| 417 | PropertyPanel pp = new PropertyPanel(title, typeId) |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 418 | .id(deviceId.toString()) |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 419 | |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 420 | .addProp(Properties.URI, deviceId.toString()) |
| 421 | .addProp(Properties.VENDOR, device.manufacturer()) |
| 422 | .addProp(Properties.HW_VERSION, device.hwVersion()) |
| 423 | .addProp(Properties.SW_VERSION, device.swVersion()) |
| 424 | .addProp(Properties.SERIAL_NUMBER, device.serialNumber()) |
| 425 | .addProp(Properties.PROTOCOL, annot.value(AnnotationKeys.PROTOCOL)) |
| 426 | .addSeparator() |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 427 | |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 428 | .addProp(Properties.LATITUDE, annot.value(AnnotationKeys.LATITUDE)) |
| 429 | .addProp(Properties.LONGITUDE, annot.value(AnnotationKeys.LONGITUDE)) |
| 430 | .addSeparator() |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 431 | |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 432 | .addProp(Properties.PORTS, portCount) |
| 433 | .addProp(Properties.FLOWS, flowCount) |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 434 | .addProp(Properties.TUNNELS, tunnelCount) |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 435 | |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 436 | .addButton(CoreButtons.SHOW_DEVICE_VIEW) |
| 437 | .addButton(CoreButtons.SHOW_FLOW_VIEW) |
| 438 | .addButton(CoreButtons.SHOW_PORT_VIEW) |
Jian Li | 79f6732 | 2016-01-06 18:22:37 -0800 | [diff] [blame] | 439 | .addButton(CoreButtons.SHOW_GROUP_VIEW) |
| 440 | .addButton(CoreButtons.SHOW_METER_VIEW); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 441 | |
| 442 | return pp; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | protected int getFlowCount(DeviceId deviceId) { |
| 446 | int count = 0; |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 447 | for (FlowEntry flowEntry : flowService.getFlowEntries(deviceId)) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 448 | count++; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 449 | } |
| 450 | return count; |
| 451 | } |
| 452 | |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 453 | protected int getTunnelCount(DeviceId deviceId) { |
| 454 | int count = 0; |
| 455 | Collection<Tunnel> tunnels = tunnelService.queryAllTunnels(); |
| 456 | for (Tunnel tunnel : tunnels) { |
chengfan | c553c95 | 2016-07-22 15:48:23 +0800 | [diff] [blame] | 457 | //Only OpticalTunnelEndPoint has a device |
| 458 | if (!(tunnel.src() instanceof OpticalTunnelEndPoint) || |
| 459 | !(tunnel.dst() instanceof OpticalTunnelEndPoint)) { |
| 460 | continue; |
| 461 | } |
| 462 | |
| 463 | Optional<ElementId> srcElementId = ((OpticalTunnelEndPoint) tunnel.src()).elementId(); |
| 464 | Optional<ElementId> dstElementId = ((OpticalTunnelEndPoint) tunnel.dst()).elementId(); |
| 465 | if (!srcElementId.isPresent() || !dstElementId.isPresent()) { |
| 466 | continue; |
| 467 | } |
| 468 | DeviceId srcDeviceId = (DeviceId) srcElementId.get(); |
| 469 | DeviceId dstDeviceId = (DeviceId) dstElementId.get(); |
| 470 | if (srcDeviceId.equals(deviceId) || dstDeviceId.equals(deviceId)) { |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 471 | count++; |
| 472 | } |
| 473 | } |
| 474 | return count; |
| 475 | } |
| 476 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 477 | // Returns host details response. |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 478 | protected PropertyPanel hostDetails(HostId hostId) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 479 | Host host = hostService.getHost(hostId); |
| 480 | Annotations annot = host.annotations(); |
| 481 | String type = annot.value(AnnotationKeys.TYPE); |
| 482 | String name = annot.value(AnnotationKeys.NAME); |
| 483 | String vlan = host.vlan().toString(); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 484 | |
| 485 | String title = isNullOrEmpty(name) ? hostId.toString() : name; |
| 486 | String typeId = isNullOrEmpty(type) ? "endstation" : type; |
| 487 | |
| 488 | PropertyPanel pp = new PropertyPanel(title, typeId) |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 489 | .id(hostId.toString()) |
| 490 | .addProp(Properties.MAC, host.mac()) |
| 491 | .addProp(Properties.IP, host.ipAddresses(), "[\\[\\]]") |
| 492 | .addProp(Properties.VLAN, vlan.equals("-1") ? "none" : vlan) |
| 493 | .addSeparator() |
| 494 | .addProp(Properties.LATITUDE, annot.value(AnnotationKeys.LATITUDE)) |
| 495 | .addProp(Properties.LONGITUDE, annot.value(AnnotationKeys.LONGITUDE)); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 496 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 497 | // Potentially add button descriptors here |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 498 | return pp; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 501 | } |