Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | */ |
| 16 | package org.onosproject.incubator.net.impl; |
| 17 | |
| 18 | import com.google.common.collect.Maps; |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.onosproject.incubator.net.PortStatisticsService; |
| 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.device.DeviceEvent; |
| 29 | import org.onosproject.net.device.DeviceListener; |
| 30 | import org.onosproject.net.device.DeviceService; |
| 31 | import org.onosproject.net.device.PortStatistics; |
| 32 | import org.onosproject.net.statistic.DefaultLoad; |
| 33 | import org.onosproject.net.statistic.Load; |
| 34 | import org.slf4j.Logger; |
| 35 | |
| 36 | import java.util.Map; |
| 37 | import java.util.stream.Collectors; |
| 38 | |
| 39 | import static org.onosproject.net.PortNumber.portNumber; |
| 40 | import static org.onosproject.net.device.DeviceEvent.Type.*; |
| 41 | import static org.slf4j.LoggerFactory.getLogger; |
| 42 | |
| 43 | /** |
| 44 | * Implementation of the port statistics service. |
| 45 | */ |
| 46 | @Component(immediate = true) |
| 47 | @Service |
| 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 | |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 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) { |
| 78 | DataPoint c = current.get(connectPoint); |
| 79 | DataPoint p = previous.get(connectPoint); |
Thomas Vachuska | 028ddb9 | 2015-06-03 17:36:32 -0700 | [diff] [blame] | 80 | long now = System.currentTimeMillis(); |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 81 | |
| 82 | if (c != null && p != null && (now - c.time < STALE_LIMIT)) { |
Srikanth Vavilapalli | 8c1ccca | 2015-06-08 19:23:24 -0700 | [diff] [blame] | 83 | if (c.time > p.time + SECOND) { |
| 84 | //Use max of either Tx or Rx load as the total load of a port |
| 85 | Load load = null; |
| 86 | if (c.stats.bytesSent() >= p.stats.bytesSent()) { |
| 87 | load = new DefaultLoad(c.stats.bytesSent(), p.stats.bytesSent(), |
| 88 | (int) (c.time - p.time) / SECOND); |
| 89 | } |
| 90 | if (c.stats.bytesReceived() >= p.stats.bytesReceived()) { |
| 91 | Load rcvLoad = new DefaultLoad(c.stats.bytesReceived(), p.stats.bytesReceived(), |
| 92 | (int) (c.time - p.time) / SECOND); |
| 93 | load = ((load == null) || (rcvLoad.rate() > load.rate())) ? rcvLoad : load; |
| 94 | } |
| 95 | return load; |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 96 | } |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 97 | } |
| 98 | return null; |
| 99 | } |
| 100 | |
| 101 | // Monitors port stats update messages. |
| 102 | private class InternalDeviceListener implements DeviceListener { |
| 103 | @Override |
| 104 | public void event(DeviceEvent event) { |
| 105 | DeviceEvent.Type type = event.type(); |
| 106 | DeviceId deviceId = event.subject().id(); |
| 107 | if (type == PORT_STATS_UPDATED) { |
| 108 | // Update port load |
| 109 | updateDeviceData(deviceId); |
| 110 | |
| 111 | } else if (type == DEVICE_REMOVED || |
| 112 | (type == DEVICE_AVAILABILITY_CHANGED && |
| 113 | !deviceService.isAvailable(deviceId))) { |
| 114 | // Clean-up all port loads |
| 115 | pruneDeviceData(deviceId); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Updates the port stats for the specified device |
| 121 | private void updateDeviceData(DeviceId deviceId) { |
| 122 | deviceService.getPortStatistics(deviceId) |
| 123 | .forEach(stats -> updatePortData(deviceId, stats)); |
| 124 | } |
| 125 | |
| 126 | // Updates the port stats for the specified port |
| 127 | private void updatePortData(DeviceId deviceId, PortStatistics stats) { |
| 128 | ConnectPoint cp = new ConnectPoint(deviceId, portNumber(stats.port())); |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 129 | DataPoint c = current.get(cp); |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 130 | |
| 131 | // Create a new data point and make it the current one |
| 132 | current.put(cp, new DataPoint(stats)); |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 133 | |
| 134 | // If we have a current data point, demote it to previous |
| 135 | if (c != null) { |
| 136 | previous.put(cp, c); |
| 137 | } |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // Cleans all port loads for the specified device |
| 141 | private void pruneDeviceData(DeviceId deviceId) { |
| 142 | pruneMap(current, deviceId); |
| 143 | pruneMap(previous, deviceId); |
| 144 | } |
| 145 | |
| 146 | private void pruneMap(Map<ConnectPoint, DataPoint> map, DeviceId deviceId) { |
| 147 | map.keySet().stream().filter(cp -> deviceId.equals(cp.deviceId())) |
| 148 | .collect(Collectors.toSet()).forEach(map::remove); |
| 149 | } |
| 150 | |
| 151 | // Auxiliary data point to track when we receive different samples. |
| 152 | private class DataPoint { |
| 153 | long time; |
| 154 | PortStatistics stats; |
| 155 | |
| 156 | DataPoint(PortStatistics stats) { |
| 157 | time = System.currentTimeMillis(); |
| 158 | this.stats = stats; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | } |