blob: dca1906e43e160f56437e2b3290b5cf552148363 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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;
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;
24import org.onosproject.net.Link;
Ray Milkey5a7787a2015-02-23 11:44:30 -080025import org.onosproject.net.NetTestTools;
Ray Milkey43a28222015-02-23 13:57:58 -080026import org.onosproject.net.NetworkResource;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.Path;
28import org.onosproject.net.flow.FlowId;
29import org.onosproject.net.flow.FlowRule;
30import org.onosproject.net.flow.TrafficSelector;
31import org.onosproject.net.flow.TrafficTreatment;
32import org.onosproject.net.flow.criteria.Criterion;
33import org.onosproject.net.flow.criteria.Criterion.Type;
34import org.onosproject.net.flow.instructions.Instruction;
alshabib346b5b32015-03-06 00:42:16 -080035import org.onosproject.net.flow.instructions.Instructions;
Ray Milkeycf590df2015-02-23 17:43:24 -080036import org.onosproject.net.resource.Bandwidth;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.resource.BandwidthResourceRequest;
Ray Milkeyb1b66d32015-03-02 10:27:11 -080038import org.onosproject.net.resource.Lambda;
39import org.onosproject.net.resource.LambdaResourceAllocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.resource.LambdaResourceRequest;
41import org.onosproject.net.resource.LinkResourceAllocations;
42import org.onosproject.net.resource.LinkResourceListener;
43import org.onosproject.net.resource.LinkResourceRequest;
44import org.onosproject.net.resource.LinkResourceService;
Ray Milkeybb320482015-02-25 15:16:46 -080045import org.onosproject.net.resource.MplsLabel;
46import org.onosproject.net.resource.MplsLabelResourceAllocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.resource.ResourceAllocation;
48import org.onosproject.net.resource.ResourceRequest;
49import org.onosproject.net.resource.ResourceType;
50import org.onosproject.net.topology.DefaultTopologyEdge;
51import org.onosproject.net.topology.DefaultTopologyVertex;
52import org.onosproject.net.topology.LinkWeight;
53import org.onosproject.net.topology.PathService;
54import org.onosproject.net.topology.TopologyVertex;
Ray Milkey5a7787a2015-02-23 11:44:30 -080055import org.onosproject.store.Timestamp;
Ray Milkeye6684082014-10-16 16:59:47 -070056
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070057import java.util.ArrayList;
58import java.util.Arrays;
59import java.util.Collection;
60import java.util.Collections;
61import java.util.HashSet;
62import java.util.LinkedList;
63import java.util.List;
64import java.util.Objects;
65import java.util.Set;
66import java.util.concurrent.atomic.AtomicLong;
67
68import static org.onosproject.net.NetTestTools.createPath;
69import static org.onosproject.net.NetTestTools.did;
70import static org.onosproject.net.NetTestTools.link;
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
96 public List<Instruction> instructions() {
Ayaka Koshibeb1224ab2015-01-31 00:48:58 +000097 return new ArrayList<>();
Ray Milkeye6684082014-10-16 16:59:47 -070098 }
alshabib346b5b32015-03-06 00:42:16 -080099
100 @Override
101 public List<Instruction> deferred() {
102 return null;
103 }
104
105 @Override
106 public List<Instruction> immediate() {
107 return null;
108 }
109
110 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700111 public List<Instruction> allInstructions() {
112 return null;
113 }
114
115 @Override
alshabib346b5b32015-03-06 00:42:16 -0800116 public Instructions.TableTypeTransition tableTransition() {
117 return null;
118 }
119
120 @Override
121 public Boolean clearedDeferred() {
122 return null;
123 }
Ray Milkeye6684082014-10-16 16:59:47 -0700124 }
125
126 /**
127 * Mock path service for creating paths within the test.
128 */
129 public static class MockPathService implements PathService {
130
131 final String[] pathHops;
132 final String[] reversePathHops;
133
134 /**
135 * Constructor that provides a set of hops to mock.
136 *
137 * @param pathHops path hops to mock
138 */
139 public MockPathService(String[] pathHops) {
140 this.pathHops = pathHops;
141 String[] reversed = pathHops.clone();
142 Collections.reverse(Arrays.asList(reversed));
143 reversePathHops = reversed;
144 }
145
146 @Override
147 public Set<Path> getPaths(ElementId src, ElementId dst) {
148 Set<Path> result = new HashSet<>();
149
150 String[] allHops = new String[pathHops.length];
151
152 if (src.toString().endsWith(pathHops[0])) {
153 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
154 } else {
155 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
156 }
157
158 result.add(createPath(allHops));
159 return result;
160 }
161
162 @Override
163 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800164 final Set<Path> paths = getPaths(src, dst);
165
166 for (Path path : paths) {
167 final DeviceId srcDevice = path.src().deviceId();
168 final DeviceId dstDevice = path.dst().deviceId();
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);
172
173 final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
174 if (weightValue < 0) {
175 return new HashSet<>();
176 }
177 }
178 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700179 }
180 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800181
182 public static class MockLinkResourceAllocations implements LinkResourceAllocations {
183 @Override
184 public Set<ResourceAllocation> getResourceAllocation(Link link) {
Ray Milkeyb1b66d32015-03-02 10:27:11 -0800185 return ImmutableSet.of(
186 new LambdaResourceAllocation(Lambda.valueOf(77)),
187 new MplsLabelResourceAllocation(MplsLabel.valueOf(10)));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800188 }
189
190 @Override
191 public IntentId intendId() {
192 return null;
193 }
194
195 @Override
196 public Collection<Link> links() {
197 return null;
198 }
199
200 @Override
201 public Set<ResourceRequest> resources() {
202 return null;
203 }
204
205 @Override
206 public ResourceType type() {
207 return null;
208 }
209 }
210
211 public static class MockedAllocationFailure extends RuntimeException { }
212
213 public static class MockResourceService implements LinkResourceService {
214
215 double availableBandwidth = -1.0;
216 int availableLambda = -1;
217
218 /**
219 * Allocates a resource service that will allow bandwidth allocations
220 * up to a limit.
221 *
222 * @param bandwidth available bandwidth limit
223 * @return resource manager for bandwidth requests
224 */
225 public static MockResourceService makeBandwidthResourceService(double bandwidth) {
226 final MockResourceService result = new MockResourceService();
227 result.availableBandwidth = bandwidth;
228 return result;
229 }
230
231 /**
232 * Allocates a resource service that will allow lambda allocations.
233 *
234 * @param lambda Lambda to return for allocation requests. Currently unused
235 * @return resource manager for lambda requests
236 */
237 public static MockResourceService makeLambdaResourceService(int lambda) {
238 final MockResourceService result = new MockResourceService();
239 result.availableLambda = lambda;
240 return result;
241 }
242
243 public void setAvailableBandwidth(double availableBandwidth) {
244 this.availableBandwidth = availableBandwidth;
245 }
246
247 public void setAvailableLambda(int availableLambda) {
248 this.availableLambda = availableLambda;
249 }
250
251
252 @Override
253 public LinkResourceAllocations requestResources(LinkResourceRequest req) {
254 int lambda = -1;
255 double bandwidth = -1.0;
256
257 for (ResourceRequest resourceRequest : req.resources()) {
258 if (resourceRequest.type() == ResourceType.BANDWIDTH) {
259 final BandwidthResourceRequest brr = (BandwidthResourceRequest) resourceRequest;
260 bandwidth = brr.bandwidth().toDouble();
261 } else if (resourceRequest.type() == ResourceType.LAMBDA) {
262 lambda = 1;
263 }
264 }
265
266 if (availableBandwidth < bandwidth) {
267 throw new MockedAllocationFailure();
268 }
269 if (lambda > 0 && availableLambda == 0) {
270 throw new MockedAllocationFailure();
271 }
272
273 return new IntentTestsMocks.MockLinkResourceAllocations();
274 }
275
276 @Override
277 public void releaseResources(LinkResourceAllocations allocations) {
278 // Mock
279 }
280
281 @Override
282 public LinkResourceAllocations updateResources(LinkResourceRequest req,
283 LinkResourceAllocations oldAllocations) {
284 return null;
285 }
286
287 @Override
288 public Iterable<LinkResourceAllocations> getAllocations() {
Ray Milkeybb320482015-02-25 15:16:46 -0800289 return ImmutableSet.of(
290 new IntentTestsMocks.MockLinkResourceAllocations());
Ray Milkey8d3ce432014-11-07 16:21:10 -0800291 }
292
293 @Override
294 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
Ray Milkeybb320482015-02-25 15:16:46 -0800295 return ImmutableSet.of(
296 new IntentTestsMocks.MockLinkResourceAllocations());
Ray Milkey8d3ce432014-11-07 16:21:10 -0800297 }
298
299 @Override
300 public LinkResourceAllocations getAllocations(IntentId intentId) {
Ray Milkeybb320482015-02-25 15:16:46 -0800301 return new IntentTestsMocks.MockLinkResourceAllocations();
Ray Milkey8d3ce432014-11-07 16:21:10 -0800302 }
303
304 @Override
305 public Iterable<ResourceRequest> getAvailableResources(Link link) {
306 final List<ResourceRequest> result = new LinkedList<>();
307 if (availableBandwidth > 0.0) {
Ray Milkeycf590df2015-02-23 17:43:24 -0800308 result.add(new BandwidthResourceRequest(
309 Bandwidth.bps(availableBandwidth)));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800310 }
311 if (availableLambda > 0) {
312 result.add(new LambdaResourceRequest());
313 }
314 return result;
315 }
316
317 @Override
weibit00c94f52014-11-16 07:09:05 -0800318 public Iterable<ResourceRequest> getAvailableResources(Link link, LinkResourceAllocations allocations) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800319 return null;
320 }
Ray Milkeye97ede92014-11-20 10:43:12 -0800321
322 @Override
323 public void addListener(LinkResourceListener listener) {
324
325 }
326
327 @Override
328 public void removeListener(LinkResourceListener listener) {
329
330 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800331 }
332
Ray Milkey930fc662014-11-11 16:07:45 -0800333 private static final IntentTestsMocks.MockSelector SELECTOR =
334 new IntentTestsMocks.MockSelector();
335 private static final IntentTestsMocks.MockTreatment TREATMENT =
336 new IntentTestsMocks.MockTreatment();
337
338 public static class MockFlowRule implements FlowRule {
339
340 int priority;
sangho11c30ac2015-01-22 14:30:55 -0800341 Type type;
342
Ray Milkey930fc662014-11-11 16:07:45 -0800343 public MockFlowRule(int priority) {
344 this.priority = priority;
sangho11c30ac2015-01-22 14:30:55 -0800345 this.type = Type.DEFAULT;
Ray Milkey930fc662014-11-11 16:07:45 -0800346 }
347
348 @Override
349 public FlowId id() {
350 return FlowId.valueOf(1);
351 }
352
353 @Override
354 public short appId() {
355 return 0;
356 }
357
358 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800359 public GroupId groupId() {
360 return new DefaultGroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800361 }
362
363 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800364 public int priority() {
365 return priority;
366 }
367
368 @Override
369 public DeviceId deviceId() {
370 return did("1");
371 }
372
373 @Override
374 public TrafficSelector selector() {
375 return SELECTOR;
376 }
377
378 @Override
379 public TrafficTreatment treatment() {
380 return TREATMENT;
381 }
382
383 @Override
384 public int timeout() {
385 return 0;
386 }
387
388 @Override
389 public boolean isPermanent() {
390 return false;
391 }
392
Brian O'Connor427a1762014-11-19 18:40:32 -0800393 @Override
394 public int hashCode() {
395 return Objects.hash(priority);
396 }
Ray Milkey930fc662014-11-11 16:07:45 -0800397
Brian O'Connor427a1762014-11-19 18:40:32 -0800398 @Override
399 public boolean equals(Object obj) {
400 if (this == obj) {
401 return true;
402 }
403 if (obj == null || getClass() != obj.getClass()) {
404 return false;
405 }
406 final MockFlowRule other = (MockFlowRule) obj;
407 return Objects.equals(this.priority, other.priority);
408 }
sangho11c30ac2015-01-22 14:30:55 -0800409
410 @Override
411 public Type type() {
412 return type;
413 }
Ray Milkey930fc662014-11-11 16:07:45 -0800414 }
415
Ray Milkey5a7787a2015-02-23 11:44:30 -0800416 public static class MockIntent extends Intent {
417 private static AtomicLong counter = new AtomicLong(0);
418
419 private final Long number;
420
421 public MockIntent(Long number) {
422 super(NetTestTools.APP_ID, Collections.emptyList());
423 this.number = number;
424 }
425
Ray Milkey43a28222015-02-23 13:57:58 -0800426 public MockIntent(Long number, Collection<NetworkResource> resources) {
427 super(NetTestTools.APP_ID, resources);
428 this.number = number;
429 }
430
Ray Milkey5a7787a2015-02-23 11:44:30 -0800431 public Long number() {
432 return number;
433 }
434
435 public static Long nextId() {
436 return counter.getAndIncrement();
437 }
Ray Milkey43a28222015-02-23 13:57:58 -0800438
439 @Override
440 public String toString() {
441 return MoreObjects.toStringHelper(getClass())
442 .add("id", id())
443 .add("appId", appId())
444 .toString();
445 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800446 }
447
448 public static class MockTimestamp implements Timestamp {
449 final int value;
450
451 public MockTimestamp(int value) {
452 this.value = value;
453 }
454
455 @Override
456 public int compareTo(Timestamp o) {
457 if (!(o instanceof MockTimestamp)) {
458 return -1;
459 }
460 MockTimestamp that = (MockTimestamp) o;
461 return (this.value > that.value ? -1 : (this.value == that.value ? 0 : 1));
462 }
463 }
464
Ray Milkey930fc662014-11-11 16:07:45 -0800465
Ray Milkeye6684082014-10-16 16:59:47 -0700466}