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