blob: 6c7fe1371eb9f631986085cd124cc368a2b2ebcc [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;
Brian O'Connor6de2e202015-05-21 14:30:41 -070039import org.onosproject.net.resource.link.BandwidthResource;
40import org.onosproject.net.resource.link.BandwidthResourceRequest;
41import org.onosproject.net.resource.link.LambdaResource;
42import org.onosproject.net.resource.link.LambdaResourceAllocation;
43import org.onosproject.net.resource.link.LambdaResourceRequest;
44import org.onosproject.net.resource.link.LinkResourceAllocations;
45import org.onosproject.net.resource.link.LinkResourceListener;
46import org.onosproject.net.resource.link.LinkResourceRequest;
47import org.onosproject.net.resource.link.LinkResourceService;
48import org.onosproject.net.resource.link.MplsLabel;
49import org.onosproject.net.resource.link.MplsLabelResourceAllocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import org.onosproject.net.resource.ResourceAllocation;
51import org.onosproject.net.resource.ResourceRequest;
52import org.onosproject.net.resource.ResourceType;
53import org.onosproject.net.topology.DefaultTopologyEdge;
54import org.onosproject.net.topology.DefaultTopologyVertex;
55import org.onosproject.net.topology.LinkWeight;
56import org.onosproject.net.topology.PathService;
57import 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
70import static org.onosproject.net.NetTestTools.createPath;
71import static org.onosproject.net.NetTestTools.did;
72import static org.onosproject.net.NetTestTools.link;
Ray Milkey43a28222015-02-23 13:57:58 -080073
Ray Milkeye6684082014-10-16 16:59:47 -070074/**
75 * Common mocks used by the intent framework tests.
76 */
77public class IntentTestsMocks {
78 /**
79 * Mock traffic selector class used for satisfying API requirements.
80 */
81 public static class MockSelector implements TrafficSelector {
82 @Override
83 public Set<Criterion> criteria() {
84 return new HashSet<>();
85 }
Jonathan Hart936c49d2014-10-23 16:38:59 -070086
87 @Override
88 public Criterion getCriterion(Type type) {
89 return null;
90 }
Ray Milkeye6684082014-10-16 16:59:47 -070091 }
92
93 /**
94 * Mock traffic treatment class used for satisfying API requirements.
95 */
96 public static class MockTreatment implements TrafficTreatment {
97 @Override
alshabib346b5b32015-03-06 00:42:16 -080098 public List<Instruction> deferred() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070099 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -0800100 }
101
102 @Override
103 public List<Instruction> immediate() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700104 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -0800105 }
106
107 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700108 public List<Instruction> allInstructions() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700109 return Collections.emptyList();
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700110 }
111
112 @Override
alshabib346b5b32015-03-06 00:42:16 -0800113 public Instructions.TableTypeTransition tableTransition() {
114 return null;
115 }
116
117 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700118 public boolean clearedDeferred() {
119 return false;
alshabib346b5b32015-03-06 00:42:16 -0800120 }
Ray Milkeye6684082014-10-16 16:59:47 -0700121 }
122
123 /**
124 * Mock path service for creating paths within the test.
125 */
126 public static class MockPathService implements PathService {
127
128 final String[] pathHops;
129 final String[] reversePathHops;
130
131 /**
132 * Constructor that provides a set of hops to mock.
133 *
134 * @param pathHops path hops to mock
135 */
136 public MockPathService(String[] pathHops) {
137 this.pathHops = pathHops;
138 String[] reversed = pathHops.clone();
139 Collections.reverse(Arrays.asList(reversed));
140 reversePathHops = reversed;
141 }
142
143 @Override
144 public Set<Path> getPaths(ElementId src, ElementId dst) {
145 Set<Path> result = new HashSet<>();
146
147 String[] allHops = new String[pathHops.length];
148
149 if (src.toString().endsWith(pathHops[0])) {
150 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
151 } else {
152 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
153 }
154
155 result.add(createPath(allHops));
156 return result;
157 }
158
159 @Override
160 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800161 final Set<Path> paths = getPaths(src, dst);
162
163 for (Path path : paths) {
164 final DeviceId srcDevice = path.src().deviceId();
165 final DeviceId dstDevice = path.dst().deviceId();
166 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
167 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
168 final Link link = link(src.toString(), 1, dst.toString(), 1);
169
170 final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
171 if (weightValue < 0) {
172 return new HashSet<>();
173 }
174 }
175 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700176 }
177 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800178
179 public static class MockLinkResourceAllocations implements LinkResourceAllocations {
180 @Override
181 public Set<ResourceAllocation> getResourceAllocation(Link link) {
Ray Milkeyb1b66d32015-03-02 10:27:11 -0800182 return ImmutableSet.of(
Sho SHIMIZU94b7ff42015-05-06 17:51:49 -0700183 new LambdaResourceAllocation(LambdaResource.valueOf(77)),
Ray Milkeyb1b66d32015-03-02 10:27:11 -0800184 new MplsLabelResourceAllocation(MplsLabel.valueOf(10)));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800185 }
186
187 @Override
Ayaka Koshibee114f042015-05-01 11:43:00 -0700188 public IntentId intentId() {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800189 return null;
190 }
191
192 @Override
193 public Collection<Link> links() {
194 return null;
195 }
196
197 @Override
198 public Set<ResourceRequest> resources() {
199 return null;
200 }
201
202 @Override
203 public ResourceType type() {
204 return null;
205 }
206 }
207
208 public static class MockedAllocationFailure extends RuntimeException { }
209
210 public static class MockResourceService implements LinkResourceService {
211
212 double availableBandwidth = -1.0;
213 int availableLambda = -1;
214
215 /**
216 * Allocates a resource service that will allow bandwidth allocations
217 * up to a limit.
218 *
219 * @param bandwidth available bandwidth limit
220 * @return resource manager for bandwidth requests
221 */
222 public static MockResourceService makeBandwidthResourceService(double bandwidth) {
223 final MockResourceService result = new MockResourceService();
224 result.availableBandwidth = bandwidth;
225 return result;
226 }
227
228 /**
229 * Allocates a resource service that will allow lambda allocations.
230 *
231 * @param lambda Lambda to return for allocation requests. Currently unused
232 * @return resource manager for lambda requests
233 */
234 public static MockResourceService makeLambdaResourceService(int lambda) {
235 final MockResourceService result = new MockResourceService();
236 result.availableLambda = lambda;
237 return result;
238 }
239
240 public void setAvailableBandwidth(double availableBandwidth) {
241 this.availableBandwidth = availableBandwidth;
242 }
243
244 public void setAvailableLambda(int availableLambda) {
245 this.availableLambda = availableLambda;
246 }
247
248
249 @Override
250 public LinkResourceAllocations requestResources(LinkResourceRequest req) {
251 int lambda = -1;
252 double bandwidth = -1.0;
253
254 for (ResourceRequest resourceRequest : req.resources()) {
255 if (resourceRequest.type() == ResourceType.BANDWIDTH) {
256 final BandwidthResourceRequest brr = (BandwidthResourceRequest) resourceRequest;
257 bandwidth = brr.bandwidth().toDouble();
258 } else if (resourceRequest.type() == ResourceType.LAMBDA) {
259 lambda = 1;
260 }
261 }
262
263 if (availableBandwidth < bandwidth) {
264 throw new MockedAllocationFailure();
265 }
266 if (lambda > 0 && availableLambda == 0) {
267 throw new MockedAllocationFailure();
268 }
269
270 return new IntentTestsMocks.MockLinkResourceAllocations();
271 }
272
273 @Override
274 public void releaseResources(LinkResourceAllocations allocations) {
275 // Mock
276 }
277
278 @Override
279 public LinkResourceAllocations updateResources(LinkResourceRequest req,
280 LinkResourceAllocations oldAllocations) {
281 return null;
282 }
283
284 @Override
285 public Iterable<LinkResourceAllocations> getAllocations() {
Ray Milkeybb320482015-02-25 15:16:46 -0800286 return ImmutableSet.of(
287 new IntentTestsMocks.MockLinkResourceAllocations());
Ray Milkey8d3ce432014-11-07 16:21:10 -0800288 }
289
290 @Override
291 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
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 LinkResourceAllocations getAllocations(IntentId intentId) {
Ray Milkeybb320482015-02-25 15:16:46 -0800298 return new IntentTestsMocks.MockLinkResourceAllocations();
Ray Milkey8d3ce432014-11-07 16:21:10 -0800299 }
300
301 @Override
302 public Iterable<ResourceRequest> getAvailableResources(Link link) {
303 final List<ResourceRequest> result = new LinkedList<>();
304 if (availableBandwidth > 0.0) {
Ray Milkeycf590df2015-02-23 17:43:24 -0800305 result.add(new BandwidthResourceRequest(
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -0700306 new BandwidthResource(Bandwidth.bps(availableBandwidth))));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800307 }
308 if (availableLambda > 0) {
309 result.add(new LambdaResourceRequest());
310 }
311 return result;
312 }
313
314 @Override
weibit00c94f52014-11-16 07:09:05 -0800315 public Iterable<ResourceRequest> getAvailableResources(Link link, LinkResourceAllocations allocations) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800316 return null;
317 }
Ray Milkeye97ede92014-11-20 10:43:12 -0800318
319 @Override
320 public void addListener(LinkResourceListener listener) {
321
322 }
323
324 @Override
325 public void removeListener(LinkResourceListener listener) {
326
327 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800328 }
329
Ray Milkey930fc662014-11-11 16:07:45 -0800330 private static final IntentTestsMocks.MockSelector SELECTOR =
331 new IntentTestsMocks.MockSelector();
332 private static final IntentTestsMocks.MockTreatment TREATMENT =
333 new IntentTestsMocks.MockTreatment();
334
335 public static class MockFlowRule implements FlowRule {
Ray Milkey77a455f2015-03-27 10:08:17 -0700336 static int nextId = 0;
Ray Milkey930fc662014-11-11 16:07:45 -0800337
338 int priority;
alshabib08d98982015-04-21 16:25:50 -0700339 int tableId;
Ray Milkey77a455f2015-03-27 10:08:17 -0700340 long timestamp;
341 int id;
jcc3d4e14a2015-04-21 11:32:05 +0800342 FlowRuleExtPayLoad payLoad;
sangho11c30ac2015-01-22 14:30:55 -0800343
Ray Milkey930fc662014-11-11 16:07:45 -0800344 public MockFlowRule(int priority) {
345 this.priority = priority;
alshabib08d98982015-04-21 16:25:50 -0700346 this.tableId = 0;
Ray Milkey77a455f2015-03-27 10:08:17 -0700347 this.timestamp = System.currentTimeMillis();
348 this.id = nextId++;
jcc3d4e14a2015-04-21 11:32:05 +0800349 this.payLoad = null;
350 }
351
352 public MockFlowRule(int priority, FlowRuleExtPayLoad payLoad) {
353 this.priority = priority;
354 this.timestamp = System.currentTimeMillis();
355 this.id = nextId++;
356 this.payLoad = payLoad;
Ray Milkey930fc662014-11-11 16:07:45 -0800357 }
358
359 @Override
360 public FlowId id() {
Ray Milkey77a455f2015-03-27 10:08:17 -0700361 return FlowId.valueOf(id);
Ray Milkey930fc662014-11-11 16:07:45 -0800362 }
363
364 @Override
365 public short appId() {
366 return 0;
367 }
368
369 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800370 public GroupId groupId() {
371 return new DefaultGroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800372 }
373
374 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800375 public int priority() {
376 return priority;
377 }
378
379 @Override
380 public DeviceId deviceId() {
381 return did("1");
382 }
383
384 @Override
385 public TrafficSelector selector() {
386 return SELECTOR;
387 }
388
389 @Override
390 public TrafficTreatment treatment() {
391 return TREATMENT;
392 }
393
394 @Override
395 public int timeout() {
396 return 0;
397 }
398
399 @Override
400 public boolean isPermanent() {
401 return false;
402 }
403
Brian O'Connor427a1762014-11-19 18:40:32 -0800404 @Override
405 public int hashCode() {
406 return Objects.hash(priority);
407 }
Ray Milkey930fc662014-11-11 16:07:45 -0800408
Brian O'Connor427a1762014-11-19 18:40:32 -0800409 @Override
410 public boolean equals(Object obj) {
411 if (this == obj) {
412 return true;
413 }
414 if (obj == null || getClass() != obj.getClass()) {
415 return false;
416 }
417 final MockFlowRule other = (MockFlowRule) obj;
Ray Milkey77a455f2015-03-27 10:08:17 -0700418 return Objects.equals(this.timestamp, other.timestamp) &&
419 this.id == other.id;
Brian O'Connor427a1762014-11-19 18:40:32 -0800420 }
sangho11c30ac2015-01-22 14:30:55 -0800421
422 @Override
alshabibdb774072015-04-20 13:13:51 -0700423 public int tableId() {
alshabib08d98982015-04-21 16:25:50 -0700424 return tableId;
alshabibdb774072015-04-20 13:13:51 -0700425 }
jcc3d4e14a2015-04-21 11:32:05 +0800426
427 @Override
428 public FlowRuleExtPayLoad payLoad() {
429 return payLoad;
430 }
Ray Milkey930fc662014-11-11 16:07:45 -0800431 }
432
Ray Milkey5a7787a2015-02-23 11:44:30 -0800433 public static class MockIntent extends Intent {
434 private static AtomicLong counter = new AtomicLong(0);
435
436 private final Long number;
437
438 public MockIntent(Long number) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700439 super(NetTestTools.APP_ID, null, Collections.emptyList(),
440 Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey5a7787a2015-02-23 11:44:30 -0800441 this.number = number;
442 }
443
Ray Milkey43a28222015-02-23 13:57:58 -0800444 public MockIntent(Long number, Collection<NetworkResource> resources) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700445 super(NetTestTools.APP_ID, null, resources, Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey43a28222015-02-23 13:57:58 -0800446 this.number = number;
447 }
448
Ray Milkey5a7787a2015-02-23 11:44:30 -0800449 public Long number() {
450 return number;
451 }
452
453 public static Long nextId() {
454 return counter.getAndIncrement();
455 }
Ray Milkey43a28222015-02-23 13:57:58 -0800456
457 @Override
458 public String toString() {
459 return MoreObjects.toStringHelper(getClass())
460 .add("id", id())
461 .add("appId", appId())
462 .toString();
463 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800464 }
465
466 public static class MockTimestamp implements Timestamp {
467 final int value;
468
469 public MockTimestamp(int value) {
470 this.value = value;
471 }
472
473 @Override
474 public int compareTo(Timestamp o) {
475 if (!(o instanceof MockTimestamp)) {
476 return -1;
477 }
478 MockTimestamp that = (MockTimestamp) o;
Jonathan Hart72175c22015-03-24 18:55:58 -0700479 return this.value - that.value;
Ray Milkey5a7787a2015-02-23 11:44:30 -0800480 }
481 }
482
Ray Milkeye6684082014-10-16 16:59:47 -0700483}