blob: 9f2320ed58341a88e1a5519514d6cd66aa2864dc [file] [log] [blame]
Thomas Vachuskac40d4632015-04-09 16:55:03 -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.provider.nil;
17
18import com.google.common.collect.Lists;
19import org.onlab.osgi.ServiceDirectory;
20import org.onlab.packet.ChassisId;
21import org.onlab.packet.IpAddress;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
24import org.onosproject.cluster.ClusterService;
25import org.onosproject.cluster.NodeId;
26import org.onosproject.mastership.MastershipService;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.Device;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.Host;
31import org.onosproject.net.HostId;
32import org.onosproject.net.HostLocation;
33import org.onosproject.net.Link;
34import org.onosproject.net.Port;
35import org.onosproject.net.PortNumber;
36import org.onosproject.net.device.DefaultDeviceDescription;
37import org.onosproject.net.device.DefaultPortDescription;
38import org.onosproject.net.device.DeviceAdminService;
39import org.onosproject.net.device.DeviceDescription;
40import org.onosproject.net.device.DeviceEvent;
41import org.onosproject.net.device.DeviceListener;
42import org.onosproject.net.device.DeviceProviderService;
43import org.onosproject.net.device.PortDescription;
44import org.onosproject.net.host.DefaultHostDescription;
45import org.onosproject.net.host.HostDescription;
46import org.onosproject.net.host.HostProviderService;
47import org.onosproject.net.host.HostService;
48import org.onosproject.net.link.DefaultLinkDescription;
49import org.onosproject.net.link.LinkProviderService;
50import org.onosproject.net.link.LinkService;
51import org.slf4j.Logger;
52import org.slf4j.LoggerFactory;
53
54import java.util.List;
55import java.util.concurrent.CountDownLatch;
56import java.util.concurrent.TimeUnit;
57
58import static org.onlab.util.Tools.toHex;
59import static org.onosproject.net.HostId.hostId;
60import static org.onosproject.net.Link.Type.DIRECT;
61import static org.onosproject.net.PortNumber.portNumber;
62import static org.onosproject.net.device.DeviceEvent.Type.*;
63import static org.onosproject.provider.nil.NullProviders.SCHEME;
64
65/**
66 * Abstraction of a provider capable to simulate some network topology.
67 */
68public abstract class TopologySimulator {
69
70 protected final Logger log = LoggerFactory.getLogger(getClass());
71
72 protected String[] topoShape;
73 protected int deviceCount;
74 protected int hostCount;
75
76 protected ServiceDirectory directory;
77 protected NodeId localNode;
78
79 protected ClusterService clusterService;
80 protected MastershipService mastershipService;
81
82 protected DeviceAdminService deviceService;
83 protected HostService hostService;
84 protected LinkService linkService;
85
86 protected DeviceProviderService deviceProviderService;
87 protected HostProviderService hostProviderService;
88 protected LinkProviderService linkProviderService;
89
90 protected int maxWaitSeconds = 1;
91 protected int infrastructurePorts = 2;
92 protected CountDownLatch deviceLatch;
93
94 protected final List<DeviceId> deviceIds = Lists.newArrayList();
95
96 private DeviceListener deviceEventCounter = new DeviceEventCounter();
97
98 /**
99 * Initializes a new topology simulator with access to the specified service
100 * directory and various provider services.
101 *
102 * @param topoShape topology shape specifier
103 * @param deviceCount number of devices in the topology
104 * @param hostCount number of hosts per device
105 * @param directory service directory
106 * @param deviceProviderService device provider service
107 * @param hostProviderService host provider service
108 * @param linkProviderService link provider service
109 */
110 protected void init(String topoShape, int deviceCount, int hostCount,
111 ServiceDirectory directory,
112 DeviceProviderService deviceProviderService,
113 HostProviderService hostProviderService,
114 LinkProviderService linkProviderService) {
115 this.deviceCount = deviceCount;
116 this.hostCount = hostCount;
117 this.directory = directory;
118
119 this.clusterService = directory.get(ClusterService.class);
120 this.mastershipService = directory.get(MastershipService.class);
121
122 this.deviceService = directory.get(DeviceAdminService.class);
123 this.hostService = directory.get(HostService.class);
124 this.linkService = directory.get(LinkService.class);
125 this.deviceProviderService = deviceProviderService;
126 this.hostProviderService = hostProviderService;
127 this.linkProviderService = linkProviderService;
128
129 localNode = clusterService.getLocalNode().id();
130
131 processTopoShape(topoShape);
132 }
133
134 /**
135 * Processes the topology shape specifier.
136 *
137 * @param shape topology shape specifier
138 */
139 protected void processTopoShape(String shape) {
140 this.topoShape = shape.split(",");
141 }
142
143 /**
144 * Sets up network topology simulation.
145 */
146 public void setUpTopology() {
147 prepareForDeviceEvents(deviceCount);
148 createDevices();
149 waitForDeviceEvents();
150
151 createLinks();
152 createHosts();
153 }
154
155 /**
156 * Creates simulated devices.
157 */
158 protected void createDevices() {
159 for (int i = 0; i < deviceCount; i++) {
160 createDevice(i + 1);
161 }
162 }
163
164 /**
165 * Creates simulated links.
166 */
167 protected abstract void createLinks();
168
169 /**
170 * Creates simulated hosts.
171 */
172 protected abstract void createHosts();
173
174 /**
175 * Creates simulated device.
176 *
177 * @param i index of the device id in the list.
178 */
179 protected void createDevice(int i) {
180 DeviceId id = DeviceId.deviceId(SCHEME + ":" + toHex(i));
181 DeviceDescription desc =
182 new DefaultDeviceDescription(id.uri(), Device.Type.SWITCH,
183 "ON.Lab", "0.1", "0.1", "1234",
184 new ChassisId(i));
Thomas Vachuska5f429d62015-05-28 15:34:36 -0700185 deviceIds.add(id);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700186 deviceProviderService.deviceConnected(id, desc);
187 deviceProviderService.updatePorts(id, buildPorts(hostCount + infrastructurePorts));
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700188 }
189
Thomas Vachuskaf651cc92015-04-14 16:11:44 -0700190// /**
191// * Creates simulated link between two devices on port 1 and port 2.
192// *
193// * @param i index of one simulated device
194// * @param j index of another simulated device
195// */
196// protected void createLink(int i, int j) {
197// createLink(i, j, 1, 2);
198// }
199
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700200 /**
201 * Creates simulated link between two devices.
202 *
Thomas Vachuskaf651cc92015-04-14 16:11:44 -0700203 * @param i index of one simulated device
204 * @param j index of another simulated device
205 * @param pi port number of i-th device
206 * @param pj port number of j-th device
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700207 */
Thomas Vachuskaf651cc92015-04-14 16:11:44 -0700208 protected void createLink(int i, int j, int pi, int pj) {
209 ConnectPoint one = new ConnectPoint(deviceIds.get(i), PortNumber.portNumber(pi));
210 ConnectPoint two = new ConnectPoint(deviceIds.get(j), PortNumber.portNumber(pj));
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700211 linkProviderService.linkDetected(new DefaultLinkDescription(one, two, DIRECT));
212 linkProviderService.linkDetected(new DefaultLinkDescription(two, one, DIRECT));
213 }
214
215 /**
216 * Creates simularted hosts for the specified device.
217 *
218 * @param deviceId device identifier
219 * @param portOffset port offset where to start attaching hosts
220 */
221 protected void createHosts(DeviceId deviceId, int portOffset) {
222 String s = deviceId.toString();
223 byte dByte = Byte.parseByte(s.substring(s.length() - 1), 16);
224 // TODO: this limits the simulation to 256 devices & 256 hosts/device.
225 byte[] macBytes = new byte[]{0, 0, 0, 0, dByte, 0};
226 byte[] ipBytes = new byte[]{(byte) 192, (byte) 168, dByte, 0};
227
228 for (int i = 0; i < hostCount; i++) {
229 int port = portOffset + i + 1;
230 macBytes[5] = (byte) (i + 1);
231 ipBytes[3] = (byte) (i + 1);
232 HostId id = hostId(MacAddress.valueOf(macBytes), VlanId.NONE);
233 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET, ipBytes);
234 hostProviderService.hostDetected(id, description(id, ip, deviceId, port));
235 }
236 }
237
238 /**
239 * Prepares to count device added/available/removed events.
240 *
241 * @param count number of events to count
242 */
243 protected void prepareForDeviceEvents(int count) {
244 deviceLatch = new CountDownLatch(count);
245 deviceService.addListener(deviceEventCounter);
246 }
247
248 /**
249 * Waits for all expected device added/available/removed events.
250 */
251 protected void waitForDeviceEvents() {
252 try {
253 deviceLatch.await(maxWaitSeconds, TimeUnit.SECONDS);
254 } catch (InterruptedException e) {
255 log.warn("Device events did not arrive in time");
256 }
257 deviceService.removeListener(deviceEventCounter);
258 }
259
260
261 /**
262 * Tears down network topology simulation.
263 */
264 public void tearDownTopology() {
265 removeHosts();
266 removeLinks();
267 removeDevices();
268 }
269
270 /**
271 * Removes any hosts previously advertised by this provider.
272 */
273 protected void removeHosts() {
274 hostService.getHosts()
275 .forEach(host -> hostProviderService.hostVanished(host.id()));
276 }
277
278 /**
279 * Removes any links previously advertised by this provider.
280 */
281 protected void removeLinks() {
282 linkService.getLinks()
283 .forEach(link -> linkProviderService.linkVanished(description(link)));
284 }
285
286 /**
287 * Removes any devices previously advertised by this provider.
288 */
289 protected void removeDevices() {
Thomas Vachuska5f429d62015-05-28 15:34:36 -0700290 prepareForDeviceEvents(deviceIds.size());
291 deviceIds.forEach(deviceProviderService::deviceDisconnected);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700292 waitForDeviceEvents();
Thomas Vachuska5f429d62015-05-28 15:34:36 -0700293 deviceIds.clear();
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700294 }
295
296
297 /**
298 * Produces a device description from the given device.
299 *
300 * @param device device to copy
301 * @return device description
302 */
303 static DeviceDescription description(Device device) {
304 return new DefaultDeviceDescription(device.id().uri(), device.type(),
305 device.manufacturer(),
306 device.hwVersion(), device.swVersion(),
307 device.serialNumber(), device.chassisId());
308 }
309
310 /**
311 * Produces a link description from the given link.
312 *
313 * @param link link to copy
314 * @return link description
315 */
316 static DefaultLinkDescription description(Link link) {
317 return new DefaultLinkDescription(link.src(), link.dst(), link.type());
318 }
319
320 /**
321 * Produces a host description from the given host.
322 *
323 * @param host host to copy
324 * @return host description
325 */
326 static DefaultHostDescription description(Host host) {
327 return new DefaultHostDescription(host.mac(), host.vlan(), host.location(),
328 host.ipAddresses());
329 }
330
331 /**
332 * Generates a host description from the given id and location information.
333 *
334 * @param hostId host identifier
335 * @param ip host IP
336 * @param deviceId edge device
337 * @param port edge port
338 * @return host description
339 */
340 static HostDescription description(HostId hostId, IpAddress ip,
341 DeviceId deviceId, int port) {
342 HostLocation location = new HostLocation(deviceId, portNumber(port), 0L);
343 return new DefaultHostDescription(hostId.mac(), hostId.vlanId(), location, ip);
344 }
345
346 /**
347 * Generates a list of a configured number of ports.
348 *
349 * @param portCount number of ports
350 * @return list of ports
351 */
352 protected List<PortDescription> buildPorts(int portCount) {
353 List<PortDescription> ports = Lists.newArrayList();
354 for (int i = 0; i < portCount; i++) {
355 ports.add(new DefaultPortDescription(PortNumber.portNumber(i), true,
356 Port.Type.COPPER, 0));
357 }
358 return ports;
359 }
360
Thomas Vachuska5f429d62015-05-28 15:34:36 -0700361 /**
362 * Indicates whether or not the simulation knows of this device.
363 *
364 * @param deviceId device identifier
365 * @return true if device is known
366 */
367 public boolean contains(DeviceId deviceId) {
368 return deviceIds.contains(deviceId);
369 }
370
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700371 // Counts down number of device added/available/removed events.
372 private class DeviceEventCounter implements DeviceListener {
373 @Override
374 public void event(DeviceEvent event) {
375 DeviceEvent.Type type = event.type();
376 if (type == DEVICE_ADDED || type == DEVICE_REMOVED ||
377 type == DEVICE_AVAILABILITY_CHANGED) {
378 deviceLatch.countDown();
379 }
380 }
381 }
382}