blob: 2b8c6d710729450c98a15314234747a1da7c78a5 [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;
jcc3d4e14a2015-04-21 11:32:05 +080036import org.onosproject.net.flow.FlowRuleExtPayLoad;
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;
jcc3d4e14a2015-04-21 11:32:05 +0800453 FlowRuleExtPayLoad payLoad;
sangho11c30ac2015-01-22 14:30:55 -0800454
Ray Milkey930fc662014-11-11 16:07:45 -0800455 public MockFlowRule(int priority) {
456 this.priority = priority;
Carmelo Cascone41605742017-06-19 15:46:44 +0900457 this.tableId = DEFAULT_TABLE;
Ray Milkey77a455f2015-03-27 10:08:17 -0700458 this.timestamp = System.currentTimeMillis();
459 this.id = nextId++;
jcc3d4e14a2015-04-21 11:32:05 +0800460 this.payLoad = null;
461 }
462
463 public MockFlowRule(int priority, FlowRuleExtPayLoad payLoad) {
464 this.priority = priority;
465 this.timestamp = System.currentTimeMillis();
466 this.id = nextId++;
467 this.payLoad = payLoad;
Ray Milkey930fc662014-11-11 16:07:45 -0800468 }
469
470 @Override
471 public FlowId id() {
Ray Milkey77a455f2015-03-27 10:08:17 -0700472 return FlowId.valueOf(id);
Ray Milkey930fc662014-11-11 16:07:45 -0800473 }
474
475 @Override
476 public short appId() {
477 return 0;
478 }
479
480 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800481 public GroupId groupId() {
Yi Tsengfa394de2017-02-01 11:26:40 -0800482 return new GroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800483 }
484
485 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800486 public int priority() {
487 return priority;
488 }
489
490 @Override
491 public DeviceId deviceId() {
492 return did("1");
493 }
494
495 @Override
496 public TrafficSelector selector() {
497 return SELECTOR;
498 }
499
500 @Override
501 public TrafficTreatment treatment() {
502 return TREATMENT;
503 }
504
505 @Override
506 public int timeout() {
507 return 0;
508 }
509
510 @Override
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700511 public int hardTimeout() {
512 return 0;
513 }
514
515 @Override
516 public FlowRemoveReason reason() {
517 return FlowRemoveReason.NO_REASON;
518 }
519
520 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800521 public boolean isPermanent() {
522 return false;
523 }
524
Brian O'Connor427a1762014-11-19 18:40:32 -0800525 @Override
526 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700527 return priority;
Brian O'Connor427a1762014-11-19 18:40:32 -0800528 }
Ray Milkey930fc662014-11-11 16:07:45 -0800529
Brian O'Connor427a1762014-11-19 18:40:32 -0800530 @Override
531 public boolean equals(Object obj) {
532 if (this == obj) {
533 return true;
534 }
535 if (obj == null || getClass() != obj.getClass()) {
536 return false;
537 }
538 final MockFlowRule other = (MockFlowRule) obj;
Ray Milkey77a455f2015-03-27 10:08:17 -0700539 return Objects.equals(this.timestamp, other.timestamp) &&
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700540 this.id == other.id;
Brian O'Connor427a1762014-11-19 18:40:32 -0800541 }
sangho11c30ac2015-01-22 14:30:55 -0800542
543 @Override
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700544 public boolean exactMatch(FlowRule rule) {
545 return this.equals(rule);
546 }
547
548 @Override
alshabibdb774072015-04-20 13:13:51 -0700549 public int tableId() {
Carmelo Cascone41605742017-06-19 15:46:44 +0900550 return tableId.id();
551 }
552
553 @Override
554 public TableId table() {
alshabib08d98982015-04-21 16:25:50 -0700555 return tableId;
alshabibdb774072015-04-20 13:13:51 -0700556 }
jcc3d4e14a2015-04-21 11:32:05 +0800557
558 @Override
559 public FlowRuleExtPayLoad payLoad() {
560 return payLoad;
561 }
Ray Milkey930fc662014-11-11 16:07:45 -0800562 }
563
Ray Milkey5a7787a2015-02-23 11:44:30 -0800564 public static class MockIntent extends Intent {
565 private static AtomicLong counter = new AtomicLong(0);
566
567 private final Long number;
568
569 public MockIntent(Long number) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700570 super(NetTestTools.APP_ID, null, Collections.emptyList(),
Ray Milkey39f78b62018-01-05 15:17:37 -0800571 Intent.DEFAULT_INTENT_PRIORITY, null);
Ray Milkey5a7787a2015-02-23 11:44:30 -0800572 this.number = number;
573 }
574
Ray Milkey43a28222015-02-23 13:57:58 -0800575 public MockIntent(Long number, Collection<NetworkResource> resources) {
Ray Milkey39f78b62018-01-05 15:17:37 -0800576 super(NetTestTools.APP_ID, null, resources, Intent.DEFAULT_INTENT_PRIORITY, null);
Ray Milkey43a28222015-02-23 13:57:58 -0800577 this.number = number;
578 }
579
Ray Milkey5a7787a2015-02-23 11:44:30 -0800580 public Long number() {
581 return number;
582 }
583
584 public static Long nextId() {
585 return counter.getAndIncrement();
586 }
Ray Milkey43a28222015-02-23 13:57:58 -0800587
588 @Override
589 public String toString() {
590 return MoreObjects.toStringHelper(getClass())
591 .add("id", id())
592 .add("appId", appId())
593 .toString();
594 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800595 }
596
597 public static class MockTimestamp implements Timestamp {
598 final int value;
599
600 public MockTimestamp(int value) {
601 this.value = value;
602 }
603
604 @Override
605 public int compareTo(Timestamp o) {
606 if (!(o instanceof MockTimestamp)) {
607 return -1;
608 }
609 MockTimestamp that = (MockTimestamp) o;
Jonathan Hart72175c22015-03-24 18:55:58 -0700610 return this.value - that.value;
Ray Milkey5a7787a2015-02-23 11:44:30 -0800611 }
Yuta HIGUCHI4f8a3772017-05-16 20:23:49 -0700612
613 @Override
614 public int hashCode() {
615 return value;
616 }
617
618 @Override
619 public boolean equals(Object obj) {
620 if (obj instanceof MockTimestamp) {
621 return this.compareTo((MockTimestamp) obj) == 0;
622 }
623 return false;
624 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800625 }
626
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700627 /**
628 * Mocks the device service so that a device appears available in the test.
629 */
630 public static class MockDeviceService extends DeviceServiceAdapter {
631 @Override
632 public boolean isAvailable(DeviceId deviceId) {
633 return true;
634 }
635 }
Ray Milkeye6684082014-10-16 16:59:47 -0700636}