blob: dd03a0988301e6f51ac563ebf155617c4367fd21 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
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 Milkey5b94aea2015-08-05 11:00:55 -070019import org.onlab.junit.TestUtils;
alshabib7911a052014-10-16 17:49:37 -070020import org.onlab.packet.ChassisId;
Pier Ventre766995d2016-10-05 22:15:56 -070021import org.onlab.packet.IpPrefix;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.MplsLabel;
24import org.onlab.packet.VlanId;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070025import org.onosproject.TestApplicationId;
Madan Jampani6f8b7022015-12-07 16:59:59 -080026import org.onosproject.cluster.NodeId;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070027import org.onosproject.core.ApplicationId;
28import org.onosproject.event.EventDeliveryService;
Pier Ventre766995d2016-10-05 22:15:56 -070029import org.onosproject.net.flow.DefaultTrafficSelector;
30import org.onosproject.net.flow.DefaultTrafficTreatment;
31import org.onosproject.net.flow.TrafficSelector;
32import org.onosproject.net.flow.TrafficTreatment;
33import org.onosproject.net.intent.Constraint;
34import org.onosproject.net.intent.constraint.EncapsulationConstraint;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070035import org.onosproject.net.provider.ProviderId;
tomca90c462014-09-22 11:40:58 -070036
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070037import java.lang.reflect.Field;
tomca90c462014-09-22 11:40:58 -070038import java.util.ArrayList;
39import java.util.HashSet;
40import java.util.List;
41
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080042import static org.junit.Assert.assertEquals;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070043import static org.onlab.packet.MacAddress.valueOf;
44import static org.onlab.packet.VlanId.vlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import static org.onosproject.net.DeviceId.deviceId;
46import static org.onosproject.net.HostId.hostId;
47import static org.onosproject.net.PortNumber.portNumber;
tomca90c462014-09-22 11:40:58 -070048
49/**
50 * Miscellaneous tools for testing core related to the network model.
51 */
52public final class NetTestTools {
53
54 private NetTestTools() {
55 }
56
57 public static final ProviderId PID = new ProviderId("of", "foo");
Ray Milkey8d3ce432014-11-07 16:21:10 -080058 public static final ApplicationId APP_ID = new TestApplicationId("foo");
Madan Jampani6f8b7022015-12-07 16:59:59 -080059 public static final NodeId NODE_ID = new NodeId("node1");
tomca90c462014-09-22 11:40:58 -070060
61 // Short-hand for producing a device id from a string
62 public static DeviceId did(String id) {
63 return deviceId("of:" + id);
64 }
65
66
67 // Short-hand for producing a host id from a string
68 public static HostId hid(String id) {
tom545708e2014-10-09 17:10:02 -070069 return hostId(id);
tomca90c462014-09-22 11:40:58 -070070 }
71
72 // Crates a new device with the specified id
73 public static Device device(String id) {
74 return new DefaultDevice(PID, did(id), Device.Type.SWITCH,
alshabib7911a052014-10-16 17:49:37 -070075 "mfg", "1.0", "1.1", "1234", new ChassisId());
tomca90c462014-09-22 11:40:58 -070076 }
77
78 // Crates a new host with the specified id
Luca Pretede10c782017-01-05 17:23:08 -080079 public static Host host(String id, String did, long port) {
tomca90c462014-09-22 11:40:58 -070080 return new DefaultHost(PID, hid(id), valueOf(1234), vlanId((short) 2),
Luca Pretede10c782017-01-05 17:23:08 -080081 new HostLocation(did(did), portNumber(port), 321),
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -070082 new HashSet<>());
tomca90c462014-09-22 11:40:58 -070083 }
84
Luca Pretede10c782017-01-05 17:23:08 -080085 // Crates a new host with the specified id
86 public static Host host(String id, String did) {
87 return host(id, did, 1);
88 }
89
Ray Milkeye6684082014-10-16 16:59:47 -070090 // Short-hand for creating a connection point.
91 public static ConnectPoint connectPoint(String id, int port) {
92 return new ConnectPoint(did(id), portNumber(port));
93 }
94
Luca Pretede10c782017-01-05 17:23:08 -080095 // Short-hand for creating a connection point.
96 public static ConnectPoint connectPointNoOF(String id, int port) {
97 return new ConnectPoint(DeviceId.deviceId(id), portNumber(port));
98 }
99
tomca90c462014-09-22 11:40:58 -0700100 // Short-hand for creating a link.
101 public static Link link(String src, int sp, String dst, int dp) {
Ray Milkeye6684082014-10-16 16:59:47 -0700102 return new DefaultLink(PID,
103 connectPoint(src, sp),
104 connectPoint(dst, dp),
Brian Stanke612cebf2016-05-02 10:21:33 -0400105 Link.Type.DIRECT, Link.State.ACTIVE);
tomca90c462014-09-22 11:40:58 -0700106 }
107
Luca Pretede10c782017-01-05 17:23:08 -0800108 // Short-hand for creating a link.
109 public static Link linkNoPrefixes(String src, int sp, String dst, int dp) {
110 return new DefaultLink(PID,
111 connectPointNoOF(src, sp),
112 connectPointNoOF(dst, dp),
113 Link.Type.DIRECT, Link.State.ACTIVE);
114 }
115
Pier Ventre766995d2016-10-05 22:15:56 -0700116 /**
117 * Short-hand for creating a link.
118 *
119 * @param src the src of the link
120 * @param dst the dst of the link
121 * @return a link
122 */
123 public static Link link(ConnectPoint src, ConnectPoint dst) {
124 return new DefaultLink(PID, src, dst, Link.Type.DIRECT, Link.State.ACTIVE);
125 }
126
tomca90c462014-09-22 11:40:58 -0700127 // Creates a path that leads through the given devices.
128 public static Path createPath(String... ids) {
129 List<Link> links = new ArrayList<>();
130 for (int i = 0; i < ids.length - 1; i++) {
Luca Pretede10c782017-01-05 17:23:08 -0800131 links.add(link(ids[i], 2, ids[i + 1], 1));
tomca90c462014-09-22 11:40:58 -0700132 }
133 return new DefaultPath(PID, links, ids.length);
134 }
135
Luca Pretede10c782017-01-05 17:23:08 -0800136 // Creates a path that leads through the given hosts.
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700137 public static Path createPath(boolean srcIsEdge, boolean dstIsEdge, String... ids) {
138 List<Link> links = new ArrayList<>();
139 for (int i = 0; i < ids.length - 1; i++) {
140 if (i == 0 && srcIsEdge) {
Luca Pretede10c782017-01-05 17:23:08 -0800141 links.add(DefaultEdgeLink.createEdgeLink(host(ids[i], ids[i + 1], 1), true));
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700142 } else if (i == ids.length - 2 && dstIsEdge) {
Luca Pretede10c782017-01-05 17:23:08 -0800143 links.add(DefaultEdgeLink.createEdgeLink(host(ids[i + 1], ids[i], 2), false));
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700144 } else {
Luca Pretede10c782017-01-05 17:23:08 -0800145 links.add(link(ids[i], 2, ids[i + 1], 1));
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700146 }
147 }
148 return new DefaultPath(PID, links, ids.length);
149 }
150
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700151 // Creates OCh signal
152 public static OchSignal createLambda() {
153 return new OchSignal(GridType.DWDM, ChannelSpacing.CHL_6P25GHZ, 8, 4);
154 }
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800155
156 /**
157 * Verifies that Annotations created by merging {@code annotations} is
158 * equal to actual Annotations.
159 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700160 * @param actual annotations to check
161 * @param annotations expected annotations
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800162 */
163 public static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
164 DefaultAnnotations expected = DefaultAnnotations.builder().build();
165 for (SparseAnnotations a : annotations) {
166 expected = DefaultAnnotations.merge(expected, a);
167 }
168 assertEquals(expected.keys(), actual.keys());
169 for (String key : expected.keys()) {
170 assertEquals(expected.value(key), actual.value(key));
171 }
172 }
173
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700174 /**
175 * Injects the given event delivery service into the specified manager
176 * component.
177 *
178 * @param manager manager component
179 * @param svc service reference to be injected
180 */
181 public static void injectEventDispatcher(Object manager, EventDeliveryService svc) {
182 Class mc = manager.getClass();
183 for (Field f : mc.getSuperclass().getDeclaredFields()) {
184 if (f.getType().equals(EventDeliveryService.class)) {
185 try {
Ray Milkey5b94aea2015-08-05 11:00:55 -0700186 TestUtils.setField(manager, f.getName(), svc);
187 } catch (TestUtils.TestUtilsException e) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700188 throw new IllegalArgumentException("Unable to inject reference", e);
189 }
Ray Milkey5b94aea2015-08-05 11:00:55 -0700190 break;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700191 }
192 }
193 }
194
Pier Ventre766995d2016-10-05 22:15:56 -0700195 /**
196 * Builds an empty selector.
197 *
198 * @return the selector
199 */
200 public static TrafficSelector emptySelector() {
201 return DefaultTrafficSelector.emptySelector();
202 }
203
204 /**
205 * Builds a vlan selector.
206 *
207 * @return the selector
208 */
209 public static TrafficSelector vlanSelector(String vlanId) {
210 return DefaultTrafficSelector.builder()
211 .matchVlanId(VlanId.vlanId(vlanId))
212 .build();
213 }
214
215 /**
216 * Builds a mpls selector.
217 *
218 * @return the selector
219 */
220 public static TrafficSelector mplsSelector(String mplsLabel) {
221 return DefaultTrafficSelector.builder()
222 .matchMplsLabel(MplsLabel.mplsLabel(mplsLabel))
223 .build();
224 }
225
226 /**
227 * Builds an ip prefix dst selector.
228 *
229 * @return the selector
230 */
231 public static TrafficSelector ipPrefixDstSelector(String prefix) {
232 return DefaultTrafficSelector.builder()
233 .matchIPDst(IpPrefix.valueOf(prefix))
234 .build();
235 }
236
237 /**
238 * Builds an empty treatment.
239 *
240 * @return the treatment
241 */
242 public static TrafficTreatment emptyTreatment() {
243 return DefaultTrafficTreatment.emptyTreatment();
244 }
245
246 /**
247 * Builds a mac dst treatment.
248 *
249 * @return the treatment
250 */
251 public static TrafficTreatment macDstTreatment(String mac) {
252 return DefaultTrafficTreatment.builder()
253 .setEthDst(MacAddress.valueOf(mac))
254 .build();
255 }
256
257 /**
258 * Builds a list containing a vlan encapsulation constraint.
259 *
260 * @return the list of constraints
261 */
262 public static List<Constraint> vlanConstraint() {
263 return ImmutableList.of(
264 new EncapsulationConstraint(EncapsulationType.VLAN)
265 );
266 }
267
268 /**
269 * Builds a list containing a mpls encapsulation constraint.
270 *
271 * @return the list of constraints
272 */
273 public static List<Constraint> mplsConstraint() {
274 return ImmutableList.of(
275 new EncapsulationConstraint(EncapsulationType.MPLS)
276 );
277 }
278
Pier Ventre81c47bf2016-11-04 07:26:22 -0700279 /**
280 * Builds a treatment which contains the dec ttl
281 * actions.
282 *
283 * @return the treatment
284 */
285 public static TrafficTreatment decTtlTreatment() {
286 return DefaultTrafficTreatment.builder()
287 .decMplsTtl()
288 .decNwTtl()
289 .build();
290 }
291
tomca90c462014-09-22 11:40:58 -0700292}