blob: 14201472045d368a638b6fcd3f862aaf1cdda553 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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;
19import com.google.common.collect.ImmutableSet;
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070020import org.onlab.util.Bandwidth;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.core.DefaultGroupId;
22import org.onosproject.core.GroupId;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.ElementId;
25import 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;
29import org.onosproject.net.flow.FlowId;
30import org.onosproject.net.flow.FlowRule;
jcc3d4e14a2015-04-21 11:32:05 +080031import org.onosproject.net.flow.FlowRuleExtPayLoad;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.flow.TrafficSelector;
33import org.onosproject.net.flow.TrafficTreatment;
34import org.onosproject.net.flow.criteria.Criterion;
35import org.onosproject.net.flow.criteria.Criterion.Type;
36import org.onosproject.net.flow.instructions.Instruction;
alshabib346b5b32015-03-06 00:42:16 -080037import org.onosproject.net.flow.instructions.Instructions;
Saurav Das86af8f12015-05-25 23:55:33 -070038import org.onosproject.net.flow.instructions.Instructions.MetadataInstruction;
Thomas Vachuska48e64e42015-09-22 15:32:55 -070039import org.onosproject.net.resource.ResourceAllocation;
40import org.onosproject.net.resource.ResourceRequest;
41import org.onosproject.net.resource.ResourceType;
Brian O'Connor6de2e202015-05-21 14:30:41 -070042import org.onosproject.net.resource.link.BandwidthResource;
43import org.onosproject.net.resource.link.BandwidthResourceRequest;
44import org.onosproject.net.resource.link.LambdaResource;
45import org.onosproject.net.resource.link.LambdaResourceAllocation;
46import org.onosproject.net.resource.link.LambdaResourceRequest;
47import org.onosproject.net.resource.link.LinkResourceAllocations;
48import org.onosproject.net.resource.link.LinkResourceListener;
49import org.onosproject.net.resource.link.LinkResourceRequest;
50import org.onosproject.net.resource.link.LinkResourceService;
51import org.onosproject.net.resource.link.MplsLabel;
52import org.onosproject.net.resource.link.MplsLabelResourceAllocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080053import org.onosproject.net.topology.DefaultTopologyEdge;
54import org.onosproject.net.topology.DefaultTopologyVertex;
55import org.onosproject.net.topology.LinkWeight;
Thomas Vachuska48e64e42015-09-22 15:32:55 -070056import org.onosproject.net.topology.PathServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080057import org.onosproject.net.topology.TopologyVertex;
Ray Milkey5a7787a2015-02-23 11:44:30 -080058import org.onosproject.store.Timestamp;
Ray Milkeye6684082014-10-16 16:59:47 -070059
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070060import java.util.Arrays;
61import java.util.Collection;
62import java.util.Collections;
63import java.util.HashSet;
64import java.util.LinkedList;
65import java.util.List;
66import java.util.Objects;
67import java.util.Set;
68import java.util.concurrent.atomic.AtomicLong;
69
Thomas Vachuska48e64e42015-09-22 15:32:55 -070070import static org.onosproject.net.NetTestTools.*;
Ray Milkey43a28222015-02-23 13:57:58 -080071
Ray Milkeye6684082014-10-16 16:59:47 -070072/**
73 * Common mocks used by the intent framework tests.
74 */
75public class IntentTestsMocks {
76 /**
77 * Mock traffic selector class used for satisfying API requirements.
78 */
79 public static class MockSelector implements TrafficSelector {
80 @Override
81 public Set<Criterion> criteria() {
82 return new HashSet<>();
83 }
Jonathan Hart936c49d2014-10-23 16:38:59 -070084
85 @Override
86 public Criterion getCriterion(Type type) {
87 return null;
88 }
Ray Milkeye6684082014-10-16 16:59:47 -070089 }
90
91 /**
92 * Mock traffic treatment class used for satisfying API requirements.
93 */
94 public static class MockTreatment implements TrafficTreatment {
95 @Override
alshabib346b5b32015-03-06 00:42:16 -080096 public List<Instruction> deferred() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070097 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -080098 }
99
100 @Override
101 public List<Instruction> immediate() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700102 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -0800103 }
104
105 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700106 public List<Instruction> allInstructions() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700107 return Collections.emptyList();
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700108 }
109
110 @Override
alshabib346b5b32015-03-06 00:42:16 -0800111 public Instructions.TableTypeTransition tableTransition() {
112 return null;
113 }
114
115 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700116 public boolean clearedDeferred() {
117 return false;
alshabib346b5b32015-03-06 00:42:16 -0800118 }
Saurav Das86af8f12015-05-25 23:55:33 -0700119
120 @Override
121 public MetadataInstruction writeMetadata() {
122 return null;
123 }
alshabib10c810b2015-08-18 16:59:04 -0700124
125 @Override
126 public Instructions.MeterInstruction metered() {
127 return null;
128 }
Ray Milkeye6684082014-10-16 16:59:47 -0700129 }
130
131 /**
132 * Mock path service for creating paths within the test.
133 */
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700134 public static class MockPathService extends PathServiceAdapter {
Ray Milkeye6684082014-10-16 16:59:47 -0700135
136 final String[] pathHops;
137 final String[] reversePathHops;
138
139 /**
140 * Constructor that provides a set of hops to mock.
141 *
142 * @param pathHops path hops to mock
143 */
144 public MockPathService(String[] pathHops) {
145 this.pathHops = pathHops;
146 String[] reversed = pathHops.clone();
147 Collections.reverse(Arrays.asList(reversed));
148 reversePathHops = reversed;
149 }
150
151 @Override
152 public Set<Path> getPaths(ElementId src, ElementId dst) {
153 Set<Path> result = new HashSet<>();
154
155 String[] allHops = new String[pathHops.length];
156
157 if (src.toString().endsWith(pathHops[0])) {
158 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
159 } else {
160 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
161 }
162
163 result.add(createPath(allHops));
164 return result;
165 }
166
167 @Override
168 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800169 final Set<Path> paths = getPaths(src, dst);
170
171 for (Path path : paths) {
172 final DeviceId srcDevice = path.src().deviceId();
173 final DeviceId dstDevice = path.dst().deviceId();
174 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
175 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
176 final Link link = link(src.toString(), 1, dst.toString(), 1);
177
178 final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
179 if (weightValue < 0) {
180 return new HashSet<>();
181 }
182 }
183 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700184 }
185 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800186
187 public static class MockLinkResourceAllocations implements LinkResourceAllocations {
188 @Override
189 public Set<ResourceAllocation> getResourceAllocation(Link link) {
Ray Milkeyb1b66d32015-03-02 10:27:11 -0800190 return ImmutableSet.of(
Sho SHIMIZU94b7ff42015-05-06 17:51:49 -0700191 new LambdaResourceAllocation(LambdaResource.valueOf(77)),
Ray Milkeyb1b66d32015-03-02 10:27:11 -0800192 new MplsLabelResourceAllocation(MplsLabel.valueOf(10)));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800193 }
194
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700195 @Override
Ayaka Koshibee114f042015-05-01 11:43:00 -0700196 public IntentId intentId() {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800197 return null;
198 }
199
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700200 @Override
Ray Milkey8d3ce432014-11-07 16:21:10 -0800201 public Collection<Link> links() {
202 return null;
203 }
204
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700205 @Override
Ray Milkey8d3ce432014-11-07 16:21:10 -0800206 public Set<ResourceRequest> resources() {
207 return null;
208 }
209
210 @Override
211 public ResourceType type() {
212 return null;
213 }
214 }
215
216 public static class MockedAllocationFailure extends RuntimeException { }
217
218 public static class MockResourceService implements LinkResourceService {
219
220 double availableBandwidth = -1.0;
221 int availableLambda = -1;
222
223 /**
224 * Allocates a resource service that will allow bandwidth allocations
225 * up to a limit.
226 *
227 * @param bandwidth available bandwidth limit
228 * @return resource manager for bandwidth requests
229 */
230 public static MockResourceService makeBandwidthResourceService(double bandwidth) {
231 final MockResourceService result = new MockResourceService();
232 result.availableBandwidth = bandwidth;
233 return result;
234 }
235
236 /**
237 * Allocates a resource service that will allow lambda allocations.
238 *
239 * @param lambda Lambda to return for allocation requests. Currently unused
240 * @return resource manager for lambda requests
241 */
242 public static MockResourceService makeLambdaResourceService(int lambda) {
243 final MockResourceService result = new MockResourceService();
244 result.availableLambda = lambda;
245 return result;
246 }
247
248 public void setAvailableBandwidth(double availableBandwidth) {
249 this.availableBandwidth = availableBandwidth;
250 }
251
252 public void setAvailableLambda(int availableLambda) {
253 this.availableLambda = availableLambda;
254 }
255
256
257 @Override
258 public LinkResourceAllocations requestResources(LinkResourceRequest req) {
259 int lambda = -1;
260 double bandwidth = -1.0;
261
262 for (ResourceRequest resourceRequest : req.resources()) {
263 if (resourceRequest.type() == ResourceType.BANDWIDTH) {
264 final BandwidthResourceRequest brr = (BandwidthResourceRequest) resourceRequest;
265 bandwidth = brr.bandwidth().toDouble();
266 } else if (resourceRequest.type() == ResourceType.LAMBDA) {
267 lambda = 1;
268 }
269 }
270
271 if (availableBandwidth < bandwidth) {
272 throw new MockedAllocationFailure();
273 }
274 if (lambda > 0 && availableLambda == 0) {
275 throw new MockedAllocationFailure();
276 }
277
278 return new IntentTestsMocks.MockLinkResourceAllocations();
279 }
280
281 @Override
282 public void releaseResources(LinkResourceAllocations allocations) {
283 // Mock
284 }
285
286 @Override
287 public LinkResourceAllocations updateResources(LinkResourceRequest req,
288 LinkResourceAllocations oldAllocations) {
289 return null;
290 }
291
292 @Override
293 public Iterable<LinkResourceAllocations> getAllocations() {
Ray Milkeybb320482015-02-25 15:16:46 -0800294 return ImmutableSet.of(
295 new IntentTestsMocks.MockLinkResourceAllocations());
Ray Milkey8d3ce432014-11-07 16:21:10 -0800296 }
297
298 @Override
299 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
Ray Milkeybb320482015-02-25 15:16:46 -0800300 return ImmutableSet.of(
301 new IntentTestsMocks.MockLinkResourceAllocations());
Ray Milkey8d3ce432014-11-07 16:21:10 -0800302 }
303
304 @Override
305 public LinkResourceAllocations getAllocations(IntentId intentId) {
Ray Milkeybb320482015-02-25 15:16:46 -0800306 return new IntentTestsMocks.MockLinkResourceAllocations();
Ray Milkey8d3ce432014-11-07 16:21:10 -0800307 }
308
309 @Override
310 public Iterable<ResourceRequest> getAvailableResources(Link link) {
311 final List<ResourceRequest> result = new LinkedList<>();
312 if (availableBandwidth > 0.0) {
Ray Milkeycf590df2015-02-23 17:43:24 -0800313 result.add(new BandwidthResourceRequest(
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -0700314 new BandwidthResource(Bandwidth.bps(availableBandwidth))));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800315 }
316 if (availableLambda > 0) {
317 result.add(new LambdaResourceRequest());
318 }
319 return result;
320 }
321
322 @Override
weibit00c94f52014-11-16 07:09:05 -0800323 public Iterable<ResourceRequest> getAvailableResources(Link link, LinkResourceAllocations allocations) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800324 return null;
325 }
Ray Milkeye97ede92014-11-20 10:43:12 -0800326
327 @Override
328 public void addListener(LinkResourceListener listener) {
329
330 }
331
332 @Override
333 public void removeListener(LinkResourceListener listener) {
334
335 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800336 }
337
Ray Milkey930fc662014-11-11 16:07:45 -0800338 private static final IntentTestsMocks.MockSelector SELECTOR =
339 new IntentTestsMocks.MockSelector();
340 private static final IntentTestsMocks.MockTreatment TREATMENT =
341 new IntentTestsMocks.MockTreatment();
342
343 public static class MockFlowRule implements FlowRule {
Ray Milkey77a455f2015-03-27 10:08:17 -0700344 static int nextId = 0;
Ray Milkey930fc662014-11-11 16:07:45 -0800345
346 int priority;
alshabib08d98982015-04-21 16:25:50 -0700347 int tableId;
Ray Milkey77a455f2015-03-27 10:08:17 -0700348 long timestamp;
349 int id;
jcc3d4e14a2015-04-21 11:32:05 +0800350 FlowRuleExtPayLoad payLoad;
sangho11c30ac2015-01-22 14:30:55 -0800351
Ray Milkey930fc662014-11-11 16:07:45 -0800352 public MockFlowRule(int priority) {
353 this.priority = priority;
alshabib08d98982015-04-21 16:25:50 -0700354 this.tableId = 0;
Ray Milkey77a455f2015-03-27 10:08:17 -0700355 this.timestamp = System.currentTimeMillis();
356 this.id = nextId++;
jcc3d4e14a2015-04-21 11:32:05 +0800357 this.payLoad = null;
358 }
359
360 public MockFlowRule(int priority, FlowRuleExtPayLoad payLoad) {
361 this.priority = priority;
362 this.timestamp = System.currentTimeMillis();
363 this.id = nextId++;
364 this.payLoad = payLoad;
Ray Milkey930fc662014-11-11 16:07:45 -0800365 }
366
367 @Override
368 public FlowId id() {
Ray Milkey77a455f2015-03-27 10:08:17 -0700369 return FlowId.valueOf(id);
Ray Milkey930fc662014-11-11 16:07:45 -0800370 }
371
372 @Override
373 public short appId() {
374 return 0;
375 }
376
377 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800378 public GroupId groupId() {
379 return new DefaultGroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800380 }
381
382 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800383 public int priority() {
384 return priority;
385 }
386
387 @Override
388 public DeviceId deviceId() {
389 return did("1");
390 }
391
392 @Override
393 public TrafficSelector selector() {
394 return SELECTOR;
395 }
396
397 @Override
398 public TrafficTreatment treatment() {
399 return TREATMENT;
400 }
401
402 @Override
403 public int timeout() {
404 return 0;
405 }
406
407 @Override
408 public boolean isPermanent() {
409 return false;
410 }
411
Brian O'Connor427a1762014-11-19 18:40:32 -0800412 @Override
413 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700414 return priority;
Brian O'Connor427a1762014-11-19 18:40:32 -0800415 }
Ray Milkey930fc662014-11-11 16:07:45 -0800416
Brian O'Connor427a1762014-11-19 18:40:32 -0800417 @Override
418 public boolean equals(Object obj) {
419 if (this == obj) {
420 return true;
421 }
422 if (obj == null || getClass() != obj.getClass()) {
423 return false;
424 }
425 final MockFlowRule other = (MockFlowRule) obj;
Ray Milkey77a455f2015-03-27 10:08:17 -0700426 return Objects.equals(this.timestamp, other.timestamp) &&
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700427 this.id == other.id;
Brian O'Connor427a1762014-11-19 18:40:32 -0800428 }
sangho11c30ac2015-01-22 14:30:55 -0800429
430 @Override
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700431 public boolean exactMatch(FlowRule rule) {
432 return this.equals(rule);
433 }
434
435 @Override
alshabibdb774072015-04-20 13:13:51 -0700436 public int tableId() {
alshabib08d98982015-04-21 16:25:50 -0700437 return tableId;
alshabibdb774072015-04-20 13:13:51 -0700438 }
jcc3d4e14a2015-04-21 11:32:05 +0800439
440 @Override
441 public FlowRuleExtPayLoad payLoad() {
442 return payLoad;
443 }
Ray Milkey930fc662014-11-11 16:07:45 -0800444 }
445
Ray Milkey5a7787a2015-02-23 11:44:30 -0800446 public static class MockIntent extends Intent {
447 private static AtomicLong counter = new AtomicLong(0);
448
449 private final Long number;
450
451 public MockIntent(Long number) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700452 super(NetTestTools.APP_ID, null, Collections.emptyList(),
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700453 Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey5a7787a2015-02-23 11:44:30 -0800454 this.number = number;
455 }
456
Ray Milkey43a28222015-02-23 13:57:58 -0800457 public MockIntent(Long number, Collection<NetworkResource> resources) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700458 super(NetTestTools.APP_ID, null, resources, Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey43a28222015-02-23 13:57:58 -0800459 this.number = number;
460 }
461
Ray Milkey5a7787a2015-02-23 11:44:30 -0800462 public Long number() {
463 return number;
464 }
465
466 public static Long nextId() {
467 return counter.getAndIncrement();
468 }
Ray Milkey43a28222015-02-23 13:57:58 -0800469
470 @Override
471 public String toString() {
472 return MoreObjects.toStringHelper(getClass())
473 .add("id", id())
474 .add("appId", appId())
475 .toString();
476 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800477 }
478
479 public static class MockTimestamp implements Timestamp {
480 final int value;
481
482 public MockTimestamp(int value) {
483 this.value = value;
484 }
485
486 @Override
487 public int compareTo(Timestamp o) {
488 if (!(o instanceof MockTimestamp)) {
489 return -1;
490 }
491 MockTimestamp that = (MockTimestamp) o;
Jonathan Hart72175c22015-03-24 18:55:58 -0700492 return this.value - that.value;
Ray Milkey5a7787a2015-02-23 11:44:30 -0800493 }
494 }
495
Ray Milkeye6684082014-10-16 16:59:47 -0700496}