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