blob: 931ac72955be38b852eb4f4e674dad7f599ad7a4 [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;
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070019import org.onlab.util.Bandwidth;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.core.DefaultGroupId;
21import org.onosproject.core.GroupId;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.ElementId;
Thomas Vachuskadc91b552016-03-29 14:02:47 -070024import org.onosproject.net.HostId;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.Link;
Ray Milkey5a7787a2015-02-23 11:44:30 -080026import org.onosproject.net.NetTestTools;
Ray Milkey43a28222015-02-23 13:57:58 -080027import org.onosproject.net.NetworkResource;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.Path;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070029import org.onosproject.net.device.DeviceServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.flow.FlowId;
31import org.onosproject.net.flow.FlowRule;
jcc3d4e14a2015-04-21 11:32:05 +080032import org.onosproject.net.flow.FlowRuleExtPayLoad;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.flow.TrafficSelector;
34import org.onosproject.net.flow.TrafficTreatment;
35import org.onosproject.net.flow.criteria.Criterion;
36import org.onosproject.net.flow.criteria.Criterion.Type;
37import org.onosproject.net.flow.instructions.Instruction;
alshabib346b5b32015-03-06 00:42:16 -080038import org.onosproject.net.flow.instructions.Instructions;
Saurav Das86af8f12015-05-25 23:55:33 -070039import org.onosproject.net.flow.instructions.Instructions.MetadataInstruction;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080040import org.onosproject.net.resource.DiscreteResourceId;
41import org.onosproject.net.resource.Resource;
42import org.onosproject.net.resource.ResourceAllocation;
43import org.onosproject.net.resource.ResourceConsumer;
44import org.onosproject.net.resource.ResourceId;
45import org.onosproject.net.resource.ResourceListener;
46import org.onosproject.net.resource.ResourceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.topology.DefaultTopologyEdge;
48import org.onosproject.net.topology.DefaultTopologyVertex;
49import org.onosproject.net.topology.LinkWeight;
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
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070054import java.util.Arrays;
55import java.util.Collection;
56import java.util.Collections;
57import java.util.HashSet;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070058import java.util.List;
59import java.util.Objects;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080060import java.util.Optional;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070061import java.util.Set;
62import java.util.concurrent.atomic.AtomicLong;
63
Thomas Vachuska48e64e42015-09-22 15:32:55 -070064import static org.onosproject.net.NetTestTools.*;
Ray Milkey43a28222015-02-23 13:57:58 -080065
Ray Milkeye6684082014-10-16 16:59:47 -070066/**
67 * Common mocks used by the intent framework tests.
68 */
69public class IntentTestsMocks {
70 /**
71 * Mock traffic selector class used for satisfying API requirements.
72 */
73 public static class MockSelector implements TrafficSelector {
74 @Override
75 public Set<Criterion> criteria() {
76 return new HashSet<>();
77 }
Jonathan Hart936c49d2014-10-23 16:38:59 -070078
79 @Override
80 public Criterion getCriterion(Type type) {
81 return null;
82 }
Ray Milkeye6684082014-10-16 16:59:47 -070083 }
84
85 /**
86 * Mock traffic treatment class used for satisfying API requirements.
87 */
88 public static class MockTreatment implements TrafficTreatment {
89 @Override
alshabib346b5b32015-03-06 00:42:16 -080090 public List<Instruction> deferred() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070091 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -080092 }
93
94 @Override
95 public List<Instruction> immediate() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070096 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -080097 }
98
99 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700100 public List<Instruction> allInstructions() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700101 return Collections.emptyList();
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700102 }
103
104 @Override
alshabib346b5b32015-03-06 00:42:16 -0800105 public Instructions.TableTypeTransition tableTransition() {
106 return null;
107 }
108
109 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700110 public boolean clearedDeferred() {
111 return false;
alshabib346b5b32015-03-06 00:42:16 -0800112 }
Saurav Das86af8f12015-05-25 23:55:33 -0700113
114 @Override
115 public MetadataInstruction writeMetadata() {
116 return null;
117 }
alshabib10c810b2015-08-18 16:59:04 -0700118
119 @Override
120 public Instructions.MeterInstruction metered() {
121 return null;
122 }
Ray Milkeye6684082014-10-16 16:59:47 -0700123 }
124
125 /**
126 * Mock path service for creating paths within the test.
127 */
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700128 public static class MockPathService extends PathServiceAdapter {
Ray Milkeye6684082014-10-16 16:59:47 -0700129
130 final String[] pathHops;
131 final String[] reversePathHops;
132
133 /**
134 * Constructor that provides a set of hops to mock.
135 *
136 * @param pathHops path hops to mock
137 */
138 public MockPathService(String[] pathHops) {
139 this.pathHops = pathHops;
140 String[] reversed = pathHops.clone();
141 Collections.reverse(Arrays.asList(reversed));
142 reversePathHops = reversed;
143 }
144
145 @Override
146 public Set<Path> getPaths(ElementId src, ElementId dst) {
147 Set<Path> result = new HashSet<>();
148
149 String[] allHops = new String[pathHops.length];
150
151 if (src.toString().endsWith(pathHops[0])) {
152 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
153 } else {
154 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
155 }
156
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700157 result.add(createPath(src instanceof HostId, dst instanceof HostId, allHops));
Ray Milkeye6684082014-10-16 16:59:47 -0700158 return result;
159 }
160
161 @Override
162 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800163 final Set<Path> paths = getPaths(src, dst);
164
165 for (Path path : paths) {
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700166 final DeviceId srcDevice = path.src().elementId() instanceof DeviceId ? path.src().deviceId() : null;
167 final DeviceId dstDevice = path.dst().elementId() instanceof DeviceId ? path.dst().deviceId() : null;
168 if (srcDevice != null && dstDevice != null) {
169 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
170 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
171 final Link link = link(src.toString(), 1, dst.toString(), 1);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800172
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700173 final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
174 if (weightValue < 0) {
175 return new HashSet<>();
176 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800177 }
178 }
179 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700180 }
181 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800182
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700183 /**
184 * Mock path service for creating paths within the test.
185 *
186 */
187 public static class Mp2MpMockPathService
188 extends PathServiceAdapter {
189
190 final String[] pathHops;
191 final String[] reversePathHops;
192
193 /**
194 * Constructor that provides a set of hops to mock.
195 *
196 * @param pathHops path hops to mock
197 */
198 public Mp2MpMockPathService(String[] pathHops) {
199 this.pathHops = pathHops;
200 String[] reversed = pathHops.clone();
201 Collections.reverse(Arrays.asList(reversed));
202 reversePathHops = reversed;
203 }
204
205 @Override
206 public Set<Path> getPaths(ElementId src, ElementId dst) {
207 Set<Path> result = new HashSet<>();
208
209 String[] allHops = new String[pathHops.length + 2];
210 allHops[0] = src.toString();
211 allHops[allHops.length - 1] = dst.toString();
212
213 if (pathHops.length != 0) {
214 System.arraycopy(pathHops, 0, allHops, 1, pathHops.length);
215 }
216
217 result.add(createPath(allHops));
218
219 return result;
220 }
221
222 @Override
223 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
224 final Set<Path> paths = getPaths(src, dst);
225
226 for (Path path : paths) {
227 final DeviceId srcDevice = path.src().elementId() instanceof DeviceId ? path.src().deviceId() : null;
228 final DeviceId dstDevice = path.dst().elementId() instanceof DeviceId ? path.dst().deviceId() : null;
229 if (srcDevice != null && dstDevice != null) {
230 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
231 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
232 final Link link = link(src.toString(), 1, dst.toString(), 1);
233
234 final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
235 if (weightValue < 0) {
236 return new HashSet<>();
237 }
238 }
239 }
240 return paths;
241 }
242 }
243
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800244 public static final class MockResourceService implements ResourceService {
245
246 private final double bandwidth;
247
248 public static ResourceService makeBandwidthResourceService(double bandwidth) {
249 return new MockResourceService(bandwidth);
250 }
251
252 private MockResourceService(double bandwidth) {
253 this.bandwidth = bandwidth;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800254 }
255
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700256 @Override
Sho SHIMIZUef835c92016-08-08 13:51:17 -0700257 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<? extends Resource> resources) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800258 return null;
259 }
260
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700261 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800262 public boolean release(List<ResourceAllocation> allocations) {
263 return false;
264 }
265
266 @Override
267 public boolean release(ResourceConsumer consumer) {
268 return false;
269 }
270
271 @Override
272 public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800273 return null;
274 }
275
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700276 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800277 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800278 return null;
279 }
280
281 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800282 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800283 return null;
284 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800285
Ray Milkey8d3ce432014-11-07 16:21:10 -0800286 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800287 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
288 return null;
289 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800290
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800291 @Override
292 public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
293 return null;
294 }
295
296 @Override
297 public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
298 return null;
299 }
300
301 @Override
302 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
303 return null;
304 }
305
306 @Override
307 public boolean isAvailable(Resource resource) {
308 if (!resource.isTypeOf(Bandwidth.class)) {
309 return false;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800310 }
311
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800312 Optional<Double> value = resource.valueAs(Double.class);
313 return value.filter(requested -> requested <= bandwidth).isPresent();
Ray Milkey8d3ce432014-11-07 16:21:10 -0800314 }
315
316 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800317 public void addListener(ResourceListener listener) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800318 }
319
320 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800321 public void removeListener(ResourceListener listener) {
Ray Milkeye97ede92014-11-20 10:43:12 -0800322 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800323 }
324
Ray Milkey930fc662014-11-11 16:07:45 -0800325 private static final IntentTestsMocks.MockSelector SELECTOR =
326 new IntentTestsMocks.MockSelector();
327 private static final IntentTestsMocks.MockTreatment TREATMENT =
328 new IntentTestsMocks.MockTreatment();
329
330 public static class MockFlowRule implements FlowRule {
Ray Milkey77a455f2015-03-27 10:08:17 -0700331 static int nextId = 0;
Ray Milkey930fc662014-11-11 16:07:45 -0800332
333 int priority;
alshabib08d98982015-04-21 16:25:50 -0700334 int tableId;
Ray Milkey77a455f2015-03-27 10:08:17 -0700335 long timestamp;
336 int id;
jcc3d4e14a2015-04-21 11:32:05 +0800337 FlowRuleExtPayLoad payLoad;
sangho11c30ac2015-01-22 14:30:55 -0800338
Ray Milkey930fc662014-11-11 16:07:45 -0800339 public MockFlowRule(int priority) {
340 this.priority = priority;
alshabib08d98982015-04-21 16:25:50 -0700341 this.tableId = 0;
Ray Milkey77a455f2015-03-27 10:08:17 -0700342 this.timestamp = System.currentTimeMillis();
343 this.id = nextId++;
jcc3d4e14a2015-04-21 11:32:05 +0800344 this.payLoad = null;
345 }
346
347 public MockFlowRule(int priority, FlowRuleExtPayLoad payLoad) {
348 this.priority = priority;
349 this.timestamp = System.currentTimeMillis();
350 this.id = nextId++;
351 this.payLoad = payLoad;
Ray Milkey930fc662014-11-11 16:07:45 -0800352 }
353
354 @Override
355 public FlowId id() {
Ray Milkey77a455f2015-03-27 10:08:17 -0700356 return FlowId.valueOf(id);
Ray Milkey930fc662014-11-11 16:07:45 -0800357 }
358
359 @Override
360 public short appId() {
361 return 0;
362 }
363
364 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800365 public GroupId groupId() {
366 return new DefaultGroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800367 }
368
369 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800370 public int priority() {
371 return priority;
372 }
373
374 @Override
375 public DeviceId deviceId() {
376 return did("1");
377 }
378
379 @Override
380 public TrafficSelector selector() {
381 return SELECTOR;
382 }
383
384 @Override
385 public TrafficTreatment treatment() {
386 return TREATMENT;
387 }
388
389 @Override
390 public int timeout() {
391 return 0;
392 }
393
394 @Override
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700395 public int hardTimeout() {
396 return 0;
397 }
398
399 @Override
400 public FlowRemoveReason reason() {
401 return FlowRemoveReason.NO_REASON;
402 }
403
404 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800405 public boolean isPermanent() {
406 return false;
407 }
408
Brian O'Connor427a1762014-11-19 18:40:32 -0800409 @Override
410 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700411 return priority;
Brian O'Connor427a1762014-11-19 18:40:32 -0800412 }
Ray Milkey930fc662014-11-11 16:07:45 -0800413
Brian O'Connor427a1762014-11-19 18:40:32 -0800414 @Override
415 public boolean equals(Object obj) {
416 if (this == obj) {
417 return true;
418 }
419 if (obj == null || getClass() != obj.getClass()) {
420 return false;
421 }
422 final MockFlowRule other = (MockFlowRule) obj;
Ray Milkey77a455f2015-03-27 10:08:17 -0700423 return Objects.equals(this.timestamp, other.timestamp) &&
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700424 this.id == other.id;
Brian O'Connor427a1762014-11-19 18:40:32 -0800425 }
sangho11c30ac2015-01-22 14:30:55 -0800426
427 @Override
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700428 public boolean exactMatch(FlowRule rule) {
429 return this.equals(rule);
430 }
431
432 @Override
alshabibdb774072015-04-20 13:13:51 -0700433 public int tableId() {
alshabib08d98982015-04-21 16:25:50 -0700434 return tableId;
alshabibdb774072015-04-20 13:13:51 -0700435 }
jcc3d4e14a2015-04-21 11:32:05 +0800436
437 @Override
438 public FlowRuleExtPayLoad payLoad() {
439 return payLoad;
440 }
Ray Milkey930fc662014-11-11 16:07:45 -0800441 }
442
Ray Milkey5a7787a2015-02-23 11:44:30 -0800443 public static class MockIntent extends Intent {
444 private static AtomicLong counter = new AtomicLong(0);
445
446 private final Long number;
447
448 public MockIntent(Long number) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700449 super(NetTestTools.APP_ID, null, Collections.emptyList(),
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700450 Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey5a7787a2015-02-23 11:44:30 -0800451 this.number = number;
452 }
453
Ray Milkey43a28222015-02-23 13:57:58 -0800454 public MockIntent(Long number, Collection<NetworkResource> resources) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700455 super(NetTestTools.APP_ID, null, resources, Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey43a28222015-02-23 13:57:58 -0800456 this.number = number;
457 }
458
Ray Milkey5a7787a2015-02-23 11:44:30 -0800459 public Long number() {
460 return number;
461 }
462
463 public static Long nextId() {
464 return counter.getAndIncrement();
465 }
Ray Milkey43a28222015-02-23 13:57:58 -0800466
467 @Override
468 public String toString() {
469 return MoreObjects.toStringHelper(getClass())
470 .add("id", id())
471 .add("appId", appId())
472 .toString();
473 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800474 }
475
476 public static class MockTimestamp implements Timestamp {
477 final int value;
478
479 public MockTimestamp(int value) {
480 this.value = value;
481 }
482
483 @Override
484 public int compareTo(Timestamp o) {
485 if (!(o instanceof MockTimestamp)) {
486 return -1;
487 }
488 MockTimestamp that = (MockTimestamp) o;
Jonathan Hart72175c22015-03-24 18:55:58 -0700489 return this.value - that.value;
Ray Milkey5a7787a2015-02-23 11:44:30 -0800490 }
491 }
492
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700493 /**
494 * Mocks the device service so that a device appears available in the test.
495 */
496 public static class MockDeviceService extends DeviceServiceAdapter {
497 @Override
498 public boolean isAvailable(DeviceId deviceId) {
499 return true;
500 }
501 }
502
Ray Milkeye6684082014-10-16 16:59:47 -0700503}