blob: 19b093c97c39aaaa8db564e2afd5f3ad68f53de1 [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 Vachuskad910a5c2015-05-29 17:09:59 -070052 private static final int SECOND = 1_000;
53
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070054 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected DeviceService deviceService;
56
57 private final DeviceListener deviceListener = new InternalDeviceListener();
58
59 private Map<ConnectPoint, DataPoint> current = Maps.newConcurrentMap();
60 private Map<ConnectPoint, DataPoint> previous = Maps.newConcurrentMap();
61
62 @Activate
63 public void activate() {
64 deviceService.addListener(deviceListener);
65 log.info("Started");
66
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 Vachuskad910a5c2015-05-29 17:09:59 -070079 if (c != null && p != null && (c.time > p.time + SECOND)) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070080 return new DefaultLoad(c.stats.bytesSent(), p.stats.bytesSent(),
Thomas Vachuskad910a5c2015-05-29 17:09:59 -070081 (int) (c.time - p.time) / SECOND);
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070082 }
83 return null;
84 }
85
86 // Monitors port stats update messages.
87 private class InternalDeviceListener implements DeviceListener {
88 @Override
89 public void event(DeviceEvent event) {
90 DeviceEvent.Type type = event.type();
91 DeviceId deviceId = event.subject().id();
92 if (type == PORT_STATS_UPDATED) {
93 // Update port load
94 updateDeviceData(deviceId);
95
96 } else if (type == DEVICE_REMOVED ||
97 (type == DEVICE_AVAILABILITY_CHANGED &&
98 !deviceService.isAvailable(deviceId))) {
99 // Clean-up all port loads
100 pruneDeviceData(deviceId);
101 }
102 }
103 }
104
105 // Updates the port stats for the specified device
106 private void updateDeviceData(DeviceId deviceId) {
107 deviceService.getPortStatistics(deviceId)
108 .forEach(stats -> updatePortData(deviceId, stats));
109 }
110
111 // Updates the port stats for the specified port
112 private void updatePortData(DeviceId deviceId, PortStatistics stats) {
113 ConnectPoint cp = new ConnectPoint(deviceId, portNumber(stats.port()));
114
115 // If we have a current data point, demote it to previous
116 DataPoint c = current.get(cp);
117 if (c != null) {
118 previous.put(cp, c);
119 }
120
121 // Create a new data point and make it the current one
122 current.put(cp, new DataPoint(stats));
123 }
124
125 // Cleans all port loads for the specified device
126 private void pruneDeviceData(DeviceId deviceId) {
127 pruneMap(current, deviceId);
128 pruneMap(previous, deviceId);
129 }
130
131 private void pruneMap(Map<ConnectPoint, DataPoint> map, DeviceId deviceId) {
132 map.keySet().stream().filter(cp -> deviceId.equals(cp.deviceId()))
133 .collect(Collectors.toSet()).forEach(map::remove);
134 }
135
136 // Auxiliary data point to track when we receive different samples.
137 private class DataPoint {
138 long time;
139 PortStatistics stats;
140
141 DataPoint(PortStatistics stats) {
142 time = System.currentTimeMillis();
143 this.stats = stats;
144 }
145 }
146
147}