blob: 9d6e1cbc3154cf8d9bbd48f20a03f3a8d9e768ef [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 {
49 private final Logger log = getLogger(getClass());
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected DeviceService deviceService;
53
54 private final DeviceListener deviceListener = new InternalDeviceListener();
55
56 private Map<ConnectPoint, DataPoint> current = Maps.newConcurrentMap();
57 private Map<ConnectPoint, DataPoint> previous = Maps.newConcurrentMap();
58
59 @Activate
60 public void activate() {
61 deviceService.addListener(deviceListener);
62 log.info("Started");
63
64 }
65
66 @Deactivate
67 public void deactivate() {
68 deviceService.removeListener(deviceListener);
69 log.info("Stopped");
70 }
71
72 @Override
73 public Load load(ConnectPoint connectPoint) {
74 DataPoint c = current.get(connectPoint);
75 DataPoint p = previous.get(connectPoint);
76 if (c != null && p != null) {
77 return new DefaultLoad(c.stats.bytesSent(), p.stats.bytesSent(),
78 (int) (c.time - p.time) / 1000);
79 }
80 return null;
81 }
82
83 // Monitors port stats update messages.
84 private class InternalDeviceListener implements DeviceListener {
85 @Override
86 public void event(DeviceEvent event) {
87 DeviceEvent.Type type = event.type();
88 DeviceId deviceId = event.subject().id();
89 if (type == PORT_STATS_UPDATED) {
90 // Update port load
91 updateDeviceData(deviceId);
92
93 } else if (type == DEVICE_REMOVED ||
94 (type == DEVICE_AVAILABILITY_CHANGED &&
95 !deviceService.isAvailable(deviceId))) {
96 // Clean-up all port loads
97 pruneDeviceData(deviceId);
98 }
99 }
100 }
101
102 // Updates the port stats for the specified device
103 private void updateDeviceData(DeviceId deviceId) {
104 deviceService.getPortStatistics(deviceId)
105 .forEach(stats -> updatePortData(deviceId, stats));
106 }
107
108 // Updates the port stats for the specified port
109 private void updatePortData(DeviceId deviceId, PortStatistics stats) {
110 ConnectPoint cp = new ConnectPoint(deviceId, portNumber(stats.port()));
111
112 // If we have a current data point, demote it to previous
113 DataPoint c = current.get(cp);
114 if (c != null) {
115 previous.put(cp, c);
116 }
117
118 // Create a new data point and make it the current one
119 current.put(cp, new DataPoint(stats));
120 }
121
122 // Cleans all port loads for the specified device
123 private void pruneDeviceData(DeviceId deviceId) {
124 pruneMap(current, deviceId);
125 pruneMap(previous, deviceId);
126 }
127
128 private void pruneMap(Map<ConnectPoint, DataPoint> map, DeviceId deviceId) {
129 map.keySet().stream().filter(cp -> deviceId.equals(cp.deviceId()))
130 .collect(Collectors.toSet()).forEach(map::remove);
131 }
132
133 // Auxiliary data point to track when we receive different samples.
134 private class DataPoint {
135 long time;
136 PortStatistics stats;
137
138 DataPoint(PortStatistics stats) {
139 time = System.currentTimeMillis();
140 this.stats = stats;
141 }
142 }
143
144}