Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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 | |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Ray Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 19 | import org.onlab.graph.ScalarWeight; |
Ray Milkey | 5b94aea | 2015-08-05 11:00:55 -0700 | [diff] [blame] | 20 | import org.onlab.junit.TestUtils; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 21 | import org.onlab.packet.ChassisId; |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 22 | import org.onlab.packet.IpPrefix; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.MplsLabel; |
| 25 | import org.onlab.packet.VlanId; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 26 | import org.onosproject.TestApplicationId; |
Madan Jampani | 6f8b702 | 2015-12-07 16:59:59 -0800 | [diff] [blame] | 27 | import org.onosproject.cluster.NodeId; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.event.EventDeliveryService; |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 30 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 31 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 32 | import org.onosproject.net.flow.TrafficSelector; |
| 33 | import org.onosproject.net.flow.TrafficTreatment; |
| 34 | import org.onosproject.net.intent.Constraint; |
| 35 | import org.onosproject.net.intent.constraint.EncapsulationConstraint; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 36 | import org.onosproject.net.provider.ProviderId; |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 37 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 38 | import java.lang.reflect.Field; |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 39 | import java.util.ArrayList; |
| 40 | import java.util.HashSet; |
| 41 | import java.util.List; |
| 42 | |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 43 | import static org.junit.Assert.assertEquals; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 44 | import static org.onlab.packet.MacAddress.valueOf; |
| 45 | import static org.onlab.packet.VlanId.vlanId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import static org.onosproject.net.DeviceId.deviceId; |
| 47 | import static org.onosproject.net.HostId.hostId; |
| 48 | import static org.onosproject.net.PortNumber.portNumber; |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Miscellaneous tools for testing core related to the network model. |
| 52 | */ |
| 53 | public final class NetTestTools { |
| 54 | |
| 55 | private NetTestTools() { |
| 56 | } |
| 57 | |
| 58 | public static final ProviderId PID = new ProviderId("of", "foo"); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 59 | public static final ApplicationId APP_ID = new TestApplicationId("foo"); |
Daniele Moro | 641b4f1 | 2021-07-15 17:02:59 +0200 | [diff] [blame^] | 60 | public static final ApplicationId APP_ID_2 = new TestApplicationId("foo2"); |
Madan Jampani | 6f8b702 | 2015-12-07 16:59:59 -0800 | [diff] [blame] | 61 | public static final NodeId NODE_ID = new NodeId("node1"); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 62 | |
| 63 | // Short-hand for producing a device id from a string |
| 64 | public static DeviceId did(String id) { |
| 65 | return deviceId("of:" + id); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | // Short-hand for producing a host id from a string |
| 70 | public static HostId hid(String id) { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 71 | return hostId(id); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Crates a new device with the specified id |
| 75 | public static Device device(String id) { |
| 76 | return new DefaultDevice(PID, did(id), Device.Type.SWITCH, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 77 | "mfg", "1.0", "1.1", "1234", new ChassisId()); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // Crates a new host with the specified id |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 81 | public static Host host(String id, String did, long port) { |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 82 | return new DefaultHost(PID, hid(id), valueOf(1234), vlanId((short) 2), |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 83 | new HostLocation(did(did), portNumber(port), 321), |
Sho SHIMIZU | d88db6f | 2015-09-09 14:22:06 -0700 | [diff] [blame] | 84 | new HashSet<>()); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 87 | // Crates a new host with the specified id |
| 88 | public static Host host(String id, String did) { |
| 89 | return host(id, did, 1); |
| 90 | } |
| 91 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 92 | // Short-hand for creating a connection point. |
| 93 | public static ConnectPoint connectPoint(String id, int port) { |
| 94 | return new ConnectPoint(did(id), portNumber(port)); |
| 95 | } |
| 96 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 97 | // Short-hand for creating a connection point. |
| 98 | public static ConnectPoint connectPointNoOF(String id, int port) { |
| 99 | return new ConnectPoint(DeviceId.deviceId(id), portNumber(port)); |
| 100 | } |
| 101 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 102 | // Short-hand for creating a link. |
| 103 | public static Link link(String src, int sp, String dst, int dp) { |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 104 | return new DefaultLink(PID, |
| 105 | connectPoint(src, sp), |
| 106 | connectPoint(dst, dp), |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 107 | Link.Type.DIRECT, Link.State.ACTIVE); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 110 | // Short-hand for creating a link. |
| 111 | public static Link linkNoPrefixes(String src, int sp, String dst, int dp) { |
| 112 | return new DefaultLink(PID, |
| 113 | connectPointNoOF(src, sp), |
| 114 | connectPointNoOF(dst, dp), |
| 115 | Link.Type.DIRECT, Link.State.ACTIVE); |
| 116 | } |
| 117 | |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 118 | /** |
| 119 | * Short-hand for creating a link. |
| 120 | * |
| 121 | * @param src the src of the link |
| 122 | * @param dst the dst of the link |
| 123 | * @return a link |
| 124 | */ |
| 125 | public static Link link(ConnectPoint src, ConnectPoint dst) { |
| 126 | return new DefaultLink(PID, src, dst, Link.Type.DIRECT, Link.State.ACTIVE); |
| 127 | } |
| 128 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 129 | // Creates a path that leads through the given devices. |
| 130 | public static Path createPath(String... ids) { |
| 131 | List<Link> links = new ArrayList<>(); |
| 132 | for (int i = 0; i < ids.length - 1; i++) { |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 133 | links.add(link(ids[i], 2, ids[i + 1], 1)); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 134 | } |
Ray Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 135 | return new DefaultPath(PID, links, ScalarWeight.toWeight(ids.length)); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 138 | // Creates a path that leads through the given hosts. |
Thomas Vachuska | dc91b55 | 2016-03-29 14:02:47 -0700 | [diff] [blame] | 139 | public static Path createPath(boolean srcIsEdge, boolean dstIsEdge, String... ids) { |
| 140 | List<Link> links = new ArrayList<>(); |
| 141 | for (int i = 0; i < ids.length - 1; i++) { |
| 142 | if (i == 0 && srcIsEdge) { |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 143 | links.add(DefaultEdgeLink.createEdgeLink(host(ids[i], ids[i + 1], 1), true)); |
Thomas Vachuska | dc91b55 | 2016-03-29 14:02:47 -0700 | [diff] [blame] | 144 | } else if (i == ids.length - 2 && dstIsEdge) { |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 145 | links.add(DefaultEdgeLink.createEdgeLink(host(ids[i + 1], ids[i], 2), false)); |
Thomas Vachuska | dc91b55 | 2016-03-29 14:02:47 -0700 | [diff] [blame] | 146 | } else { |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 147 | links.add(link(ids[i], 2, ids[i + 1], 1)); |
Thomas Vachuska | dc91b55 | 2016-03-29 14:02:47 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
Ray Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 150 | return new DefaultPath(PID, links, ScalarWeight.toWeight(ids.length)); |
Thomas Vachuska | dc91b55 | 2016-03-29 14:02:47 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 153 | // Creates OCh signal |
| 154 | public static OchSignal createLambda() { |
| 155 | return new OchSignal(GridType.DWDM, ChannelSpacing.CHL_6P25GHZ, 8, 4); |
| 156 | } |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 157 | |
| 158 | /** |
| 159 | * Verifies that Annotations created by merging {@code annotations} is |
| 160 | * equal to actual Annotations. |
| 161 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 162 | * @param actual annotations to check |
| 163 | * @param annotations expected annotations |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 164 | */ |
| 165 | public static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) { |
| 166 | DefaultAnnotations expected = DefaultAnnotations.builder().build(); |
| 167 | for (SparseAnnotations a : annotations) { |
| 168 | expected = DefaultAnnotations.merge(expected, a); |
| 169 | } |
| 170 | assertEquals(expected.keys(), actual.keys()); |
| 171 | for (String key : expected.keys()) { |
| 172 | assertEquals(expected.value(key), actual.value(key)); |
| 173 | } |
| 174 | } |
| 175 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 176 | /** |
| 177 | * Injects the given event delivery service into the specified manager |
| 178 | * component. |
| 179 | * |
| 180 | * @param manager manager component |
| 181 | * @param svc service reference to be injected |
| 182 | */ |
| 183 | public static void injectEventDispatcher(Object manager, EventDeliveryService svc) { |
| 184 | Class mc = manager.getClass(); |
| 185 | for (Field f : mc.getSuperclass().getDeclaredFields()) { |
| 186 | if (f.getType().equals(EventDeliveryService.class)) { |
| 187 | try { |
Ray Milkey | 5b94aea | 2015-08-05 11:00:55 -0700 | [diff] [blame] | 188 | TestUtils.setField(manager, f.getName(), svc); |
| 189 | } catch (TestUtils.TestUtilsException e) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 190 | throw new IllegalArgumentException("Unable to inject reference", e); |
| 191 | } |
Ray Milkey | 5b94aea | 2015-08-05 11:00:55 -0700 | [diff] [blame] | 192 | break; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 197 | /** |
| 198 | * Builds an empty selector. |
| 199 | * |
| 200 | * @return the selector |
| 201 | */ |
| 202 | public static TrafficSelector emptySelector() { |
| 203 | return DefaultTrafficSelector.emptySelector(); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Builds a vlan selector. |
| 208 | * |
| 209 | * @return the selector |
| 210 | */ |
| 211 | public static TrafficSelector vlanSelector(String vlanId) { |
| 212 | return DefaultTrafficSelector.builder() |
| 213 | .matchVlanId(VlanId.vlanId(vlanId)) |
| 214 | .build(); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Builds a mpls selector. |
| 219 | * |
| 220 | * @return the selector |
| 221 | */ |
| 222 | public static TrafficSelector mplsSelector(String mplsLabel) { |
| 223 | return DefaultTrafficSelector.builder() |
| 224 | .matchMplsLabel(MplsLabel.mplsLabel(mplsLabel)) |
| 225 | .build(); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Builds an ip prefix dst selector. |
| 230 | * |
| 231 | * @return the selector |
| 232 | */ |
| 233 | public static TrafficSelector ipPrefixDstSelector(String prefix) { |
| 234 | return DefaultTrafficSelector.builder() |
| 235 | .matchIPDst(IpPrefix.valueOf(prefix)) |
| 236 | .build(); |
| 237 | } |
| 238 | |
Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 239 | public static TrafficSelector ethDstSelector(String macAddress) { |
| 240 | return DefaultTrafficSelector.builder() |
| 241 | .matchEthDst(MacAddress.valueOf(macAddress)) |
| 242 | .build(); |
| 243 | } |
| 244 | |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 245 | /** |
| 246 | * Builds an empty treatment. |
| 247 | * |
| 248 | * @return the treatment |
| 249 | */ |
| 250 | public static TrafficTreatment emptyTreatment() { |
| 251 | return DefaultTrafficTreatment.emptyTreatment(); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Builds a mac dst treatment. |
| 256 | * |
| 257 | * @return the treatment |
| 258 | */ |
| 259 | public static TrafficTreatment macDstTreatment(String mac) { |
| 260 | return DefaultTrafficTreatment.builder() |
| 261 | .setEthDst(MacAddress.valueOf(mac)) |
| 262 | .build(); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Builds a list containing a vlan encapsulation constraint. |
| 267 | * |
| 268 | * @return the list of constraints |
| 269 | */ |
| 270 | public static List<Constraint> vlanConstraint() { |
| 271 | return ImmutableList.of( |
| 272 | new EncapsulationConstraint(EncapsulationType.VLAN) |
| 273 | ); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Builds a list containing a mpls encapsulation constraint. |
| 278 | * |
| 279 | * @return the list of constraints |
| 280 | */ |
| 281 | public static List<Constraint> mplsConstraint() { |
| 282 | return ImmutableList.of( |
| 283 | new EncapsulationConstraint(EncapsulationType.MPLS) |
| 284 | ); |
| 285 | } |
| 286 | |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 287 | /** |
| 288 | * Builds a treatment which contains the dec ttl |
| 289 | * actions. |
| 290 | * |
| 291 | * @return the treatment |
| 292 | */ |
| 293 | public static TrafficTreatment decTtlTreatment() { |
| 294 | return DefaultTrafficTreatment.builder() |
| 295 | .decMplsTtl() |
| 296 | .decNwTtl() |
| 297 | .build(); |
| 298 | } |
| 299 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 300 | } |