blob: 62410a358178a4a0b3e46875ec20e69885e9eb9d [file] [log] [blame]
Thomas Vachuskac40d4632015-04-09 16:55:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuskac40d4632015-04-09 16:55:03 -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.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;
Thomas Vachuskacab29d22018-02-21 15:47:29 -080026import org.onosproject.mastership.MastershipAdminService;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070027import org.onosproject.mastership.MastershipService;
28import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.Device;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.Host;
32import org.onosproject.net.HostId;
33import org.onosproject.net.HostLocation;
34import org.onosproject.net.Link;
35import org.onosproject.net.Port;
36import org.onosproject.net.PortNumber;
37import org.onosproject.net.device.DefaultDeviceDescription;
38import org.onosproject.net.device.DefaultPortDescription;
39import org.onosproject.net.device.DeviceAdminService;
40import org.onosproject.net.device.DeviceDescription;
41import org.onosproject.net.device.DeviceEvent;
42import org.onosproject.net.device.DeviceListener;
43import org.onosproject.net.device.DeviceProviderService;
44import org.onosproject.net.device.PortDescription;
45import org.onosproject.net.host.DefaultHostDescription;
46import org.onosproject.net.host.HostDescription;
47import org.onosproject.net.host.HostProviderService;
48import org.onosproject.net.host.HostService;
49import org.onosproject.net.link.DefaultLinkDescription;
50import org.onosproject.net.link.LinkProviderService;
51import org.onosproject.net.link.LinkService;
52import org.slf4j.Logger;
53import org.slf4j.LoggerFactory;
54
55import java.util.List;
56import java.util.concurrent.CountDownLatch;
57import java.util.concurrent.TimeUnit;
58
59import static org.onlab.util.Tools.toHex;
60import static org.onosproject.net.HostId.hostId;
61import static org.onosproject.net.Link.Type.DIRECT;
Thomas Vachuskacab29d22018-02-21 15:47:29 -080062import static org.onosproject.net.MastershipRole.MASTER;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070063import static org.onosproject.net.PortNumber.portNumber;
64import static org.onosproject.net.device.DeviceEvent.Type.*;
65import static org.onosproject.provider.nil.NullProviders.SCHEME;
66
67/**
68 * Abstraction of a provider capable to simulate some network topology.
69 */
70public abstract class TopologySimulator {
71
72 protected final Logger log = LoggerFactory.getLogger(getClass());
73
74 protected String[] topoShape;
75 protected int deviceCount;
76 protected int hostCount;
77
78 protected ServiceDirectory directory;
79 protected NodeId localNode;
80
81 protected ClusterService clusterService;
82 protected MastershipService mastershipService;
Thomas Vachuskacab29d22018-02-21 15:47:29 -080083 protected MastershipAdminService mastershipAdminService;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070084
85 protected DeviceAdminService deviceService;
86 protected HostService hostService;
87 protected LinkService linkService;
88
89 protected DeviceProviderService deviceProviderService;
90 protected HostProviderService hostProviderService;
91 protected LinkProviderService linkProviderService;
92
93 protected int maxWaitSeconds = 1;
94 protected int infrastructurePorts = 2;
95 protected CountDownLatch deviceLatch;
96
97 protected final List<DeviceId> deviceIds = Lists.newArrayList();
98
Thomas Vachuskacab29d22018-02-21 15:47:29 -080099 private final DeviceListener deviceEventCounter = new DeviceEventCounter();
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700100
101 /**
102 * Initializes a new topology simulator with access to the specified service
103 * directory and various provider services.
104 *
105 * @param topoShape topology shape specifier
106 * @param deviceCount number of devices in the topology
107 * @param hostCount number of hosts per device
108 * @param directory service directory
109 * @param deviceProviderService device provider service
110 * @param hostProviderService host provider service
111 * @param linkProviderService link provider service
112 */
113 protected void init(String topoShape, int deviceCount, int hostCount,
114 ServiceDirectory directory,
115 DeviceProviderService deviceProviderService,
116 HostProviderService hostProviderService,
117 LinkProviderService linkProviderService) {
118 this.deviceCount = deviceCount;
119 this.hostCount = hostCount;
120 this.directory = directory;
121
122 this.clusterService = directory.get(ClusterService.class);
123 this.mastershipService = directory.get(MastershipService.class);
Thomas Vachuskacab29d22018-02-21 15:47:29 -0800124 this.mastershipAdminService = directory.get(MastershipAdminService.class);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700125
126 this.deviceService = directory.get(DeviceAdminService.class);
127 this.hostService = directory.get(HostService.class);
128 this.linkService = directory.get(LinkService.class);
129 this.deviceProviderService = deviceProviderService;
130 this.hostProviderService = hostProviderService;
131 this.linkProviderService = linkProviderService;
132
133 localNode = clusterService.getLocalNode().id();
134
135 processTopoShape(topoShape);
136 }
137
138 /**
139 * Processes the topology shape specifier.
140 *
141 * @param shape topology shape specifier
142 */
143 protected void processTopoShape(String shape) {
144 this.topoShape = shape.split(",");
145 }
146
147 /**
148 * Sets up network topology simulation.
149 */
150 public void setUpTopology() {
Thomas Vachuskacab29d22018-02-21 15:47:29 -0800151 deviceIds.clear();
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700152 prepareForDeviceEvents(deviceCount);
153 createDevices();
154 waitForDeviceEvents();
155
156 createLinks();
157 createHosts();
158 }
159
160 /**
161 * Creates simulated devices.
162 */
163 protected void createDevices() {
164 for (int i = 0; i < deviceCount; i++) {
165 createDevice(i + 1);
166 }
167 }
168
169 /**
170 * Creates simulated links.
171 */
172 protected abstract void createLinks();
173
174 /**
175 * Creates simulated hosts.
176 */
177 protected abstract void createHosts();
178
179 /**
Thomas Vachuska8e038eb2016-02-23 23:28:23 -0800180 * Creates simulated device and adds its id to the list of devices ids.
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700181 *
182 * @param i index of the device id in the list.
183 */
184 protected void createDevice(int i) {
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800185 createDevice(DeviceId.deviceId(SCHEME + ":" + toHex(i)), i);
Thomas Vachuska8e038eb2016-02-23 23:28:23 -0800186 }
187
188 /**
189 * Creates simulated device.
190 *
Thomas Vachuska5c6ed222016-06-29 11:02:22 +0200191 * @param id device identifier
Thomas Vachuska8e038eb2016-02-23 23:28:23 -0800192 * @param chassisId chassis identifier number
193 */
Thomas Vachuska5c6ed222016-06-29 11:02:22 +0200194 public void createDevice(DeviceId id, int chassisId) {
195 createDevice(id, chassisId, Device.Type.SWITCH, hostCount + infrastructurePorts);
196 }
197
198 /**
199 * Creates simulated device.
200 *
201 * @param id device identifier
202 * @param chassisId chassis identifier number
203 * @param type device type
204 * @param portCount number of device ports
205 */
206 public void createDevice(DeviceId id, int chassisId, Device.Type type, int portCount) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700207 DeviceDescription desc =
Thomas Vachuska5c6ed222016-06-29 11:02:22 +0200208 new DefaultDeviceDescription(id.uri(), type,
Thomas Vachuskaa10137c2018-04-03 16:45:59 -0700209 "ONF", "0.1", "0.1", "1234",
Thomas Vachuska8e038eb2016-02-23 23:28:23 -0800210 new ChassisId(chassisId));
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800211 deviceIds.add(id);
Thomas Vachuskacab29d22018-02-21 15:47:29 -0800212 mastershipAdminService.setRoleSync(localNode, id, MASTER);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700213 deviceProviderService.deviceConnected(id, desc);
Thomas Vachuska5c6ed222016-06-29 11:02:22 +0200214 deviceProviderService.updatePorts(id, buildPorts(portCount));
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700215 }
216
217 /**
218 * Creates simulated link between two devices.
219 *
Thomas Vachuskaf651cc92015-04-14 16:11:44 -0700220 * @param i index of one simulated device
221 * @param j index of another simulated device
222 * @param pi port number of i-th device
223 * @param pj port number of j-th device
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700224 */
Thomas Vachuska5c6ed222016-06-29 11:02:22 +0200225 public void createLink(int i, int j, int pi, int pj) {
Thomas Vachuskaf651cc92015-04-14 16:11:44 -0700226 ConnectPoint one = new ConnectPoint(deviceIds.get(i), PortNumber.portNumber(pi));
227 ConnectPoint two = new ConnectPoint(deviceIds.get(j), PortNumber.portNumber(pj));
Thomas Vachuska8e038eb2016-02-23 23:28:23 -0800228 createLink(one, two);
229 }
230
231 /**
232 * Creates simulated link between two connection points.
233 *
Thomas Vachuska5c6ed222016-06-29 11:02:22 +0200234 * @param one one connection point
235 * @param two another connection point
Thomas Vachuska8e038eb2016-02-23 23:28:23 -0800236 */
Thomas Vachuska5c6ed222016-06-29 11:02:22 +0200237 public void createLink(ConnectPoint one, ConnectPoint two) {
238 createLink(one, two, DIRECT, true);
239 }
240
241 /**
242 * Creates simulated link between two connection points.
243 *
244 * @param one one connection point
245 * @param two another connection point
246 * @param type link type
247 * @param isBidirectional true if link is bidirectional
248 */
249 public void createLink(ConnectPoint one, ConnectPoint two, Link.Type type, boolean isBidirectional) {
250 linkProviderService.linkDetected(new DefaultLinkDescription(one, two, type));
251 if (isBidirectional) {
252 linkProviderService.linkDetected(new DefaultLinkDescription(two, one, type));
253 }
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700254 }
255
256 /**
257 * Creates simularted hosts for the specified device.
258 *
259 * @param deviceId device identifier
260 * @param portOffset port offset where to start attaching hosts
261 */
Thomas Vachuska5c6ed222016-06-29 11:02:22 +0200262 public void createHosts(DeviceId deviceId, int portOffset) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700263 String s = deviceId.toString();
Thomas Vachuska1cbd65e2016-06-25 16:27:57 +0200264 byte dByte = Byte.parseByte(s.substring(s.length() - 2), 16);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700265 // TODO: this limits the simulation to 256 devices & 256 hosts/device.
266 byte[] macBytes = new byte[]{0, 0, 0, 0, dByte, 0};
267 byte[] ipBytes = new byte[]{(byte) 192, (byte) 168, dByte, 0};
268
269 for (int i = 0; i < hostCount; i++) {
270 int port = portOffset + i + 1;
271 macBytes[5] = (byte) (i + 1);
272 ipBytes[3] = (byte) (i + 1);
273 HostId id = hostId(MacAddress.valueOf(macBytes), VlanId.NONE);
274 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET, ipBytes);
Ray Milkeydc083442016-02-22 11:27:57 -0800275 hostProviderService.hostDetected(id, description(id, ip, deviceId, port), false);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700276 }
277 }
278
279 /**
280 * Prepares to count device added/available/removed events.
281 *
282 * @param count number of events to count
283 */
284 protected void prepareForDeviceEvents(int count) {
285 deviceLatch = new CountDownLatch(count);
286 deviceService.addListener(deviceEventCounter);
287 }
288
289 /**
290 * Waits for all expected device added/available/removed events.
291 */
292 protected void waitForDeviceEvents() {
293 try {
294 deviceLatch.await(maxWaitSeconds, TimeUnit.SECONDS);
295 } catch (InterruptedException e) {
296 log.warn("Device events did not arrive in time");
Ray Milkey5c7d4882018-02-05 14:50:39 -0800297 Thread.currentThread().interrupt();
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700298 }
299 deviceService.removeListener(deviceEventCounter);
300 }
301
302
303 /**
304 * Tears down network topology simulation.
305 */
306 public void tearDownTopology() {
307 removeHosts();
308 removeLinks();
309 removeDevices();
310 }
311
312 /**
313 * Removes any hosts previously advertised by this provider.
314 */
315 protected void removeHosts() {
316 hostService.getHosts()
317 .forEach(host -> hostProviderService.hostVanished(host.id()));
318 }
319
320 /**
321 * Removes any links previously advertised by this provider.
322 */
323 protected void removeLinks() {
324 linkService.getLinks()
325 .forEach(link -> linkProviderService.linkVanished(description(link)));
326 }
327
328 /**
329 * Removes any devices previously advertised by this provider.
330 */
331 protected void removeDevices() {
Thomas Vachuska5f429d62015-05-28 15:34:36 -0700332 prepareForDeviceEvents(deviceIds.size());
333 deviceIds.forEach(deviceProviderService::deviceDisconnected);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700334 waitForDeviceEvents();
Thomas Vachuskacab29d22018-02-21 15:47:29 -0800335 mastershipService.getDevicesOf(localNode).forEach(mastershipService::relinquishMastership);
336 deviceIds.forEach(mastershipService::relinquishMastership);
Thomas Vachuska5f429d62015-05-28 15:34:36 -0700337 deviceIds.clear();
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700338 }
339
340
341 /**
342 * Produces a device description from the given device.
343 *
344 * @param device device to copy
345 * @return device description
346 */
347 static DeviceDescription description(Device device) {
348 return new DefaultDeviceDescription(device.id().uri(), device.type(),
349 device.manufacturer(),
350 device.hwVersion(), device.swVersion(),
351 device.serialNumber(), device.chassisId());
352 }
353
354 /**
355 * Produces a link description from the given link.
356 *
357 * @param link link to copy
358 * @return link description
359 */
360 static DefaultLinkDescription description(Link link) {
361 return new DefaultLinkDescription(link.src(), link.dst(), link.type());
362 }
363
364 /**
365 * Produces a host description from the given host.
366 *
367 * @param host host to copy
368 * @return host description
369 */
370 static DefaultHostDescription description(Host host) {
371 return new DefaultHostDescription(host.mac(), host.vlan(), host.location(),
372 host.ipAddresses());
373 }
374
375 /**
376 * Generates a host description from the given id and location information.
377 *
378 * @param hostId host identifier
379 * @param ip host IP
380 * @param deviceId edge device
381 * @param port edge port
382 * @return host description
383 */
384 static HostDescription description(HostId hostId, IpAddress ip,
385 DeviceId deviceId, int port) {
386 HostLocation location = new HostLocation(deviceId, portNumber(port), 0L);
387 return new DefaultHostDescription(hostId.mac(), hostId.vlanId(), location, ip);
388 }
389
390 /**
391 * Generates a list of a configured number of ports.
392 *
393 * @param portCount number of ports
394 * @return list of ports
395 */
396 protected List<PortDescription> buildPorts(int portCount) {
397 List<PortDescription> ports = Lists.newArrayList();
Thomas Vachuska6c0582e2016-07-05 12:20:31 -0700398 for (int i = 1; i <= portCount; i++) {
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800399 ports.add(DefaultPortDescription.builder()
400 .withPortNumber(PortNumber.portNumber(i))
401 .isEnabled(true)
402 .type(Port.Type.COPPER)
403 .portSpeed(0)
404 .build());
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700405 }
406 return ports;
407 }
408
Thomas Vachuska5f429d62015-05-28 15:34:36 -0700409 /**
Thomas Vachuska8e038eb2016-02-23 23:28:23 -0800410 * Indicates whether or not the simulation deeps the device as available.
Thomas Vachuska5f429d62015-05-28 15:34:36 -0700411 *
412 * @param deviceId device identifier
413 * @return true if device is known
414 */
415 public boolean contains(DeviceId deviceId) {
416 return deviceIds.contains(deviceId);
417 }
418
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700419 // Counts down number of device added/available/removed events.
420 private class DeviceEventCounter implements DeviceListener {
421 @Override
422 public void event(DeviceEvent event) {
423 DeviceEvent.Type type = event.type();
424 if (type == DEVICE_ADDED || type == DEVICE_REMOVED ||
425 type == DEVICE_AVAILABILITY_CHANGED) {
426 deviceLatch.countDown();
427 }
428 }
429 }
Ray Milkeydc083442016-02-22 11:27:57 -0800430}