blob: 8d2a61bddf08ef24d3c0687bec688019410371e2 [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
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080026import static org.junit.Assert.assertEquals;
tomca90c462014-09-22 11:40:58 -070027import static org.onlab.onos.net.DeviceId.deviceId;
28import static org.onlab.onos.net.HostId.hostId;
29import static org.onlab.onos.net.PortNumber.portNumber;
30import static org.onlab.packet.MacAddress.valueOf;
31import static org.onlab.packet.VlanId.vlanId;
32
33/**
34 * Miscellaneous tools for testing core related to the network model.
35 */
36public final class NetTestTools {
37
38 private NetTestTools() {
39 }
40
41 public static final ProviderId PID = new ProviderId("of", "foo");
42
43 // Short-hand for producing a device id from a string
44 public static DeviceId did(String id) {
45 return deviceId("of:" + id);
46 }
47
48
49 // Short-hand for producing a host id from a string
50 public static HostId hid(String id) {
tom545708e2014-10-09 17:10:02 -070051 return hostId(id);
tomca90c462014-09-22 11:40:58 -070052 }
53
54 // Crates a new device with the specified id
55 public static Device device(String id) {
56 return new DefaultDevice(PID, did(id), Device.Type.SWITCH,
alshabib7911a052014-10-16 17:49:37 -070057 "mfg", "1.0", "1.1", "1234", new ChassisId());
tomca90c462014-09-22 11:40:58 -070058 }
59
60 // Crates a new host with the specified id
61 public static Host host(String id, String did) {
62 return new DefaultHost(PID, hid(id), valueOf(1234), vlanId((short) 2),
63 new HostLocation(did(did), portNumber(1), 321),
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070064 new HashSet<IpAddress>());
tomca90c462014-09-22 11:40:58 -070065 }
66
Ray Milkeye6684082014-10-16 16:59:47 -070067 // Short-hand for creating a connection point.
68 public static ConnectPoint connectPoint(String id, int port) {
69 return new ConnectPoint(did(id), portNumber(port));
70 }
71
tomca90c462014-09-22 11:40:58 -070072 // Short-hand for creating a link.
73 public static Link link(String src, int sp, String dst, int dp) {
Ray Milkeye6684082014-10-16 16:59:47 -070074 return new DefaultLink(PID,
75 connectPoint(src, sp),
76 connectPoint(dst, dp),
tomca90c462014-09-22 11:40:58 -070077 Link.Type.DIRECT);
78 }
79
80 // Creates a path that leads through the given devices.
81 public static Path createPath(String... ids) {
82 List<Link> links = new ArrayList<>();
83 for (int i = 0; i < ids.length - 1; i++) {
84 links.add(link(ids[i], i, ids[i + 1], i));
85 }
86 return new DefaultPath(PID, links, ids.length);
87 }
88
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080089
90 /**
91 * Verifies that Annotations created by merging {@code annotations} is
92 * equal to actual Annotations.
93 *
94 * @param actual Annotations to check
95 * @param annotations
96 */
97 public static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
98 DefaultAnnotations expected = DefaultAnnotations.builder().build();
99 for (SparseAnnotations a : annotations) {
100 expected = DefaultAnnotations.merge(expected, a);
101 }
102 assertEquals(expected.keys(), actual.keys());
103 for (String key : expected.keys()) {
104 assertEquals(expected.value(key), actual.value(key));
105 }
106 }
107
tomca90c462014-09-22 11:40:58 -0700108}