blob: 1a615481f58839599f17911d2ca431acf8f1ba2c [file] [log] [blame]
Thomas Vachuskaf0397b52015-05-29 13:50:17 -07001/*
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 */
16package org.onosproject.incubator.net.impl;
17
18import com.google.common.collect.Maps;
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.onosproject.incubator.net.PortStatisticsService;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.device.DeviceEvent;
29import org.onosproject.net.device.DeviceListener;
30import org.onosproject.net.device.DeviceService;
31import org.onosproject.net.device.PortStatistics;
32import org.onosproject.net.statistic.DefaultLoad;
33import org.onosproject.net.statistic.Load;
34import org.slf4j.Logger;
35
36import java.util.Map;
37import java.util.stream.Collectors;
38
39import static org.onosproject.net.PortNumber.portNumber;
40import static org.onosproject.net.device.DeviceEvent.Type.*;
41import static org.slf4j.LoggerFactory.getLogger;
42
43/**
44 * Implementation of the port statistics service.
45 */
46@Component(immediate = true)
47@Service
48public class PortStatisticsManager implements PortStatisticsService {
Thomas Vachuskad910a5c2015-05-29 17:09:59 -070049
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070050 private final Logger log = getLogger(getClass());
51
Thomas Vachuska204cb6c2015-06-04 00:03:06 -070052 private static final long POLL_FREQUENCY = 10_000; // milliseconds
53 private static final long STALE_LIMIT = (long) (1.5 * POLL_FREQUENCY);
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -070054 private static final int SECOND = 1_000; // milliseconds
Thomas Vachuskad910a5c2015-05-29 17:09:59 -070055
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070056 @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 Vachuskaf0397b52015-05-29 13:50:17 -070068 }
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 Vachuska028ddb92015-06-03 17:36:32 -070080 long now = System.currentTimeMillis();
Thomas Vachuska204cb6c2015-06-04 00:03:06 -070081
82 if (c != null && p != null && (now - c.time < STALE_LIMIT)) {
Srikanth Vavilapalli8c1ccca2015-06-08 19:23:24 -070083 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 Vachuska204cb6c2015-06-04 00:03:06 -070096 }
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070097 }
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 Vachuskaf0397b52015-05-29 13:50:17 -0700129 DataPoint c = current.get(cp);
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700130
131 // Create a new data point and make it the current one
132 current.put(cp, new DataPoint(stats));
Thomas Vachuska204cb6c2015-06-04 00:03:06 -0700133
134 // If we have a current data point, demote it to previous
135 if (c != null) {
136 previous.put(cp, c);
137 }
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700138 }
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}