blob: 52883a2ef983ef5d46e6996975ed9684b399c4f2 [file] [log] [blame]
Thomas Vachuska5c6ed222016-06-29 11:02:22 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Thomas Vachuska5c6ed222016-06-29 11:02:22 +02003 *
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 */
16
17package org.onosproject.provider.nil;
18
19import com.google.common.collect.Maps;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020020import org.onlab.packet.IpAddress;
Thomas Vachuska5c6ed222016-06-29 11:02:22 +020021import org.onlab.packet.MacAddress;
22import org.onlab.packet.VlanId;
23import org.onosproject.net.Device;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.HostId;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020026import org.onosproject.net.HostLocation;
27import org.onosproject.net.host.DefaultHostDescription;
Thomas Vachuska5c6ed222016-06-29 11:02:22 +020028
29import java.util.Map;
Thomas Vachuskab1906d22018-02-14 16:50:16 -080030import java.util.Set;
Thomas Vachuska5c6ed222016-06-29 11:02:22 +020031
32import static org.onlab.util.Tools.toHex;
33import static org.onosproject.provider.nil.NullProviders.SCHEME;
34
35/**
36 * Custom topology defined by a concise language.
37 */
38public class CustomTopologySimulator extends TopologySimulator {
39
40 private int nextDeviceId = 0;
41 private int nextHostId = 0;
42
43 private Map<String, DeviceId> nameToId = Maps.newConcurrentMap();
44
45 /**
46 * Returns the next device id.
47 *
48 * @return the next device id
49 */
50 public DeviceId nextDeviceId() {
51 return DeviceId.deviceId(SCHEME + ":" + toHex(++nextDeviceId));
52 }
53
54 /**
55 * Returns the next host id.
56 *
57 * @return the next host id
58 */
59 public HostId nextHostId() {
Thomas Vachuska83da9b72016-07-02 12:52:23 +020060 return HostId.hostId(MacAddress.valueOf(++nextHostId), VlanId.NONE);
Thomas Vachuska5c6ed222016-06-29 11:02:22 +020061 }
62
63 /**
64 * Returns the identifier of the device with the specified alias.
65 *
66 * @param name device name
67 * @return device identifier
68 */
69 public DeviceId deviceId(String name) {
70 return nameToId.get(name);
71 }
72
73 /**
74 * Creates simulated device.
75 *
76 * @param id device identifier
77 * @param name device name
78 * @param type device type
79 * @param portCount number of device ports
80 */
81 public void createDevice(DeviceId id, String name, Device.Type type, int portCount) {
Andreas Pantelopoulosf28c0562016-11-12 14:12:05 +010082 int chassisId = Integer.parseInt(id.uri().getSchemeSpecificPart(), 16);
Thomas Vachuska5c6ed222016-06-29 11:02:22 +020083 createDevice(id, chassisId, type, portCount);
84 nameToId.put(name, id);
85 }
86
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020087 /**
88 * Creates a simulated host.
89 *
90 * @param hostId host identifier
91 * @param location host location
92 * @param hostIp host IP address
93 */
94 public void createHost(HostId hostId, HostLocation location, IpAddress hostIp) {
95 DefaultHostDescription description =
96 new DefaultHostDescription(hostId.mac(), hostId.vlanId(), location, hostIp);
97 hostProviderService.hostDetected(hostId, description, false);
98 }
99
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800100 /**
101 * Creates a simulated multi-homed host.
102 *
103 * @param hostId host identifier
104 * @param locations host locations
105 * @param hostIps host IP addresses
106 */
107 public void createHost(HostId hostId, Set<HostLocation> locations, Set<IpAddress> hostIps) {
108 DefaultHostDescription description =
109 new DefaultHostDescription(hostId.mac(), hostId.vlanId(), locations, hostIps, false);
110 hostProviderService.hostDetected(hostId, description, false);
111 }
112
Thomas Vachuska5c6ed222016-06-29 11:02:22 +0200113 @Override
114 protected void createDevices() {
115 }
116
117 @Override
118 protected void createLinks() {
119 }
120
121 @Override
122 protected void createHosts() {
123 }
Simon Huntb3567282017-04-07 22:59:28 -0700124
125 /**
126 * Resets the device and host ID seeds to the default values. That is, the
127 * next assigned values will start from 1 again.
128 */
129 public void resetIdSeeds() {
130 nextDeviceId = 0;
131 nextHostId = 0;
132 }
Thomas Vachuska5c6ed222016-06-29 11:02:22 +0200133}