blob: 8ad269ce3cabacf6cec5dc235bccf2d9784e8321 [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) {
64 stopped = false;
65 this.deviceService = deviceService;
66 this.deviceProviderService = deviceProviderService;
67 executor.execute(this);
68 }
69
70 /**
71 * Stops the mutation process.
72 */
73 void stop() {
74 stopped = true;
75 }
76
77 @Override
78 public void run() {
79 while (!stopped) {
80 delay(WAIT_DELAY);
81 deviceService.getAvailableDevices().forEach(this::updatePorts);
82 }
83 }
84
85 public void updatePorts(Device device) {
86 Set<PortStatistics> portStats = new HashSet<>();
87 for (Port port : deviceService.getPorts(device.id())) {
88 portStats.add(DefaultPortStatistics.builder()
89 .setBytesReceived(Math.abs(random.nextInt()))
90 .setBytesSent(Math.abs(random.nextInt()))
91 .setPacketsReceived(Math.abs(random.nextInt()))
92 .setPacketsSent(Math.abs(random.nextInt()))
93 .setDurationSec(2)
94 .setDeviceId(device.id())
95 .setPort(port.number())
96 .build());
97 }
98 deviceProviderService.updatePortStatistics(device.id(), portStats);
99 }
100
101}