Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net; |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 17 | |
Ray Milkey | 5b94aea | 2015-08-05 11:00:55 -0700 | [diff] [blame] | 18 | import org.onlab.junit.TestUtils; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 19 | import org.onlab.packet.ChassisId; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 20 | import org.onosproject.TestApplicationId; |
| 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.event.EventDeliveryService; |
| 23 | import org.onosproject.net.provider.ProviderId; |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 24 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 25 | import java.lang.reflect.Field; |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 26 | import java.util.ArrayList; |
| 27 | import java.util.HashSet; |
| 28 | import java.util.List; |
| 29 | |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 30 | import static org.junit.Assert.assertEquals; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 31 | import static org.onlab.packet.MacAddress.valueOf; |
| 32 | import static org.onlab.packet.VlanId.vlanId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import static org.onosproject.net.DeviceId.deviceId; |
| 34 | import static org.onosproject.net.HostId.hostId; |
| 35 | import static org.onosproject.net.PortNumber.portNumber; |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Miscellaneous tools for testing core related to the network model. |
| 39 | */ |
| 40 | public final class NetTestTools { |
| 41 | |
| 42 | private NetTestTools() { |
| 43 | } |
| 44 | |
| 45 | public static final ProviderId PID = new ProviderId("of", "foo"); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 46 | public static final ApplicationId APP_ID = new TestApplicationId("foo"); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 47 | |
| 48 | // Short-hand for producing a device id from a string |
| 49 | public static DeviceId did(String id) { |
| 50 | return deviceId("of:" + id); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | // Short-hand for producing a host id from a string |
| 55 | public static HostId hid(String id) { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 56 | return hostId(id); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // Crates a new device with the specified id |
| 60 | public static Device device(String id) { |
| 61 | return new DefaultDevice(PID, did(id), Device.Type.SWITCH, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 62 | "mfg", "1.0", "1.1", "1234", new ChassisId()); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Crates a new host with the specified id |
| 66 | public static Host host(String id, String did) { |
| 67 | return new DefaultHost(PID, hid(id), valueOf(1234), vlanId((short) 2), |
| 68 | new HostLocation(did(did), portNumber(1), 321), |
Sho SHIMIZU | d88db6f | 2015-09-09 14:22:06 -0700 | [diff] [blame] | 69 | new HashSet<>()); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 72 | // Short-hand for creating a connection point. |
| 73 | public static ConnectPoint connectPoint(String id, int port) { |
| 74 | return new ConnectPoint(did(id), portNumber(port)); |
| 75 | } |
| 76 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 77 | // Short-hand for creating a link. |
| 78 | public static Link link(String src, int sp, String dst, int dp) { |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 79 | return new DefaultLink(PID, |
| 80 | connectPoint(src, sp), |
| 81 | connectPoint(dst, dp), |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 82 | Link.Type.DIRECT); |
| 83 | } |
| 84 | |
| 85 | // Creates a path that leads through the given devices. |
| 86 | public static Path createPath(String... ids) { |
| 87 | List<Link> links = new ArrayList<>(); |
| 88 | for (int i = 0; i < ids.length - 1; i++) { |
| 89 | links.add(link(ids[i], i, ids[i + 1], i)); |
| 90 | } |
| 91 | return new DefaultPath(PID, links, ids.length); |
| 92 | } |
| 93 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 94 | // Creates OCh signal |
| 95 | public static OchSignal createLambda() { |
| 96 | return new OchSignal(GridType.DWDM, ChannelSpacing.CHL_6P25GHZ, 8, 4); |
| 97 | } |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * Verifies that Annotations created by merging {@code annotations} is |
| 101 | * equal to actual Annotations. |
| 102 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 103 | * @param actual annotations to check |
| 104 | * @param annotations expected annotations |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 105 | */ |
| 106 | public static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) { |
| 107 | DefaultAnnotations expected = DefaultAnnotations.builder().build(); |
| 108 | for (SparseAnnotations a : annotations) { |
| 109 | expected = DefaultAnnotations.merge(expected, a); |
| 110 | } |
| 111 | assertEquals(expected.keys(), actual.keys()); |
| 112 | for (String key : expected.keys()) { |
| 113 | assertEquals(expected.value(key), actual.value(key)); |
| 114 | } |
| 115 | } |
| 116 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 117 | /** |
| 118 | * Injects the given event delivery service into the specified manager |
| 119 | * component. |
| 120 | * |
| 121 | * @param manager manager component |
| 122 | * @param svc service reference to be injected |
| 123 | */ |
| 124 | public static void injectEventDispatcher(Object manager, EventDeliveryService svc) { |
| 125 | Class mc = manager.getClass(); |
| 126 | for (Field f : mc.getSuperclass().getDeclaredFields()) { |
| 127 | if (f.getType().equals(EventDeliveryService.class)) { |
| 128 | try { |
Ray Milkey | 5b94aea | 2015-08-05 11:00:55 -0700 | [diff] [blame] | 129 | TestUtils.setField(manager, f.getName(), svc); |
| 130 | } catch (TestUtils.TestUtilsException e) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 131 | throw new IllegalArgumentException("Unable to inject reference", e); |
| 132 | } |
Ray Milkey | 5b94aea | 2015-08-05 11:00:55 -0700 | [diff] [blame] | 133 | break; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 138 | } |