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