blob: 5f8920b806ea15e9205b652e03a2945c645047e0 [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
Ray Milkey8d3ce432014-11-07 16:21:10 -080018import org.onlab.onos.TestApplicationId;
19import org.onlab.onos.core.ApplicationId;
tomca90c462014-09-22 11:40:58 -070020import org.onlab.onos.net.provider.ProviderId;
alshabib7911a052014-10-16 17:49:37 -070021import org.onlab.packet.ChassisId;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070022import org.onlab.packet.IpAddress;
tomca90c462014-09-22 11:40:58 -070023
24import java.util.ArrayList;
25import java.util.HashSet;
26import java.util.List;
27
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080028import static org.junit.Assert.assertEquals;
tomca90c462014-09-22 11:40:58 -070029import static org.onlab.onos.net.DeviceId.deviceId;
30import static org.onlab.onos.net.HostId.hostId;
31import static org.onlab.onos.net.PortNumber.portNumber;
32import static org.onlab.packet.MacAddress.valueOf;
33import static org.onlab.packet.VlanId.vlanId;
34
35/**
36 * Miscellaneous tools for testing core related to the network model.
37 */
38public final class NetTestTools {
39
40 private NetTestTools() {
41 }
42
43 public static final ProviderId PID = new ProviderId("of", "foo");
Ray Milkey8d3ce432014-11-07 16:21:10 -080044 public static final ApplicationId APP_ID = new TestApplicationId("foo");
tomca90c462014-09-22 11:40:58 -070045
46 // Short-hand for producing a device id from a string
47 public static DeviceId did(String id) {
48 return deviceId("of:" + id);
49 }
50
51
52 // Short-hand for producing a host id from a string
53 public static HostId hid(String id) {
tom545708e2014-10-09 17:10:02 -070054 return hostId(id);
tomca90c462014-09-22 11:40:58 -070055 }
56
57 // Crates a new device with the specified id
58 public static Device device(String id) {
59 return new DefaultDevice(PID, did(id), Device.Type.SWITCH,
alshabib7911a052014-10-16 17:49:37 -070060 "mfg", "1.0", "1.1", "1234", new ChassisId());
tomca90c462014-09-22 11:40:58 -070061 }
62
63 // Crates a new host with the specified id
64 public static Host host(String id, String did) {
65 return new DefaultHost(PID, hid(id), valueOf(1234), vlanId((short) 2),
66 new HostLocation(did(did), portNumber(1), 321),
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070067 new HashSet<IpAddress>());
tomca90c462014-09-22 11:40:58 -070068 }
69
Ray Milkeye6684082014-10-16 16:59:47 -070070 // Short-hand for creating a connection point.
71 public static ConnectPoint connectPoint(String id, int port) {
72 return new ConnectPoint(did(id), portNumber(port));
73 }
74
tomca90c462014-09-22 11:40:58 -070075 // Short-hand for creating a link.
76 public static Link link(String src, int sp, String dst, int dp) {
Ray Milkeye6684082014-10-16 16:59:47 -070077 return new DefaultLink(PID,
78 connectPoint(src, sp),
79 connectPoint(dst, dp),
tomca90c462014-09-22 11:40:58 -070080 Link.Type.DIRECT);
81 }
82
83 // Creates a path that leads through the given devices.
84 public static Path createPath(String... ids) {
85 List<Link> links = new ArrayList<>();
86 for (int i = 0; i < ids.length - 1; i++) {
87 links.add(link(ids[i], i, ids[i + 1], i));
88 }
89 return new DefaultPath(PID, links, ids.length);
90 }
91
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080092
93 /**
94 * Verifies that Annotations created by merging {@code annotations} is
95 * equal to actual Annotations.
96 *
97 * @param actual Annotations to check
98 * @param annotations
99 */
100 public static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
101 DefaultAnnotations expected = DefaultAnnotations.builder().build();
102 for (SparseAnnotations a : annotations) {
103 expected = DefaultAnnotations.merge(expected, a);
104 }
105 assertEquals(expected.keys(), actual.keys());
106 for (String key : expected.keys()) {
107 assertEquals(expected.value(key), actual.value(key));
108 }
109 }
110
tomca90c462014-09-22 11:40:58 -0700111}