blob: e63fc9c9263f9eb8af3d76d929a0bd84bdc68196 [file] [log] [blame]
Thomas Vachuskaf0397b52015-05-29 13:50:17 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuskaf0397b52015-05-29 13:50:17 -07003 *
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;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070019import org.onosproject.incubator.net.PortStatisticsService;
20import org.onosproject.net.ConnectPoint;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.device.DeviceEvent;
23import org.onosproject.net.device.DeviceListener;
24import org.onosproject.net.device.DeviceService;
25import org.onosproject.net.device.PortStatistics;
26import org.onosproject.net.statistic.DefaultLoad;
27import org.onosproject.net.statistic.Load;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070028import org.osgi.service.component.annotations.Activate;
29import org.osgi.service.component.annotations.Component;
30import org.osgi.service.component.annotations.Deactivate;
31import org.osgi.service.component.annotations.Reference;
32import org.osgi.service.component.annotations.ReferenceCardinality;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070033import org.slf4j.Logger;
34
35import java.util.Map;
36import java.util.stream.Collectors;
37
38import static org.onosproject.net.PortNumber.portNumber;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070039import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED;
40import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED;
41import static org.onosproject.net.device.DeviceEvent.Type.PORT_STATS_UPDATED;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070042import static org.slf4j.LoggerFactory.getLogger;
43
44/**
45 * Implementation of the port statistics service.
46 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070047@Component(immediate = true, service = PortStatisticsService.class)
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070048public 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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070056 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070057 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) {
Thomas Vachuska0932ac52017-03-30 13:28:49 -070078 return load(connectPoint, MetricType.BYTES);
79 }
80
81 @Override
82 public Load load(ConnectPoint connectPoint, MetricType metricType) {
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070083 DataPoint c = current.get(connectPoint);
84 DataPoint p = previous.get(connectPoint);
Thomas Vachuska028ddb92015-06-03 17:36:32 -070085 long now = System.currentTimeMillis();
Thomas Vachuska204cb6c2015-06-04 00:03:06 -070086
87 if (c != null && p != null && (now - c.time < STALE_LIMIT)) {
Srikanth Vavilapalli8c1ccca2015-06-08 19:23:24 -070088 if (c.time > p.time + SECOND) {
Thomas Vachuska0932ac52017-03-30 13:28:49 -070089 long cve = getEgressValue(c.stats, metricType);
90 long cvi = getIngressValue(c.stats, metricType);
91 long pve = getEgressValue(p.stats, metricType);
92 long pvi = getIngressValue(p.stats, metricType);
Srikanth Vavilapalli8c1ccca2015-06-08 19:23:24 -070093 //Use max of either Tx or Rx load as the total load of a port
94 Load load = null;
Thomas Vachuska0932ac52017-03-30 13:28:49 -070095 if (cve >= pve) {
96 load = new DefaultLoad(cve, pve, (int) (c.time - p.time) / SECOND);
Srikanth Vavilapalli8c1ccca2015-06-08 19:23:24 -070097 }
Thomas Vachuska0932ac52017-03-30 13:28:49 -070098 if (cvi >= pvi) {
99 Load rcvLoad = new DefaultLoad(cvi, pvi, (int) (c.time - p.time) / SECOND);
Srikanth Vavilapalli8c1ccca2015-06-08 19:23:24 -0700100 load = ((load == null) || (rcvLoad.rate() > load.rate())) ? rcvLoad : load;
101 }
102 return load;
Thomas Vachuska204cb6c2015-06-04 00:03:06 -0700103 }
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700104 }
105 return null;
106 }
107
Thomas Vachuska0932ac52017-03-30 13:28:49 -0700108 private long getEgressValue(PortStatistics stats, MetricType metricType) {
109 return metricType == MetricType.BYTES ? stats.bytesSent() : stats.packetsSent();
110 }
111
112 private long getIngressValue(PortStatistics stats, MetricType metricType) {
113 return metricType == MetricType.BYTES ? stats.bytesReceived() : stats.packetsReceived();
114 }
115
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700116 // Monitors port stats update messages.
117 private class InternalDeviceListener implements DeviceListener {
118 @Override
119 public void event(DeviceEvent event) {
120 DeviceEvent.Type type = event.type();
121 DeviceId deviceId = event.subject().id();
122 if (type == PORT_STATS_UPDATED) {
123 // Update port load
124 updateDeviceData(deviceId);
125
126 } else if (type == DEVICE_REMOVED ||
127 (type == DEVICE_AVAILABILITY_CHANGED &&
128 !deviceService.isAvailable(deviceId))) {
129 // Clean-up all port loads
130 pruneDeviceData(deviceId);
131 }
132 }
133 }
134
135 // Updates the port stats for the specified device
136 private void updateDeviceData(DeviceId deviceId) {
137 deviceService.getPortStatistics(deviceId)
138 .forEach(stats -> updatePortData(deviceId, stats));
139 }
140
141 // Updates the port stats for the specified port
142 private void updatePortData(DeviceId deviceId, PortStatistics stats) {
143 ConnectPoint cp = new ConnectPoint(deviceId, portNumber(stats.port()));
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700144 DataPoint c = current.get(cp);
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700145
146 // Create a new data point and make it the current one
147 current.put(cp, new DataPoint(stats));
Thomas Vachuska204cb6c2015-06-04 00:03:06 -0700148
149 // If we have a current data point, demote it to previous
150 if (c != null) {
151 previous.put(cp, c);
152 }
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700153 }
154
155 // Cleans all port loads for the specified device
156 private void pruneDeviceData(DeviceId deviceId) {
157 pruneMap(current, deviceId);
158 pruneMap(previous, deviceId);
159 }
160
161 private void pruneMap(Map<ConnectPoint, DataPoint> map, DeviceId deviceId) {
162 map.keySet().stream().filter(cp -> deviceId.equals(cp.deviceId()))
163 .collect(Collectors.toSet()).forEach(map::remove);
164 }
165
166 // Auxiliary data point to track when we receive different samples.
167 private class DataPoint {
168 long time;
169 PortStatistics stats;
170
171 DataPoint(PortStatistics stats) {
172 time = System.currentTimeMillis();
173 this.stats = stats;
174 }
175 }
176
177}