blob: 8ed469ec655bf633f4bfd747473b037c38b8fddf [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 /**
Thomas Vachuska7b1fadc2018-09-27 11:20:41 -070088 * Creates simulated device.
89 *
90 * @param id device identifier
91 * @param name device name
92 * @param type device type
93 * @param hw hardware revision
94 * @param sw software revision
95 * @param portCount number of device ports
96 */
97 public void createDevice(DeviceId id, String name, Device.Type type,
98 String hw, String sw, int portCount) {
99 int chassisId = Integer.parseInt(id.uri().getSchemeSpecificPart(), 16);
100 createDevice(id, chassisId, type, hw, sw, portCount);
101 nameToId.put(name, id);
102 }
103
104 /**
Thomas Vachuska4407a7e2016-07-02 12:38:01 +0200105 * Creates a simulated host.
106 *
107 * @param hostId host identifier
108 * @param location host location
109 * @param hostIp host IP address
110 */
111 public void createHost(HostId hostId, HostLocation location, IpAddress hostIp) {
112 DefaultHostDescription description =
113 new DefaultHostDescription(hostId.mac(), hostId.vlanId(), location, hostIp);
114 hostProviderService.hostDetected(hostId, description, false);
115 }
116
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800117 /**
118 * Creates a simulated multi-homed host.
119 *
120 * @param hostId host identifier
121 * @param locations host locations
122 * @param hostIps host IP addresses
123 */
124 public void createHost(HostId hostId, Set<HostLocation> locations, Set<IpAddress> hostIps) {
125 DefaultHostDescription description =
126 new DefaultHostDescription(hostId.mac(), hostId.vlanId(), locations, hostIps, false);
127 hostProviderService.hostDetected(hostId, description, false);
128 }
129
Thomas Vachuska5c6ed222016-06-29 11:02:22 +0200130 @Override
131 protected void createDevices() {
132 }
133
134 @Override
135 protected void createLinks() {
136 }
137
138 @Override
139 protected void createHosts() {
140 }
Simon Huntb3567282017-04-07 22:59:28 -0700141
Thomas Vachuskacab29d22018-02-21 15:47:29 -0800142 @Override
143 public void tearDownTopology() {
144 super.tearDownTopology();
Simon Huntb3567282017-04-07 22:59:28 -0700145 nextDeviceId = 0;
146 nextHostId = 0;
Thomas Vachuskacab29d22018-02-21 15:47:29 -0800147 nameToId.clear();
Simon Huntb3567282017-04-07 22:59:28 -0700148 }
Thomas Vachuskacab29d22018-02-21 15:47:29 -0800149
Thomas Vachuska5c6ed222016-06-29 11:02:22 +0200150}