tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net; |
| 2 | |
| 3 | import org.onlab.onos.net.provider.ProviderId; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 4 | import org.onlab.packet.ChassisId; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame^] | 5 | import org.onlab.packet.IpAddress; |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 6 | |
| 7 | import java.util.ArrayList; |
| 8 | import java.util.HashSet; |
| 9 | import java.util.List; |
| 10 | |
| 11 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 12 | import static org.onlab.onos.net.HostId.hostId; |
| 13 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 14 | import static org.onlab.packet.MacAddress.valueOf; |
| 15 | import static org.onlab.packet.VlanId.vlanId; |
| 16 | |
| 17 | /** |
| 18 | * Miscellaneous tools for testing core related to the network model. |
| 19 | */ |
| 20 | public final class NetTestTools { |
| 21 | |
| 22 | private NetTestTools() { |
| 23 | } |
| 24 | |
| 25 | public static final ProviderId PID = new ProviderId("of", "foo"); |
| 26 | |
| 27 | // Short-hand for producing a device id from a string |
| 28 | public static DeviceId did(String id) { |
| 29 | return deviceId("of:" + id); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | // Short-hand for producing a host id from a string |
| 34 | public static HostId hid(String id) { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 35 | return hostId(id); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | // Crates a new device with the specified id |
| 39 | public static Device device(String id) { |
| 40 | return new DefaultDevice(PID, did(id), Device.Type.SWITCH, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 41 | "mfg", "1.0", "1.1", "1234", new ChassisId()); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // Crates a new host with the specified id |
| 45 | public static Host host(String id, String did) { |
| 46 | return new DefaultHost(PID, hid(id), valueOf(1234), vlanId((short) 2), |
| 47 | new HostLocation(did(did), portNumber(1), 321), |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame^] | 48 | new HashSet<IpAddress>()); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 51 | // Short-hand for creating a connection point. |
| 52 | public static ConnectPoint connectPoint(String id, int port) { |
| 53 | return new ConnectPoint(did(id), portNumber(port)); |
| 54 | } |
| 55 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 56 | // Short-hand for creating a link. |
| 57 | public static Link link(String src, int sp, String dst, int dp) { |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 58 | return new DefaultLink(PID, |
| 59 | connectPoint(src, sp), |
| 60 | connectPoint(dst, dp), |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 61 | Link.Type.DIRECT); |
| 62 | } |
| 63 | |
| 64 | // Creates a path that leads through the given devices. |
| 65 | public static Path createPath(String... ids) { |
| 66 | List<Link> links = new ArrayList<>(); |
| 67 | for (int i = 0; i < ids.length - 1; i++) { |
| 68 | links.add(link(ids[i], i, ids[i + 1], i)); |
| 69 | } |
| 70 | return new DefaultPath(PID, links, ids.length); |
| 71 | } |
| 72 | |
| 73 | } |