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 | |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 18 | import org.onlab.onos.TestApplicationId; |
| 19 | import org.onlab.onos.core.ApplicationId; |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.provider.ProviderId; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 21 | import org.onlab.packet.ChassisId; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 23 | |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.HashSet; |
| 26 | import java.util.List; |
| 27 | |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 28 | import static org.junit.Assert.assertEquals; |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 29 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 30 | import static org.onlab.onos.net.HostId.hostId; |
| 31 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 32 | import static org.onlab.packet.MacAddress.valueOf; |
| 33 | import static org.onlab.packet.VlanId.vlanId; |
| 34 | |
| 35 | /** |
| 36 | * Miscellaneous tools for testing core related to the network model. |
| 37 | */ |
| 38 | public final class NetTestTools { |
| 39 | |
| 40 | private NetTestTools() { |
| 41 | } |
| 42 | |
| 43 | public static final ProviderId PID = new ProviderId("of", "foo"); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 44 | public static final ApplicationId APP_ID = new TestApplicationId("foo"); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 45 | |
| 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) { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 54 | return hostId(id); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 55 | } |
| 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, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 60 | "mfg", "1.0", "1.1", "1234", new ChassisId()); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 61 | } |
| 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 Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 67 | new HashSet<IpAddress>()); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 70 | // 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 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 75 | // Short-hand for creating a link. |
| 76 | public static Link link(String src, int sp, String dst, int dp) { |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 77 | return new DefaultLink(PID, |
| 78 | connectPoint(src, sp), |
| 79 | connectPoint(dst, dp), |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 80 | 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 HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 92 | |
| 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 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 111 | } |