blob: 8e896321cdd006a91253d7a710d2d7d6819950ef [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
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;
Ray Milkeycf590df2015-02-23 17:43:24 -080038import org.onosproject.net.resource.Bandwidth;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.resource.BandwidthResourceRequest;
Ray Milkeyb1b66d32015-03-02 10:27:11 -080040import org.onosproject.net.resource.Lambda;
41import org.onosproject.net.resource.LambdaResourceAllocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.resource.LambdaResourceRequest;
43import org.onosproject.net.resource.LinkResourceAllocations;
44import org.onosproject.net.resource.LinkResourceListener;
45import org.onosproject.net.resource.LinkResourceRequest;
46import org.onosproject.net.resource.LinkResourceService;
Ray Milkeybb320482015-02-25 15:16:46 -080047import org.onosproject.net.resource.MplsLabel;
48import org.onosproject.net.resource.MplsLabelResourceAllocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.resource.ResourceAllocation;
50import org.onosproject.net.resource.ResourceRequest;
51import org.onosproject.net.resource.ResourceType;
52import org.onosproject.net.topology.DefaultTopologyEdge;
53import org.onosproject.net.topology.DefaultTopologyVertex;
54import org.onosproject.net.topology.LinkWeight;
55import org.onosproject.net.topology.PathService;
56import org.onosproject.net.topology.TopologyVertex;
Ray Milkey5a7787a2015-02-23 11:44:30 -080057import org.onosproject.store.Timestamp;
Ray Milkeye6684082014-10-16 16:59:47 -070058
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070059import java.util.Arrays;
60import java.util.Collection;
61import java.util.Collections;
62import java.util.HashSet;
63import java.util.LinkedList;
64import java.util.List;
65import java.util.Objects;
66import java.util.Set;
67import java.util.concurrent.atomic.AtomicLong;
68
69import static org.onosproject.net.NetTestTools.createPath;
70import static org.onosproject.net.NetTestTools.did;
71import static org.onosproject.net.NetTestTools.link;
Ray Milkey43a28222015-02-23 13:57:58 -080072
Ray Milkeye6684082014-10-16 16:59:47 -070073/**
74 * Common mocks used by the intent framework tests.
75 */
76public class IntentTestsMocks {
77 /**
78 * Mock traffic selector class used for satisfying API requirements.
79 */
80 public static class MockSelector implements TrafficSelector {
81 @Override
82 public Set<Criterion> criteria() {
83 return new HashSet<>();
84 }
Jonathan Hart936c49d2014-10-23 16:38:59 -070085
86 @Override
87 public Criterion getCriterion(Type type) {
88 return null;
89 }
Ray Milkeye6684082014-10-16 16:59:47 -070090 }
91
92 /**
93 * Mock traffic treatment class used for satisfying API requirements.
94 */
95 public static class MockTreatment implements TrafficTreatment {
96 @Override
alshabib346b5b32015-03-06 00:42:16 -080097 public List<Instruction> deferred() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070098 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -080099 }
100
101 @Override
102 public List<Instruction> immediate() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700103 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -0800104 }
105
106 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700107 public List<Instruction> allInstructions() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700108 return Collections.emptyList();
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700109 }
110
111 @Override
alshabib346b5b32015-03-06 00:42:16 -0800112 public Instructions.TableTypeTransition tableTransition() {
113 return null;
114 }
115
116 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700117 public boolean clearedDeferred() {
118 return false;
alshabib346b5b32015-03-06 00:42:16 -0800119 }
Ray Milkeye6684082014-10-16 16:59:47 -0700120 }
121
122 /**
123 * Mock path service for creating paths within the test.
124 */
125 public static class MockPathService implements PathService {
126
127 final String[] pathHops;
128 final String[] reversePathHops;
129
130 /**
131 * Constructor that provides a set of hops to mock.
132 *
133 * @param pathHops path hops to mock
134 */
135 public MockPathService(String[] pathHops) {
136 this.pathHops = pathHops;
137 String[] reversed = pathHops.clone();
138 Collections.reverse(Arrays.asList(reversed));
139 reversePathHops = reversed;
140 }
141
142 @Override
143 public Set<Path> getPaths(ElementId src, ElementId dst) {
144 Set<Path> result = new HashSet<>();
145
146 String[] allHops = new String[pathHops.length];
147
148 if (src.toString().endsWith(pathHops[0])) {
149 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
150 } else {
151 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
152 }
153
154 result.add(createPath(allHops));
155 return result;
156 }
157
158 @Override
159 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800160 final Set<Path> paths = getPaths(src, dst);
161
162 for (Path path : paths) {
163 final DeviceId srcDevice = path.src().deviceId();
164 final DeviceId dstDevice = path.dst().deviceId();
165 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
166 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
167 final Link link = link(src.toString(), 1, dst.toString(), 1);
168
169 final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
170 if (weightValue < 0) {
171 return new HashSet<>();
172 }
173 }
174 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700175 }
176 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800177
178 public static class MockLinkResourceAllocations implements LinkResourceAllocations {
179 @Override
180 public Set<ResourceAllocation> getResourceAllocation(Link link) {
Ray Milkeyb1b66d32015-03-02 10:27:11 -0800181 return ImmutableSet.of(
182 new LambdaResourceAllocation(Lambda.valueOf(77)),
183 new MplsLabelResourceAllocation(MplsLabel.valueOf(10)));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800184 }
185
186 @Override
Ayaka Koshibee114f042015-05-01 11:43:00 -0700187 public IntentId intentId() {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800188 return null;
189 }
190
191 @Override
192 public Collection<Link> links() {
193 return null;
194 }
195
196 @Override
197 public Set<ResourceRequest> resources() {
198 return null;
199 }
200
201 @Override
202 public ResourceType type() {
203 return null;
204 }
205 }
206
207 public static class MockedAllocationFailure extends RuntimeException { }
208
209 public static class MockResourceService implements LinkResourceService {
210
211 double availableBandwidth = -1.0;
212 int availableLambda = -1;
213
214 /**
215 * Allocates a resource service that will allow bandwidth allocations
216 * up to a limit.
217 *
218 * @param bandwidth available bandwidth limit
219 * @return resource manager for bandwidth requests
220 */
221 public static MockResourceService makeBandwidthResourceService(double bandwidth) {
222 final MockResourceService result = new MockResourceService();
223 result.availableBandwidth = bandwidth;
224 return result;
225 }
226
227 /**
228 * Allocates a resource service that will allow lambda allocations.
229 *
230 * @param lambda Lambda to return for allocation requests. Currently unused
231 * @return resource manager for lambda requests
232 */
233 public static MockResourceService makeLambdaResourceService(int lambda) {
234 final MockResourceService result = new MockResourceService();
235 result.availableLambda = lambda;
236 return result;
237 }
238
239 public void setAvailableBandwidth(double availableBandwidth) {
240 this.availableBandwidth = availableBandwidth;
241 }
242
243 public void setAvailableLambda(int availableLambda) {
244 this.availableLambda = availableLambda;
245 }
246
247
248 @Override
249 public LinkResourceAllocations requestResources(LinkResourceRequest req) {
250 int lambda = -1;
251 double bandwidth = -1.0;
252
253 for (ResourceRequest resourceRequest : req.resources()) {
254 if (resourceRequest.type() == ResourceType.BANDWIDTH) {
255 final BandwidthResourceRequest brr = (BandwidthResourceRequest) resourceRequest;
256 bandwidth = brr.bandwidth().toDouble();
257 } else if (resourceRequest.type() == ResourceType.LAMBDA) {
258 lambda = 1;
259 }
260 }
261
262 if (availableBandwidth < bandwidth) {
263 throw new MockedAllocationFailure();
264 }
265 if (lambda > 0 && availableLambda == 0) {
266 throw new MockedAllocationFailure();
267 }
268
269 return new IntentTestsMocks.MockLinkResourceAllocations();
270 }
271
272 @Override
273 public void releaseResources(LinkResourceAllocations allocations) {
274 // Mock
275 }
276
277 @Override
278 public LinkResourceAllocations updateResources(LinkResourceRequest req,
279 LinkResourceAllocations oldAllocations) {
280 return null;
281 }
282
283 @Override
284 public Iterable<LinkResourceAllocations> getAllocations() {
Ray Milkeybb320482015-02-25 15:16:46 -0800285 return ImmutableSet.of(
286 new IntentTestsMocks.MockLinkResourceAllocations());
Ray Milkey8d3ce432014-11-07 16:21:10 -0800287 }
288
289 @Override
290 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
Ray Milkeybb320482015-02-25 15:16:46 -0800291 return ImmutableSet.of(
292 new IntentTestsMocks.MockLinkResourceAllocations());
Ray Milkey8d3ce432014-11-07 16:21:10 -0800293 }
294
295 @Override
296 public LinkResourceAllocations getAllocations(IntentId intentId) {
Ray Milkeybb320482015-02-25 15:16:46 -0800297 return new IntentTestsMocks.MockLinkResourceAllocations();
Ray Milkey8d3ce432014-11-07 16:21:10 -0800298 }
299
300 @Override
301 public Iterable<ResourceRequest> getAvailableResources(Link link) {
302 final List<ResourceRequest> result = new LinkedList<>();
303 if (availableBandwidth > 0.0) {
Ray Milkeycf590df2015-02-23 17:43:24 -0800304 result.add(new BandwidthResourceRequest(
305 Bandwidth.bps(availableBandwidth)));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800306 }
307 if (availableLambda > 0) {
308 result.add(new LambdaResourceRequest());
309 }
310 return result;
311 }
312
313 @Override
weibit00c94f52014-11-16 07:09:05 -0800314 public Iterable<ResourceRequest> getAvailableResources(Link link, LinkResourceAllocations allocations) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800315 return null;
316 }
Ray Milkeye97ede92014-11-20 10:43:12 -0800317
318 @Override
319 public void addListener(LinkResourceListener listener) {
320
321 }
322
323 @Override
324 public void removeListener(LinkResourceListener listener) {
325
326 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800327 }
328
Ray Milkey930fc662014-11-11 16:07:45 -0800329 private static final IntentTestsMocks.MockSelector SELECTOR =
330 new IntentTestsMocks.MockSelector();
331 private static final IntentTestsMocks.MockTreatment TREATMENT =
332 new IntentTestsMocks.MockTreatment();
333
334 public static class MockFlowRule implements FlowRule {
Ray Milkey77a455f2015-03-27 10:08:17 -0700335 static int nextId = 0;
Ray Milkey930fc662014-11-11 16:07:45 -0800336
337 int priority;
alshabib08d98982015-04-21 16:25:50 -0700338 int tableId;
Ray Milkey77a455f2015-03-27 10:08:17 -0700339 long timestamp;
340 int id;
jcc3d4e14a2015-04-21 11:32:05 +0800341 FlowRuleExtPayLoad payLoad;
sangho11c30ac2015-01-22 14:30:55 -0800342
Ray Milkey930fc662014-11-11 16:07:45 -0800343 public MockFlowRule(int priority) {
344 this.priority = priority;
alshabib08d98982015-04-21 16:25:50 -0700345 this.tableId = 0;
Ray Milkey77a455f2015-03-27 10:08:17 -0700346 this.timestamp = System.currentTimeMillis();
347 this.id = nextId++;
jcc3d4e14a2015-04-21 11:32:05 +0800348 this.payLoad = null;
349 }
350
351 public MockFlowRule(int priority, FlowRuleExtPayLoad payLoad) {
352 this.priority = priority;
353 this.timestamp = System.currentTimeMillis();
354 this.id = nextId++;
355 this.payLoad = payLoad;
Ray Milkey930fc662014-11-11 16:07:45 -0800356 }
357
358 @Override
359 public FlowId id() {
Ray Milkey77a455f2015-03-27 10:08:17 -0700360 return FlowId.valueOf(id);
Ray Milkey930fc662014-11-11 16:07:45 -0800361 }
362
363 @Override
364 public short appId() {
365 return 0;
366 }
367
368 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800369 public GroupId groupId() {
370 return new DefaultGroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800371 }
372
373 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800374 public int priority() {
375 return priority;
376 }
377
378 @Override
379 public DeviceId deviceId() {
380 return did("1");
381 }
382
383 @Override
384 public TrafficSelector selector() {
385 return SELECTOR;
386 }
387
388 @Override
389 public TrafficTreatment treatment() {
390 return TREATMENT;
391 }
392
393 @Override
394 public int timeout() {
395 return 0;
396 }
397
398 @Override
399 public boolean isPermanent() {
400 return false;
401 }
402
Brian O'Connor427a1762014-11-19 18:40:32 -0800403 @Override
404 public int hashCode() {
405 return Objects.hash(priority);
406 }
Ray Milkey930fc662014-11-11 16:07:45 -0800407
Brian O'Connor427a1762014-11-19 18:40:32 -0800408 @Override
409 public boolean equals(Object obj) {
410 if (this == obj) {
411 return true;
412 }
413 if (obj == null || getClass() != obj.getClass()) {
414 return false;
415 }
416 final MockFlowRule other = (MockFlowRule) obj;
Ray Milkey77a455f2015-03-27 10:08:17 -0700417 return Objects.equals(this.timestamp, other.timestamp) &&
418 this.id == other.id;
Brian O'Connor427a1762014-11-19 18:40:32 -0800419 }
sangho11c30ac2015-01-22 14:30:55 -0800420
421 @Override
alshabibdb774072015-04-20 13:13:51 -0700422 public int tableId() {
alshabib08d98982015-04-21 16:25:50 -0700423 return tableId;
alshabibdb774072015-04-20 13:13:51 -0700424 }
jcc3d4e14a2015-04-21 11:32:05 +0800425
426 @Override
427 public FlowRuleExtPayLoad payLoad() {
428 return payLoad;
429 }
Ray Milkey930fc662014-11-11 16:07:45 -0800430 }
431
Ray Milkey5a7787a2015-02-23 11:44:30 -0800432 public static class MockIntent extends Intent {
433 private static AtomicLong counter = new AtomicLong(0);
434
435 private final Long number;
436
437 public MockIntent(Long number) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700438 super(NetTestTools.APP_ID, null, Collections.emptyList(),
439 Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey5a7787a2015-02-23 11:44:30 -0800440 this.number = number;
441 }
442
Ray Milkey43a28222015-02-23 13:57:58 -0800443 public MockIntent(Long number, Collection<NetworkResource> resources) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700444 super(NetTestTools.APP_ID, null, resources, Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey43a28222015-02-23 13:57:58 -0800445 this.number = number;
446 }
447
Ray Milkey5a7787a2015-02-23 11:44:30 -0800448 public Long number() {
449 return number;
450 }
451
452 public static Long nextId() {
453 return counter.getAndIncrement();
454 }
Ray Milkey43a28222015-02-23 13:57:58 -0800455
456 @Override
457 public String toString() {
458 return MoreObjects.toStringHelper(getClass())
459 .add("id", id())
460 .add("appId", appId())
461 .toString();
462 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800463 }
464
465 public static class MockTimestamp implements Timestamp {
466 final int value;
467
468 public MockTimestamp(int value) {
469 this.value = value;
470 }
471
472 @Override
473 public int compareTo(Timestamp o) {
474 if (!(o instanceof MockTimestamp)) {
475 return -1;
476 }
477 MockTimestamp that = (MockTimestamp) o;
Jonathan Hart72175c22015-03-24 18:55:58 -0700478 return this.value - that.value;
Ray Milkey5a7787a2015-02-23 11:44:30 -0800479 }
480 }
481
Ray Milkeye6684082014-10-16 16:59:47 -0700482}