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