Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | */ |
| 17 | |
| 18 | package org.onosproject.ui.impl.topo; |
| 19 | |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 22 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onosproject.cluster.ClusterEvent; |
| 25 | import org.onosproject.cluster.ClusterService; |
| 26 | import org.onosproject.cluster.ControllerNode; |
| 27 | import org.onosproject.cluster.NodeId; |
| 28 | import org.onosproject.mastership.MastershipService; |
| 29 | import org.onosproject.net.Annotated; |
| 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; |
| 41 | import org.onosproject.net.PortNumber; |
| 42 | import org.onosproject.net.device.DeviceEvent; |
| 43 | import org.onosproject.net.device.DeviceService; |
| 44 | import org.onosproject.net.host.HostEvent; |
| 45 | import org.onosproject.net.host.HostService; |
| 46 | import org.onosproject.net.link.LinkEvent; |
| 47 | import org.onosproject.net.link.LinkService; |
| 48 | import org.onosproject.net.provider.ProviderId; |
| 49 | import org.onosproject.ui.JsonUtils; |
| 50 | import org.slf4j.Logger; |
| 51 | import org.slf4j.LoggerFactory; |
| 52 | |
| 53 | import java.util.HashMap; |
| 54 | import java.util.Iterator; |
| 55 | import java.util.Map; |
| 56 | import java.util.Set; |
| 57 | |
| 58 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 59 | import static org.onosproject.cluster.ControllerNode.State.ACTIVE; |
| 60 | import static org.onosproject.net.PortNumber.portNumber; |
| 61 | |
| 62 | /** |
| 63 | * Facility for generating messages in {@link ObjectNode} form from |
| 64 | * ONOS model events. |
| 65 | */ |
| 66 | // package private |
| 67 | class TopoMessageFactory { |
| 68 | |
| 69 | private static final ProviderId PROVIDER_ID = |
| 70 | new ProviderId("core", "org.onosproject.core", true); |
| 71 | private static final String COMPACT = "%s/%s-%s/%s"; |
| 72 | private static final PortNumber PORT_ZERO = portNumber(0); |
| 73 | |
| 74 | private static final Map<Enum<?>, String> LOOKUP = new HashMap<>(); |
| 75 | |
| 76 | static { |
| 77 | LOOKUP.put(ClusterEvent.Type.INSTANCE_ADDED, "addInstance"); |
| 78 | LOOKUP.put(ClusterEvent.Type.INSTANCE_REMOVED, "removeInstance"); |
| 79 | LOOKUP.put(DeviceEvent.Type.DEVICE_ADDED, "addDevice"); |
| 80 | LOOKUP.put(DeviceEvent.Type.DEVICE_UPDATED, "updateDevice"); |
| 81 | LOOKUP.put(DeviceEvent.Type.DEVICE_REMOVED, "removeDevice"); |
| 82 | LOOKUP.put(LinkEvent.Type.LINK_ADDED, "addLink"); |
| 83 | LOOKUP.put(LinkEvent.Type.LINK_UPDATED, "updateLink"); |
| 84 | LOOKUP.put(LinkEvent.Type.LINK_REMOVED, "removeLink"); |
| 85 | LOOKUP.put(HostEvent.Type.HOST_ADDED, "addHost"); |
| 86 | LOOKUP.put(HostEvent.Type.HOST_UPDATED, "updateHost"); |
| 87 | LOOKUP.put(HostEvent.Type.HOST_REMOVED, "removeHost"); |
| 88 | } |
| 89 | |
| 90 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 91 | |
| 92 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 93 | |
| 94 | private MetaDb metaDb; |
| 95 | |
| 96 | private ClusterService clusterService; |
| 97 | private DeviceService deviceService; |
| 98 | private LinkService linkService; |
| 99 | private HostService hostService; |
| 100 | private MastershipService mastershipService; |
| 101 | |
| 102 | |
| 103 | // =================================================================== |
| 104 | // Private helper methods |
| 105 | |
| 106 | private ObjectNode objectNode() { |
| 107 | return MAPPER.createObjectNode(); |
| 108 | } |
| 109 | |
| 110 | private ArrayNode arrayNode() { |
| 111 | return MAPPER.createArrayNode(); |
| 112 | } |
| 113 | |
| 114 | private String toLc(Object o) { |
| 115 | return o.toString().toLowerCase(); |
| 116 | } |
| 117 | |
| 118 | // Event type to message type lookup (with fallback). |
| 119 | private String messageTypeLookup(Enum<?> type, String fallback) { |
| 120 | String msgType = LOOKUP.get(type); |
| 121 | return msgType == null ? fallback : msgType; |
| 122 | } |
| 123 | |
| 124 | // Returns the name of the master node for the specified device ID. |
| 125 | private String master(DeviceId deviceId) { |
| 126 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 127 | return master != null ? master.toString() : ""; |
| 128 | } |
| 129 | |
| 130 | // Produces JSON structure from annotations. |
| 131 | private ObjectNode props(Annotations annotations) { |
| 132 | ObjectNode props = objectNode(); |
| 133 | if (annotations != null) { |
| 134 | for (String key : annotations.keys()) { |
| 135 | props.put(key, annotations.value(key)); |
| 136 | } |
| 137 | } |
| 138 | return props; |
| 139 | } |
| 140 | |
| 141 | // Adds a geo location JSON to the specified payload object. |
| 142 | private void addGeoLocation(Annotated annotated, ObjectNode payload) { |
| 143 | Annotations annot = annotated.annotations(); |
| 144 | if (annot == null) { |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | String slat = annot.value(AnnotationKeys.LATITUDE); |
| 149 | String slng = annot.value(AnnotationKeys.LONGITUDE); |
| 150 | try { |
| 151 | if (!isNullOrEmpty(slat) && !isNullOrEmpty(slng)) { |
| 152 | double lat = Double.parseDouble(slat); |
| 153 | double lng = Double.parseDouble(slng); |
| 154 | ObjectNode loc = objectNode() |
| 155 | .put("type", "latlng") |
| 156 | .put("lat", lat) |
| 157 | .put("lng", lng); |
| 158 | payload.set("location", loc); |
| 159 | } |
| 160 | } catch (NumberFormatException e) { |
| 161 | log.warn("Invalid geo data latitude={}; longitude={}", slat, slng); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // Produces compact string representation of a link. |
| 166 | private String compactLinkString(Link link) { |
| 167 | return String.format(COMPACT, link.src().elementId(), link.src().port(), |
| 168 | link.dst().elementId(), link.dst().port()); |
| 169 | } |
| 170 | |
| 171 | // Generates an edge link from the specified host location. |
| 172 | private EdgeLink edgeLink(Host host, boolean isIngress) { |
| 173 | ConnectPoint cp = new ConnectPoint(host.id(), PORT_ZERO); |
| 174 | return new DefaultEdgeLink(PROVIDER_ID, cp, host.location(), isIngress); |
| 175 | } |
| 176 | |
| 177 | // Encodes the specified host location into a JSON object. |
| 178 | private ObjectNode hostConnect(HostLocation loc) { |
| 179 | return objectNode() |
| 180 | .put("device", loc.deviceId().toString()) |
| 181 | .put("port", loc.port().toLong()); |
| 182 | } |
| 183 | |
| 184 | // Returns the first IP address from the specified set. |
| 185 | private String firstIp(Set<IpAddress> addresses) { |
| 186 | Iterator<IpAddress> it = addresses.iterator(); |
| 187 | return it.hasNext() ? it.next().toString() : "unknown"; |
| 188 | } |
| 189 | |
| 190 | // Returns a JSON array of the specified strings. |
| 191 | private ArrayNode labels(String... labels) { |
| 192 | ArrayNode array = arrayNode(); |
| 193 | for (String label : labels) { |
| 194 | array.add(label); |
| 195 | } |
| 196 | return array; |
| 197 | } |
| 198 | |
| 199 | // =================================================================== |
| 200 | // API for generating messages |
| 201 | |
| 202 | /** |
| 203 | * Injects service references so that the message compilation methods |
| 204 | * can do required lookups when needed. |
| 205 | * |
| 206 | * @param meta meta DB |
| 207 | * @param cs cluster service |
| 208 | * @param ds device service |
| 209 | * @param ls link service |
| 210 | * @param hs host service |
| 211 | * @param ms mastership service |
| 212 | */ |
| 213 | public void injectServices(MetaDb meta, ClusterService cs, DeviceService ds, |
| 214 | LinkService ls, HostService hs, |
| 215 | MastershipService ms) { |
| 216 | metaDb = meta; |
| 217 | clusterService = cs; |
| 218 | deviceService = ds; |
| 219 | linkService = ls; |
| 220 | hostService = hs; |
| 221 | mastershipService = ms; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Transforms a cluster event into an object-node-based message. |
| 226 | * |
| 227 | * @param ev cluster event |
| 228 | * @return marshaled event message |
| 229 | */ |
| 230 | public ObjectNode instanceMessage(ClusterEvent ev) { |
| 231 | ControllerNode node = ev.subject(); |
| 232 | NodeId nid = node.id(); |
| 233 | String id = nid.toString(); |
| 234 | String ip = node.ip().toString(); |
| 235 | int switchCount = mastershipService.getDevicesOf(nid).size(); |
| 236 | |
| 237 | ObjectNode payload = objectNode() |
| 238 | .put("id", id) |
| 239 | .put("ip", ip) |
| 240 | .put("online", clusterService.getState(nid) == ACTIVE) |
| 241 | .put("uiAttached", node.equals(clusterService.getLocalNode())) |
| 242 | .put("switches", switchCount); |
| 243 | |
| 244 | ArrayNode labels = arrayNode().add(id).add(ip); |
| 245 | |
| 246 | payload.set("labels", labels); |
| 247 | metaDb.addMetaUi(id, payload); |
| 248 | |
| 249 | String msgType = messageTypeLookup(ev.type(), "addInstance"); |
| 250 | return JsonUtils.envelope(msgType, payload); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Transforms a device event into an object-node-based message. |
| 255 | * |
| 256 | * @param ev device event |
| 257 | * @return marshaled event message |
| 258 | */ |
| 259 | public ObjectNode deviceMessage(DeviceEvent ev) { |
| 260 | Device device = ev.subject(); |
| 261 | DeviceId did = device.id(); |
| 262 | String id = did.toString(); |
| 263 | |
| 264 | ObjectNode payload = objectNode() |
| 265 | .put("id", id) |
| 266 | .put("type", toLc(device.type())) |
| 267 | .put("online", deviceService.isAvailable(did)) |
| 268 | .put("master", master(did)); |
| 269 | |
| 270 | Annotations annot = device.annotations(); |
| 271 | String name = annot.value(AnnotationKeys.NAME); |
| 272 | String friendly = isNullOrEmpty(name) ? id : name; |
| 273 | payload.set("labels", labels("", friendly, id)); |
| 274 | payload.set("props", props(annot)); |
| 275 | |
| 276 | addGeoLocation(device, payload); |
| 277 | metaDb.addMetaUi(id, payload); |
| 278 | |
| 279 | String msgType = messageTypeLookup(ev.type(), "updateDevice"); |
| 280 | return JsonUtils.envelope(msgType, payload); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Transforms a link event into an object-node-based message. |
| 285 | * |
| 286 | * @param ev link event |
| 287 | * @return marshaled event message |
| 288 | */ |
| 289 | public ObjectNode linkMessage(LinkEvent ev) { |
| 290 | Link link = ev.subject(); |
| 291 | ObjectNode payload = objectNode() |
| 292 | .put("id", compactLinkString(link)) |
| 293 | .put("type", toLc(link.type())) |
| 294 | .put("online", link.state() == Link.State.ACTIVE) |
| 295 | .put("linkWidth", 1.2) |
| 296 | .put("src", link.src().deviceId().toString()) |
| 297 | .put("srcPort", link.src().port().toString()) |
| 298 | .put("dst", link.dst().deviceId().toString()) |
| 299 | .put("dstPort", link.dst().port().toString()); |
| 300 | |
| 301 | String msgType = messageTypeLookup(ev.type(), "updateLink"); |
| 302 | return JsonUtils.envelope(msgType, payload); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Transforms a host event into an object-node-based message. |
| 307 | * |
| 308 | * @param ev host event |
| 309 | * @return marshaled event message |
| 310 | */ |
| 311 | public ObjectNode hostMessage(HostEvent ev) { |
| 312 | Host host = ev.subject(); |
| 313 | HostId hid = host.id(); |
| 314 | String id = hid.toString(); |
| 315 | Annotations annot = host.annotations(); |
| 316 | |
| 317 | String hostType = annot.value(AnnotationKeys.TYPE); |
| 318 | |
| 319 | ObjectNode payload = objectNode() |
| 320 | .put("id", id) |
| 321 | .put("type", isNullOrEmpty(hostType) ? "endstation" : hostType) |
| 322 | .put("ingress", compactLinkString(edgeLink(host, true))) |
| 323 | .put("egress", compactLinkString(edgeLink(host, false))); |
| 324 | |
| 325 | // TODO: make cp an array of connect point objects (multi-homed) |
| 326 | payload.set("cp", hostConnect(host.location())); |
| 327 | String ipStr = firstIp(host.ipAddresses()); |
| 328 | String macStr = host.mac().toString(); |
| 329 | payload.set("labels", labels(ipStr, macStr)); |
| 330 | payload.set("props", props(annot)); |
| 331 | addGeoLocation(host, payload); |
| 332 | metaDb.addMetaUi(id, payload); |
| 333 | |
| 334 | String mstType = messageTypeLookup(ev.type(), "updateHost"); |
| 335 | return JsonUtils.envelope(mstType, payload); |
| 336 | } |
| 337 | } |