blob: cef5b6707a4f9cc2cefcb73d4f5c7b384c01c207 [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.intent;
Ray Milkeye6684082014-10-16 16:59:47 -070017
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070018import com.google.common.base.MoreObjects;
Andrey Komarov2398d962016-09-26 15:11:23 +030019import org.onlab.graph.Weight;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.core.DefaultGroupId;
21import org.onosproject.core.GroupId;
Luca Pretede10c782017-01-05 17:23:08 -080022import org.onosproject.net.DefaultPath;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.DeviceId;
24import org.onosproject.net.ElementId;
Thomas Vachuskadc91b552016-03-29 14:02:47 -070025import org.onosproject.net.HostId;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.Link;
Ray Milkey5a7787a2015-02-23 11:44:30 -080027import org.onosproject.net.NetTestTools;
Ray Milkey43a28222015-02-23 13:57:58 -080028import org.onosproject.net.NetworkResource;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.Path;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070030import org.onosproject.net.device.DeviceServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.flow.FlowId;
32import org.onosproject.net.flow.FlowRule;
jcc3d4e14a2015-04-21 11:32:05 +080033import org.onosproject.net.flow.FlowRuleExtPayLoad;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.flow.TrafficSelector;
35import org.onosproject.net.flow.TrafficTreatment;
36import org.onosproject.net.flow.criteria.Criterion;
37import org.onosproject.net.flow.criteria.Criterion.Type;
38import org.onosproject.net.flow.instructions.Instruction;
alshabib346b5b32015-03-06 00:42:16 -080039import org.onosproject.net.flow.instructions.Instructions;
Saurav Das86af8f12015-05-25 23:55:33 -070040import org.onosproject.net.flow.instructions.Instructions.MetadataInstruction;
Luca Pretede10c782017-01-05 17:23:08 -080041import org.onosproject.net.provider.ProviderId;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.topology.DefaultTopologyEdge;
43import org.onosproject.net.topology.DefaultTopologyVertex;
Andrey Komarov2398d962016-09-26 15:11:23 +030044import org.onosproject.net.topology.LinkWeigher;
Thomas Vachuska48e64e42015-09-22 15:32:55 -070045import org.onosproject.net.topology.PathServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.topology.TopologyVertex;
Ray Milkey5a7787a2015-02-23 11:44:30 -080047import org.onosproject.store.Timestamp;
Ray Milkeye6684082014-10-16 16:59:47 -070048
Luca Pretede10c782017-01-05 17:23:08 -080049import java.util.ArrayList;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070050import java.util.Arrays;
51import java.util.Collection;
52import java.util.Collections;
53import java.util.HashSet;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070054import java.util.List;
55import java.util.Objects;
56import java.util.Set;
57import java.util.concurrent.atomic.AtomicLong;
58
Thomas Vachuska48e64e42015-09-22 15:32:55 -070059import static org.onosproject.net.NetTestTools.*;
Ray Milkey43a28222015-02-23 13:57:58 -080060
Ray Milkeye6684082014-10-16 16:59:47 -070061/**
62 * Common mocks used by the intent framework tests.
63 */
64public class IntentTestsMocks {
65 /**
66 * Mock traffic selector class used for satisfying API requirements.
67 */
68 public static class MockSelector implements TrafficSelector {
69 @Override
70 public Set<Criterion> criteria() {
71 return new HashSet<>();
72 }
Jonathan Hart936c49d2014-10-23 16:38:59 -070073
74 @Override
75 public Criterion getCriterion(Type type) {
76 return null;
77 }
Ray Milkeye6684082014-10-16 16:59:47 -070078 }
79
80 /**
81 * Mock traffic treatment class used for satisfying API requirements.
82 */
83 public static class MockTreatment implements TrafficTreatment {
84 @Override
alshabib346b5b32015-03-06 00:42:16 -080085 public List<Instruction> deferred() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070086 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -080087 }
88
89 @Override
90 public List<Instruction> immediate() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070091 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -080092 }
93
94 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070095 public List<Instruction> allInstructions() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070096 return Collections.emptyList();
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070097 }
98
99 @Override
alshabib346b5b32015-03-06 00:42:16 -0800100 public Instructions.TableTypeTransition tableTransition() {
101 return null;
102 }
103
104 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700105 public boolean clearedDeferred() {
106 return false;
alshabib346b5b32015-03-06 00:42:16 -0800107 }
Saurav Das86af8f12015-05-25 23:55:33 -0700108
109 @Override
110 public MetadataInstruction writeMetadata() {
111 return null;
112 }
alshabib10c810b2015-08-18 16:59:04 -0700113
114 @Override
115 public Instructions.MeterInstruction metered() {
116 return null;
117 }
Ray Milkeye6684082014-10-16 16:59:47 -0700118 }
119
120 /**
121 * Mock path service for creating paths within the test.
122 */
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700123 public static class MockPathService extends PathServiceAdapter {
Ray Milkeye6684082014-10-16 16:59:47 -0700124
125 final String[] pathHops;
126 final String[] reversePathHops;
127
128 /**
129 * Constructor that provides a set of hops to mock.
130 *
131 * @param pathHops path hops to mock
132 */
133 public MockPathService(String[] pathHops) {
134 this.pathHops = pathHops;
135 String[] reversed = pathHops.clone();
136 Collections.reverse(Arrays.asList(reversed));
137 reversePathHops = reversed;
138 }
139
140 @Override
141 public Set<Path> getPaths(ElementId src, ElementId dst) {
142 Set<Path> result = new HashSet<>();
143
144 String[] allHops = new String[pathHops.length];
145
146 if (src.toString().endsWith(pathHops[0])) {
147 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
148 } else {
149 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
150 }
151
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700152 result.add(createPath(src instanceof HostId, dst instanceof HostId, allHops));
Ray Milkeye6684082014-10-16 16:59:47 -0700153 return result;
154 }
155
156 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300157 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeigher weigher) {
158 Set<Path> paths = getPaths(src, dst);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800159
160 for (Path path : paths) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300161 DeviceId srcDevice = path.src().elementId() instanceof DeviceId ? path.src().deviceId() : null;
162 DeviceId dstDevice = path.dst().elementId() instanceof DeviceId ? path.dst().deviceId() : null;
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700163 if (srcDevice != null && dstDevice != null) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300164 TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
165 TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
166 Link link = link(src.toString(), 1, dst.toString(), 1);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800167
Andrey Komarov2398d962016-09-26 15:11:23 +0300168 Weight weightValue = weigher.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
169 if (weightValue.isNegative()) {
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700170 return new HashSet<>();
171 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800172 }
173 }
174 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700175 }
176 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800177
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700178 /**
179 * Mock path service for creating paths within the test.
180 *
181 */
Luca Pretede10c782017-01-05 17:23:08 -0800182 public static class Mp2MpMockPathService extends PathServiceAdapter {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700183
184 final String[] pathHops;
185 final String[] reversePathHops;
186
187 /**
188 * Constructor that provides a set of hops to mock.
189 *
190 * @param pathHops path hops to mock
191 */
192 public Mp2MpMockPathService(String[] pathHops) {
193 this.pathHops = pathHops;
194 String[] reversed = pathHops.clone();
195 Collections.reverse(Arrays.asList(reversed));
196 reversePathHops = reversed;
197 }
198
199 @Override
200 public Set<Path> getPaths(ElementId src, ElementId dst) {
201 Set<Path> result = new HashSet<>();
202
203 String[] allHops = new String[pathHops.length + 2];
204 allHops[0] = src.toString();
205 allHops[allHops.length - 1] = dst.toString();
206
207 if (pathHops.length != 0) {
208 System.arraycopy(pathHops, 0, allHops, 1, pathHops.length);
209 }
210
211 result.add(createPath(allHops));
212
213 return result;
214 }
215
216 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300217 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeigher weigher) {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700218 final Set<Path> paths = getPaths(src, dst);
219
220 for (Path path : paths) {
221 final DeviceId srcDevice = path.src().elementId() instanceof DeviceId ? path.src().deviceId() : null;
222 final DeviceId dstDevice = path.dst().elementId() instanceof DeviceId ? path.dst().deviceId() : null;
223 if (srcDevice != null && dstDevice != null) {
224 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
225 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
226 final Link link = link(src.toString(), 1, dst.toString(), 1);
227
Andrey Komarov2398d962016-09-26 15:11:23 +0300228 final Weight weightValue = weigher.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
229 if (weightValue.isNegative()) {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700230 return new HashSet<>();
231 }
232 }
233 }
234 return paths;
235 }
236 }
237
Luca Pretede10c782017-01-05 17:23:08 -0800238 /**
239 * Mock path service for creating paths for MP2SP intent tests, returning
240 * pre-determined paths.
241 */
242 public static class FixedMP2MPMockPathService extends PathServiceAdapter {
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800243
Luca Pretede10c782017-01-05 17:23:08 -0800244 final String[] pathHops;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800245
Luca Pretede10c782017-01-05 17:23:08 -0800246 public static final String DPID_1 = "of:s1";
247 public static final String DPID_2 = "of:s2";
248 public static final String DPID_3 = "of:s3";
249 public static final String DPID_4 = "of:s4";
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800250
Luca Pretede10c782017-01-05 17:23:08 -0800251 /**
252 * Constructor that provides a set of hops to mock.
253 *
254 * @param pathHops path hops to mock
255 */
256 public FixedMP2MPMockPathService(String[] pathHops) {
257 this.pathHops = pathHops;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800258 }
259
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700260 @Override
Luca Pretede10c782017-01-05 17:23:08 -0800261 public Set<Path> getPaths(ElementId src, ElementId dst) {
262 List<Link> links = new ArrayList<>();
263 Set<Path> result = new HashSet<>();
264 ProviderId providerId = new ProviderId("of", "foo");
265 DefaultPath path;
266 if (src.toString().equals(DPID_1) && dst.toString().equals(DPID_4)) {
267 links.add(NetTestTools.linkNoPrefixes(src.toString(), 2, pathHops[0], 1));
268 links.add(NetTestTools.linkNoPrefixes(pathHops[0], 2, dst.toString(), 1));
269 } else if (src.toString().equals(DPID_2) && dst.toString().equals(DPID_4)) {
270 links.add(NetTestTools.linkNoPrefixes(src.toString(), 2, pathHops[0], 3));
271 links.add(NetTestTools.linkNoPrefixes(pathHops[0], 2, dst.toString(), 1));
272 } else if (src.toString().equals(DPID_4) && dst.toString().equals(DPID_1)) {
273 links.add(NetTestTools.linkNoPrefixes(src.toString(), 2, pathHops[0], 1));
274 links.add(NetTestTools.linkNoPrefixes(pathHops[0], 2, dst.toString(), 1));
275 } else if (src.toString().equals(DPID_4) && dst.toString().equals(DPID_2)) {
276 links.add(NetTestTools.linkNoPrefixes(src.toString(), 2, pathHops[0], 1));
277 links.add(NetTestTools.linkNoPrefixes(pathHops[0], 3, dst.toString(), 1));
278 } else {
279 return result;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800280 }
Luca Pretede10c782017-01-05 17:23:08 -0800281 path = new DefaultPath(providerId, links, 3);
282 result.add(path);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800283
Luca Pretede10c782017-01-05 17:23:08 -0800284 return result;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800285 }
286
287 @Override
Luca Pretede10c782017-01-05 17:23:08 -0800288 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeigher weigher) {
289 final Set<Path> paths = getPaths(src, dst);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800290
Luca Pretede10c782017-01-05 17:23:08 -0800291 for (Path path : paths) {
292 final DeviceId srcDevice = path.src().elementId() instanceof DeviceId ? path.src().deviceId() : null;
293 final DeviceId dstDevice = path.dst().elementId() instanceof DeviceId ? path.dst().deviceId() : null;
294 if (srcDevice != null && dstDevice != null) {
295 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
296 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
297 final Link link = link(src.toString(), 1, dst.toString(), 1);
298
299 final Weight weightValue = weigher.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
300 if (weightValue.isNegative()) {
301 return new HashSet<>();
302 }
303 }
304 }
305 return paths;
Ray Milkeye97ede92014-11-20 10:43:12 -0800306 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800307 }
308
Ray Milkey930fc662014-11-11 16:07:45 -0800309 private static final IntentTestsMocks.MockSelector SELECTOR =
310 new IntentTestsMocks.MockSelector();
311 private static final IntentTestsMocks.MockTreatment TREATMENT =
312 new IntentTestsMocks.MockTreatment();
313
314 public static class MockFlowRule implements FlowRule {
Ray Milkey77a455f2015-03-27 10:08:17 -0700315 static int nextId = 0;
Ray Milkey930fc662014-11-11 16:07:45 -0800316
317 int priority;
alshabib08d98982015-04-21 16:25:50 -0700318 int tableId;
Ray Milkey77a455f2015-03-27 10:08:17 -0700319 long timestamp;
320 int id;
jcc3d4e14a2015-04-21 11:32:05 +0800321 FlowRuleExtPayLoad payLoad;
sangho11c30ac2015-01-22 14:30:55 -0800322
Ray Milkey930fc662014-11-11 16:07:45 -0800323 public MockFlowRule(int priority) {
324 this.priority = priority;
alshabib08d98982015-04-21 16:25:50 -0700325 this.tableId = 0;
Ray Milkey77a455f2015-03-27 10:08:17 -0700326 this.timestamp = System.currentTimeMillis();
327 this.id = nextId++;
jcc3d4e14a2015-04-21 11:32:05 +0800328 this.payLoad = null;
329 }
330
331 public MockFlowRule(int priority, FlowRuleExtPayLoad payLoad) {
332 this.priority = priority;
333 this.timestamp = System.currentTimeMillis();
334 this.id = nextId++;
335 this.payLoad = payLoad;
Ray Milkey930fc662014-11-11 16:07:45 -0800336 }
337
338 @Override
339 public FlowId id() {
Ray Milkey77a455f2015-03-27 10:08:17 -0700340 return FlowId.valueOf(id);
Ray Milkey930fc662014-11-11 16:07:45 -0800341 }
342
343 @Override
344 public short appId() {
345 return 0;
346 }
347
348 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800349 public GroupId groupId() {
350 return new DefaultGroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800351 }
352
353 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800354 public int priority() {
355 return priority;
356 }
357
358 @Override
359 public DeviceId deviceId() {
360 return did("1");
361 }
362
363 @Override
364 public TrafficSelector selector() {
365 return SELECTOR;
366 }
367
368 @Override
369 public TrafficTreatment treatment() {
370 return TREATMENT;
371 }
372
373 @Override
374 public int timeout() {
375 return 0;
376 }
377
378 @Override
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700379 public int hardTimeout() {
380 return 0;
381 }
382
383 @Override
384 public FlowRemoveReason reason() {
385 return FlowRemoveReason.NO_REASON;
386 }
387
388 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800389 public boolean isPermanent() {
390 return false;
391 }
392
Brian O'Connor427a1762014-11-19 18:40:32 -0800393 @Override
394 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700395 return priority;
Brian O'Connor427a1762014-11-19 18:40:32 -0800396 }
Ray Milkey930fc662014-11-11 16:07:45 -0800397
Brian O'Connor427a1762014-11-19 18:40:32 -0800398 @Override
399 public boolean equals(Object obj) {
400 if (this == obj) {
401 return true;
402 }
403 if (obj == null || getClass() != obj.getClass()) {
404 return false;
405 }
406 final MockFlowRule other = (MockFlowRule) obj;
Ray Milkey77a455f2015-03-27 10:08:17 -0700407 return Objects.equals(this.timestamp, other.timestamp) &&
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700408 this.id == other.id;
Brian O'Connor427a1762014-11-19 18:40:32 -0800409 }
sangho11c30ac2015-01-22 14:30:55 -0800410
411 @Override
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700412 public boolean exactMatch(FlowRule rule) {
413 return this.equals(rule);
414 }
415
416 @Override
alshabibdb774072015-04-20 13:13:51 -0700417 public int tableId() {
alshabib08d98982015-04-21 16:25:50 -0700418 return tableId;
alshabibdb774072015-04-20 13:13:51 -0700419 }
jcc3d4e14a2015-04-21 11:32:05 +0800420
421 @Override
422 public FlowRuleExtPayLoad payLoad() {
423 return payLoad;
424 }
Ray Milkey930fc662014-11-11 16:07:45 -0800425 }
426
Ray Milkey5a7787a2015-02-23 11:44:30 -0800427 public static class MockIntent extends Intent {
428 private static AtomicLong counter = new AtomicLong(0);
429
430 private final Long number;
431
432 public MockIntent(Long number) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700433 super(NetTestTools.APP_ID, null, Collections.emptyList(),
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700434 Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey5a7787a2015-02-23 11:44:30 -0800435 this.number = number;
436 }
437
Ray Milkey43a28222015-02-23 13:57:58 -0800438 public MockIntent(Long number, Collection<NetworkResource> resources) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700439 super(NetTestTools.APP_ID, null, resources, Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey43a28222015-02-23 13:57:58 -0800440 this.number = number;
441 }
442
Ray Milkey5a7787a2015-02-23 11:44:30 -0800443 public Long number() {
444 return number;
445 }
446
447 public static Long nextId() {
448 return counter.getAndIncrement();
449 }
Ray Milkey43a28222015-02-23 13:57:58 -0800450
451 @Override
452 public String toString() {
453 return MoreObjects.toStringHelper(getClass())
454 .add("id", id())
455 .add("appId", appId())
456 .toString();
457 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800458 }
459
460 public static class MockTimestamp implements Timestamp {
461 final int value;
462
463 public MockTimestamp(int value) {
464 this.value = value;
465 }
466
467 @Override
468 public int compareTo(Timestamp o) {
469 if (!(o instanceof MockTimestamp)) {
470 return -1;
471 }
472 MockTimestamp that = (MockTimestamp) o;
Jonathan Hart72175c22015-03-24 18:55:58 -0700473 return this.value - that.value;
Ray Milkey5a7787a2015-02-23 11:44:30 -0800474 }
475 }
476
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700477 /**
478 * Mocks the device service so that a device appears available in the test.
479 */
480 public static class MockDeviceService extends DeviceServiceAdapter {
481 @Override
482 public boolean isAvailable(DeviceId deviceId) {
483 return true;
484 }
485 }
Ray Milkeye6684082014-10-16 16:59:47 -0700486}