Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net; |
| 17 | |
| 18 | import org.onlab.onos.net.provider.ProviderId; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 19 | import org.onlab.packet.ChassisId; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 21 | |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.HashSet; |
| 24 | import java.util.List; |
| 25 | |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 26 | import static org.junit.Assert.assertEquals; |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 27 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 28 | import static org.onlab.onos.net.HostId.hostId; |
| 29 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 30 | import static org.onlab.packet.MacAddress.valueOf; |
| 31 | import static org.onlab.packet.VlanId.vlanId; |
| 32 | |
| 33 | /** |
| 34 | * Miscellaneous tools for testing core related to the network model. |
| 35 | */ |
| 36 | public 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) { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 51 | return hostId(id); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 52 | } |
| 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, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 57 | "mfg", "1.0", "1.1", "1234", new ChassisId()); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 58 | } |
| 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 Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 64 | new HashSet<IpAddress>()); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 67 | // 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 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 72 | // Short-hand for creating a link. |
| 73 | public static Link link(String src, int sp, String dst, int dp) { |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 74 | return new DefaultLink(PID, |
| 75 | connectPoint(src, sp), |
| 76 | connectPoint(dst, dp), |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 77 | 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 HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 89 | |
| 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 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 108 | } |