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