blob: 81ebdae5882860ca290c0a52839295c30ee169e0 [file] [log] [blame]
Thomas Vachuskaa4eac6a2021-03-01 15:11:59 -08001/*
2 * Copyright 2015-present Open Networking Foundation
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.provider.nil;
17
18import org.onosproject.net.Device;
19import org.onosproject.net.Port;
20import org.onosproject.net.device.DefaultPortStatistics;
21import org.onosproject.net.device.DeviceProviderService;
22import org.onosproject.net.device.DeviceService;
23import org.onosproject.net.device.PortStatistics;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import java.security.SecureRandom;
28import java.util.HashSet;
29import java.util.Random;
30import java.util.Set;
31import java.util.concurrent.ExecutorService;
32
33import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
34import static org.onlab.util.Tools.delay;
35import static org.onlab.util.Tools.groupedThreads;
36
37/**
38 * Drives port statistics simulation using random generator.
39 */
40class PortStatsDriver implements Runnable {
41
42 private final Logger log = LoggerFactory.getLogger(getClass());
43
44 private static final int WAIT_DELAY = 2_000;
45
46 private final Random random = new SecureRandom();
47
48 private volatile boolean stopped = true;
49
50 private DeviceService deviceService;
51 private DeviceProviderService deviceProviderService;
52
53 private final ExecutorService executor =
54 newSingleThreadScheduledExecutor(groupedThreads("onos/null", "port-stats-mutator", log));
55
56 /**
57 * Starts the mutation process.
58 *
59 * @param deviceService device service
60 * @param deviceProviderService device provider service
61 */
62 void start(DeviceService deviceService,
63 DeviceProviderService deviceProviderService) {
Thomas Vachuskaf22fd662021-03-02 22:43:16 -080064 if (stopped) {
65 stopped = false;
66 this.deviceService = deviceService;
67 this.deviceProviderService = deviceProviderService;
68 executor.execute(this);
69 }
Thomas Vachuskaa4eac6a2021-03-01 15:11:59 -080070 }
71
72 /**
73 * Stops the mutation process.
74 */
75 void stop() {
76 stopped = true;
77 }
78
79 @Override
80 public void run() {
81 while (!stopped) {
82 delay(WAIT_DELAY);
83 deviceService.getAvailableDevices().forEach(this::updatePorts);
84 }
85 }
86
87 public void updatePorts(Device device) {
88 Set<PortStatistics> portStats = new HashSet<>();
89 for (Port port : deviceService.getPorts(device.id())) {
90 portStats.add(DefaultPortStatistics.builder()
91 .setBytesReceived(Math.abs(random.nextInt()))
92 .setBytesSent(Math.abs(random.nextInt()))
93 .setPacketsReceived(Math.abs(random.nextInt()))
94 .setPacketsSent(Math.abs(random.nextInt()))
95 .setDurationSec(2)
96 .setDeviceId(device.id())
97 .setPort(port.number())
98 .build());
99 }
100 deviceProviderService.updatePortStatistics(device.id(), portStats);
101 }
102
103}