blob: c403bb9ee925ce87ee493c722024b7102b9581b6 [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 Vavilapalli78baf582015-06-05 11:40:14 -070083 if ((c.time > p.time + SECOND) &&
84 (c.stats.bytesSent() >= p.stats.bytesSent())) {
Thomas Vachuska204cb6c2015-06-04 00:03:06 -070085 return new DefaultLoad(c.stats.bytesSent(), p.stats.bytesSent(),
Srikanth Vavilapalli78baf582015-06-05 11:40:14 -070086 (int) (c.time - p.time) / SECOND);
Thomas Vachuska204cb6c2015-06-04 00:03:06 -070087 }
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070088 }
89 return null;
90 }
91
92 // Monitors port stats update messages.
93 private class InternalDeviceListener implements DeviceListener {
94 @Override
95 public void event(DeviceEvent event) {
96 DeviceEvent.Type type = event.type();
97 DeviceId deviceId = event.subject().id();
98 if (type == PORT_STATS_UPDATED) {
99 // Update port load
100 updateDeviceData(deviceId);
101
102 } else if (type == DEVICE_REMOVED ||
103 (type == DEVICE_AVAILABILITY_CHANGED &&
104 !deviceService.isAvailable(deviceId))) {
105 // Clean-up all port loads
106 pruneDeviceData(deviceId);
107 }
108 }
109 }
110
111 // Updates the port stats for the specified device
112 private void updateDeviceData(DeviceId deviceId) {
113 deviceService.getPortStatistics(deviceId)
114 .forEach(stats -> updatePortData(deviceId, stats));
115 }
116
117 // Updates the port stats for the specified port
118 private void updatePortData(DeviceId deviceId, PortStatistics stats) {
119 ConnectPoint cp = new ConnectPoint(deviceId, portNumber(stats.port()));
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700120 DataPoint c = current.get(cp);
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700121
122 // Create a new data point and make it the current one
123 current.put(cp, new DataPoint(stats));
Thomas Vachuska204cb6c2015-06-04 00:03:06 -0700124
125 // If we have a current data point, demote it to previous
126 if (c != null) {
127 previous.put(cp, c);
128 }
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700129 }
130
131 // Cleans all port loads for the specified device
132 private void pruneDeviceData(DeviceId deviceId) {
133 pruneMap(current, deviceId);
134 pruneMap(previous, deviceId);
135 }
136
137 private void pruneMap(Map<ConnectPoint, DataPoint> map, DeviceId deviceId) {
138 map.keySet().stream().filter(cp -> deviceId.equals(cp.deviceId()))
139 .collect(Collectors.toSet()).forEach(map::remove);
140 }
141
142 // Auxiliary data point to track when we receive different samples.
143 private class DataPoint {
144 long time;
145 PortStatistics stats;
146
147 DataPoint(PortStatistics stats) {
148 time = System.currentTimeMillis();
149 this.stats = stats;
150 }
151 }
152
153}