blob: 42f828f1821f26c9aa535db1cc736f7cbb4d7b02 [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");
Daniele Moro43ac2892021-07-15 17:02:59 +020060 public static final ApplicationId APP_ID_2 = new TestApplicationId("foo2");
Madan Jampani6f8b7022015-12-07 16:59:59 -080061 public static final NodeId NODE_ID = new NodeId("node1");
tomca90c462014-09-22 11:40:58 -070062
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) {
tom545708e2014-10-09 17:10:02 -070071 return hostId(id);
tomca90c462014-09-22 11:40:58 -070072 }
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,
alshabib7911a052014-10-16 17:49:37 -070077 "mfg", "1.0", "1.1", "1234", new ChassisId());
tomca90c462014-09-22 11:40:58 -070078 }
79
80 // Crates a new host with the specified id
Luca Pretede10c782017-01-05 17:23:08 -080081 public static Host host(String id, String did, long port) {
tomca90c462014-09-22 11:40:58 -070082 return new DefaultHost(PID, hid(id), valueOf(1234), vlanId((short) 2),
Luca Pretede10c782017-01-05 17:23:08 -080083 new HostLocation(did(did), portNumber(port), 321),
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -070084 new HashSet<>());
tomca90c462014-09-22 11:40:58 -070085 }
86
Luca Pretede10c782017-01-05 17:23:08 -080087 // 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 Milkeye6684082014-10-16 16:59:47 -070092 // 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 Pretede10c782017-01-05 17:23:08 -080097 // 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
tomca90c462014-09-22 11:40:58 -0700102 // Short-hand for creating a link.
103 public static Link link(String src, int sp, String dst, int dp) {
Ray Milkeye6684082014-10-16 16:59:47 -0700104 return new DefaultLink(PID,
105 connectPoint(src, sp),
106 connectPoint(dst, dp),
Brian Stanke612cebf2016-05-02 10:21:33 -0400107 Link.Type.DIRECT, Link.State.ACTIVE);
tomca90c462014-09-22 11:40:58 -0700108 }
109
Luca Pretede10c782017-01-05 17:23:08 -0800110 // 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 Ventre766995d2016-10-05 22:15:56 -0700118 /**
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
tomca90c462014-09-22 11:40:58 -0700129 // 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 Pretede10c782017-01-05 17:23:08 -0800133 links.add(link(ids[i], 2, ids[i + 1], 1));
tomca90c462014-09-22 11:40:58 -0700134 }
Ray Milkeya7cf8c82018-02-08 15:07:06 -0800135 return new DefaultPath(PID, links, ScalarWeight.toWeight(ids.length));
tomca90c462014-09-22 11:40:58 -0700136 }
137
Luca Pretede10c782017-01-05 17:23:08 -0800138 // Creates a path that leads through the given hosts.
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700139 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 Pretede10c782017-01-05 17:23:08 -0800143 links.add(DefaultEdgeLink.createEdgeLink(host(ids[i], ids[i + 1], 1), true));
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700144 } else if (i == ids.length - 2 && dstIsEdge) {
Luca Pretede10c782017-01-05 17:23:08 -0800145 links.add(DefaultEdgeLink.createEdgeLink(host(ids[i + 1], ids[i], 2), false));
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700146 } else {
Luca Pretede10c782017-01-05 17:23:08 -0800147 links.add(link(ids[i], 2, ids[i + 1], 1));
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700148 }
149 }
Ray Milkeya7cf8c82018-02-08 15:07:06 -0800150 return new DefaultPath(PID, links, ScalarWeight.toWeight(ids.length));
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700151 }
152
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700153 // Creates OCh signal
154 public static OchSignal createLambda() {
155 return new OchSignal(GridType.DWDM, ChannelSpacing.CHL_6P25GHZ, 8, 4);
156 }
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800157
158 /**
159 * Verifies that Annotations created by merging {@code annotations} is
160 * equal to actual Annotations.
161 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700162 * @param actual annotations to check
163 * @param annotations expected annotations
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800164 */
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 Vachuska42e8cce2015-07-29 19:25:18 -0700176 /**
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 Milkey5b94aea2015-08-05 11:00:55 -0700188 TestUtils.setField(manager, f.getName(), svc);
189 } catch (TestUtils.TestUtilsException e) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700190 throw new IllegalArgumentException("Unable to inject reference", e);
191 }
Ray Milkey5b94aea2015-08-05 11:00:55 -0700192 break;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700193 }
194 }
195 }
196
Pier Ventre766995d2016-10-05 22:15:56 -0700197 /**
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 Tsenga64f0c82017-02-03 11:17:15 -0800239 public static TrafficSelector ethDstSelector(String macAddress) {
240 return DefaultTrafficSelector.builder()
241 .matchEthDst(MacAddress.valueOf(macAddress))
242 .build();
243 }
244
Pier Ventre766995d2016-10-05 22:15:56 -0700245 /**
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 Ventre81c47bf2016-11-04 07:26:22 -0700287 /**
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
tomca90c462014-09-22 11:40:58 -0700300}