blob: fb18f7b7fdad83ea9164c57b813825ca3624f9d3 [file] [log] [blame]
alshabib61286342014-12-24 10:34:00 -08001/*
2 * Copyright 2014 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.provider.nil.device.impl;
17
18
19import com.google.common.collect.Lists;
20import com.google.common.collect.Maps;
21import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.onlab.packet.ChassisId;
27import org.onosproject.net.Device;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.MastershipRole;
30import org.onosproject.net.Port;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.device.DefaultDeviceDescription;
33import org.onosproject.net.device.DefaultPortDescription;
34import org.onosproject.net.device.DeviceDescription;
35import org.onosproject.net.device.DeviceProvider;
36import org.onosproject.net.device.DeviceProviderRegistry;
37import org.onosproject.net.device.DeviceProviderService;
38import org.onosproject.net.device.PortDescription;
39import org.onosproject.net.provider.AbstractProvider;
40import org.onosproject.net.provider.ProviderId;
41import org.slf4j.Logger;
42
43import java.util.List;
44import java.util.Map;
45import java.util.concurrent.ExecutorService;
46import java.util.concurrent.Executors;
47import java.util.concurrent.TimeUnit;
48
49import static org.onlab.util.Tools.delay;
50import static org.onlab.util.Tools.namedThreads;
51import static org.slf4j.LoggerFactory.getLogger;
52
53/**
54 * Provider which advertises fake/nonexistant devices to the core.
55 * To be used for benchmarking only.
56 */
57@Component(immediate = true)
58public class NullDeviceProvider extends AbstractProvider implements DeviceProvider {
59
60 private static final Logger log = getLogger(NullDeviceProvider.class);
61 private static final String SCHEME = "null";
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected DeviceProviderRegistry providerRegistry;
65
66 private DeviceProviderService providerService;
67
68 private ExecutorService deviceBuilder = Executors.newFixedThreadPool(1,
69 namedThreads("null-device-creator"));
70
71
72
73 //currently hardcoded. will be made configurable via rest/cli.
74 private static final int NUMDEVICES = 10;
75 private static final int NUMPORTSPERDEVICE = 10;
76
77 //Delay between events in ms.
78 private static final int EVENTINTERVAL = 5;
79
80 private final Map<Integer, DeviceDescription> descriptions = Maps.newHashMap();
81
82 private DeviceCreator creator;
83
84
85 /**
86 *
87 * Creates a provider with the supplier identifier.
88 *
89 */
90 public NullDeviceProvider() {
91 super(new ProviderId("null", "org.onosproject.provider.nil"));
92 }
93
94 @Activate
95 public void activate() {
96 providerService = providerRegistry.register(this);
97 deviceBuilder.submit(new DeviceCreator(true));
98 log.info("Started");
99
100 }
101
102 @Deactivate
103 public void deactivate() {
104 deviceBuilder.submit(new DeviceCreator(false));
105 try {
106 deviceBuilder.awaitTermination(1000, TimeUnit.MILLISECONDS);
107 } catch (InterruptedException e) {
108 log.error("Device builder did not terminate");
109 }
110 deviceBuilder.shutdownNow();
111 providerRegistry.unregister(this);
112 providerService = null;
113
114 log.info("Stopped");
115 }
116
117 @Override
118 public void triggerProbe(DeviceId deviceId) {}
119
120 @Override
121 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {}
122
123 @Override
124 public boolean isReachable(DeviceId deviceId) {
125 return descriptions.values().stream()
126 .anyMatch(desc -> desc.deviceURI().equals(deviceId.uri()));
127 }
128
129
130 private class DeviceCreator implements Runnable {
131
132 private boolean setup;
133
134 public DeviceCreator(boolean setup) {
135 this.setup = setup;
136 }
137
138 @Override
139 public void run() {
140 if (setup) {
141 advertiseDevices();
142 } else {
143 removeDevices();
144 }
145 }
146
147 private void removeDevices() {
148 for (DeviceDescription desc : descriptions.values()) {
149 providerService.deviceDisconnected(
150 DeviceId.deviceId(desc.deviceURI()));
151 delay(EVENTINTERVAL);
152 }
153 descriptions.clear();
154 }
155
156 private void advertiseDevices() {
157 DeviceId did;
158 ChassisId cid;
159 for (int i = 0; i < NUMDEVICES; i++) {
160 did = DeviceId.deviceId(String.format("%s:%d", SCHEME, i));
161 cid = new ChassisId(i);
162 DeviceDescription desc =
163 new DefaultDeviceDescription(did.uri(), Device.Type.SWITCH,
164 "ON.Lab", "0.0.1", "0.0.1", "1234",
165 cid);
166 descriptions.put(i, desc);
167 providerService.deviceConnected(did, desc);
168 providerService.updatePorts(did, buildPorts());
169 delay(EVENTINTERVAL);
170 }
171 }
172
173 private List<PortDescription> buildPorts() {
174 List<PortDescription> ports = Lists.newArrayList();
175 for (int i = 0; i < NUMPORTSPERDEVICE; i++) {
176 ports.add(new DefaultPortDescription(PortNumber.portNumber(i), true,
177 Port.Type.COPPER,
178 (long) 0));
179 }
180 return ports;
181 }
182 }
183}