blob: dcc3e1502bcc6549f59b84efd84b75936a97eb38 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net;
tomca90c462014-09-22 11:40:58 -070017
Pier Ventre766995d2016-10-05 22:15:56 -070018import com.google.common.collect.ImmutableList;
Ray Milkeya7cf8c82018-02-08 15:07:06 -080019import org.onlab.graph.ScalarWeight;
Ray Milkey5b94aea2015-08-05 11:00:55 -070020import org.onlab.junit.TestUtils;
alshabib7911a052014-10-16 17:49:37 -070021import org.onlab.packet.ChassisId;
Pier Ventre766995d2016-10-05 22:15:56 -070022import org.onlab.packet.IpPrefix;
23import org.onlab.packet.MacAddress;
24import org.onlab.packet.MplsLabel;
25import org.onlab.packet.VlanId;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070026import org.onosproject.TestApplicationId;
Madan Jampani6f8b7022015-12-07 16:59:59 -080027import org.onosproject.cluster.NodeId;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070028import org.onosproject.core.ApplicationId;
29import org.onosproject.event.EventDeliveryService;
Pier Ventre766995d2016-10-05 22:15:56 -070030import org.onosproject.net.flow.DefaultTrafficSelector;
31import org.onosproject.net.flow.DefaultTrafficTreatment;
32import org.onosproject.net.flow.TrafficSelector;
33import org.onosproject.net.flow.TrafficTreatment;
34import org.onosproject.net.intent.Constraint;
35import org.onosproject.net.intent.constraint.EncapsulationConstraint;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070036import org.onosproject.net.provider.ProviderId;
tomca90c462014-09-22 11:40:58 -070037
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070038import java.lang.reflect.Field;
tomca90c462014-09-22 11:40:58 -070039import java.util.ArrayList;
40import java.util.HashSet;
41import java.util.List;
42
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080043import static org.junit.Assert.assertEquals;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070044import static org.onlab.packet.MacAddress.valueOf;
45import static org.onlab.packet.VlanId.vlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import static org.onosproject.net.DeviceId.deviceId;
47import static org.onosproject.net.HostId.hostId;
48import static org.onosproject.net.PortNumber.portNumber;
tomca90c462014-09-22 11:40:58 -070049
50/**
51 * Miscellaneous tools for testing core related to the network model.
52 */
53public final class NetTestTools {
54
55 private NetTestTools() {
56 }
57
58 public static final ProviderId PID = new ProviderId("of", "foo");
Ray Milkey8d3ce432014-11-07 16:21:10 -080059 public static final ApplicationId APP_ID = new TestApplicationId("foo");
Madan Jampani6f8b7022015-12-07 16:59:59 -080060 public static final NodeId NODE_ID = new NodeId("node1");
tomca90c462014-09-22 11:40:58 -070061
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) {
tom545708e2014-10-09 17:10:02 -070070 return hostId(id);
tomca90c462014-09-22 11:40:58 -070071 }
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,
alshabib7911a052014-10-16 17:49:37 -070076 "mfg", "1.0", "1.1", "1234", new ChassisId());
tomca90c462014-09-22 11:40:58 -070077 }
78
79 // Crates a new host with the specified id
Luca Pretede10c782017-01-05 17:23:08 -080080 public static Host host(String id, String did, long port) {
tomca90c462014-09-22 11:40:58 -070081 return new DefaultHost(PID, hid(id), valueOf(1234), vlanId((short) 2),
Luca Pretede10c782017-01-05 17:23:08 -080082 new HostLocation(did(did), portNumber(port), 321),
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -070083 new HashSet<>());
tomca90c462014-09-22 11:40:58 -070084 }
85
Luca Pretede10c782017-01-05 17:23:08 -080086 // 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 Milkeye6684082014-10-16 16:59:47 -070091 // 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 Pretede10c782017-01-05 17:23:08 -080096 // 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
tomca90c462014-09-22 11:40:58 -0700101 // Short-hand for creating a link.
102 public static Link link(String src, int sp, String dst, int dp) {
Ray Milkeye6684082014-10-16 16:59:47 -0700103 return new DefaultLink(PID,
104 connectPoint(src, sp),
105 connectPoint(dst, dp),
Brian Stanke612cebf2016-05-02 10:21:33 -0400106 Link.Type.DIRECT, Link.State.ACTIVE);
tomca90c462014-09-22 11:40:58 -0700107 }
108
Luca Pretede10c782017-01-05 17:23:08 -0800109 // 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 Ventre766995d2016-10-05 22:15:56 -0700117 /**
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
tomca90c462014-09-22 11:40:58 -0700128 // 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 Pretede10c782017-01-05 17:23:08 -0800132 links.add(link(ids[i], 2, ids[i + 1], 1));
tomca90c462014-09-22 11:40:58 -0700133 }
Ray Milkeya7cf8c82018-02-08 15:07:06 -0800134 return new DefaultPath(PID, links, ScalarWeight.toWeight(ids.length));
tomca90c462014-09-22 11:40:58 -0700135 }
136
Luca Pretede10c782017-01-05 17:23:08 -0800137 // Creates a path that leads through the given hosts.
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700138 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 Pretede10c782017-01-05 17:23:08 -0800142 links.add(DefaultEdgeLink.createEdgeLink(host(ids[i], ids[i + 1], 1), true));
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700143 } else if (i == ids.length - 2 && dstIsEdge) {
Luca Pretede10c782017-01-05 17:23:08 -0800144 links.add(DefaultEdgeLink.createEdgeLink(host(ids[i + 1], ids[i], 2), false));
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700145 } else {
Luca Pretede10c782017-01-05 17:23:08 -0800146 links.add(link(ids[i], 2, ids[i + 1], 1));
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700147 }
148 }
Ray Milkeya7cf8c82018-02-08 15:07:06 -0800149 return new DefaultPath(PID, links, ScalarWeight.toWeight(ids.length));
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700150 }
151
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700152 // Creates OCh signal
153 public static OchSignal createLambda() {
154 return new OchSignal(GridType.DWDM, ChannelSpacing.CHL_6P25GHZ, 8, 4);
155 }
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800156
157 /**
158 * Verifies that Annotations created by merging {@code annotations} is
159 * equal to actual Annotations.
160 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700161 * @param actual annotations to check
162 * @param annotations expected annotations
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800163 */
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 Vachuska42e8cce2015-07-29 19:25:18 -0700175 /**
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 Milkey5b94aea2015-08-05 11:00:55 -0700187 TestUtils.setField(manager, f.getName(), svc);
188 } catch (TestUtils.TestUtilsException e) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700189 throw new IllegalArgumentException("Unable to inject reference", e);
190 }
Ray Milkey5b94aea2015-08-05 11:00:55 -0700191 break;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700192 }
193 }
194 }
195
Pier Ventre766995d2016-10-05 22:15:56 -0700196 /**
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 Tsenga64f0c82017-02-03 11:17:15 -0800238 public static TrafficSelector ethDstSelector(String macAddress) {
239 return DefaultTrafficSelector.builder()
240 .matchEthDst(MacAddress.valueOf(macAddress))
241 .build();
242 }
243
Pier Ventre766995d2016-10-05 22:15:56 -0700244 /**
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 Ventre81c47bf2016-11-04 07:26:22 -0700286 /**
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
tomca90c462014-09-22 11:40:58 -0700299}