Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -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 | */ |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 16 | package org.onosproject.net.statistic.impl; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 17 | |
| 18 | import com.google.common.collect.Maps; |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 19 | import org.onosproject.net.statistic.PortStatisticsService; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 20 | import org.onosproject.net.ConnectPoint; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.device.DeviceEvent; |
| 23 | import org.onosproject.net.device.DeviceListener; |
| 24 | import org.onosproject.net.device.DeviceService; |
| 25 | import org.onosproject.net.device.PortStatistics; |
| 26 | import org.onosproject.net.statistic.DefaultLoad; |
| 27 | import org.onosproject.net.statistic.Load; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 28 | import org.osgi.service.component.annotations.Activate; |
| 29 | import org.osgi.service.component.annotations.Component; |
| 30 | import org.osgi.service.component.annotations.Deactivate; |
| 31 | import org.osgi.service.component.annotations.Reference; |
| 32 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import java.util.Map; |
| 36 | import java.util.stream.Collectors; |
| 37 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 38 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED; |
| 39 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED; |
| 40 | import static org.onosproject.net.device.DeviceEvent.Type.PORT_STATS_UPDATED; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 41 | import static org.slf4j.LoggerFactory.getLogger; |
| 42 | |
| 43 | /** |
| 44 | * Implementation of the port statistics service. |
| 45 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 46 | @Component(immediate = true, service = PortStatisticsService.class) |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 47 | public class PortStatisticsManager implements PortStatisticsService { |
Thomas Vachuska | d910a5c | 2015-05-29 17:09:59 -0700 | [diff] [blame] | 48 | |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 49 | private final Logger log = getLogger(getClass()); |
| 50 | |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 51 | private static final long POLL_FREQUENCY = 10_000; // milliseconds |
| 52 | private static final long STALE_LIMIT = (long) (1.5 * POLL_FREQUENCY); |
Srikanth Vavilapalli | 78baf58 | 2015-06-05 11:40:14 -0700 | [diff] [blame] | 53 | private static final int SECOND = 1_000; // milliseconds |
Thomas Vachuska | d910a5c | 2015-05-29 17:09:59 -0700 | [diff] [blame] | 54 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 56 | protected DeviceService deviceService; |
| 57 | |
| 58 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 59 | |
| 60 | private Map<ConnectPoint, DataPoint> current = Maps.newConcurrentMap(); |
| 61 | private Map<ConnectPoint, DataPoint> previous = Maps.newConcurrentMap(); |
| 62 | |
| 63 | @Activate |
| 64 | public void activate() { |
| 65 | deviceService.addListener(deviceListener); |
| 66 | log.info("Started"); |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Deactivate |
| 70 | public void deactivate() { |
| 71 | deviceService.removeListener(deviceListener); |
| 72 | log.info("Stopped"); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public Load load(ConnectPoint connectPoint) { |
Thomas Vachuska | 0932ac5 | 2017-03-30 13:28:49 -0700 | [diff] [blame] | 77 | return load(connectPoint, MetricType.BYTES); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public Load load(ConnectPoint connectPoint, MetricType metricType) { |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 82 | DataPoint c = current.get(connectPoint); |
| 83 | DataPoint p = previous.get(connectPoint); |
Thomas Vachuska | 028ddb9 | 2015-06-03 17:36:32 -0700 | [diff] [blame] | 84 | long now = System.currentTimeMillis(); |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 85 | |
| 86 | if (c != null && p != null && (now - c.time < STALE_LIMIT)) { |
Srikanth Vavilapalli | 8c1ccca | 2015-06-08 19:23:24 -0700 | [diff] [blame] | 87 | if (c.time > p.time + SECOND) { |
Thomas Vachuska | 0932ac5 | 2017-03-30 13:28:49 -0700 | [diff] [blame] | 88 | long cve = getEgressValue(c.stats, metricType); |
| 89 | long cvi = getIngressValue(c.stats, metricType); |
| 90 | long pve = getEgressValue(p.stats, metricType); |
| 91 | long pvi = getIngressValue(p.stats, metricType); |
Srikanth Vavilapalli | 8c1ccca | 2015-06-08 19:23:24 -0700 | [diff] [blame] | 92 | //Use max of either Tx or Rx load as the total load of a port |
| 93 | Load load = null; |
Thomas Vachuska | 0932ac5 | 2017-03-30 13:28:49 -0700 | [diff] [blame] | 94 | if (cve >= pve) { |
| 95 | load = new DefaultLoad(cve, pve, (int) (c.time - p.time) / SECOND); |
Srikanth Vavilapalli | 8c1ccca | 2015-06-08 19:23:24 -0700 | [diff] [blame] | 96 | } |
Thomas Vachuska | 0932ac5 | 2017-03-30 13:28:49 -0700 | [diff] [blame] | 97 | if (cvi >= pvi) { |
| 98 | Load rcvLoad = new DefaultLoad(cvi, pvi, (int) (c.time - p.time) / SECOND); |
Srikanth Vavilapalli | 8c1ccca | 2015-06-08 19:23:24 -0700 | [diff] [blame] | 99 | load = ((load == null) || (rcvLoad.rate() > load.rate())) ? rcvLoad : load; |
| 100 | } |
| 101 | return load; |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 102 | } |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 103 | } |
| 104 | return null; |
| 105 | } |
| 106 | |
Thomas Vachuska | 0932ac5 | 2017-03-30 13:28:49 -0700 | [diff] [blame] | 107 | private long getEgressValue(PortStatistics stats, MetricType metricType) { |
| 108 | return metricType == MetricType.BYTES ? stats.bytesSent() : stats.packetsSent(); |
| 109 | } |
| 110 | |
| 111 | private long getIngressValue(PortStatistics stats, MetricType metricType) { |
| 112 | return metricType == MetricType.BYTES ? stats.bytesReceived() : stats.packetsReceived(); |
| 113 | } |
| 114 | |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 115 | // Monitors port stats update messages. |
| 116 | private class InternalDeviceListener implements DeviceListener { |
| 117 | @Override |
| 118 | public void event(DeviceEvent event) { |
| 119 | DeviceEvent.Type type = event.type(); |
| 120 | DeviceId deviceId = event.subject().id(); |
| 121 | if (type == PORT_STATS_UPDATED) { |
| 122 | // Update port load |
| 123 | updateDeviceData(deviceId); |
| 124 | |
| 125 | } else if (type == DEVICE_REMOVED || |
| 126 | (type == DEVICE_AVAILABILITY_CHANGED && |
| 127 | !deviceService.isAvailable(deviceId))) { |
| 128 | // Clean-up all port loads |
| 129 | pruneDeviceData(deviceId); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Updates the port stats for the specified device |
| 135 | private void updateDeviceData(DeviceId deviceId) { |
| 136 | deviceService.getPortStatistics(deviceId) |
| 137 | .forEach(stats -> updatePortData(deviceId, stats)); |
| 138 | } |
| 139 | |
| 140 | // Updates the port stats for the specified port |
| 141 | private void updatePortData(DeviceId deviceId, PortStatistics stats) { |
Ray Milkey | 5ec4208 | 2019-02-13 09:56:07 -0800 | [diff] [blame] | 142 | ConnectPoint cp = new ConnectPoint(deviceId, stats.portNumber()); |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 143 | DataPoint c = current.get(cp); |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 144 | |
| 145 | // Create a new data point and make it the current one |
| 146 | current.put(cp, new DataPoint(stats)); |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 147 | |
| 148 | // If we have a current data point, demote it to previous |
| 149 | if (c != null) { |
| 150 | previous.put(cp, c); |
| 151 | } |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // Cleans all port loads for the specified device |
| 155 | private void pruneDeviceData(DeviceId deviceId) { |
| 156 | pruneMap(current, deviceId); |
| 157 | pruneMap(previous, deviceId); |
| 158 | } |
| 159 | |
| 160 | private void pruneMap(Map<ConnectPoint, DataPoint> map, DeviceId deviceId) { |
| 161 | map.keySet().stream().filter(cp -> deviceId.equals(cp.deviceId())) |
| 162 | .collect(Collectors.toSet()).forEach(map::remove); |
| 163 | } |
| 164 | |
| 165 | // Auxiliary data point to track when we receive different samples. |
| 166 | private class DataPoint { |
| 167 | long time; |
| 168 | PortStatistics stats; |
| 169 | |
| 170 | DataPoint(PortStatistics stats) { |
| 171 | time = System.currentTimeMillis(); |
| 172 | this.stats = stats; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | } |