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; |
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"; |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 77 | private static final String DASH = "-"; |
| 78 | |
| 79 | // nav paths are the view names for hot-link navigation from topo view... |
| 80 | private static final String DEVICE_NAV_PATH = "device"; |
| 81 | private static final String HOST_NAV_PATH = "host"; |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 82 | |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 83 | // default to an "add" event... |
| 84 | private static final DefaultHashMap<ClusterEvent.Type, String> CLUSTER_EVENT = |
| 85 | new DefaultHashMap<>("addInstance"); |
| 86 | |
| 87 | // default to an "update" event... |
| 88 | private static final DefaultHashMap<DeviceEvent.Type, String> DEVICE_EVENT = |
| 89 | new DefaultHashMap<>("updateDevice"); |
| 90 | private static final DefaultHashMap<LinkEvent.Type, String> LINK_EVENT = |
| 91 | new DefaultHashMap<>("updateLink"); |
| 92 | private static final DefaultHashMap<HostEvent.Type, String> HOST_EVENT = |
| 93 | new DefaultHashMap<>("updateHost"); |
| 94 | |
| 95 | // but call out specific events that we care to differentiate... |
| 96 | static { |
| 97 | CLUSTER_EVENT.put(ClusterEvent.Type.INSTANCE_REMOVED, "removeInstance"); |
| 98 | |
| 99 | DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_ADDED, "addDevice"); |
| 100 | DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_REMOVED, "removeDevice"); |
| 101 | |
| 102 | LINK_EVENT.put(LinkEvent.Type.LINK_ADDED, "addLink"); |
| 103 | LINK_EVENT.put(LinkEvent.Type.LINK_REMOVED, "removeLink"); |
| 104 | |
| 105 | HOST_EVENT.put(HostEvent.Type.HOST_ADDED, "addHost"); |
| 106 | HOST_EVENT.put(HostEvent.Type.HOST_REMOVED, "removeHost"); |
| 107 | HOST_EVENT.put(HostEvent.Type.HOST_MOVED, "moveHost"); |
| 108 | } |
| 109 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 110 | protected static final Logger log = |
| 111 | LoggerFactory.getLogger(TopologyViewMessageHandlerBase.class); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 112 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 113 | private static final ProviderId PID = |
| 114 | new ProviderId("core", "org.onosproject.core", true); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 115 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 116 | // TODO: extract into an external & durable state; good enough for now and demo |
| 117 | private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>(); |
| 118 | |
| 119 | /** |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 120 | * Returns read-only view of the meta-ui information. |
| 121 | * |
| 122 | * @return map of id to meta-ui mementos |
| 123 | */ |
| 124 | static Map<String, ObjectNode> getMetaUi() { |
| 125 | return Collections.unmodifiableMap(metaUi); |
| 126 | } |
| 127 | |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 128 | |
| 129 | protected ServicesBundle services; |
| 130 | |
| 131 | private String version; |
| 132 | |
| 133 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 134 | @Override |
| 135 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 136 | super.init(connection, directory); |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 137 | services = new ServicesBundle(directory); |
| 138 | setVersionString(directory); |
| 139 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 140 | |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 141 | // Creates a palatable version string to display on the summary panel |
| 142 | private void setVersionString(ServiceDirectory directory) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 143 | String ver = directory.get(CoreService.class).version().toString(); |
| 144 | version = ver.replace(".SNAPSHOT", "*").replaceFirst("~.*$", ""); |
| 145 | } |
| 146 | |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 147 | // Returns the first of the given set of IP addresses as a string. |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 148 | private String ip(Set<IpAddress> ipAddresses) { |
| 149 | Iterator<IpAddress> it = ipAddresses.iterator(); |
| 150 | return it.hasNext() ? it.next().toString() : "unknown"; |
| 151 | } |
| 152 | |
| 153 | // Produces JSON structure from annotations. |
| 154 | private JsonNode props(Annotations annotations) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 155 | ObjectNode props = objectNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 156 | if (annotations != null) { |
| 157 | for (String key : annotations.keys()) { |
| 158 | props.put(key, annotations.value(key)); |
| 159 | } |
| 160 | } |
| 161 | return props; |
| 162 | } |
| 163 | |
| 164 | // Produces an informational log message event bound to the client. |
Simon Hunt | 36740d0 | 2017-06-07 11:25:51 -0700 | [diff] [blame] | 165 | protected ObjectNode info(String message) { |
| 166 | return message("info", message); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | // Produces a warning log message event bound to the client. |
Simon Hunt | 36740d0 | 2017-06-07 11:25:51 -0700 | [diff] [blame] | 170 | protected ObjectNode warning(String message) { |
| 171 | return message("warning", message); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // Produces an error log message event bound to the client. |
Simon Hunt | 36740d0 | 2017-06-07 11:25:51 -0700 | [diff] [blame] | 175 | protected ObjectNode error(String message) { |
| 176 | return message("error", message); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // Produces a log message event bound to the client. |
Simon Hunt | 36740d0 | 2017-06-07 11:25:51 -0700 | [diff] [blame] | 180 | private ObjectNode message(String severity, String message) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 181 | ObjectNode payload = objectNode() |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 182 | .put("severity", severity) |
| 183 | .put("message", message); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 184 | |
Simon Hunt | 36740d0 | 2017-06-07 11:25:51 -0700 | [diff] [blame] | 185 | return JsonUtils.envelope("message", payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 188 | // Produces a cluster instance message to the client. |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 189 | protected ObjectNode instanceMessage(ClusterEvent event, String msgType) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 190 | ControllerNode node = event.subject(); |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 191 | int switchCount = services.mastership().getDevicesOf(node.id()).size(); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 192 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 193 | .put("id", node.id().toString()) |
| 194 | .put("ip", node.ip().toString()) |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 195 | .put("online", services.cluster().getState(node.id()).isActive()) |
| 196 | .put("ready", services.cluster().getState(node.id()).isReady()) |
| 197 | .put("uiAttached", node.equals(services.cluster().getLocalNode())) |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 198 | .put("switches", switchCount); |
| 199 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 200 | ArrayNode labels = arrayNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 201 | labels.add(node.id().toString()); |
| 202 | labels.add(node.ip().toString()); |
| 203 | |
| 204 | // Add labels, props and stuff the payload into envelope. |
| 205 | payload.set("labels", labels); |
| 206 | addMetaUi(node.id().toString(), payload); |
| 207 | |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 208 | String type = msgType != null ? msgType : CLUSTER_EVENT.get(event.type()); |
Simon Hunt | 36740d0 | 2017-06-07 11:25:51 -0700 | [diff] [blame] | 209 | return JsonUtils.envelope(type, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // Produces a device event message to the client. |
| 213 | protected ObjectNode deviceMessage(DeviceEvent event) { |
| 214 | Device device = event.subject(); |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 215 | String uiType = device.annotations().value(AnnotationKeys.UI_TYPE); |
| 216 | String devType = uiType != null ? uiType : |
| 217 | device.type().toString().toLowerCase(); |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 218 | String name = device.annotations().value(AnnotationKeys.NAME); |
| 219 | name = isNullOrEmpty(name) ? device.id().toString() : name; |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 220 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 221 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 222 | .put("id", device.id().toString()) |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 223 | .put("type", devType) |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 224 | .put("online", services.device().isAvailable(device.id())) |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 225 | .put("master", master(device.id())); |
| 226 | |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 227 | payload.set("labels", labels("", name, device.id().toString())); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 228 | payload.set("props", props(device.annotations())); |
| 229 | addGeoLocation(device, payload); |
| 230 | addMetaUi(device.id().toString(), payload); |
| 231 | |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 232 | String type = DEVICE_EVENT.get(event.type()); |
Simon Hunt | 36740d0 | 2017-06-07 11:25:51 -0700 | [diff] [blame] | 233 | return JsonUtils.envelope(type, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // Produces a link event message to the client. |
| 237 | protected ObjectNode linkMessage(LinkEvent event) { |
| 238 | Link link = event.subject(); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 239 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 240 | .put("id", compactLinkString(link)) |
| 241 | .put("type", link.type().toString().toLowerCase()) |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 242 | .put("expected", link.isExpected()) |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 243 | .put("online", link.state() == Link.State.ACTIVE) |
| 244 | .put("linkWidth", 1.2) |
| 245 | .put("src", link.src().deviceId().toString()) |
| 246 | .put("srcPort", link.src().port().toString()) |
| 247 | .put("dst", link.dst().deviceId().toString()) |
| 248 | .put("dstPort", link.dst().port().toString()); |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 249 | String type = LINK_EVENT.get(event.type()); |
Simon Hunt | 36740d0 | 2017-06-07 11:25:51 -0700 | [diff] [blame] | 250 | return JsonUtils.envelope(type, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | // Produces a host event message to the client. |
| 254 | protected ObjectNode hostMessage(HostEvent event) { |
| 255 | Host host = event.subject(); |
Charles Chan | 33f28a9 | 2015-11-13 13:12:38 -0800 | [diff] [blame] | 256 | Host prevHost = event.prevSubject(); |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 257 | String hostType = host.annotations().value(AnnotationKeys.UI_TYPE); |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 258 | String ip = ip(host.ipAddresses()); |
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 | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 265 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 266 | payload.set("cp", hostConnect(host.location())); |
Charles Chan | 33f28a9 | 2015-11-13 13:12:38 -0800 | [diff] [blame] | 267 | if (prevHost != null && prevHost.location() != null) { |
| 268 | payload.set("prevCp", hostConnect(prevHost.location())); |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 269 | } |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 270 | payload.set("labels", labels(nameForHost(host), ip, host.mac().toString())); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 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 | 36740d0 | 2017-06-07 11:25:51 -0700 | [diff] [blame] | 276 | return JsonUtils.envelope(type, 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) |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 381 | .navPath(DEVICE_NAV_PATH) |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 382 | .id(deviceId.toString()) |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 383 | |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 384 | .addProp(Properties.URI, deviceId.toString()) |
| 385 | .addProp(Properties.VENDOR, device.manufacturer()) |
| 386 | .addProp(Properties.HW_VERSION, device.hwVersion()) |
| 387 | .addProp(Properties.SW_VERSION, device.swVersion()) |
| 388 | .addProp(Properties.SERIAL_NUMBER, device.serialNumber()) |
| 389 | .addProp(Properties.PROTOCOL, annot.value(AnnotationKeys.PROTOCOL)) |
| 390 | .addSeparator() |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 391 | |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 392 | .addProp(Properties.LATITUDE, annot.value(AnnotationKeys.LATITUDE)) |
| 393 | .addProp(Properties.LONGITUDE, annot.value(AnnotationKeys.LONGITUDE)) |
| 394 | .addSeparator() |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 395 | |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 396 | .addProp(Properties.PORTS, portCount) |
| 397 | .addProp(Properties.FLOWS, flowCount) |
| 398 | .addProp(Properties.TUNNELS, tunnelCount) |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 399 | |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 400 | .addButton(CoreButtons.SHOW_DEVICE_VIEW) |
| 401 | .addButton(CoreButtons.SHOW_FLOW_VIEW) |
| 402 | .addButton(CoreButtons.SHOW_PORT_VIEW) |
| 403 | .addButton(CoreButtons.SHOW_GROUP_VIEW) |
| 404 | .addButton(CoreButtons.SHOW_METER_VIEW); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | protected int getFlowCount(DeviceId deviceId) { |
| 408 | int count = 0; |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 409 | for (FlowEntry flowEntry : services.flow().getFlowEntries(deviceId)) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 410 | count++; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 411 | } |
| 412 | return count; |
| 413 | } |
| 414 | |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 415 | protected int getTunnelCount(DeviceId deviceId) { |
| 416 | int count = 0; |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 417 | Collection<Tunnel> tunnels = services.tunnel().queryAllTunnels(); |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 418 | for (Tunnel tunnel : tunnels) { |
chengfan | c553c95 | 2016-07-22 15:48:23 +0800 | [diff] [blame] | 419 | //Only OpticalTunnelEndPoint has a device |
| 420 | if (!(tunnel.src() instanceof OpticalTunnelEndPoint) || |
| 421 | !(tunnel.dst() instanceof OpticalTunnelEndPoint)) { |
| 422 | continue; |
| 423 | } |
| 424 | |
| 425 | Optional<ElementId> srcElementId = ((OpticalTunnelEndPoint) tunnel.src()).elementId(); |
| 426 | Optional<ElementId> dstElementId = ((OpticalTunnelEndPoint) tunnel.dst()).elementId(); |
| 427 | if (!srcElementId.isPresent() || !dstElementId.isPresent()) { |
| 428 | continue; |
| 429 | } |
| 430 | DeviceId srcDeviceId = (DeviceId) srcElementId.get(); |
| 431 | DeviceId dstDeviceId = (DeviceId) dstElementId.get(); |
| 432 | if (srcDeviceId.equals(deviceId) || dstDeviceId.equals(deviceId)) { |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 433 | count++; |
| 434 | } |
| 435 | } |
| 436 | return count; |
| 437 | } |
| 438 | |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 439 | private boolean useDefaultName(String annotName) { |
| 440 | return isNullOrEmpty(annotName) || DASH.equals(annotName); |
| 441 | } |
| 442 | |
| 443 | private String nameForHost(Host host) { |
| 444 | String name = host.annotations().value(AnnotationKeys.NAME); |
| 445 | return useDefaultName(name) ? ip(host.ipAddresses()) : name; |
| 446 | } |
| 447 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 448 | // Returns host details response. |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 449 | protected PropertyPanel hostDetails(HostId hostId) { |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 450 | Host host = services.host().getHost(hostId); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 451 | Annotations annot = host.annotations(); |
| 452 | String type = annot.value(AnnotationKeys.TYPE); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 453 | String vlan = host.vlan().toString(); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 454 | String typeId = isNullOrEmpty(type) ? "endstation" : type; |
| 455 | |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 456 | return new PropertyPanel(nameForHost(host), typeId) |
| 457 | .navPath(HOST_NAV_PATH) |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 458 | .id(hostId.toString()) |
| 459 | .addProp(Properties.MAC, host.mac()) |
| 460 | .addProp(Properties.IP, host.ipAddresses(), "[\\[\\]]") |
| 461 | .addProp(Properties.VLAN, "-1".equals(vlan) ? "none" : vlan) |
| 462 | .addSeparator() |
| 463 | .addProp(Properties.LATITUDE, annot.value(AnnotationKeys.LATITUDE)) |
| 464 | .addProp(Properties.LONGITUDE, annot.value(AnnotationKeys.LONGITUDE)); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 467 | } |