Thomas Vachuska | f0397b5 | 2015-05-29 13:50: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 | 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); |
Thomas Vachuska | d910a5c | 2015-05-29 17:09:59 -0700 | [diff] [blame] | 54 | |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 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) { |
| 77 | DataPoint c = current.get(connectPoint); |
| 78 | DataPoint p = previous.get(connectPoint); |
Thomas Vachuska | 028ddb9 | 2015-06-03 17:36:32 -0700 | [diff] [blame] | 79 | long now = System.currentTimeMillis(); |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 80 | |
| 81 | if (c != null && p != null && (now - c.time < STALE_LIMIT)) { |
| 82 | if (c.stats.durationSec() > p.stats.durationSec() && |
| 83 | c.stats.bytesSent() >= p.stats.bytesSent() && |
| 84 | c.stats.durationSec() >= POLL_FREQUENCY / 1_000) { |
| 85 | return new DefaultLoad(c.stats.bytesSent(), p.stats.bytesSent(), |
| 86 | c.stats.durationSec() - p.stats.durationSec()); |
| 87 | } |
| 88 | return new DefaultLoad(c.stats.bytesSent(), 0, c.stats.durationSec()); |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 89 | } |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | // Monitors port stats update messages. |
| 94 | private class InternalDeviceListener implements DeviceListener { |
| 95 | @Override |
| 96 | public void event(DeviceEvent event) { |
| 97 | DeviceEvent.Type type = event.type(); |
| 98 | DeviceId deviceId = event.subject().id(); |
| 99 | if (type == PORT_STATS_UPDATED) { |
| 100 | // Update port load |
| 101 | updateDeviceData(deviceId); |
| 102 | |
| 103 | } else if (type == DEVICE_REMOVED || |
| 104 | (type == DEVICE_AVAILABILITY_CHANGED && |
| 105 | !deviceService.isAvailable(deviceId))) { |
| 106 | // Clean-up all port loads |
| 107 | pruneDeviceData(deviceId); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Updates the port stats for the specified device |
| 113 | private void updateDeviceData(DeviceId deviceId) { |
| 114 | deviceService.getPortStatistics(deviceId) |
| 115 | .forEach(stats -> updatePortData(deviceId, stats)); |
| 116 | } |
| 117 | |
| 118 | // Updates the port stats for the specified port |
| 119 | private void updatePortData(DeviceId deviceId, PortStatistics stats) { |
| 120 | ConnectPoint cp = new ConnectPoint(deviceId, portNumber(stats.port())); |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 121 | DataPoint c = current.get(cp); |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 122 | |
| 123 | // Create a new data point and make it the current one |
| 124 | current.put(cp, new DataPoint(stats)); |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 125 | |
| 126 | // If we have a current data point, demote it to previous |
| 127 | if (c != null) { |
| 128 | previous.put(cp, c); |
| 129 | } |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // Cleans all port loads for the specified device |
| 133 | private void pruneDeviceData(DeviceId deviceId) { |
| 134 | pruneMap(current, deviceId); |
| 135 | pruneMap(previous, deviceId); |
| 136 | } |
| 137 | |
| 138 | private void pruneMap(Map<ConnectPoint, DataPoint> map, DeviceId deviceId) { |
| 139 | map.keySet().stream().filter(cp -> deviceId.equals(cp.deviceId())) |
| 140 | .collect(Collectors.toSet()).forEach(map::remove); |
| 141 | } |
| 142 | |
| 143 | // Auxiliary data point to track when we receive different samples. |
| 144 | private class DataPoint { |
| 145 | long time; |
| 146 | PortStatistics stats; |
| 147 | |
| 148 | DataPoint(PortStatistics stats) { |
| 149 | time = System.currentTimeMillis(); |
| 150 | this.stats = stats; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | } |