blob: 9bc04502022fb8d3d73eec4d3ec4ce1507e9bc16 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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 */
tomca90c462014-09-22 11:40:58 -070016package org.onlab.onos.net;
17
18import org.onlab.onos.net.provider.ProviderId;
alshabib7911a052014-10-16 17:49:37 -070019import org.onlab.packet.ChassisId;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070020import org.onlab.packet.IpAddress;
tomca90c462014-09-22 11:40:58 -070021
22import java.util.ArrayList;
23import java.util.HashSet;
24import java.util.List;
25
26import static org.onlab.onos.net.DeviceId.deviceId;
27import static org.onlab.onos.net.HostId.hostId;
28import static org.onlab.onos.net.PortNumber.portNumber;
29import static org.onlab.packet.MacAddress.valueOf;
30import static org.onlab.packet.VlanId.vlanId;
31
32/**
33 * Miscellaneous tools for testing core related to the network model.
34 */
35public final class NetTestTools {
36
37 private NetTestTools() {
38 }
39
40 public static final ProviderId PID = new ProviderId("of", "foo");
41
42 // Short-hand for producing a device id from a string
43 public static DeviceId did(String id) {
44 return deviceId("of:" + id);
45 }
46
47
48 // Short-hand for producing a host id from a string
49 public static HostId hid(String id) {
tom545708e2014-10-09 17:10:02 -070050 return hostId(id);
tomca90c462014-09-22 11:40:58 -070051 }
52
53 // Crates a new device with the specified id
54 public static Device device(String id) {
55 return new DefaultDevice(PID, did(id), Device.Type.SWITCH,
alshabib7911a052014-10-16 17:49:37 -070056 "mfg", "1.0", "1.1", "1234", new ChassisId());
tomca90c462014-09-22 11:40:58 -070057 }
58
59 // Crates a new host with the specified id
60 public static Host host(String id, String did) {
61 return new DefaultHost(PID, hid(id), valueOf(1234), vlanId((short) 2),
62 new HostLocation(did(did), portNumber(1), 321),
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070063 new HashSet<IpAddress>());
tomca90c462014-09-22 11:40:58 -070064 }
65
Ray Milkeye6684082014-10-16 16:59:47 -070066 // Short-hand for creating a connection point.
67 public static ConnectPoint connectPoint(String id, int port) {
68 return new ConnectPoint(did(id), portNumber(port));
69 }
70
tomca90c462014-09-22 11:40:58 -070071 // Short-hand for creating a link.
72 public static Link link(String src, int sp, String dst, int dp) {
Ray Milkeye6684082014-10-16 16:59:47 -070073 return new DefaultLink(PID,
74 connectPoint(src, sp),
75 connectPoint(dst, dp),
tomca90c462014-09-22 11:40:58 -070076 Link.Type.DIRECT);
77 }
78
79 // Creates a path that leads through the given devices.
80 public static Path createPath(String... ids) {
81 List<Link> links = new ArrayList<>();
82 for (int i = 0; i < ids.length - 1; i++) {
83 links.add(link(ids[i], i, ids[i + 1], i));
84 }
85 return new DefaultPath(PID, links, ids.length);
86 }
87
88}