Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -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; |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 22 | import org.onlab.osgi.ServiceDirectory; |
| 23 | import org.onlab.packet.IpAddress; |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 24 | import org.onlab.packet.VlanId; |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 25 | import org.onlab.util.DefaultHashMap; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 26 | import org.onosproject.cluster.ClusterEvent; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 27 | import org.onosproject.cluster.ControllerNode; |
| 28 | import org.onosproject.cluster.NodeId; |
| 29 | import org.onosproject.core.CoreService; |
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; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 32 | import org.onosproject.net.Annotated; |
| 33 | import org.onosproject.net.AnnotationKeys; |
| 34 | import org.onosproject.net.Annotations; |
| 35 | import org.onosproject.net.ConnectPoint; |
| 36 | import org.onosproject.net.DefaultEdgeLink; |
| 37 | import org.onosproject.net.Device; |
| 38 | import org.onosproject.net.DeviceId; |
| 39 | import org.onosproject.net.EdgeLink; |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 40 | import org.onosproject.net.ElementId; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 41 | import org.onosproject.net.Host; |
| 42 | import org.onosproject.net.HostId; |
| 43 | import org.onosproject.net.HostLocation; |
| 44 | import org.onosproject.net.Link; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 45 | import org.onosproject.net.device.DeviceEvent; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 46 | import org.onosproject.net.flow.FlowEntry; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 47 | import org.onosproject.net.host.HostEvent; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 48 | import org.onosproject.net.link.LinkEvent; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 49 | import org.onosproject.net.provider.ProviderId; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 50 | import org.onosproject.net.topology.Topology; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 51 | import org.onosproject.ui.JsonUtils; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 52 | import org.onosproject.ui.UiConnection; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 53 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 54 | import org.onosproject.ui.impl.topo.util.ServicesBundle; |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 55 | import org.onosproject.ui.lion.LionBundle; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 56 | import org.onosproject.ui.topo.PropertyPanel; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 57 | import org.slf4j.Logger; |
| 58 | import org.slf4j.LoggerFactory; |
| 59 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 60 | import java.util.Collection; |
| 61 | import java.util.Collections; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 62 | import java.util.Iterator; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 63 | import java.util.Map; |
chengfan | c553c95 | 2016-07-22 15:48:23 +0800 | [diff] [blame] | 64 | import java.util.Optional; |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 65 | import java.util.Set; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 66 | import java.util.concurrent.ConcurrentHashMap; |
| 67 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 68 | import static com.google.common.base.Strings.isNullOrEmpty; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 69 | import static org.onosproject.net.PortNumber.portNumber; |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 70 | import static org.onosproject.ui.topo.TopoConstants.CoreButtons; |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 71 | import static org.onosproject.ui.topo.TopoConstants.Properties.DEVICES; |
| 72 | import static org.onosproject.ui.topo.TopoConstants.Properties.FLOWS; |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 73 | import static org.onosproject.ui.topo.TopoConstants.Properties.GRID_X; |
| 74 | import static org.onosproject.ui.topo.TopoConstants.Properties.GRID_Y; |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 75 | import static org.onosproject.ui.topo.TopoConstants.Properties.HOSTS; |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 76 | import static org.onosproject.ui.topo.TopoConstants.Properties.HW_VERSION; |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 77 | import static org.onosproject.ui.topo.TopoConstants.Properties.INTENTS; |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 78 | import static org.onosproject.ui.topo.TopoConstants.Properties.IP; |
| 79 | import static org.onosproject.ui.topo.TopoConstants.Properties.LATITUDE; |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 80 | import static org.onosproject.ui.topo.TopoConstants.Properties.LINKS; |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 81 | import static org.onosproject.ui.topo.TopoConstants.Properties.LONGITUDE; |
| 82 | import static org.onosproject.ui.topo.TopoConstants.Properties.MAC; |
| 83 | import static org.onosproject.ui.topo.TopoConstants.Properties.PORTS; |
| 84 | import static org.onosproject.ui.topo.TopoConstants.Properties.PROTOCOL; |
| 85 | import static org.onosproject.ui.topo.TopoConstants.Properties.SERIAL_NUMBER; |
| 86 | import static org.onosproject.ui.topo.TopoConstants.Properties.SW_VERSION; |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 87 | import static org.onosproject.ui.topo.TopoConstants.Properties.TOPOLOGY_SSCS; |
| 88 | import static org.onosproject.ui.topo.TopoConstants.Properties.TUNNELS; |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 89 | import static org.onosproject.ui.topo.TopoConstants.Properties.URI; |
| 90 | import static org.onosproject.ui.topo.TopoConstants.Properties.VENDOR; |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 91 | import static org.onosproject.ui.topo.TopoConstants.Properties.VERSION; |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 92 | import static org.onosproject.ui.topo.TopoConstants.Properties.VLAN; |
| 93 | import static org.onosproject.ui.topo.TopoConstants.Properties.VLAN_NONE; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 94 | import static org.onosproject.ui.topo.TopoUtils.compactLinkString; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * Facility for creating messages bound for the topology viewer. |
| 98 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 99 | public abstract class TopologyViewMessageHandlerBase extends UiMessageHandler { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 100 | |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 101 | private static final String NO_GEO_VALUE = "0.0"; |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 102 | private static final String DASH = "-"; |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 103 | private static final String SLASH = " / "; |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 104 | |
| 105 | // nav paths are the view names for hot-link navigation from topo view... |
| 106 | private static final String DEVICE_NAV_PATH = "device"; |
| 107 | private static final String HOST_NAV_PATH = "host"; |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 108 | |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 109 | // link panel label keys |
| 110 | private static final String LPL_FRIENDLY = "lp_label_friendly"; |
| 111 | private static final String LPL_A_TYPE = "lp_label_a_type"; |
| 112 | private static final String LPL_A_ID = "lp_label_a_id"; |
| 113 | private static final String LPL_A_FRIENDLY = "lp_label_a_friendly"; |
| 114 | private static final String LPL_A_PORT = "lp_label_a_port"; |
| 115 | private static final String LPL_B_TYPE = "lp_label_b_type"; |
| 116 | private static final String LPL_B_ID = "lp_label_b_id"; |
| 117 | private static final String LPL_B_FRIENDLY = "lp_label_b_friendly"; |
| 118 | private static final String LPL_B_PORT = "lp_label_b_port"; |
| 119 | private static final String LPL_A2B = "lp_label_a2b"; |
| 120 | private static final String LPL_B2A = "lp_label_b2a"; |
| 121 | private static final String LPV_NO_LINK = "lp_value_no_link"; |
| 122 | |
| 123 | // other Lion keys |
| 124 | private static final String HOST = "host"; |
| 125 | private static final String DEVICE = "device"; |
| 126 | private static final String EXPECTED = "expected"; |
| 127 | private static final String NOT_EXPECTED = "not_expected"; |
| 128 | |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 129 | // default to an "add" event... |
| 130 | private static final DefaultHashMap<ClusterEvent.Type, String> CLUSTER_EVENT = |
| 131 | new DefaultHashMap<>("addInstance"); |
| 132 | |
| 133 | // default to an "update" event... |
| 134 | private static final DefaultHashMap<DeviceEvent.Type, String> DEVICE_EVENT = |
| 135 | new DefaultHashMap<>("updateDevice"); |
| 136 | private static final DefaultHashMap<LinkEvent.Type, String> LINK_EVENT = |
| 137 | new DefaultHashMap<>("updateLink"); |
| 138 | private static final DefaultHashMap<HostEvent.Type, String> HOST_EVENT = |
| 139 | new DefaultHashMap<>("updateHost"); |
| 140 | |
| 141 | // but call out specific events that we care to differentiate... |
| 142 | static { |
| 143 | CLUSTER_EVENT.put(ClusterEvent.Type.INSTANCE_REMOVED, "removeInstance"); |
| 144 | |
| 145 | DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_ADDED, "addDevice"); |
| 146 | DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_REMOVED, "removeDevice"); |
| 147 | |
| 148 | LINK_EVENT.put(LinkEvent.Type.LINK_ADDED, "addLink"); |
| 149 | LINK_EVENT.put(LinkEvent.Type.LINK_REMOVED, "removeLink"); |
| 150 | |
| 151 | HOST_EVENT.put(HostEvent.Type.HOST_ADDED, "addHost"); |
| 152 | HOST_EVENT.put(HostEvent.Type.HOST_REMOVED, "removeHost"); |
| 153 | HOST_EVENT.put(HostEvent.Type.HOST_MOVED, "moveHost"); |
| 154 | } |
| 155 | |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 156 | private static final DefaultHashMap<Device.Type, String> DEVICE_GLYPHS = |
| 157 | new DefaultHashMap<>("m_unknown"); |
| 158 | |
| 159 | static { |
| 160 | DEVICE_GLYPHS.put(Device.Type.SWITCH, "m_switch"); |
| 161 | DEVICE_GLYPHS.put(Device.Type.ROUTER, "m_router"); |
| 162 | DEVICE_GLYPHS.put(Device.Type.ROADM, "m_roadm"); |
| 163 | DEVICE_GLYPHS.put(Device.Type.OTN, "m_otn"); |
| 164 | DEVICE_GLYPHS.put(Device.Type.ROADM_OTN, "m_roadm_otn"); |
| 165 | DEVICE_GLYPHS.put(Device.Type.BALANCER, "m_balancer"); |
| 166 | DEVICE_GLYPHS.put(Device.Type.IPS, "m_ips"); |
| 167 | DEVICE_GLYPHS.put(Device.Type.IDS, "m_ids"); |
| 168 | DEVICE_GLYPHS.put(Device.Type.CONTROLLER, "m_controller"); |
| 169 | DEVICE_GLYPHS.put(Device.Type.VIRTUAL, "m_virtual"); |
| 170 | DEVICE_GLYPHS.put(Device.Type.FIBER_SWITCH, "m_fiberSwitch"); |
| 171 | DEVICE_GLYPHS.put(Device.Type.MICROWAVE, "m_microwave"); |
| 172 | DEVICE_GLYPHS.put(Device.Type.OLT, "m_olt"); |
| 173 | DEVICE_GLYPHS.put(Device.Type.ONU, "m_onu"); |
| 174 | DEVICE_GLYPHS.put(Device.Type.OPTICAL_AMPLIFIER, "unknown"); // TODO glyph needed |
| 175 | DEVICE_GLYPHS.put(Device.Type.OTHER, "m_other"); |
| 176 | } |
| 177 | |
| 178 | private static final String DEFAULT_HOST_GLYPH = "m_endstation"; |
| 179 | private static final String LINK_GLYPH = "m_ports"; |
| 180 | |
| 181 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 182 | static final Logger log = |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 183 | LoggerFactory.getLogger(TopologyViewMessageHandlerBase.class); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 184 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 185 | private static final ProviderId PID = |
| 186 | new ProviderId("core", "org.onosproject.core", true); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 187 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 188 | // TODO: extract into an external & durable state; good enough for now and demo |
| 189 | private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>(); |
| 190 | |
| 191 | /** |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 192 | * Returns read-only view of the meta-ui information. |
| 193 | * |
| 194 | * @return map of id to meta-ui mementos |
| 195 | */ |
| 196 | static Map<String, ObjectNode> getMetaUi() { |
| 197 | return Collections.unmodifiableMap(metaUi); |
| 198 | } |
| 199 | |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 200 | private static final String LION_TOPO = "core.view.Topo"; |
| 201 | |
| 202 | private static final Set<String> REQ_LION_BUNDLES = ImmutableSet.of( |
| 203 | LION_TOPO |
| 204 | ); |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 205 | |
| 206 | protected ServicesBundle services; |
| 207 | |
| 208 | private String version; |
| 209 | |
| 210 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 211 | @Override |
| 212 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 213 | super.init(connection, directory); |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 214 | services = new ServicesBundle(directory); |
| 215 | setVersionString(directory); |
| 216 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 217 | |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 218 | // Creates a palatable version string to display on the summary panel |
| 219 | private void setVersionString(ServiceDirectory directory) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 220 | String ver = directory.get(CoreService.class).version().toString(); |
| 221 | version = ver.replace(".SNAPSHOT", "*").replaceFirst("~.*$", ""); |
| 222 | } |
| 223 | |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 224 | @Override |
| 225 | public Set<String> requiredLionBundles() { |
| 226 | return REQ_LION_BUNDLES; |
| 227 | } |
| 228 | |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 229 | // 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] | 230 | private String ip(Set<IpAddress> ipAddresses) { |
| 231 | Iterator<IpAddress> it = ipAddresses.iterator(); |
| 232 | return it.hasNext() ? it.next().toString() : "unknown"; |
| 233 | } |
| 234 | |
| 235 | // Produces JSON structure from annotations. |
| 236 | private JsonNode props(Annotations annotations) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 237 | ObjectNode props = objectNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 238 | if (annotations != null) { |
| 239 | for (String key : annotations.keys()) { |
| 240 | props.put(key, annotations.value(key)); |
| 241 | } |
| 242 | } |
| 243 | return props; |
| 244 | } |
| 245 | |
| 246 | // Produces an informational log message event bound to the client. |
Simon Hunt | 36740d0 | 2017-06-07 11:25:51 -0700 | [diff] [blame] | 247 | protected ObjectNode info(String message) { |
| 248 | return message("info", message); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | // Produces a warning log message event bound to the client. |
Simon Hunt | 36740d0 | 2017-06-07 11:25:51 -0700 | [diff] [blame] | 252 | protected ObjectNode warning(String message) { |
| 253 | return message("warning", message); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // Produces an error log message event bound to the client. |
Simon Hunt | 36740d0 | 2017-06-07 11:25:51 -0700 | [diff] [blame] | 257 | protected ObjectNode error(String message) { |
| 258 | return message("error", message); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // Produces a log message event bound to the client. |
Simon Hunt | 36740d0 | 2017-06-07 11:25:51 -0700 | [diff] [blame] | 262 | private ObjectNode message(String severity, String message) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 263 | ObjectNode payload = objectNode() |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 264 | .put("severity", severity) |
| 265 | .put("message", message); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 266 | |
Simon Hunt | 36740d0 | 2017-06-07 11:25:51 -0700 | [diff] [blame] | 267 | return JsonUtils.envelope("message", payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 270 | // Produces a cluster instance message to the client. |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 271 | protected ObjectNode instanceMessage(ClusterEvent event, String msgType) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 272 | ControllerNode node = event.subject(); |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 273 | int switchCount = services.mastership().getDevicesOf(node.id()).size(); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 274 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 275 | .put("id", node.id().toString()) |
| 276 | .put("ip", node.ip().toString()) |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 277 | .put("online", services.cluster().getState(node.id()).isActive()) |
| 278 | .put("ready", services.cluster().getState(node.id()).isReady()) |
| 279 | .put("uiAttached", node.equals(services.cluster().getLocalNode())) |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 280 | .put("switches", switchCount); |
| 281 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 282 | ArrayNode labels = arrayNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 283 | labels.add(node.id().toString()); |
| 284 | labels.add(node.ip().toString()); |
| 285 | |
| 286 | // Add labels, props and stuff the payload into envelope. |
| 287 | payload.set("labels", labels); |
| 288 | addMetaUi(node.id().toString(), payload); |
| 289 | |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 290 | String type = msgType != null ? msgType : CLUSTER_EVENT.get(event.type()); |
Simon Hunt | 36740d0 | 2017-06-07 11:25:51 -0700 | [diff] [blame] | 291 | return JsonUtils.envelope(type, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // Produces a device event message to the client. |
| 295 | protected ObjectNode deviceMessage(DeviceEvent event) { |
| 296 | Device device = event.subject(); |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 297 | String uiType = device.annotations().value(AnnotationKeys.UI_TYPE); |
| 298 | String devType = uiType != null ? uiType : |
| 299 | device.type().toString().toLowerCase(); |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 300 | String name = device.annotations().value(AnnotationKeys.NAME); |
| 301 | name = isNullOrEmpty(name) ? device.id().toString() : name; |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 302 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 303 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 304 | .put("id", device.id().toString()) |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 305 | .put("type", devType) |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 306 | .put("online", services.device().isAvailable(device.id())) |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 307 | .put("master", master(device.id())); |
| 308 | |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 309 | payload.set("labels", labels("", name, device.id().toString())); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 310 | payload.set("props", props(device.annotations())); |
| 311 | addGeoLocation(device, payload); |
| 312 | addMetaUi(device.id().toString(), payload); |
| 313 | |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 314 | String type = DEVICE_EVENT.get(event.type()); |
Simon Hunt | 36740d0 | 2017-06-07 11:25:51 -0700 | [diff] [blame] | 315 | return JsonUtils.envelope(type, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // Produces a link event message to the client. |
| 319 | protected ObjectNode linkMessage(LinkEvent event) { |
| 320 | Link link = event.subject(); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 321 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 322 | .put("id", compactLinkString(link)) |
| 323 | .put("type", link.type().toString().toLowerCase()) |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 324 | .put("expected", link.isExpected()) |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 325 | .put("online", link.state() == Link.State.ACTIVE) |
| 326 | .put("linkWidth", 1.2) |
| 327 | .put("src", link.src().deviceId().toString()) |
| 328 | .put("srcPort", link.src().port().toString()) |
| 329 | .put("dst", link.dst().deviceId().toString()) |
| 330 | .put("dstPort", link.dst().port().toString()); |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 331 | String type = LINK_EVENT.get(event.type()); |
Simon Hunt | 36740d0 | 2017-06-07 11:25:51 -0700 | [diff] [blame] | 332 | return JsonUtils.envelope(type, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | // Produces a host event message to the client. |
| 336 | protected ObjectNode hostMessage(HostEvent event) { |
| 337 | Host host = event.subject(); |
Charles Chan | 33f28a9 | 2015-11-13 13:12:38 -0800 | [diff] [blame] | 338 | Host prevHost = event.prevSubject(); |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 339 | String hostType = host.annotations().value(AnnotationKeys.UI_TYPE); |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 340 | String ip = ip(host.ipAddresses()); |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 341 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 342 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 343 | .put("id", host.id().toString()) |
Simon Hunt | 12c79ed | 2017-09-12 11:58:44 -0700 | [diff] [blame] | 344 | .put("type", isNullOrEmpty(hostType) ? "endstation" : hostType); |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 345 | |
Simon Hunt | 12c79ed | 2017-09-12 11:58:44 -0700 | [diff] [blame] | 346 | // set most recent connect point (and previous if we know it) |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 347 | payload.set("cp", hostConnect(host.location())); |
Charles Chan | 33f28a9 | 2015-11-13 13:12:38 -0800 | [diff] [blame] | 348 | if (prevHost != null && prevHost.location() != null) { |
| 349 | payload.set("prevCp", hostConnect(prevHost.location())); |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 350 | } |
Simon Hunt | 12c79ed | 2017-09-12 11:58:44 -0700 | [diff] [blame] | 351 | |
| 352 | // set ALL connect points |
| 353 | addAllCps(host.locations(), payload); |
| 354 | |
Thomas Vachuska | 528ea95 | 2017-10-09 18:25:35 +0200 | [diff] [blame] | 355 | payload.set("labels", labels(nameForHost(host), ip, host.mac().toString(), "")); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 356 | payload.set("props", props(host.annotations())); |
| 357 | addGeoLocation(host, payload); |
| 358 | addMetaUi(host.id().toString(), payload); |
| 359 | |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 360 | String type = HOST_EVENT.get(event.type()); |
Simon Hunt | 36740d0 | 2017-06-07 11:25:51 -0700 | [diff] [blame] | 361 | return JsonUtils.envelope(type, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Simon Hunt | 12c79ed | 2017-09-12 11:58:44 -0700 | [diff] [blame] | 364 | private void addAllCps(Set<HostLocation> locations, ObjectNode payload) { |
| 365 | ArrayNode cps = arrayNode(); |
| 366 | locations.forEach(loc -> cps.add(hostConnect(loc))); |
| 367 | payload.set("allCps", cps); |
| 368 | } |
| 369 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 370 | // Encodes the specified host location into a JSON object. |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 371 | private ObjectNode hostConnect(HostLocation location) { |
| 372 | return objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 373 | .put("device", location.deviceId().toString()) |
| 374 | .put("port", location.port().toLong()); |
| 375 | } |
| 376 | |
| 377 | // Encodes the specified list of labels a JSON array. |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 378 | private ArrayNode labels(String... labels) { |
| 379 | ArrayNode json = arrayNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 380 | for (String label : labels) { |
| 381 | json.add(label); |
| 382 | } |
| 383 | return json; |
| 384 | } |
| 385 | |
| 386 | // Returns the name of the master node for the specified device id. |
| 387 | private String master(DeviceId deviceId) { |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 388 | NodeId master = services.mastership().getMasterFor(deviceId); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 389 | return master != null ? master.toString() : ""; |
| 390 | } |
| 391 | |
| 392 | // Generates an edge link from the specified host location. |
| 393 | private EdgeLink edgeLink(Host host, boolean ingress) { |
| 394 | return new DefaultEdgeLink(PID, new ConnectPoint(host.id(), portNumber(0)), |
| 395 | host.location(), ingress); |
| 396 | } |
| 397 | |
| 398 | // Adds meta UI information for the specified object. |
| 399 | private void addMetaUi(String id, ObjectNode payload) { |
| 400 | ObjectNode meta = metaUi.get(id); |
| 401 | if (meta != null) { |
| 402 | payload.set("metaUi", meta); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | // Adds a geo location JSON to the specified payload object. |
| 407 | private void addGeoLocation(Annotated annotated, ObjectNode payload) { |
| 408 | Annotations annotations = annotated.annotations(); |
| 409 | if (annotations == null) { |
| 410 | return; |
| 411 | } |
| 412 | |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 413 | String slat = annotations.value(AnnotationKeys.LATITUDE); |
Simon Hunt | f27a929 | 2017-05-04 17:36:26 -0700 | [diff] [blame] | 414 | String slng = annotations.value(AnnotationKeys.LONGITUDE); |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 415 | boolean validLat = slat != null && !slat.equals(NO_GEO_VALUE); |
Simon Hunt | f27a929 | 2017-05-04 17:36:26 -0700 | [diff] [blame] | 416 | boolean validLng = slng != null && !slng.equals(NO_GEO_VALUE); |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 417 | if (validLat && validLng) { |
| 418 | try { |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 419 | double lat = Double.parseDouble(slat); |
Simon Hunt | f27a929 | 2017-05-04 17:36:26 -0700 | [diff] [blame] | 420 | double lng = Double.parseDouble(slng); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 421 | ObjectNode loc = objectNode() |
Simon Hunt | f27a929 | 2017-05-04 17:36:26 -0700 | [diff] [blame] | 422 | .put("locType", "geo") |
| 423 | .put("latOrY", lat) |
| 424 | .put("longOrX", lng); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 425 | payload.set("location", loc); |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 426 | } catch (NumberFormatException e) { |
Simon Hunt | f27a929 | 2017-05-04 17:36:26 -0700 | [diff] [blame] | 427 | log.warn("Invalid geo data: latitude={}, longitude={}", slat, slng); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 428 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
| 432 | // Updates meta UI information for the specified object. |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 433 | protected void updateMetaUi(ObjectNode payload) { |
| 434 | metaUi.put(JsonUtils.string(payload, "id"), |
| 435 | JsonUtils.node(payload, "memento")); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 438 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 439 | // ----------------------------------------------------------------------- |
| 440 | // Create models of the data to return, that overlays can adjust / augment |
| 441 | |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 442 | private String lookupGlyph(Device device) { |
| 443 | return DEVICE_GLYPHS.get(device.type()); |
| 444 | } |
| 445 | |
| 446 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 447 | // Returns property panel model for summary response. |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 448 | protected PropertyPanel summmaryMessage() { |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 449 | // chose NOT to add debug messages, since this is called every few seconds |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 450 | Topology topology = services.topology().currentTopology(); |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 451 | LionBundle lion = getLionBundle(LION_TOPO); |
| 452 | String panelTitle = lion.getSafe("title_panel_summary"); |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 453 | |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 454 | return new PropertyPanel(panelTitle, "bird") |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 455 | .addProp(VERSION, lion.getSafe(VERSION), version) |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 456 | .addSeparator() |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 457 | .addProp(DEVICES, lion.getSafe(DEVICES), services.device().getDeviceCount()) |
| 458 | .addProp(LINKS, lion.getSafe(LINKS), topology.linkCount()) |
| 459 | .addProp(HOSTS, lion.getSafe(HOSTS), services.host().getHostCount()) |
| 460 | .addProp(TOPOLOGY_SSCS, lion.getSafe(TOPOLOGY_SSCS), topology.clusterCount()) |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 461 | .addSeparator() |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 462 | .addProp(INTENTS, lion.getSafe(INTENTS), services.intent().getIntentCount()) |
| 463 | .addProp(TUNNELS, lion.getSafe(TUNNELS), services.tunnel().tunnelCount()) |
| 464 | .addProp(FLOWS, lion.getSafe(FLOWS), services.flow().getFlowRuleCount()); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 467 | |
| 468 | private String friendlyDevice(DeviceId deviceId) { |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 469 | Device device = services.device().getDevice(deviceId); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 470 | Annotations annot = device.annotations(); |
| 471 | String name = annot.value(AnnotationKeys.NAME); |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 472 | return isNullOrEmpty(name) ? deviceId.toString() : name; |
| 473 | } |
| 474 | |
| 475 | // Generates a property panel model for device details response |
| 476 | protected PropertyPanel deviceDetails(DeviceId deviceId) { |
| 477 | log.debug("generate prop panel data for device {}", deviceId); |
| 478 | Device device = services.device().getDevice(deviceId); |
| 479 | Annotations annot = device.annotations(); |
| 480 | String proto = annot.value(AnnotationKeys.PROTOCOL); |
| 481 | String title = friendlyDevice(deviceId); |
| 482 | LionBundle lion = getLionBundle(LION_TOPO); |
| 483 | |
| 484 | PropertyPanel pp = new PropertyPanel(title, lookupGlyph(device)) |
| 485 | .navPath(DEVICE_NAV_PATH) |
| 486 | .id(deviceId.toString()); |
| 487 | addDeviceBasicProps(pp, deviceId, device, proto, lion); |
| 488 | addLocationProps(pp, annot, lion); |
| 489 | addDeviceCountStats(pp, deviceId, lion); |
| 490 | addDeviceCoreButtons(pp); |
| 491 | return pp; |
| 492 | } |
| 493 | |
| 494 | private void addDeviceBasicProps(PropertyPanel pp, DeviceId deviceId, |
| 495 | Device device, String proto, LionBundle lion) { |
| 496 | pp.addProp(URI, lion.getSafe(URI), deviceId.toString()) |
| 497 | .addProp(VENDOR, lion.getSafe(VENDOR), device.manufacturer()) |
| 498 | .addProp(HW_VERSION, lion.getSafe(HW_VERSION), device.hwVersion()) |
| 499 | .addProp(SW_VERSION, lion.getSafe(SW_VERSION), device.swVersion()) |
| 500 | .addProp(SERIAL_NUMBER, lion.getSafe(SERIAL_NUMBER), device.serialNumber()) |
| 501 | .addProp(PROTOCOL, lion.getSafe(PROTOCOL), proto) |
| 502 | .addSeparator(); |
| 503 | } |
| 504 | |
| 505 | // only add location properties if we have them |
| 506 | private void addLocationProps(PropertyPanel pp, Annotations annot, |
| 507 | LionBundle lion) { |
| 508 | String slat = annot.value(AnnotationKeys.LATITUDE); |
| 509 | String slng = annot.value(AnnotationKeys.LONGITUDE); |
| 510 | String sgrY = annot.value(AnnotationKeys.GRID_Y); |
| 511 | String sgrX = annot.value(AnnotationKeys.GRID_X); |
| 512 | |
| 513 | boolean validLat = slat != null && !slat.equals(NO_GEO_VALUE); |
| 514 | boolean validLng = slng != null && !slng.equals(NO_GEO_VALUE); |
| 515 | if (validLat && validLng) { |
| 516 | pp.addProp(LATITUDE, lion.getSafe(LATITUDE), slat) |
| 517 | .addProp(LONGITUDE, lion.getSafe(LONGITUDE), slng) |
| 518 | .addSeparator(); |
| 519 | |
| 520 | } else if (sgrY != null && sgrX != null) { |
| 521 | pp.addProp(GRID_Y, lion.getSafe(GRID_Y), sgrY) |
| 522 | .addProp(GRID_X, lion.getSafe(GRID_X), sgrX) |
| 523 | .addSeparator(); |
| 524 | } |
| 525 | // else, no location |
| 526 | } |
| 527 | |
| 528 | private void addDeviceCountStats(PropertyPanel pp, DeviceId deviceId, LionBundle lion) { |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 529 | int portCount = services.device().getPorts(deviceId).size(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 530 | int flowCount = getFlowCount(deviceId); |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 531 | int tunnelCount = getTunnelCount(deviceId); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 532 | |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 533 | pp.addProp(PORTS, lion.getSafe(PORTS), portCount) |
| 534 | .addProp(FLOWS, lion.getSafe(FLOWS), flowCount) |
| 535 | .addProp(TUNNELS, lion.getSafe(TUNNELS), tunnelCount); |
| 536 | } |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 537 | |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 538 | private void addDeviceCoreButtons(PropertyPanel pp) { |
| 539 | pp.addButton(CoreButtons.SHOW_DEVICE_VIEW) |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 540 | .addButton(CoreButtons.SHOW_FLOW_VIEW) |
| 541 | .addButton(CoreButtons.SHOW_PORT_VIEW) |
| 542 | .addButton(CoreButtons.SHOW_GROUP_VIEW) |
| 543 | .addButton(CoreButtons.SHOW_METER_VIEW); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | protected int getFlowCount(DeviceId deviceId) { |
| 547 | int count = 0; |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 548 | for (FlowEntry flowEntry : services.flow().getFlowEntries(deviceId)) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 549 | count++; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 550 | } |
| 551 | return count; |
| 552 | } |
| 553 | |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 554 | protected int getTunnelCount(DeviceId deviceId) { |
| 555 | int count = 0; |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 556 | Collection<Tunnel> tunnels = services.tunnel().queryAllTunnels(); |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 557 | for (Tunnel tunnel : tunnels) { |
chengfan | c553c95 | 2016-07-22 15:48:23 +0800 | [diff] [blame] | 558 | //Only OpticalTunnelEndPoint has a device |
| 559 | if (!(tunnel.src() instanceof OpticalTunnelEndPoint) || |
| 560 | !(tunnel.dst() instanceof OpticalTunnelEndPoint)) { |
| 561 | continue; |
| 562 | } |
| 563 | |
| 564 | Optional<ElementId> srcElementId = ((OpticalTunnelEndPoint) tunnel.src()).elementId(); |
| 565 | Optional<ElementId> dstElementId = ((OpticalTunnelEndPoint) tunnel.dst()).elementId(); |
| 566 | if (!srcElementId.isPresent() || !dstElementId.isPresent()) { |
| 567 | continue; |
| 568 | } |
| 569 | DeviceId srcDeviceId = (DeviceId) srcElementId.get(); |
| 570 | DeviceId dstDeviceId = (DeviceId) dstElementId.get(); |
| 571 | if (srcDeviceId.equals(deviceId) || dstDeviceId.equals(deviceId)) { |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 572 | count++; |
| 573 | } |
| 574 | } |
| 575 | return count; |
| 576 | } |
| 577 | |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 578 | private boolean useDefaultName(String annotName) { |
| 579 | return isNullOrEmpty(annotName) || DASH.equals(annotName); |
| 580 | } |
| 581 | |
| 582 | private String nameForHost(Host host) { |
| 583 | String name = host.annotations().value(AnnotationKeys.NAME); |
| 584 | return useDefaultName(name) ? ip(host.ipAddresses()) : name; |
| 585 | } |
| 586 | |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 587 | private String glyphForHost(Annotations annot) { |
| 588 | String uiType = annot.value(AnnotationKeys.UI_TYPE); |
| 589 | return isNullOrEmpty(uiType) ? DEFAULT_HOST_GLYPH : uiType; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 592 | // Generates a property panel model for a host details response |
| 593 | protected PropertyPanel hostDetails(HostId hostId) { |
| 594 | log.debug("generate prop panel data for host {}", hostId); |
| 595 | Host host = services.host().getHost(hostId); |
| 596 | Annotations annot = host.annotations(); |
| 597 | String glyphId = glyphForHost(annot); |
| 598 | LionBundle lion = getLionBundle(LION_TOPO); |
| 599 | |
| 600 | PropertyPanel pp = new PropertyPanel(nameForHost(host), glyphId) |
| 601 | .navPath(HOST_NAV_PATH) |
| 602 | .id(hostId.toString()); |
| 603 | addHostBasicProps(pp, host, lion); |
| 604 | addLocationProps(pp, annot, lion); |
| 605 | return pp; |
| 606 | } |
| 607 | |
| 608 | private void addHostBasicProps(PropertyPanel pp, Host host, LionBundle lion) { |
| 609 | pp.addProp(LPL_FRIENDLY, lion.getSafe(LPL_FRIENDLY), nameForHost(host)) |
| 610 | .addProp(MAC, lion.getSafe(MAC), host.mac()) |
| 611 | .addProp(IP, lion.getSafe(IP), host.ipAddresses(), "[\\[\\]]") |
| 612 | .addProp(VLAN, lion.getSafe(VLAN), displayVlan(host.vlan(), lion)) |
| 613 | .addSeparator(); |
| 614 | } |
| 615 | |
| 616 | private String displayVlan(VlanId vlan, LionBundle lion) { |
| 617 | return VlanId.NONE.equals(vlan) ? lion.getSafe(VLAN_NONE) : vlan.toString(); |
| 618 | } |
| 619 | |
| 620 | // Generates a property panel model for a link details response (edge-link) |
| 621 | protected PropertyPanel edgeLinkDetails(HostId hid, ConnectPoint cp) { |
| 622 | log.debug("generate prop panel data for edgelink {} {}", hid, cp); |
| 623 | LionBundle lion = getLionBundle(LION_TOPO); |
| 624 | String title = lion.getSafe("title_edge_link"); |
| 625 | |
| 626 | PropertyPanel pp = new PropertyPanel(title, LINK_GLYPH); |
| 627 | addLinkHostProps(pp, hid, lion); |
| 628 | addLinkCpBProps(pp, cp, lion); |
| 629 | return pp; |
| 630 | } |
| 631 | |
| 632 | // Generates a property panel model for a link details response (infra-link) |
| 633 | protected PropertyPanel infraLinkDetails(ConnectPoint cpA, ConnectPoint cpB) { |
| 634 | log.debug("generate prop panel data for infralink {} {}", cpA, cpB); |
| 635 | LionBundle lion = getLionBundle(LION_TOPO); |
| 636 | String title = lion.getSafe("title_infra_link"); |
| 637 | |
| 638 | PropertyPanel pp = new PropertyPanel(title, LINK_GLYPH); |
| 639 | addLinkCpAProps(pp, cpA, lion); |
| 640 | addLinkCpBProps(pp, cpB, lion); |
| 641 | addLinkBackingProps(pp, cpA, cpB, lion); |
| 642 | return pp; |
| 643 | } |
| 644 | |
| 645 | private void addLinkHostProps(PropertyPanel pp, HostId hostId, LionBundle lion) { |
| 646 | Host host = services.host().getHost(hostId); |
| 647 | |
| 648 | pp.addProp(LPL_A_TYPE, lion.getSafe(LPL_A_TYPE), lion.getSafe(HOST)) |
| 649 | .addProp(LPL_A_ID, lion.getSafe(LPL_A_ID), hostId.toString()) |
| 650 | .addProp(LPL_A_FRIENDLY, lion.getSafe(LPL_A_FRIENDLY), nameForHost(host)) |
| 651 | .addSeparator(); |
| 652 | } |
| 653 | |
| 654 | private void addLinkCpAProps(PropertyPanel pp, ConnectPoint cp, LionBundle lion) { |
| 655 | DeviceId did = cp.deviceId(); |
| 656 | |
| 657 | pp.addProp(LPL_A_TYPE, lion.getSafe(LPL_A_TYPE), lion.getSafe(DEVICE)) |
| 658 | .addProp(LPL_A_ID, lion.getSafe(LPL_A_ID), did.toString()) |
| 659 | .addProp(LPL_A_FRIENDLY, lion.getSafe(LPL_A_FRIENDLY), friendlyDevice(did)) |
| 660 | .addProp(LPL_A_PORT, lion.getSafe(LPL_A_PORT), cp.port().toLong()) |
| 661 | .addSeparator(); |
| 662 | } |
| 663 | |
| 664 | private void addLinkCpBProps(PropertyPanel pp, ConnectPoint cp, LionBundle lion) { |
| 665 | DeviceId did = cp.deviceId(); |
| 666 | |
| 667 | pp.addProp(LPL_B_TYPE, lion.getSafe(LPL_B_TYPE), lion.getSafe(DEVICE)) |
| 668 | .addProp(LPL_B_ID, lion.getSafe(LPL_B_ID), did.toString()) |
| 669 | .addProp(LPL_B_FRIENDLY, lion.getSafe(LPL_B_FRIENDLY), friendlyDevice(did)) |
| 670 | .addProp(LPL_B_PORT, lion.getSafe(LPL_B_PORT), cp.port().toLong()) |
| 671 | .addSeparator(); |
| 672 | } |
| 673 | |
| 674 | private void addLinkBackingProps(PropertyPanel pp, ConnectPoint cpA, |
| 675 | ConnectPoint cpB, LionBundle lion) { |
| 676 | Link a2b = services.link().getLink(cpA, cpB); |
| 677 | Link b2a = services.link().getLink(cpB, cpA); |
| 678 | |
| 679 | pp.addProp(LPL_A2B, lion.getSafe(LPL_A2B), linkPropString(a2b, lion)) |
| 680 | .addProp(LPL_B2A, lion.getSafe(LPL_B2A), linkPropString(b2a, lion)); |
| 681 | } |
| 682 | |
| 683 | private String linkPropString(Link link, LionBundle lion) { |
| 684 | if (link == null) { |
| 685 | return lion.getSafe(LPV_NO_LINK); |
| 686 | } |
| 687 | return lion.getSafe(link.type()) + SLASH + |
| 688 | lion.getSafe(link.state()) + SLASH + |
| 689 | lion.getSafe(link.isExpected() ? EXPECTED : NOT_EXPECTED); |
| 690 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 691 | } |