blob: bdfa10641f4601e9d4bae01e5ac1d99994589fe7 [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;
David Glantzdfb3ceb2021-10-04 09:10:20 -050022import org.onosproject.core.ApplicationId;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.core.GroupId;
Daniele Moro3a6e1512017-12-22 12:14:44 +010024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DefaultLink;
Luca Pretede10c782017-01-05 17:23:08 -080026import org.onosproject.net.DefaultPath;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.DeviceId;
28import org.onosproject.net.ElementId;
Thomas Vachuskadc91b552016-03-29 14:02:47 -070029import org.onosproject.net.HostId;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.Link;
Ray Milkey5a7787a2015-02-23 11:44:30 -080031import org.onosproject.net.NetTestTools;
Ray Milkey43a28222015-02-23 13:57:58 -080032import org.onosproject.net.NetworkResource;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.Path;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070034import org.onosproject.net.device.DeviceServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.flow.FlowId;
36import org.onosproject.net.flow.FlowRule;
Carmelo Cascone41605742017-06-19 15:46:44 +090037import org.onosproject.net.flow.IndexTableId;
38import org.onosproject.net.flow.TableId;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.flow.TrafficSelector;
40import org.onosproject.net.flow.TrafficTreatment;
41import org.onosproject.net.flow.criteria.Criterion;
42import org.onosproject.net.flow.criteria.Criterion.Type;
43import org.onosproject.net.flow.instructions.Instruction;
alshabib346b5b32015-03-06 00:42:16 -080044import org.onosproject.net.flow.instructions.Instructions;
Saurav Das86af8f12015-05-25 23:55:33 -070045import org.onosproject.net.flow.instructions.Instructions.MetadataInstruction;
Daniele Moro3a6e1512017-12-22 12:14:44 +010046import org.onosproject.net.link.LinkServiceAdapter;
Luca Pretede10c782017-01-05 17:23:08 -080047import org.onosproject.net.provider.ProviderId;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.net.topology.DefaultTopologyEdge;
49import org.onosproject.net.topology.DefaultTopologyVertex;
Andrey Komarov2398d962016-09-26 15:11:23 +030050import org.onosproject.net.topology.LinkWeigher;
Thomas Vachuska48e64e42015-09-22 15:32:55 -070051import org.onosproject.net.topology.PathServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080052import org.onosproject.net.topology.TopologyVertex;
Ray Milkey5a7787a2015-02-23 11:44:30 -080053import org.onosproject.store.Timestamp;
Ray Milkeye6684082014-10-16 16:59:47 -070054
Luca Pretede10c782017-01-05 17:23:08 -080055import java.util.ArrayList;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070056import java.util.Arrays;
57import java.util.Collection;
58import java.util.Collections;
59import java.util.HashSet;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070060import java.util.List;
61import java.util.Objects;
62import java.util.Set;
63import java.util.concurrent.atomic.AtomicLong;
Daniele Moro3a6e1512017-12-22 12:14:44 +010064import java.util.stream.Collectors;
65import java.util.stream.IntStream;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070066
Daniele Moro3a6e1512017-12-22 12:14:44 +010067import static org.onosproject.net.Link.Type.DIRECT;
Thomas Vachuska48e64e42015-09-22 15:32:55 -070068import static org.onosproject.net.NetTestTools.*;
Ray Milkey43a28222015-02-23 13:57:58 -080069
Ray Milkeye6684082014-10-16 16:59:47 -070070/**
71 * Common mocks used by the intent framework tests.
72 */
73public class IntentTestsMocks {
74 /**
75 * Mock traffic selector class used for satisfying API requirements.
76 */
77 public static class MockSelector implements TrafficSelector {
78 @Override
79 public Set<Criterion> criteria() {
80 return new HashSet<>();
81 }
Jonathan Hart936c49d2014-10-23 16:38:59 -070082
83 @Override
84 public Criterion getCriterion(Type type) {
85 return null;
86 }
Ray Milkeye6684082014-10-16 16:59:47 -070087 }
88
89 /**
90 * Mock traffic treatment class used for satisfying API requirements.
91 */
92 public static class MockTreatment implements TrafficTreatment {
93 @Override
alshabib346b5b32015-03-06 00:42:16 -080094 public List<Instruction> deferred() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070095 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -080096 }
97
98 @Override
99 public List<Instruction> immediate() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700100 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -0800101 }
102
103 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700104 public List<Instruction> allInstructions() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700105 return Collections.emptyList();
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700106 }
107
108 @Override
alshabib346b5b32015-03-06 00:42:16 -0800109 public Instructions.TableTypeTransition tableTransition() {
110 return null;
111 }
112
113 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700114 public boolean clearedDeferred() {
115 return false;
alshabib346b5b32015-03-06 00:42:16 -0800116 }
Saurav Das86af8f12015-05-25 23:55:33 -0700117
118 @Override
119 public MetadataInstruction writeMetadata() {
120 return null;
121 }
alshabib10c810b2015-08-18 16:59:04 -0700122
123 @Override
Cem Türker3baff672017-10-12 15:09:01 +0300124 public Instructions.StatTriggerInstruction statTrigger() {
125 return null;
126 }
127
128 @Override
alshabib10c810b2015-08-18 16:59:04 -0700129 public Instructions.MeterInstruction metered() {
130 return null;
131 }
cansu.toprak409289d2017-10-27 10:04:05 +0300132
133 @Override
134 public Set<Instructions.MeterInstruction> meters() {
135 return Sets.newHashSet();
136 }
Ray Milkeye6684082014-10-16 16:59:47 -0700137 }
138
139 /**
140 * Mock path service for creating paths within the test.
141 */
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700142 public static class MockPathService extends PathServiceAdapter {
Ray Milkeye6684082014-10-16 16:59:47 -0700143
144 final String[] pathHops;
145 final String[] reversePathHops;
146
147 /**
148 * Constructor that provides a set of hops to mock.
149 *
150 * @param pathHops path hops to mock
151 */
152 public MockPathService(String[] pathHops) {
153 this.pathHops = pathHops;
154 String[] reversed = pathHops.clone();
155 Collections.reverse(Arrays.asList(reversed));
156 reversePathHops = reversed;
157 }
158
159 @Override
160 public Set<Path> getPaths(ElementId src, ElementId dst) {
161 Set<Path> result = new HashSet<>();
162
163 String[] allHops = new String[pathHops.length];
164
165 if (src.toString().endsWith(pathHops[0])) {
166 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
167 } else {
168 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
169 }
170
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700171 result.add(createPath(src instanceof HostId, dst instanceof HostId, allHops));
Ray Milkeye6684082014-10-16 16:59:47 -0700172 return result;
173 }
174
175 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300176 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeigher weigher) {
177 Set<Path> paths = getPaths(src, dst);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800178
179 for (Path path : paths) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300180 DeviceId srcDevice = path.src().elementId() instanceof DeviceId ? path.src().deviceId() : null;
181 DeviceId dstDevice = path.dst().elementId() instanceof DeviceId ? path.dst().deviceId() : null;
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700182 if (srcDevice != null && dstDevice != null) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300183 TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
184 TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
185 Link link = link(src.toString(), 1, dst.toString(), 1);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800186
Andrey Komarov2398d962016-09-26 15:11:23 +0300187 Weight weightValue = weigher.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
188 if (weightValue.isNegative()) {
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700189 return new HashSet<>();
190 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800191 }
192 }
193 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700194 }
195 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800196
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700197 /**
Daniele Moro3a6e1512017-12-22 12:14:44 +0100198 * Mock path service for creating paths within the test with multiple possible paths.
199 */
200 public static class MockMultiplePathService extends PathServiceAdapter {
201
202 final String[][] pathsHops;
203
204 /**
205 * Constructor that provides a set of hops to mock.
206 *
207 * @param pathHops multiple path hops to mock
208 */
209 public MockMultiplePathService(String[][] pathHops) {
210 this.pathsHops = pathHops;
211 }
212
213 @Override
214 public Set<Path> getPaths(ElementId src, ElementId dst) {
215
216 //Extracts all the paths that goes from src to dst
217 Set<Path> allPaths = new HashSet<>();
218 allPaths.addAll(IntStream.range(0, pathsHops.length)
219 .filter(i -> src.toString().endsWith(pathsHops[i][0])
220 && dst.toString().endsWith(pathsHops[i][pathsHops[i].length - 1]))
221 .mapToObj(i -> createPath(src instanceof HostId,
222 dst instanceof HostId,
223 pathsHops[i]))
224 .collect(Collectors.toSet()));
225
226 // Maintain only the shortest paths
227 int minPathLength = allPaths.stream()
228 .mapToInt(o -> o.links().size())
229 .min()
230 .orElse(Integer.MAX_VALUE);
231 Set<Path> shortestPaths = allPaths.stream()
232 .filter(path -> path.links().size() <= minPathLength)
233 .collect(Collectors.toSet());
234
235 return shortestPaths;
236 }
237
238
239 @Override
240 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeigher weigher) {
241 Set<Path> paths = getPaths(src, dst);
242
243 for (Path path : paths) {
244 DeviceId srcDevice = path.src().elementId() instanceof DeviceId ? path.src().deviceId() : null;
245 DeviceId dstDevice = path.dst().elementId() instanceof DeviceId ? path.dst().deviceId() : null;
246 if (srcDevice != null && dstDevice != null) {
247 TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
248 TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
249 Link link = link(src.toString(), 1, dst.toString(), 1);
250
251 Weight weightValue = weigher.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
252 if (weightValue.isNegative()) {
253 return new HashSet<>();
254 }
255 }
256 }
257 return paths;
258 }
259 }
260
261
262 /**
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700263 * Mock path service for creating paths within the test.
264 *
265 */
Luca Pretede10c782017-01-05 17:23:08 -0800266 public static class Mp2MpMockPathService extends PathServiceAdapter {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700267
268 final String[] pathHops;
269 final String[] reversePathHops;
270
271 /**
272 * Constructor that provides a set of hops to mock.
273 *
274 * @param pathHops path hops to mock
275 */
276 public Mp2MpMockPathService(String[] pathHops) {
277 this.pathHops = pathHops;
278 String[] reversed = pathHops.clone();
279 Collections.reverse(Arrays.asList(reversed));
280 reversePathHops = reversed;
281 }
282
283 @Override
284 public Set<Path> getPaths(ElementId src, ElementId dst) {
285 Set<Path> result = new HashSet<>();
286
287 String[] allHops = new String[pathHops.length + 2];
288 allHops[0] = src.toString();
289 allHops[allHops.length - 1] = dst.toString();
290
291 if (pathHops.length != 0) {
292 System.arraycopy(pathHops, 0, allHops, 1, pathHops.length);
293 }
294
295 result.add(createPath(allHops));
296
297 return result;
298 }
299
300 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300301 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeigher weigher) {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700302 final Set<Path> paths = getPaths(src, dst);
303
304 for (Path path : paths) {
305 final DeviceId srcDevice = path.src().elementId() instanceof DeviceId ? path.src().deviceId() : null;
306 final DeviceId dstDevice = path.dst().elementId() instanceof DeviceId ? path.dst().deviceId() : null;
307 if (srcDevice != null && dstDevice != null) {
308 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
309 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
310 final Link link = link(src.toString(), 1, dst.toString(), 1);
311
Andrey Komarov2398d962016-09-26 15:11:23 +0300312 final Weight weightValue = weigher.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
313 if (weightValue.isNegative()) {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700314 return new HashSet<>();
315 }
316 }
317 }
318 return paths;
319 }
320 }
321
Luca Pretede10c782017-01-05 17:23:08 -0800322 /**
Daniele Moro3a6e1512017-12-22 12:14:44 +0100323 * Mock active and direct link.
324 */
325 public static class FakeLink extends DefaultLink {
326
327 /**
328 * Constructor that provides source and destination of the fake link.
329 *
330 * @param src Source connect point of the fake link
331 * @param dst Destination connect point of the fake link
332 */
333 public FakeLink(ConnectPoint src, ConnectPoint dst) {
334 super(null, src, dst, DIRECT, Link.State.ACTIVE);
335 }
336 }
337
338 /**
Luca Pretede10c782017-01-05 17:23:08 -0800339 * Mock path service for creating paths for MP2SP intent tests, returning
340 * pre-determined paths.
341 */
342 public static class FixedMP2MPMockPathService extends PathServiceAdapter {
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800343
Luca Pretede10c782017-01-05 17:23:08 -0800344 final String[] pathHops;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800345
Luca Pretede10c782017-01-05 17:23:08 -0800346 public static final String DPID_1 = "of:s1";
347 public static final String DPID_2 = "of:s2";
348 public static final String DPID_3 = "of:s3";
349 public static final String DPID_4 = "of:s4";
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800350
Luca Pretede10c782017-01-05 17:23:08 -0800351 /**
352 * Constructor that provides a set of hops to mock.
353 *
354 * @param pathHops path hops to mock
355 */
356 public FixedMP2MPMockPathService(String[] pathHops) {
357 this.pathHops = pathHops;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800358 }
359
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700360 @Override
Luca Pretede10c782017-01-05 17:23:08 -0800361 public Set<Path> getPaths(ElementId src, ElementId dst) {
362 List<Link> links = new ArrayList<>();
363 Set<Path> result = new HashSet<>();
364 ProviderId providerId = new ProviderId("of", "foo");
365 DefaultPath path;
366 if (src.toString().equals(DPID_1) && dst.toString().equals(DPID_4)) {
367 links.add(NetTestTools.linkNoPrefixes(src.toString(), 2, pathHops[0], 1));
368 links.add(NetTestTools.linkNoPrefixes(pathHops[0], 2, dst.toString(), 1));
369 } else if (src.toString().equals(DPID_2) && dst.toString().equals(DPID_4)) {
370 links.add(NetTestTools.linkNoPrefixes(src.toString(), 2, pathHops[0], 3));
371 links.add(NetTestTools.linkNoPrefixes(pathHops[0], 2, dst.toString(), 1));
372 } else if (src.toString().equals(DPID_4) && dst.toString().equals(DPID_1)) {
373 links.add(NetTestTools.linkNoPrefixes(src.toString(), 2, pathHops[0], 1));
374 links.add(NetTestTools.linkNoPrefixes(pathHops[0], 2, dst.toString(), 1));
375 } else if (src.toString().equals(DPID_4) && dst.toString().equals(DPID_2)) {
376 links.add(NetTestTools.linkNoPrefixes(src.toString(), 2, pathHops[0], 1));
377 links.add(NetTestTools.linkNoPrefixes(pathHops[0], 3, dst.toString(), 1));
378 } else {
379 return result;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800380 }
Ray Milkeya7cf8c82018-02-08 15:07:06 -0800381 path = new DefaultPath(providerId, links, ScalarWeight.toWeight(3));
Luca Pretede10c782017-01-05 17:23:08 -0800382 result.add(path);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800383
Luca Pretede10c782017-01-05 17:23:08 -0800384 return result;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800385 }
386
387 @Override
Luca Pretede10c782017-01-05 17:23:08 -0800388 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeigher weigher) {
389 final Set<Path> paths = getPaths(src, dst);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800390
Luca Pretede10c782017-01-05 17:23:08 -0800391 for (Path path : paths) {
392 final DeviceId srcDevice = path.src().elementId() instanceof DeviceId ? path.src().deviceId() : null;
393 final DeviceId dstDevice = path.dst().elementId() instanceof DeviceId ? path.dst().deviceId() : null;
394 if (srcDevice != null && dstDevice != null) {
395 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
396 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
397 final Link link = link(src.toString(), 1, dst.toString(), 1);
398
399 final Weight weightValue = weigher.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
400 if (weightValue.isNegative()) {
401 return new HashSet<>();
402 }
403 }
404 }
405 return paths;
Ray Milkeye97ede92014-11-20 10:43:12 -0800406 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800407 }
408
Daniele Moro3a6e1512017-12-22 12:14:44 +0100409 /**
410 * Mock link service for getting links to check path availability
411 * when a suggested path is submitted.
412 */
413 public static class MockLinkService extends LinkServiceAdapter {
414 final String[][] linksHops;
415
416 /**
417 * Constructor that provides a set of links (as a list of hops).
418 *
419 * @param linksHops links to to mock (link as a set of hops)
420 */
421 public MockLinkService(String[][] linksHops) {
422 this.linksHops = linksHops;
423 }
424
425 @Override
426 public Set<Link> getLinks() {
427 return Arrays.asList(linksHops).stream()
428 .map(path -> createPath(path).links())
429 .flatMap(List::stream)
430 .collect(Collectors.toSet());
431 }
432 @Override
433 public Set<Link> getLinks(ConnectPoint connectPoint) {
434 return getLinks().stream()
435 .filter(link -> link.src().deviceId().equals(connectPoint.deviceId())
436 || link.dst().deviceId().equals(connectPoint.deviceId()))
437 .collect(Collectors.toSet());
438 }
439 }
440
Ray Milkey930fc662014-11-11 16:07:45 -0800441 private static final IntentTestsMocks.MockSelector SELECTOR =
442 new IntentTestsMocks.MockSelector();
443 private static final IntentTestsMocks.MockTreatment TREATMENT =
444 new IntentTestsMocks.MockTreatment();
445
446 public static class MockFlowRule implements FlowRule {
Ray Milkey77a455f2015-03-27 10:08:17 -0700447 static int nextId = 0;
Ray Milkey930fc662014-11-11 16:07:45 -0800448
449 int priority;
Carmelo Cascone41605742017-06-19 15:46:44 +0900450 IndexTableId tableId;
Ray Milkey77a455f2015-03-27 10:08:17 -0700451 long timestamp;
452 int id;
sangho11c30ac2015-01-22 14:30:55 -0800453
Ray Milkey930fc662014-11-11 16:07:45 -0800454 public MockFlowRule(int priority) {
455 this.priority = priority;
Carmelo Cascone41605742017-06-19 15:46:44 +0900456 this.tableId = DEFAULT_TABLE;
Ray Milkey77a455f2015-03-27 10:08:17 -0700457 this.timestamp = System.currentTimeMillis();
458 this.id = nextId++;
Ray Milkey930fc662014-11-11 16:07:45 -0800459 }
460
461 @Override
462 public FlowId id() {
Ray Milkey77a455f2015-03-27 10:08:17 -0700463 return FlowId.valueOf(id);
Ray Milkey930fc662014-11-11 16:07:45 -0800464 }
465
466 @Override
467 public short appId() {
468 return 0;
469 }
470
471 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800472 public GroupId groupId() {
Yi Tsengfa394de2017-02-01 11:26:40 -0800473 return new GroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800474 }
475
476 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800477 public int priority() {
478 return priority;
479 }
480
481 @Override
482 public DeviceId deviceId() {
483 return did("1");
484 }
485
486 @Override
487 public TrafficSelector selector() {
488 return SELECTOR;
489 }
490
491 @Override
492 public TrafficTreatment treatment() {
493 return TREATMENT;
494 }
495
496 @Override
497 public int timeout() {
498 return 0;
499 }
500
501 @Override
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700502 public int hardTimeout() {
503 return 0;
504 }
505
506 @Override
507 public FlowRemoveReason reason() {
508 return FlowRemoveReason.NO_REASON;
509 }
510
511 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800512 public boolean isPermanent() {
513 return false;
514 }
515
Brian O'Connor427a1762014-11-19 18:40:32 -0800516 @Override
517 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700518 return priority;
Brian O'Connor427a1762014-11-19 18:40:32 -0800519 }
Ray Milkey930fc662014-11-11 16:07:45 -0800520
Brian O'Connor427a1762014-11-19 18:40:32 -0800521 @Override
522 public boolean equals(Object obj) {
523 if (this == obj) {
524 return true;
525 }
526 if (obj == null || getClass() != obj.getClass()) {
527 return false;
528 }
529 final MockFlowRule other = (MockFlowRule) obj;
Ray Milkey77a455f2015-03-27 10:08:17 -0700530 return Objects.equals(this.timestamp, other.timestamp) &&
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700531 this.id == other.id;
Brian O'Connor427a1762014-11-19 18:40:32 -0800532 }
sangho11c30ac2015-01-22 14:30:55 -0800533
534 @Override
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700535 public boolean exactMatch(FlowRule rule) {
536 return this.equals(rule);
537 }
538
539 @Override
alshabibdb774072015-04-20 13:13:51 -0700540 public int tableId() {
Carmelo Cascone41605742017-06-19 15:46:44 +0900541 return tableId.id();
542 }
543
544 @Override
545 public TableId table() {
alshabib08d98982015-04-21 16:25:50 -0700546 return tableId;
alshabibdb774072015-04-20 13:13:51 -0700547 }
Ray Milkey930fc662014-11-11 16:07:45 -0800548 }
549
Ray Milkey5a7787a2015-02-23 11:44:30 -0800550 public static class MockIntent extends Intent {
551 private static AtomicLong counter = new AtomicLong(0);
552
553 private final Long number;
554
555 public MockIntent(Long number) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700556 super(NetTestTools.APP_ID, null, Collections.emptyList(),
Ray Milkey39f78b62018-01-05 15:17:37 -0800557 Intent.DEFAULT_INTENT_PRIORITY, null);
Ray Milkey5a7787a2015-02-23 11:44:30 -0800558 this.number = number;
559 }
560
David Glantzdfb3ceb2021-10-04 09:10:20 -0500561 public MockIntent(Long number, ApplicationId appId) {
562 super(appId, null, Collections.emptyList(),
563 Intent.DEFAULT_INTENT_PRIORITY, null);
564 this.number = number;
565 }
566
Ray Milkey43a28222015-02-23 13:57:58 -0800567 public MockIntent(Long number, Collection<NetworkResource> resources) {
Ray Milkey39f78b62018-01-05 15:17:37 -0800568 super(NetTestTools.APP_ID, null, resources, Intent.DEFAULT_INTENT_PRIORITY, null);
Ray Milkey43a28222015-02-23 13:57:58 -0800569 this.number = number;
570 }
571
Ray Milkey5a7787a2015-02-23 11:44:30 -0800572 public Long number() {
573 return number;
574 }
575
576 public static Long nextId() {
577 return counter.getAndIncrement();
578 }
Ray Milkey43a28222015-02-23 13:57:58 -0800579
580 @Override
581 public String toString() {
582 return MoreObjects.toStringHelper(getClass())
583 .add("id", id())
584 .add("appId", appId())
585 .toString();
586 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800587 }
588
589 public static class MockTimestamp implements Timestamp {
590 final int value;
591
592 public MockTimestamp(int value) {
593 this.value = value;
594 }
595
596 @Override
597 public int compareTo(Timestamp o) {
598 if (!(o instanceof MockTimestamp)) {
599 return -1;
600 }
601 MockTimestamp that = (MockTimestamp) o;
Jonathan Hart72175c22015-03-24 18:55:58 -0700602 return this.value - that.value;
Ray Milkey5a7787a2015-02-23 11:44:30 -0800603 }
Yuta HIGUCHI4f8a3772017-05-16 20:23:49 -0700604
605 @Override
606 public int hashCode() {
607 return value;
608 }
609
610 @Override
611 public boolean equals(Object obj) {
612 if (obj instanceof MockTimestamp) {
613 return this.compareTo((MockTimestamp) obj) == 0;
614 }
615 return false;
616 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800617 }
618
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700619 /**
620 * Mocks the device service so that a device appears available in the test.
621 */
622 public static class MockDeviceService extends DeviceServiceAdapter {
623 @Override
624 public boolean isAvailable(DeviceId deviceId) {
625 return true;
626 }
627 }
Ray Milkeye6684082014-10-16 16:59:47 -0700628}