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