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