blob: e47d1aa4842e0ebd0b1c2cd5198885461ad0a7e9 [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;
jcc3d4e14a2015-04-21 11:32:05 +080020
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070021import org.onlab.util.Bandwidth;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.core.DefaultGroupId;
23import org.onosproject.core.GroupId;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.ElementId;
26import org.onosproject.net.Link;
Ray Milkey5a7787a2015-02-23 11:44:30 -080027import org.onosproject.net.NetTestTools;
Ray Milkey43a28222015-02-23 13:57:58 -080028import org.onosproject.net.NetworkResource;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.Path;
30import 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;
Brian O'Connor6de2e202015-05-21 14:30:41 -070040import org.onosproject.net.resource.link.BandwidthResource;
41import org.onosproject.net.resource.link.BandwidthResourceRequest;
42import org.onosproject.net.resource.link.LambdaResource;
43import org.onosproject.net.resource.link.LambdaResourceAllocation;
44import org.onosproject.net.resource.link.LambdaResourceRequest;
45import org.onosproject.net.resource.link.LinkResourceAllocations;
46import org.onosproject.net.resource.link.LinkResourceListener;
47import org.onosproject.net.resource.link.LinkResourceRequest;
48import org.onosproject.net.resource.link.LinkResourceService;
49import org.onosproject.net.resource.link.MplsLabel;
50import org.onosproject.net.resource.link.MplsLabelResourceAllocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import org.onosproject.net.resource.ResourceAllocation;
52import org.onosproject.net.resource.ResourceRequest;
53import org.onosproject.net.resource.ResourceType;
54import org.onosproject.net.topology.DefaultTopologyEdge;
55import org.onosproject.net.topology.DefaultTopologyVertex;
56import org.onosproject.net.topology.LinkWeight;
57import org.onosproject.net.topology.PathService;
58import org.onosproject.net.topology.TopologyVertex;
Ray Milkey5a7787a2015-02-23 11:44:30 -080059import org.onosproject.store.Timestamp;
Ray Milkeye6684082014-10-16 16:59:47 -070060
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070061import java.util.Arrays;
62import java.util.Collection;
63import java.util.Collections;
64import java.util.HashSet;
65import java.util.LinkedList;
66import java.util.List;
67import java.util.Objects;
68import java.util.Set;
69import java.util.concurrent.atomic.AtomicLong;
70
71import static org.onosproject.net.NetTestTools.createPath;
72import static org.onosproject.net.NetTestTools.did;
73import static org.onosproject.net.NetTestTools.link;
Ray Milkey43a28222015-02-23 13:57:58 -080074
Ray Milkeye6684082014-10-16 16:59:47 -070075/**
76 * Common mocks used by the intent framework tests.
77 */
78public class IntentTestsMocks {
79 /**
80 * Mock traffic selector class used for satisfying API requirements.
81 */
82 public static class MockSelector implements TrafficSelector {
83 @Override
84 public Set<Criterion> criteria() {
85 return new HashSet<>();
86 }
Jonathan Hart936c49d2014-10-23 16:38:59 -070087
88 @Override
89 public Criterion getCriterion(Type type) {
90 return null;
91 }
Ray Milkeye6684082014-10-16 16:59:47 -070092 }
93
94 /**
95 * Mock traffic treatment class used for satisfying API requirements.
96 */
97 public static class MockTreatment implements TrafficTreatment {
98 @Override
alshabib346b5b32015-03-06 00:42:16 -080099 public List<Instruction> deferred() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700100 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -0800101 }
102
103 @Override
104 public List<Instruction> immediate() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700105 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -0800106 }
107
108 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700109 public List<Instruction> allInstructions() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700110 return Collections.emptyList();
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700111 }
112
113 @Override
alshabib346b5b32015-03-06 00:42:16 -0800114 public Instructions.TableTypeTransition tableTransition() {
115 return null;
116 }
117
118 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700119 public boolean clearedDeferred() {
120 return false;
alshabib346b5b32015-03-06 00:42:16 -0800121 }
Saurav Das86af8f12015-05-25 23:55:33 -0700122
123 @Override
124 public MetadataInstruction writeMetadata() {
125 return null;
126 }
Ray Milkeye6684082014-10-16 16:59:47 -0700127 }
128
129 /**
130 * Mock path service for creating paths within the test.
131 */
132 public static class MockPathService implements PathService {
133
134 final String[] pathHops;
135 final String[] reversePathHops;
136
137 /**
138 * Constructor that provides a set of hops to mock.
139 *
140 * @param pathHops path hops to mock
141 */
142 public MockPathService(String[] pathHops) {
143 this.pathHops = pathHops;
144 String[] reversed = pathHops.clone();
145 Collections.reverse(Arrays.asList(reversed));
146 reversePathHops = reversed;
147 }
148
149 @Override
150 public Set<Path> getPaths(ElementId src, ElementId dst) {
151 Set<Path> result = new HashSet<>();
152
153 String[] allHops = new String[pathHops.length];
154
155 if (src.toString().endsWith(pathHops[0])) {
156 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
157 } else {
158 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
159 }
160
161 result.add(createPath(allHops));
162 return result;
163 }
164
165 @Override
166 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800167 final Set<Path> paths = getPaths(src, dst);
168
169 for (Path path : paths) {
170 final DeviceId srcDevice = path.src().deviceId();
171 final DeviceId dstDevice = path.dst().deviceId();
172 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
173 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
174 final Link link = link(src.toString(), 1, dst.toString(), 1);
175
176 final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
177 if (weightValue < 0) {
178 return new HashSet<>();
179 }
180 }
181 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700182 }
183 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800184
185 public static class MockLinkResourceAllocations implements LinkResourceAllocations {
186 @Override
187 public Set<ResourceAllocation> getResourceAllocation(Link link) {
Ray Milkeyb1b66d32015-03-02 10:27:11 -0800188 return ImmutableSet.of(
Sho SHIMIZU94b7ff42015-05-06 17:51:49 -0700189 new LambdaResourceAllocation(LambdaResource.valueOf(77)),
Ray Milkeyb1b66d32015-03-02 10:27:11 -0800190 new MplsLabelResourceAllocation(MplsLabel.valueOf(10)));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800191 }
192
193 @Override
Ayaka Koshibee114f042015-05-01 11:43:00 -0700194 public IntentId intentId() {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800195 return null;
196 }
197
198 @Override
199 public Collection<Link> links() {
200 return null;
201 }
202
203 @Override
204 public Set<ResourceRequest> resources() {
205 return null;
206 }
207
208 @Override
209 public ResourceType type() {
210 return null;
211 }
212 }
213
214 public static class MockedAllocationFailure extends RuntimeException { }
215
216 public static class MockResourceService implements LinkResourceService {
217
218 double availableBandwidth = -1.0;
219 int availableLambda = -1;
220
221 /**
222 * Allocates a resource service that will allow bandwidth allocations
223 * up to a limit.
224 *
225 * @param bandwidth available bandwidth limit
226 * @return resource manager for bandwidth requests
227 */
228 public static MockResourceService makeBandwidthResourceService(double bandwidth) {
229 final MockResourceService result = new MockResourceService();
230 result.availableBandwidth = bandwidth;
231 return result;
232 }
233
234 /**
235 * Allocates a resource service that will allow lambda allocations.
236 *
237 * @param lambda Lambda to return for allocation requests. Currently unused
238 * @return resource manager for lambda requests
239 */
240 public static MockResourceService makeLambdaResourceService(int lambda) {
241 final MockResourceService result = new MockResourceService();
242 result.availableLambda = lambda;
243 return result;
244 }
245
246 public void setAvailableBandwidth(double availableBandwidth) {
247 this.availableBandwidth = availableBandwidth;
248 }
249
250 public void setAvailableLambda(int availableLambda) {
251 this.availableLambda = availableLambda;
252 }
253
254
255 @Override
256 public LinkResourceAllocations requestResources(LinkResourceRequest req) {
257 int lambda = -1;
258 double bandwidth = -1.0;
259
260 for (ResourceRequest resourceRequest : req.resources()) {
261 if (resourceRequest.type() == ResourceType.BANDWIDTH) {
262 final BandwidthResourceRequest brr = (BandwidthResourceRequest) resourceRequest;
263 bandwidth = brr.bandwidth().toDouble();
264 } else if (resourceRequest.type() == ResourceType.LAMBDA) {
265 lambda = 1;
266 }
267 }
268
269 if (availableBandwidth < bandwidth) {
270 throw new MockedAllocationFailure();
271 }
272 if (lambda > 0 && availableLambda == 0) {
273 throw new MockedAllocationFailure();
274 }
275
276 return new IntentTestsMocks.MockLinkResourceAllocations();
277 }
278
279 @Override
280 public void releaseResources(LinkResourceAllocations allocations) {
281 // Mock
282 }
283
284 @Override
285 public LinkResourceAllocations updateResources(LinkResourceRequest req,
286 LinkResourceAllocations oldAllocations) {
287 return null;
288 }
289
290 @Override
291 public Iterable<LinkResourceAllocations> getAllocations() {
Ray Milkeybb320482015-02-25 15:16:46 -0800292 return ImmutableSet.of(
293 new IntentTestsMocks.MockLinkResourceAllocations());
Ray Milkey8d3ce432014-11-07 16:21:10 -0800294 }
295
296 @Override
297 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
Ray Milkeybb320482015-02-25 15:16:46 -0800298 return ImmutableSet.of(
299 new IntentTestsMocks.MockLinkResourceAllocations());
Ray Milkey8d3ce432014-11-07 16:21:10 -0800300 }
301
302 @Override
303 public LinkResourceAllocations getAllocations(IntentId intentId) {
Ray Milkeybb320482015-02-25 15:16:46 -0800304 return new IntentTestsMocks.MockLinkResourceAllocations();
Ray Milkey8d3ce432014-11-07 16:21:10 -0800305 }
306
307 @Override
308 public Iterable<ResourceRequest> getAvailableResources(Link link) {
309 final List<ResourceRequest> result = new LinkedList<>();
310 if (availableBandwidth > 0.0) {
Ray Milkeycf590df2015-02-23 17:43:24 -0800311 result.add(new BandwidthResourceRequest(
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -0700312 new BandwidthResource(Bandwidth.bps(availableBandwidth))));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800313 }
314 if (availableLambda > 0) {
315 result.add(new LambdaResourceRequest());
316 }
317 return result;
318 }
319
320 @Override
weibit00c94f52014-11-16 07:09:05 -0800321 public Iterable<ResourceRequest> getAvailableResources(Link link, LinkResourceAllocations allocations) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800322 return null;
323 }
Ray Milkeye97ede92014-11-20 10:43:12 -0800324
325 @Override
326 public void addListener(LinkResourceListener listener) {
327
328 }
329
330 @Override
331 public void removeListener(LinkResourceListener listener) {
332
333 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800334 }
335
Ray Milkey930fc662014-11-11 16:07:45 -0800336 private static final IntentTestsMocks.MockSelector SELECTOR =
337 new IntentTestsMocks.MockSelector();
338 private static final IntentTestsMocks.MockTreatment TREATMENT =
339 new IntentTestsMocks.MockTreatment();
340
341 public static class MockFlowRule implements FlowRule {
Ray Milkey77a455f2015-03-27 10:08:17 -0700342 static int nextId = 0;
Ray Milkey930fc662014-11-11 16:07:45 -0800343
344 int priority;
alshabib08d98982015-04-21 16:25:50 -0700345 int tableId;
Ray Milkey77a455f2015-03-27 10:08:17 -0700346 long timestamp;
347 int id;
jcc3d4e14a2015-04-21 11:32:05 +0800348 FlowRuleExtPayLoad payLoad;
sangho11c30ac2015-01-22 14:30:55 -0800349
Ray Milkey930fc662014-11-11 16:07:45 -0800350 public MockFlowRule(int priority) {
351 this.priority = priority;
alshabib08d98982015-04-21 16:25:50 -0700352 this.tableId = 0;
Ray Milkey77a455f2015-03-27 10:08:17 -0700353 this.timestamp = System.currentTimeMillis();
354 this.id = nextId++;
jcc3d4e14a2015-04-21 11:32:05 +0800355 this.payLoad = null;
356 }
357
358 public MockFlowRule(int priority, FlowRuleExtPayLoad payLoad) {
359 this.priority = priority;
360 this.timestamp = System.currentTimeMillis();
361 this.id = nextId++;
362 this.payLoad = payLoad;
Ray Milkey930fc662014-11-11 16:07:45 -0800363 }
364
365 @Override
366 public FlowId id() {
Ray Milkey77a455f2015-03-27 10:08:17 -0700367 return FlowId.valueOf(id);
Ray Milkey930fc662014-11-11 16:07:45 -0800368 }
369
370 @Override
371 public short appId() {
372 return 0;
373 }
374
375 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800376 public GroupId groupId() {
377 return new DefaultGroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800378 }
379
380 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800381 public int priority() {
382 return priority;
383 }
384
385 @Override
386 public DeviceId deviceId() {
387 return did("1");
388 }
389
390 @Override
391 public TrafficSelector selector() {
392 return SELECTOR;
393 }
394
395 @Override
396 public TrafficTreatment treatment() {
397 return TREATMENT;
398 }
399
400 @Override
401 public int timeout() {
402 return 0;
403 }
404
405 @Override
406 public boolean isPermanent() {
407 return false;
408 }
409
Brian O'Connor427a1762014-11-19 18:40:32 -0800410 @Override
411 public int hashCode() {
412 return Objects.hash(priority);
413 }
Ray Milkey930fc662014-11-11 16:07:45 -0800414
Brian O'Connor427a1762014-11-19 18:40:32 -0800415 @Override
416 public boolean equals(Object obj) {
417 if (this == obj) {
418 return true;
419 }
420 if (obj == null || getClass() != obj.getClass()) {
421 return false;
422 }
423 final MockFlowRule other = (MockFlowRule) obj;
Ray Milkey77a455f2015-03-27 10:08:17 -0700424 return Objects.equals(this.timestamp, other.timestamp) &&
425 this.id == other.id;
Brian O'Connor427a1762014-11-19 18:40:32 -0800426 }
sangho11c30ac2015-01-22 14:30:55 -0800427
428 @Override
alshabibdb774072015-04-20 13:13:51 -0700429 public int tableId() {
alshabib08d98982015-04-21 16:25:50 -0700430 return tableId;
alshabibdb774072015-04-20 13:13:51 -0700431 }
jcc3d4e14a2015-04-21 11:32:05 +0800432
433 @Override
434 public FlowRuleExtPayLoad payLoad() {
435 return payLoad;
436 }
Ray Milkey930fc662014-11-11 16:07:45 -0800437 }
438
Ray Milkey5a7787a2015-02-23 11:44:30 -0800439 public static class MockIntent extends Intent {
440 private static AtomicLong counter = new AtomicLong(0);
441
442 private final Long number;
443
444 public MockIntent(Long number) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700445 super(NetTestTools.APP_ID, null, Collections.emptyList(),
446 Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey5a7787a2015-02-23 11:44:30 -0800447 this.number = number;
448 }
449
Ray Milkey43a28222015-02-23 13:57:58 -0800450 public MockIntent(Long number, Collection<NetworkResource> resources) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700451 super(NetTestTools.APP_ID, null, resources, Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey43a28222015-02-23 13:57:58 -0800452 this.number = number;
453 }
454
Ray Milkey5a7787a2015-02-23 11:44:30 -0800455 public Long number() {
456 return number;
457 }
458
459 public static Long nextId() {
460 return counter.getAndIncrement();
461 }
Ray Milkey43a28222015-02-23 13:57:58 -0800462
463 @Override
464 public String toString() {
465 return MoreObjects.toStringHelper(getClass())
466 .add("id", id())
467 .add("appId", appId())
468 .toString();
469 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800470 }
471
472 public static class MockTimestamp implements Timestamp {
473 final int value;
474
475 public MockTimestamp(int value) {
476 this.value = value;
477 }
478
479 @Override
480 public int compareTo(Timestamp o) {
481 if (!(o instanceof MockTimestamp)) {
482 return -1;
483 }
484 MockTimestamp that = (MockTimestamp) o;
Jonathan Hart72175c22015-03-24 18:55:58 -0700485 return this.value - that.value;
Ray Milkey5a7787a2015-02-23 11:44:30 -0800486 }
487 }
488
Ray Milkeye6684082014-10-16 16:59:47 -0700489}