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