Adnaan Sachidanandan | a915da1 | 2017-08-09 15:12:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.linkprops; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import com.google.common.base.Strings; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.onlab.osgi.ServiceDirectory; |
| 22 | import org.onlab.util.Bandwidth; |
| 23 | import org.onosproject.net.Device; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.Element; |
| 26 | import org.onosproject.net.HostId; |
| 27 | import org.onosproject.net.Link; |
| 28 | import org.onosproject.net.PortNumber; |
| 29 | import org.onosproject.net.device.DeviceService; |
| 30 | import org.onosproject.net.host.HostService; |
| 31 | import org.onosproject.net.link.LinkService; |
| 32 | import org.onosproject.net.resource.ContinuousResource; |
| 33 | import org.onosproject.ui.RequestHandler; |
| 34 | import org.onosproject.ui.UiConnection; |
| 35 | import org.onosproject.ui.UiMessageHandler; |
| 36 | import org.onosproject.ui.topo.Highlights; |
| 37 | import org.onosproject.ui.topo.TopoJson; |
| 38 | import org.onosproject.net.resource.DiscreteResourceId; |
| 39 | import org.onosproject.net.resource.Resources; |
| 40 | import org.onosproject.net.resource.ResourceQueryService; |
| 41 | import org.onosproject.incubator.net.PortStatisticsService; |
| 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
| 45 | import java.util.Collection; |
| 46 | import java.util.Set; |
| 47 | |
| 48 | /** |
| 49 | * ONOS UI Topology overlay for LinkProps. Creates the highlights for each link |
| 50 | * depending on the selected mode (rate,byte,band) and adds labels to egress |
| 51 | * links of the device being hovered over. |
| 52 | */ |
| 53 | public class LinkPropsTopovMessageHandler extends UiMessageHandler { |
| 54 | |
| 55 | private static final String LP_TOPOV_DISPLAY_START = "linkPropsTopovDisplayStart"; |
| 56 | private static final String LP_TOPOV_DISPLAY_UPDATE = "linkPropsTopovDisplayUpdate"; |
| 57 | private static final String LP_TOPOV_DISPLAY_STOP = "linkPropsTopovDisplayStop"; |
| 58 | |
| 59 | private static final String ID = "id"; |
| 60 | private static final String MODE = "mode"; |
| 61 | |
| 62 | private enum Mode { IDLE, RATE, BYTE, BAND } |
| 63 | |
| 64 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 65 | |
| 66 | private DeviceService deviceService; |
| 67 | private HostService hostService; |
| 68 | private LinkService linkService; |
| 69 | private PortStatisticsService portStatisticsService; |
| 70 | private ResourceQueryService resourceQueryService; |
| 71 | |
| 72 | private Mode currentMode = Mode.IDLE; |
| 73 | private Element elementOfNote; |
| 74 | |
| 75 | |
| 76 | // ===============-=-=-=-=-=-======================-=-=-=-=-=-=-================================ |
| 77 | |
| 78 | |
| 79 | @Override |
| 80 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 81 | super.init(connection, directory); |
| 82 | deviceService = directory.get(DeviceService.class); |
| 83 | hostService = directory.get(HostService.class); |
| 84 | linkService = directory.get(LinkService.class); |
| 85 | portStatisticsService = directory.get(PortStatisticsService.class); |
| 86 | resourceQueryService = directory.get(ResourceQueryService.class); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | protected Collection<RequestHandler> createRequestHandlers() { |
| 91 | return ImmutableSet.of( |
| 92 | new DisplayStartHandler(), |
| 93 | new DisplayUpdateHandler(), |
| 94 | new DisplayStopHandler() |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | // === ------------------------- |
| 99 | // === Handler classes |
| 100 | |
| 101 | private final class DisplayStartHandler extends RequestHandler { |
| 102 | public DisplayStartHandler() { |
| 103 | super(LP_TOPOV_DISPLAY_START); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void process(ObjectNode payload) { |
| 108 | String mode = string(payload, MODE); |
| 109 | |
| 110 | log.debug("Start Display: mode [{}]", mode); |
| 111 | clearState(); |
| 112 | clearForMode(); |
| 113 | |
| 114 | switch (mode) { |
| 115 | case "rate": |
| 116 | currentMode = Mode.RATE; |
| 117 | sendRateData(); |
| 118 | break; |
| 119 | |
| 120 | case "byte": |
| 121 | currentMode = Mode.BYTE; |
| 122 | sendByteData(); |
| 123 | break; |
| 124 | |
| 125 | case "band": |
| 126 | currentMode = Mode.BAND; |
| 127 | sendBandwidth(); |
| 128 | break; |
| 129 | |
| 130 | default: |
| 131 | currentMode = Mode.IDLE; |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | private final class DisplayUpdateHandler extends RequestHandler { |
| 138 | public DisplayUpdateHandler() { |
| 139 | super(LP_TOPOV_DISPLAY_UPDATE); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void process(ObjectNode payload) { |
| 144 | String id = string(payload, ID); |
| 145 | log.debug("Update Display: id [{}]", id); |
| 146 | if (!Strings.isNullOrEmpty(id)) { |
| 147 | updateForMode(id); |
| 148 | } else { |
| 149 | clearForMode(); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | private final class DisplayStopHandler extends RequestHandler { |
| 155 | public DisplayStopHandler() { |
| 156 | super(LP_TOPOV_DISPLAY_STOP); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void process(ObjectNode payload) { |
| 161 | log.debug("Stop Display"); |
| 162 | clearState(); |
| 163 | clearForMode(); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // === ------------ |
| 168 | |
| 169 | private void clearState() { |
| 170 | currentMode = Mode.IDLE; |
| 171 | elementOfNote = null; |
| 172 | } |
| 173 | |
| 174 | private void updateForMode(String id) { |
| 175 | log.debug("host service: {}", hostService); |
| 176 | log.debug("device service: {}", deviceService); |
| 177 | |
| 178 | try { |
| 179 | HostId hid = HostId.hostId(id); |
| 180 | log.debug("host id {}", hid); |
| 181 | elementOfNote = hostService.getHost(hid); |
| 182 | log.debug("host element {}", elementOfNote); |
| 183 | |
| 184 | } catch (Exception e) { |
| 185 | try { |
| 186 | DeviceId did = DeviceId.deviceId(id); |
| 187 | log.debug("device id {}", did); |
| 188 | elementOfNote = deviceService.getDevice(did); |
| 189 | log.debug("device element {}", elementOfNote); |
| 190 | |
| 191 | } catch (Exception e2) { |
| 192 | log.debug("Unable to process ID [{}]", id); |
| 193 | elementOfNote = null; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | switch (currentMode) { |
| 198 | case RATE: |
| 199 | sendRateData(); |
| 200 | break; |
| 201 | |
| 202 | case BYTE: |
| 203 | sendByteData(); |
| 204 | break; |
| 205 | |
| 206 | case BAND: |
| 207 | sendBandwidth(); |
| 208 | break; |
| 209 | |
| 210 | default: |
| 211 | break; |
| 212 | } |
| 213 | |
| 214 | } |
| 215 | |
| 216 | private void clearForMode() { |
| 217 | sendHighlights(new Highlights()); |
| 218 | } |
| 219 | |
| 220 | private void sendHighlights(Highlights highlights) { |
| 221 | sendMessage(TopoJson.highlightsMessage(highlights)); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | private void sendRateData() { |
| 226 | if (elementOfNote != null && elementOfNote instanceof Device) { |
| 227 | DeviceId devId = (DeviceId) elementOfNote.id(); |
| 228 | Set<Link> links = linkService.getDeviceEgressLinks(devId); |
| 229 | Highlights highlights = getLinkSpeed(links, devId); |
| 230 | sendHighlights(highlights); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | private void sendBandwidth() { |
| 235 | if (elementOfNote != null && elementOfNote instanceof Device) { |
| 236 | DeviceId devId = (DeviceId) elementOfNote.id(); |
| 237 | Set<Link> links = linkService.getDeviceEgressLinks(devId); |
| 238 | Highlights highlights = getBandwidth(links, devId); |
| 239 | sendHighlights(highlights); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Gets the links connected to the highlighted device. |
| 245 | * Creates a ContinuousResource object for each link |
| 246 | * and gets the bandwidth of the link from the query |
| 247 | * and sets the label of the link as the bandwidth value. |
| 248 | */ |
| 249 | private Highlights getBandwidth(Set<Link> links, DeviceId devId) { |
| 250 | LpLinkMap linkMap = new LpLinkMap(); |
Ray Milkey | fd4f8d3 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 251 | Highlights highlights = new Highlights(); |
Adnaan Sachidanandan | a915da1 | 2017-08-09 15:12:45 -0700 | [diff] [blame] | 252 | if (links != null) { |
| 253 | log.debug("Processing {} links", links.size()); |
| 254 | links.forEach(linkMap::add); |
Ray Milkey | fd4f8d3 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 255 | |
| 256 | PortNumber portnum = PortNumber.portNumber((int) links.iterator().next().src().port().toLong()); |
| 257 | |
| 258 | for (LpLink dlink : linkMap.biLinks()) { |
| 259 | DiscreteResourceId parent = Resources.discrete(devId, portnum).id(); |
| 260 | ContinuousResource continuousResource = |
| 261 | (ContinuousResource) resourceQueryService.getAvailableResources(parent, |
| 262 | Bandwidth.class).iterator().next(); |
| 263 | double availBandwidth = continuousResource.value(); |
| 264 | |
| 265 | dlink.makeImportant().setLabel(Double.toString(availBandwidth) + " bytes/s"); |
| 266 | highlights.add(dlink.highlight(null)); |
| 267 | } |
Adnaan Sachidanandan | a915da1 | 2017-08-09 15:12:45 -0700 | [diff] [blame] | 268 | } else { |
| 269 | log.debug("No egress links found for device {}", devId); |
| 270 | } |
Adnaan Sachidanandan | a915da1 | 2017-08-09 15:12:45 -0700 | [diff] [blame] | 271 | return highlights; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Gets the links connected to the highlighted device. |
| 276 | * Uses PortStatisticsService to get a load object of |
| 277 | * the links and find the rate of data flow out of the |
| 278 | * device via that link (src) and into the device via |
| 279 | * that link (dst), because dlink.two() gives a |
| 280 | * NullPointerException. Creates a label which displays |
| 281 | * the src and dst rates for the link. |
| 282 | */ |
| 283 | private Highlights getLinkSpeed(Set<Link> links, DeviceId devId) { |
| 284 | LpLinkMap linkMap = new LpLinkMap(); |
| 285 | if (links != null) { |
| 286 | log.debug("Processing {} links", links.size()); |
| 287 | links.forEach(linkMap::add); |
| 288 | } else { |
| 289 | log.debug("No egress links found for device {}", devId); |
| 290 | } |
| 291 | |
| 292 | Highlights highlights = new Highlights(); |
| 293 | |
| 294 | for (LpLink dlink : linkMap.biLinks()) { |
| 295 | String rate = "Out: " + getSpeedString((portStatisticsService.load(dlink.one().src()).rate())) + " | In: " |
| 296 | + getSpeedString((portStatisticsService.load(dlink.one().dst()).rate())); |
| 297 | dlink.makeImportant().setLabel(rate); |
| 298 | highlights.add(dlink.highlight(null)); |
| 299 | } |
| 300 | return highlights; |
| 301 | } |
| 302 | |
| 303 | private String getSpeedString(Long speed) { |
| 304 | if (speed > 1_000_000_000) { |
| 305 | return Long.toString(speed / 1_000_000_000) + "Gb/s"; |
| 306 | } else if (speed > 1_000_000) { |
| 307 | return Long.toString(speed / 1_000_000) + "Mb/s"; |
| 308 | } else if (speed > 1_000) { |
| 309 | return Long.toString(speed / 1_000) + "kb/s"; |
| 310 | } else { |
| 311 | return Long.toString(speed) + "bytes/s"; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | private void sendByteData() { |
| 316 | if (elementOfNote != null && elementOfNote instanceof Device) { |
| 317 | DeviceId devId = (DeviceId) elementOfNote.id(); |
| 318 | Set<Link> links = linkService.getDeviceEgressLinks(devId); |
| 319 | Highlights highlights = getTotalBytes(links, devId); |
| 320 | sendHighlights(highlights); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Gets the links connected to the highlighted device. |
| 326 | * Uses PortStatisticsService to get a load object of |
| 327 | * the links and find the total number of bytes sent out |
| 328 | * of the device via that link (src) and into the device |
| 329 | * via that link (dst), because dlink.two() gives a |
| 330 | * NullPointerException. Creates a label which displays |
| 331 | * the src and dst total bytes for the link. |
| 332 | */ |
| 333 | private Highlights getTotalBytes(Set<Link> links, DeviceId devId) { |
| 334 | LpLinkMap linkMap = new LpLinkMap(); |
| 335 | if (links != null) { |
| 336 | log.debug("Processing {} links", links.size()); |
| 337 | links.forEach(linkMap::add); |
| 338 | } else { |
| 339 | log.debug("No egress links found for device {}", devId); |
| 340 | } |
| 341 | |
| 342 | Highlights highlights = new Highlights(); |
| 343 | |
| 344 | for (LpLink dlink : linkMap.biLinks()) { |
| 345 | String bytes = "Out: " + getBytesString(portStatisticsService.load(dlink.one().src()).latest()) + " | In: " |
| 346 | + getBytesString(portStatisticsService.load(dlink.one().dst()).latest()); |
| 347 | dlink.makeImportant().setLabel(bytes); |
| 348 | highlights.add(dlink.highlight(null)); |
| 349 | } |
| 350 | return highlights; |
| 351 | } |
| 352 | |
| 353 | private String getBytesString(Long bytes) { |
| 354 | final long tb = (long) 1_000_000_000_000.0; |
| 355 | if (bytes > tb) { |
| 356 | return Long.toString(bytes / tb) + "Tb"; |
| 357 | } else if (bytes > 1_000_000_000) { |
| 358 | return Long.toString(bytes / 1_000_000_000) + "Gb"; |
| 359 | } else if (bytes > 1_000_000) { |
| 360 | return Long.toString(bytes / 1_000_000) + "Mb"; |
| 361 | } else if (bytes > 1_000) { |
| 362 | return Long.toString(bytes / 1_000) + "kb"; |
| 363 | } else { |
| 364 | return Long.toString(bytes) + "bytes"; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | } |