blob: 8bef293e4a6d6ae6a6ad970f2b3d1256286805de [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
alshabib346b5b32015-03-06 00:42:16 -080096 public List<Instruction> deferred() {
97 return null;
98 }
99
100 @Override
101 public List<Instruction> immediate() {
Ray Milkey42507352015-03-20 15:16:10 -0700102 return new ArrayList<>();
alshabib346b5b32015-03-06 00:42:16 -0800103 }
104
105 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700106 public List<Instruction> allInstructions() {
107 return null;
108 }
109
110 @Override
alshabib346b5b32015-03-06 00:42:16 -0800111 public Instructions.TableTypeTransition tableTransition() {
112 return null;
113 }
114
115 @Override
116 public Boolean clearedDeferred() {
117 return null;
118 }
Ray Milkeye6684082014-10-16 16:59:47 -0700119 }
120
121 /**
122 * Mock path service for creating paths within the test.
123 */
124 public static class MockPathService implements PathService {
125
126 final String[] pathHops;
127 final String[] reversePathHops;
128
129 /**
130 * Constructor that provides a set of hops to mock.
131 *
132 * @param pathHops path hops to mock
133 */
134 public MockPathService(String[] pathHops) {
135 this.pathHops = pathHops;
136 String[] reversed = pathHops.clone();
137 Collections.reverse(Arrays.asList(reversed));
138 reversePathHops = reversed;
139 }
140
141 @Override
142 public Set<Path> getPaths(ElementId src, ElementId dst) {
143 Set<Path> result = new HashSet<>();
144
145 String[] allHops = new String[pathHops.length];
146
147 if (src.toString().endsWith(pathHops[0])) {
148 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
149 } else {
150 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
151 }
152
153 result.add(createPath(allHops));
154 return result;
155 }
156
157 @Override
158 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800159 final Set<Path> paths = getPaths(src, dst);
160
161 for (Path path : paths) {
162 final DeviceId srcDevice = path.src().deviceId();
163 final DeviceId dstDevice = path.dst().deviceId();
164 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
165 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
166 final Link link = link(src.toString(), 1, dst.toString(), 1);
167
168 final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
169 if (weightValue < 0) {
170 return new HashSet<>();
171 }
172 }
173 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700174 }
175 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800176
177 public static class MockLinkResourceAllocations implements LinkResourceAllocations {
178 @Override
179 public Set<ResourceAllocation> getResourceAllocation(Link link) {
Ray Milkeyb1b66d32015-03-02 10:27:11 -0800180 return ImmutableSet.of(
181 new LambdaResourceAllocation(Lambda.valueOf(77)),
182 new MplsLabelResourceAllocation(MplsLabel.valueOf(10)));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800183 }
184
185 @Override
186 public IntentId intendId() {
187 return null;
188 }
189
190 @Override
191 public Collection<Link> links() {
192 return null;
193 }
194
195 @Override
196 public Set<ResourceRequest> resources() {
197 return null;
198 }
199
200 @Override
201 public ResourceType type() {
202 return null;
203 }
204 }
205
206 public static class MockedAllocationFailure extends RuntimeException { }
207
208 public static class MockResourceService implements LinkResourceService {
209
210 double availableBandwidth = -1.0;
211 int availableLambda = -1;
212
213 /**
214 * Allocates a resource service that will allow bandwidth allocations
215 * up to a limit.
216 *
217 * @param bandwidth available bandwidth limit
218 * @return resource manager for bandwidth requests
219 */
220 public static MockResourceService makeBandwidthResourceService(double bandwidth) {
221 final MockResourceService result = new MockResourceService();
222 result.availableBandwidth = bandwidth;
223 return result;
224 }
225
226 /**
227 * Allocates a resource service that will allow lambda allocations.
228 *
229 * @param lambda Lambda to return for allocation requests. Currently unused
230 * @return resource manager for lambda requests
231 */
232 public static MockResourceService makeLambdaResourceService(int lambda) {
233 final MockResourceService result = new MockResourceService();
234 result.availableLambda = lambda;
235 return result;
236 }
237
238 public void setAvailableBandwidth(double availableBandwidth) {
239 this.availableBandwidth = availableBandwidth;
240 }
241
242 public void setAvailableLambda(int availableLambda) {
243 this.availableLambda = availableLambda;
244 }
245
246
247 @Override
248 public LinkResourceAllocations requestResources(LinkResourceRequest req) {
249 int lambda = -1;
250 double bandwidth = -1.0;
251
252 for (ResourceRequest resourceRequest : req.resources()) {
253 if (resourceRequest.type() == ResourceType.BANDWIDTH) {
254 final BandwidthResourceRequest brr = (BandwidthResourceRequest) resourceRequest;
255 bandwidth = brr.bandwidth().toDouble();
256 } else if (resourceRequest.type() == ResourceType.LAMBDA) {
257 lambda = 1;
258 }
259 }
260
261 if (availableBandwidth < bandwidth) {
262 throw new MockedAllocationFailure();
263 }
264 if (lambda > 0 && availableLambda == 0) {
265 throw new MockedAllocationFailure();
266 }
267
268 return new IntentTestsMocks.MockLinkResourceAllocations();
269 }
270
271 @Override
272 public void releaseResources(LinkResourceAllocations allocations) {
273 // Mock
274 }
275
276 @Override
277 public LinkResourceAllocations updateResources(LinkResourceRequest req,
278 LinkResourceAllocations oldAllocations) {
279 return null;
280 }
281
282 @Override
283 public Iterable<LinkResourceAllocations> getAllocations() {
Ray Milkeybb320482015-02-25 15:16:46 -0800284 return ImmutableSet.of(
285 new IntentTestsMocks.MockLinkResourceAllocations());
Ray Milkey8d3ce432014-11-07 16:21:10 -0800286 }
287
288 @Override
289 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
Ray Milkeybb320482015-02-25 15:16:46 -0800290 return ImmutableSet.of(
291 new IntentTestsMocks.MockLinkResourceAllocations());
Ray Milkey8d3ce432014-11-07 16:21:10 -0800292 }
293
294 @Override
295 public LinkResourceAllocations getAllocations(IntentId intentId) {
Ray Milkeybb320482015-02-25 15:16:46 -0800296 return new IntentTestsMocks.MockLinkResourceAllocations();
Ray Milkey8d3ce432014-11-07 16:21:10 -0800297 }
298
299 @Override
300 public Iterable<ResourceRequest> getAvailableResources(Link link) {
301 final List<ResourceRequest> result = new LinkedList<>();
302 if (availableBandwidth > 0.0) {
Ray Milkeycf590df2015-02-23 17:43:24 -0800303 result.add(new BandwidthResourceRequest(
304 Bandwidth.bps(availableBandwidth)));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800305 }
306 if (availableLambda > 0) {
307 result.add(new LambdaResourceRequest());
308 }
309 return result;
310 }
311
312 @Override
weibit00c94f52014-11-16 07:09:05 -0800313 public Iterable<ResourceRequest> getAvailableResources(Link link, LinkResourceAllocations allocations) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800314 return null;
315 }
Ray Milkeye97ede92014-11-20 10:43:12 -0800316
317 @Override
318 public void addListener(LinkResourceListener listener) {
319
320 }
321
322 @Override
323 public void removeListener(LinkResourceListener listener) {
324
325 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800326 }
327
Ray Milkey930fc662014-11-11 16:07:45 -0800328 private static final IntentTestsMocks.MockSelector SELECTOR =
329 new IntentTestsMocks.MockSelector();
330 private static final IntentTestsMocks.MockTreatment TREATMENT =
331 new IntentTestsMocks.MockTreatment();
332
333 public static class MockFlowRule implements FlowRule {
334
335 int priority;
sangho11c30ac2015-01-22 14:30:55 -0800336 Type type;
337
Ray Milkey930fc662014-11-11 16:07:45 -0800338 public MockFlowRule(int priority) {
339 this.priority = priority;
sangho11c30ac2015-01-22 14:30:55 -0800340 this.type = Type.DEFAULT;
Ray Milkey930fc662014-11-11 16:07:45 -0800341 }
342
343 @Override
344 public FlowId id() {
345 return FlowId.valueOf(1);
346 }
347
348 @Override
349 public short appId() {
350 return 0;
351 }
352
353 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800354 public GroupId groupId() {
355 return new DefaultGroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800356 }
357
358 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800359 public int priority() {
360 return priority;
361 }
362
363 @Override
364 public DeviceId deviceId() {
365 return did("1");
366 }
367
368 @Override
369 public TrafficSelector selector() {
370 return SELECTOR;
371 }
372
373 @Override
374 public TrafficTreatment treatment() {
375 return TREATMENT;
376 }
377
378 @Override
379 public int timeout() {
380 return 0;
381 }
382
383 @Override
384 public boolean isPermanent() {
385 return false;
386 }
387
Brian O'Connor427a1762014-11-19 18:40:32 -0800388 @Override
389 public int hashCode() {
390 return Objects.hash(priority);
391 }
Ray Milkey930fc662014-11-11 16:07:45 -0800392
Brian O'Connor427a1762014-11-19 18:40:32 -0800393 @Override
394 public boolean equals(Object obj) {
395 if (this == obj) {
396 return true;
397 }
398 if (obj == null || getClass() != obj.getClass()) {
399 return false;
400 }
401 final MockFlowRule other = (MockFlowRule) obj;
402 return Objects.equals(this.priority, other.priority);
403 }
sangho11c30ac2015-01-22 14:30:55 -0800404
405 @Override
406 public Type type() {
407 return type;
408 }
Ray Milkey930fc662014-11-11 16:07:45 -0800409 }
410
Ray Milkey5a7787a2015-02-23 11:44:30 -0800411 public static class MockIntent extends Intent {
412 private static AtomicLong counter = new AtomicLong(0);
413
414 private final Long number;
415
416 public MockIntent(Long number) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700417 super(NetTestTools.APP_ID, null, Collections.emptyList(),
418 Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey5a7787a2015-02-23 11:44:30 -0800419 this.number = number;
420 }
421
Ray Milkey43a28222015-02-23 13:57:58 -0800422 public MockIntent(Long number, Collection<NetworkResource> resources) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700423 super(NetTestTools.APP_ID, null, resources, Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey43a28222015-02-23 13:57:58 -0800424 this.number = number;
425 }
426
Ray Milkey5a7787a2015-02-23 11:44:30 -0800427 public Long number() {
428 return number;
429 }
430
431 public static Long nextId() {
432 return counter.getAndIncrement();
433 }
Ray Milkey43a28222015-02-23 13:57:58 -0800434
435 @Override
436 public String toString() {
437 return MoreObjects.toStringHelper(getClass())
438 .add("id", id())
439 .add("appId", appId())
440 .toString();
441 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800442 }
443
444 public static class MockTimestamp implements Timestamp {
445 final int value;
446
447 public MockTimestamp(int value) {
448 this.value = value;
449 }
450
451 @Override
452 public int compareTo(Timestamp o) {
453 if (!(o instanceof MockTimestamp)) {
454 return -1;
455 }
456 MockTimestamp that = (MockTimestamp) o;
Jonathan Hart72175c22015-03-24 18:55:58 -0700457 return this.value - that.value;
Ray Milkey5a7787a2015-02-23 11:44:30 -0800458 }
459 }
460
Ray Milkeye6684082014-10-16 16:59:47 -0700461}