blob: 642830e71d5846df835eec11d25c7692b5771d84 [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.intent;
Ray Milkeye6684082014-10-16 16:59:47 -070017
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070018import com.google.common.base.MoreObjects;
cansu.toprak409289d2017-10-27 10:04:05 +030019import com.google.common.collect.Sets;
Ray Milkeya7cf8c82018-02-08 15:07:06 -080020import org.onlab.graph.ScalarWeight;
Andrey Komarov2398d962016-09-26 15:11:23 +030021import org.onlab.graph.Weight;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.core.GroupId;
Daniele Moro3a6e1512017-12-22 12:14:44 +010023import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.DefaultLink;
Luca Pretede10c782017-01-05 17:23:08 -080025import org.onosproject.net.DefaultPath;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.DeviceId;
27import org.onosproject.net.ElementId;
Thomas Vachuskadc91b552016-03-29 14:02:47 -070028import org.onosproject.net.HostId;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.Link;
Ray Milkey5a7787a2015-02-23 11:44:30 -080030import org.onosproject.net.NetTestTools;
Ray Milkey43a28222015-02-23 13:57:58 -080031import org.onosproject.net.NetworkResource;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.Path;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070033import org.onosproject.net.device.DeviceServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.flow.FlowId;
35import org.onosproject.net.flow.FlowRule;
Carmelo Cascone41605742017-06-19 15:46:44 +090036import org.onosproject.net.flow.IndexTableId;
37import org.onosproject.net.flow.TableId;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.flow.TrafficSelector;
39import org.onosproject.net.flow.TrafficTreatment;
40import org.onosproject.net.flow.criteria.Criterion;
41import org.onosproject.net.flow.criteria.Criterion.Type;
42import org.onosproject.net.flow.instructions.Instruction;
alshabib346b5b32015-03-06 00:42:16 -080043import org.onosproject.net.flow.instructions.Instructions;
Saurav Das86af8f12015-05-25 23:55:33 -070044import org.onosproject.net.flow.instructions.Instructions.MetadataInstruction;
Daniele Moro3a6e1512017-12-22 12:14:44 +010045import org.onosproject.net.link.LinkServiceAdapter;
Luca Pretede10c782017-01-05 17:23:08 -080046import org.onosproject.net.provider.ProviderId;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.topology.DefaultTopologyEdge;
48import org.onosproject.net.topology.DefaultTopologyVertex;
Andrey Komarov2398d962016-09-26 15:11:23 +030049import org.onosproject.net.topology.LinkWeigher;
Thomas Vachuska48e64e42015-09-22 15:32:55 -070050import org.onosproject.net.topology.PathServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import org.onosproject.net.topology.TopologyVertex;
Ray Milkey5a7787a2015-02-23 11:44:30 -080052import org.onosproject.store.Timestamp;
Ray Milkeye6684082014-10-16 16:59:47 -070053
Luca Pretede10c782017-01-05 17:23:08 -080054import java.util.ArrayList;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070055import java.util.Arrays;
56import java.util.Collection;
57import java.util.Collections;
58import java.util.HashSet;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070059import java.util.List;
60import java.util.Objects;
61import java.util.Set;
62import java.util.concurrent.atomic.AtomicLong;
Daniele Moro3a6e1512017-12-22 12:14:44 +010063import java.util.stream.Collectors;
64import java.util.stream.IntStream;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070065
Daniele Moro3a6e1512017-12-22 12:14:44 +010066import static org.onosproject.net.Link.Type.DIRECT;
Thomas Vachuska48e64e42015-09-22 15:32:55 -070067import static org.onosproject.net.NetTestTools.*;
Ray Milkey43a28222015-02-23 13:57:58 -080068
Ray Milkeye6684082014-10-16 16:59:47 -070069/**
70 * Common mocks used by the intent framework tests.
71 */
72public class IntentTestsMocks {
73 /**
74 * Mock traffic selector class used for satisfying API requirements.
75 */
76 public static class MockSelector implements TrafficSelector {
77 @Override
78 public Set<Criterion> criteria() {
79 return new HashSet<>();
80 }
Jonathan Hart936c49d2014-10-23 16:38:59 -070081
82 @Override
83 public Criterion getCriterion(Type type) {
84 return null;
85 }
Ray Milkeye6684082014-10-16 16:59:47 -070086 }
87
88 /**
89 * Mock traffic treatment class used for satisfying API requirements.
90 */
91 public static class MockTreatment implements TrafficTreatment {
92 @Override
alshabib346b5b32015-03-06 00:42:16 -080093 public List<Instruction> deferred() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070094 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -080095 }
96
97 @Override
98 public List<Instruction> immediate() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070099 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -0800100 }
101
102 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700103 public List<Instruction> allInstructions() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700104 return Collections.emptyList();
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700105 }
106
107 @Override
alshabib346b5b32015-03-06 00:42:16 -0800108 public Instructions.TableTypeTransition tableTransition() {
109 return null;
110 }
111
112 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700113 public boolean clearedDeferred() {
114 return false;
alshabib346b5b32015-03-06 00:42:16 -0800115 }
Saurav Das86af8f12015-05-25 23:55:33 -0700116
117 @Override
118 public MetadataInstruction writeMetadata() {
119 return null;
120 }
alshabib10c810b2015-08-18 16:59:04 -0700121
122 @Override
Cem Türker3baff672017-10-12 15:09:01 +0300123 public Instructions.StatTriggerInstruction statTrigger() {
124 return null;
125 }
126
127 @Override
alshabib10c810b2015-08-18 16:59:04 -0700128 public Instructions.MeterInstruction metered() {
129 return null;
130 }
cansu.toprak409289d2017-10-27 10:04:05 +0300131
132 @Override
133 public Set<Instructions.MeterInstruction> meters() {
134 return Sets.newHashSet();
135 }
Ray Milkeye6684082014-10-16 16:59:47 -0700136 }
137
138 /**
139 * Mock path service for creating paths within the test.
140 */
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700141 public static class MockPathService extends PathServiceAdapter {
Ray Milkeye6684082014-10-16 16:59:47 -0700142
143 final String[] pathHops;
144 final String[] reversePathHops;
145
146 /**
147 * Constructor that provides a set of hops to mock.
148 *
149 * @param pathHops path hops to mock
150 */
151 public MockPathService(String[] pathHops) {
152 this.pathHops = pathHops;
153 String[] reversed = pathHops.clone();
154 Collections.reverse(Arrays.asList(reversed));
155 reversePathHops = reversed;
156 }
157
158 @Override
159 public Set<Path> getPaths(ElementId src, ElementId dst) {
160 Set<Path> result = new HashSet<>();
161
162 String[] allHops = new String[pathHops.length];
163
164 if (src.toString().endsWith(pathHops[0])) {
165 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
166 } else {
167 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
168 }
169
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700170 result.add(createPath(src instanceof HostId, dst instanceof HostId, allHops));
Ray Milkeye6684082014-10-16 16:59:47 -0700171 return result;
172 }
173
174 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300175 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeigher weigher) {
176 Set<Path> paths = getPaths(src, dst);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800177
178 for (Path path : paths) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300179 DeviceId srcDevice = path.src().elementId() instanceof DeviceId ? path.src().deviceId() : null;
180 DeviceId dstDevice = path.dst().elementId() instanceof DeviceId ? path.dst().deviceId() : null;
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700181 if (srcDevice != null && dstDevice != null) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300182 TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
183 TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
184 Link link = link(src.toString(), 1, dst.toString(), 1);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800185
Andrey Komarov2398d962016-09-26 15:11:23 +0300186 Weight weightValue = weigher.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
187 if (weightValue.isNegative()) {
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700188 return new HashSet<>();
189 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800190 }
191 }
192 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700193 }
194 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800195
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700196 /**
Daniele Moro3a6e1512017-12-22 12:14:44 +0100197 * Mock path service for creating paths within the test with multiple possible paths.
198 */
199 public static class MockMultiplePathService extends PathServiceAdapter {
200
201 final String[][] pathsHops;
202
203 /**
204 * Constructor that provides a set of hops to mock.
205 *
206 * @param pathHops multiple path hops to mock
207 */
208 public MockMultiplePathService(String[][] pathHops) {
209 this.pathsHops = pathHops;
210 }
211
212 @Override
213 public Set<Path> getPaths(ElementId src, ElementId dst) {
214
215 //Extracts all the paths that goes from src to dst
216 Set<Path> allPaths = new HashSet<>();
217 allPaths.addAll(IntStream.range(0, pathsHops.length)
218 .filter(i -> src.toString().endsWith(pathsHops[i][0])
219 && dst.toString().endsWith(pathsHops[i][pathsHops[i].length - 1]))
220 .mapToObj(i -> createPath(src instanceof HostId,
221 dst instanceof HostId,
222 pathsHops[i]))
223 .collect(Collectors.toSet()));
224
225 // Maintain only the shortest paths
226 int minPathLength = allPaths.stream()
227 .mapToInt(o -> o.links().size())
228 .min()
229 .orElse(Integer.MAX_VALUE);
230 Set<Path> shortestPaths = allPaths.stream()
231 .filter(path -> path.links().size() <= minPathLength)
232 .collect(Collectors.toSet());
233
234 return shortestPaths;
235 }
236
237
238 @Override
239 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeigher weigher) {
240 Set<Path> paths = getPaths(src, dst);
241
242 for (Path path : paths) {
243 DeviceId srcDevice = path.src().elementId() instanceof DeviceId ? path.src().deviceId() : null;
244 DeviceId dstDevice = path.dst().elementId() instanceof DeviceId ? path.dst().deviceId() : null;
245 if (srcDevice != null && dstDevice != null) {
246 TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
247 TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
248 Link link = link(src.toString(), 1, dst.toString(), 1);
249
250 Weight weightValue = weigher.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
251 if (weightValue.isNegative()) {
252 return new HashSet<>();
253 }
254 }
255 }
256 return paths;
257 }
258 }
259
260
261 /**
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700262 * Mock path service for creating paths within the test.
263 *
264 */
Luca Pretede10c782017-01-05 17:23:08 -0800265 public static class Mp2MpMockPathService extends PathServiceAdapter {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700266
267 final String[] pathHops;
268 final String[] reversePathHops;
269
270 /**
271 * Constructor that provides a set of hops to mock.
272 *
273 * @param pathHops path hops to mock
274 */
275 public Mp2MpMockPathService(String[] pathHops) {
276 this.pathHops = pathHops;
277 String[] reversed = pathHops.clone();
278 Collections.reverse(Arrays.asList(reversed));
279 reversePathHops = reversed;
280 }
281
282 @Override
283 public Set<Path> getPaths(ElementId src, ElementId dst) {
284 Set<Path> result = new HashSet<>();
285
286 String[] allHops = new String[pathHops.length + 2];
287 allHops[0] = src.toString();
288 allHops[allHops.length - 1] = dst.toString();
289
290 if (pathHops.length != 0) {
291 System.arraycopy(pathHops, 0, allHops, 1, pathHops.length);
292 }
293
294 result.add(createPath(allHops));
295
296 return result;
297 }
298
299 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300300 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeigher weigher) {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700301 final Set<Path> paths = getPaths(src, dst);
302
303 for (Path path : paths) {
304 final DeviceId srcDevice = path.src().elementId() instanceof DeviceId ? path.src().deviceId() : null;
305 final DeviceId dstDevice = path.dst().elementId() instanceof DeviceId ? path.dst().deviceId() : null;
306 if (srcDevice != null && dstDevice != null) {
307 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
308 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
309 final Link link = link(src.toString(), 1, dst.toString(), 1);
310
Andrey Komarov2398d962016-09-26 15:11:23 +0300311 final Weight weightValue = weigher.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
312 if (weightValue.isNegative()) {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700313 return new HashSet<>();
314 }
315 }
316 }
317 return paths;
318 }
319 }
320
Luca Pretede10c782017-01-05 17:23:08 -0800321 /**
Daniele Moro3a6e1512017-12-22 12:14:44 +0100322 * Mock active and direct link.
323 */
324 public static class FakeLink extends DefaultLink {
325
326 /**
327 * Constructor that provides source and destination of the fake link.
328 *
329 * @param src Source connect point of the fake link
330 * @param dst Destination connect point of the fake link
331 */
332 public FakeLink(ConnectPoint src, ConnectPoint dst) {
333 super(null, src, dst, DIRECT, Link.State.ACTIVE);
334 }
335 }
336
337 /**
Luca Pretede10c782017-01-05 17:23:08 -0800338 * Mock path service for creating paths for MP2SP intent tests, returning
339 * pre-determined paths.
340 */
341 public static class FixedMP2MPMockPathService extends PathServiceAdapter {
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800342
Luca Pretede10c782017-01-05 17:23:08 -0800343 final String[] pathHops;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800344
Luca Pretede10c782017-01-05 17:23:08 -0800345 public static final String DPID_1 = "of:s1";
346 public static final String DPID_2 = "of:s2";
347 public static final String DPID_3 = "of:s3";
348 public static final String DPID_4 = "of:s4";
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800349
Luca Pretede10c782017-01-05 17:23:08 -0800350 /**
351 * Constructor that provides a set of hops to mock.
352 *
353 * @param pathHops path hops to mock
354 */
355 public FixedMP2MPMockPathService(String[] pathHops) {
356 this.pathHops = pathHops;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800357 }
358
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700359 @Override
Luca Pretede10c782017-01-05 17:23:08 -0800360 public Set<Path> getPaths(ElementId src, ElementId dst) {
361 List<Link> links = new ArrayList<>();
362 Set<Path> result = new HashSet<>();
363 ProviderId providerId = new ProviderId("of", "foo");
364 DefaultPath path;
365 if (src.toString().equals(DPID_1) && dst.toString().equals(DPID_4)) {
366 links.add(NetTestTools.linkNoPrefixes(src.toString(), 2, pathHops[0], 1));
367 links.add(NetTestTools.linkNoPrefixes(pathHops[0], 2, dst.toString(), 1));
368 } else if (src.toString().equals(DPID_2) && dst.toString().equals(DPID_4)) {
369 links.add(NetTestTools.linkNoPrefixes(src.toString(), 2, pathHops[0], 3));
370 links.add(NetTestTools.linkNoPrefixes(pathHops[0], 2, dst.toString(), 1));
371 } else if (src.toString().equals(DPID_4) && dst.toString().equals(DPID_1)) {
372 links.add(NetTestTools.linkNoPrefixes(src.toString(), 2, pathHops[0], 1));
373 links.add(NetTestTools.linkNoPrefixes(pathHops[0], 2, dst.toString(), 1));
374 } else if (src.toString().equals(DPID_4) && dst.toString().equals(DPID_2)) {
375 links.add(NetTestTools.linkNoPrefixes(src.toString(), 2, pathHops[0], 1));
376 links.add(NetTestTools.linkNoPrefixes(pathHops[0], 3, dst.toString(), 1));
377 } else {
378 return result;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800379 }
Ray Milkeya7cf8c82018-02-08 15:07:06 -0800380 path = new DefaultPath(providerId, links, ScalarWeight.toWeight(3));
Luca Pretede10c782017-01-05 17:23:08 -0800381 result.add(path);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800382
Luca Pretede10c782017-01-05 17:23:08 -0800383 return result;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800384 }
385
386 @Override
Luca Pretede10c782017-01-05 17:23:08 -0800387 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeigher weigher) {
388 final Set<Path> paths = getPaths(src, dst);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800389
Luca Pretede10c782017-01-05 17:23:08 -0800390 for (Path path : paths) {
391 final DeviceId srcDevice = path.src().elementId() instanceof DeviceId ? path.src().deviceId() : null;
392 final DeviceId dstDevice = path.dst().elementId() instanceof DeviceId ? path.dst().deviceId() : null;
393 if (srcDevice != null && dstDevice != null) {
394 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
395 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
396 final Link link = link(src.toString(), 1, dst.toString(), 1);
397
398 final Weight weightValue = weigher.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
399 if (weightValue.isNegative()) {
400 return new HashSet<>();
401 }
402 }
403 }
404 return paths;
Ray Milkeye97ede92014-11-20 10:43:12 -0800405 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800406 }
407
Daniele Moro3a6e1512017-12-22 12:14:44 +0100408 /**
409 * Mock link service for getting links to check path availability
410 * when a suggested path is submitted.
411 */
412 public static class MockLinkService extends LinkServiceAdapter {
413 final String[][] linksHops;
414
415 /**
416 * Constructor that provides a set of links (as a list of hops).
417 *
418 * @param linksHops links to to mock (link as a set of hops)
419 */
420 public MockLinkService(String[][] linksHops) {
421 this.linksHops = linksHops;
422 }
423
424 @Override
425 public Set<Link> getLinks() {
426 return Arrays.asList(linksHops).stream()
427 .map(path -> createPath(path).links())
428 .flatMap(List::stream)
429 .collect(Collectors.toSet());
430 }
431 @Override
432 public Set<Link> getLinks(ConnectPoint connectPoint) {
433 return getLinks().stream()
434 .filter(link -> link.src().deviceId().equals(connectPoint.deviceId())
435 || link.dst().deviceId().equals(connectPoint.deviceId()))
436 .collect(Collectors.toSet());
437 }
438 }
439
Ray Milkey930fc662014-11-11 16:07:45 -0800440 private static final IntentTestsMocks.MockSelector SELECTOR =
441 new IntentTestsMocks.MockSelector();
442 private static final IntentTestsMocks.MockTreatment TREATMENT =
443 new IntentTestsMocks.MockTreatment();
444
445 public static class MockFlowRule implements FlowRule {
Ray Milkey77a455f2015-03-27 10:08:17 -0700446 static int nextId = 0;
Ray Milkey930fc662014-11-11 16:07:45 -0800447
448 int priority;
Carmelo Cascone41605742017-06-19 15:46:44 +0900449 IndexTableId tableId;
Ray Milkey77a455f2015-03-27 10:08:17 -0700450 long timestamp;
451 int id;
sangho11c30ac2015-01-22 14:30:55 -0800452
Ray Milkey930fc662014-11-11 16:07:45 -0800453 public MockFlowRule(int priority) {
454 this.priority = priority;
Carmelo Cascone41605742017-06-19 15:46:44 +0900455 this.tableId = DEFAULT_TABLE;
Ray Milkey77a455f2015-03-27 10:08:17 -0700456 this.timestamp = System.currentTimeMillis();
457 this.id = nextId++;
Ray Milkey930fc662014-11-11 16:07:45 -0800458 }
459
460 @Override
461 public FlowId id() {
Ray Milkey77a455f2015-03-27 10:08:17 -0700462 return FlowId.valueOf(id);
Ray Milkey930fc662014-11-11 16:07:45 -0800463 }
464
465 @Override
466 public short appId() {
467 return 0;
468 }
469
470 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800471 public GroupId groupId() {
Yi Tsengfa394de2017-02-01 11:26:40 -0800472 return new GroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800473 }
474
475 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800476 public int priority() {
477 return priority;
478 }
479
480 @Override
481 public DeviceId deviceId() {
482 return did("1");
483 }
484
485 @Override
486 public TrafficSelector selector() {
487 return SELECTOR;
488 }
489
490 @Override
491 public TrafficTreatment treatment() {
492 return TREATMENT;
493 }
494
495 @Override
496 public int timeout() {
497 return 0;
498 }
499
500 @Override
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700501 public int hardTimeout() {
502 return 0;
503 }
504
505 @Override
506 public FlowRemoveReason reason() {
507 return FlowRemoveReason.NO_REASON;
508 }
509
510 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800511 public boolean isPermanent() {
512 return false;
513 }
514
Brian O'Connor427a1762014-11-19 18:40:32 -0800515 @Override
516 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700517 return priority;
Brian O'Connor427a1762014-11-19 18:40:32 -0800518 }
Ray Milkey930fc662014-11-11 16:07:45 -0800519
Brian O'Connor427a1762014-11-19 18:40:32 -0800520 @Override
521 public boolean equals(Object obj) {
522 if (this == obj) {
523 return true;
524 }
525 if (obj == null || getClass() != obj.getClass()) {
526 return false;
527 }
528 final MockFlowRule other = (MockFlowRule) obj;
Ray Milkey77a455f2015-03-27 10:08:17 -0700529 return Objects.equals(this.timestamp, other.timestamp) &&
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700530 this.id == other.id;
Brian O'Connor427a1762014-11-19 18:40:32 -0800531 }
sangho11c30ac2015-01-22 14:30:55 -0800532
533 @Override
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700534 public boolean exactMatch(FlowRule rule) {
535 return this.equals(rule);
536 }
537
538 @Override
alshabibdb774072015-04-20 13:13:51 -0700539 public int tableId() {
Carmelo Cascone41605742017-06-19 15:46:44 +0900540 return tableId.id();
541 }
542
543 @Override
544 public TableId table() {
alshabib08d98982015-04-21 16:25:50 -0700545 return tableId;
alshabibdb774072015-04-20 13:13:51 -0700546 }
Ray Milkey930fc662014-11-11 16:07:45 -0800547 }
548
Ray Milkey5a7787a2015-02-23 11:44:30 -0800549 public static class MockIntent extends Intent {
550 private static AtomicLong counter = new AtomicLong(0);
551
552 private final Long number;
553
554 public MockIntent(Long number) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700555 super(NetTestTools.APP_ID, null, Collections.emptyList(),
Ray Milkey39f78b62018-01-05 15:17:37 -0800556 Intent.DEFAULT_INTENT_PRIORITY, null);
Ray Milkey5a7787a2015-02-23 11:44:30 -0800557 this.number = number;
558 }
559
Ray Milkey43a28222015-02-23 13:57:58 -0800560 public MockIntent(Long number, Collection<NetworkResource> resources) {
Ray Milkey39f78b62018-01-05 15:17:37 -0800561 super(NetTestTools.APP_ID, null, resources, Intent.DEFAULT_INTENT_PRIORITY, null);
Ray Milkey43a28222015-02-23 13:57:58 -0800562 this.number = number;
563 }
564
Ray Milkey5a7787a2015-02-23 11:44:30 -0800565 public Long number() {
566 return number;
567 }
568
569 public static Long nextId() {
570 return counter.getAndIncrement();
571 }
Ray Milkey43a28222015-02-23 13:57:58 -0800572
573 @Override
574 public String toString() {
575 return MoreObjects.toStringHelper(getClass())
576 .add("id", id())
577 .add("appId", appId())
578 .toString();
579 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800580 }
581
582 public static class MockTimestamp implements Timestamp {
583 final int value;
584
585 public MockTimestamp(int value) {
586 this.value = value;
587 }
588
589 @Override
590 public int compareTo(Timestamp o) {
591 if (!(o instanceof MockTimestamp)) {
592 return -1;
593 }
594 MockTimestamp that = (MockTimestamp) o;
Jonathan Hart72175c22015-03-24 18:55:58 -0700595 return this.value - that.value;
Ray Milkey5a7787a2015-02-23 11:44:30 -0800596 }
Yuta HIGUCHI4f8a3772017-05-16 20:23:49 -0700597
598 @Override
599 public int hashCode() {
600 return value;
601 }
602
603 @Override
604 public boolean equals(Object obj) {
605 if (obj instanceof MockTimestamp) {
606 return this.compareTo((MockTimestamp) obj) == 0;
607 }
608 return false;
609 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800610 }
611
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700612 /**
613 * Mocks the device service so that a device appears available in the test.
614 */
615 public static class MockDeviceService extends DeviceServiceAdapter {
616 @Override
617 public boolean isAvailable(DeviceId deviceId) {
618 return true;
619 }
620 }
Ray Milkeye6684082014-10-16 16:59:47 -0700621}