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) { |
| 34 | return hostId("nic:" + id); |
| 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 | |
| 50 | // Short-hand for creating a link. |
| 51 | public static Link link(String src, int sp, String dst, int dp) { |
| 52 | return new DefaultLink(PID, new ConnectPoint(did(src), portNumber(sp)), |
| 53 | new ConnectPoint(did(dst), portNumber(dp)), |
| 54 | Link.Type.DIRECT); |
| 55 | } |
| 56 | |
| 57 | // Creates a path that leads through the given devices. |
| 58 | public static Path createPath(String... ids) { |
| 59 | List<Link> links = new ArrayList<>(); |
| 60 | for (int i = 0; i < ids.length - 1; i++) { |
| 61 | links.add(link(ids[i], i, ids[i + 1], i)); |
| 62 | } |
| 63 | return new DefaultPath(PID, links, ids.length); |
| 64 | } |
| 65 | |
| 66 | } |