blob: dc8cd2b608a9f669d58f6c2653fb78c336b84b6e [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.Arrays;
58import java.util.Collection;
59import java.util.Collections;
60import java.util.HashSet;
61import java.util.LinkedList;
62import java.util.List;
63import java.util.Objects;
64import java.util.Set;
65import java.util.concurrent.atomic.AtomicLong;
66
67import static org.onosproject.net.NetTestTools.createPath;
68import static org.onosproject.net.NetTestTools.did;
69import static org.onosproject.net.NetTestTools.link;
Ray Milkey43a28222015-02-23 13:57:58 -080070
Ray Milkeye6684082014-10-16 16:59:47 -070071/**
72 * Common mocks used by the intent framework tests.
73 */
74public class IntentTestsMocks {
75 /**
76 * Mock traffic selector class used for satisfying API requirements.
77 */
78 public static class MockSelector implements TrafficSelector {
79 @Override
80 public Set<Criterion> criteria() {
81 return new HashSet<>();
82 }
Jonathan Hart936c49d2014-10-23 16:38:59 -070083
84 @Override
85 public Criterion getCriterion(Type type) {
86 return null;
87 }
Ray Milkeye6684082014-10-16 16:59:47 -070088 }
89
90 /**
91 * Mock traffic treatment class used for satisfying API requirements.
92 */
93 public static class MockTreatment implements TrafficTreatment {
94 @Override
alshabib346b5b32015-03-06 00:42:16 -080095 public List<Instruction> deferred() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070096 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -080097 }
98
99 @Override
100 public List<Instruction> immediate() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700101 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -0800102 }
103
104 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700105 public List<Instruction> allInstructions() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700106 return Collections.emptyList();
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700107 }
108
109 @Override
alshabib346b5b32015-03-06 00:42:16 -0800110 public Instructions.TableTypeTransition tableTransition() {
111 return null;
112 }
113
114 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700115 public boolean clearedDeferred() {
116 return false;
alshabib346b5b32015-03-06 00:42:16 -0800117 }
Ray Milkeye6684082014-10-16 16:59:47 -0700118 }
119
120 /**
121 * Mock path service for creating paths within the test.
122 */
123 public static class MockPathService implements PathService {
124
125 final String[] pathHops;
126 final String[] reversePathHops;
127
128 /**
129 * Constructor that provides a set of hops to mock.
130 *
131 * @param pathHops path hops to mock
132 */
133 public MockPathService(String[] pathHops) {
134 this.pathHops = pathHops;
135 String[] reversed = pathHops.clone();
136 Collections.reverse(Arrays.asList(reversed));
137 reversePathHops = reversed;
138 }
139
140 @Override
141 public Set<Path> getPaths(ElementId src, ElementId dst) {
142 Set<Path> result = new HashSet<>();
143
144 String[] allHops = new String[pathHops.length];
145
146 if (src.toString().endsWith(pathHops[0])) {
147 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
148 } else {
149 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
150 }
151
152 result.add(createPath(allHops));
153 return result;
154 }
155
156 @Override
157 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800158 final Set<Path> paths = getPaths(src, dst);
159
160 for (Path path : paths) {
161 final DeviceId srcDevice = path.src().deviceId();
162 final DeviceId dstDevice = path.dst().deviceId();
163 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
164 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
165 final Link link = link(src.toString(), 1, dst.toString(), 1);
166
167 final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
168 if (weightValue < 0) {
169 return new HashSet<>();
170 }
171 }
172 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700173 }
174 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800175
176 public static class MockLinkResourceAllocations implements LinkResourceAllocations {
177 @Override
178 public Set<ResourceAllocation> getResourceAllocation(Link link) {
Ray Milkeyb1b66d32015-03-02 10:27:11 -0800179 return ImmutableSet.of(
180 new LambdaResourceAllocation(Lambda.valueOf(77)),
181 new MplsLabelResourceAllocation(MplsLabel.valueOf(10)));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800182 }
183
184 @Override
185 public IntentId intendId() {
186 return null;
187 }
188
189 @Override
190 public Collection<Link> links() {
191 return null;
192 }
193
194 @Override
195 public Set<ResourceRequest> resources() {
196 return null;
197 }
198
199 @Override
200 public ResourceType type() {
201 return null;
202 }
203 }
204
205 public static class MockedAllocationFailure extends RuntimeException { }
206
207 public static class MockResourceService implements LinkResourceService {
208
209 double availableBandwidth = -1.0;
210 int availableLambda = -1;
211
212 /**
213 * Allocates a resource service that will allow bandwidth allocations
214 * up to a limit.
215 *
216 * @param bandwidth available bandwidth limit
217 * @return resource manager for bandwidth requests
218 */
219 public static MockResourceService makeBandwidthResourceService(double bandwidth) {
220 final MockResourceService result = new MockResourceService();
221 result.availableBandwidth = bandwidth;
222 return result;
223 }
224
225 /**
226 * Allocates a resource service that will allow lambda allocations.
227 *
228 * @param lambda Lambda to return for allocation requests. Currently unused
229 * @return resource manager for lambda requests
230 */
231 public static MockResourceService makeLambdaResourceService(int lambda) {
232 final MockResourceService result = new MockResourceService();
233 result.availableLambda = lambda;
234 return result;
235 }
236
237 public void setAvailableBandwidth(double availableBandwidth) {
238 this.availableBandwidth = availableBandwidth;
239 }
240
241 public void setAvailableLambda(int availableLambda) {
242 this.availableLambda = availableLambda;
243 }
244
245
246 @Override
247 public LinkResourceAllocations requestResources(LinkResourceRequest req) {
248 int lambda = -1;
249 double bandwidth = -1.0;
250
251 for (ResourceRequest resourceRequest : req.resources()) {
252 if (resourceRequest.type() == ResourceType.BANDWIDTH) {
253 final BandwidthResourceRequest brr = (BandwidthResourceRequest) resourceRequest;
254 bandwidth = brr.bandwidth().toDouble();
255 } else if (resourceRequest.type() == ResourceType.LAMBDA) {
256 lambda = 1;
257 }
258 }
259
260 if (availableBandwidth < bandwidth) {
261 throw new MockedAllocationFailure();
262 }
263 if (lambda > 0 && availableLambda == 0) {
264 throw new MockedAllocationFailure();
265 }
266
267 return new IntentTestsMocks.MockLinkResourceAllocations();
268 }
269
270 @Override
271 public void releaseResources(LinkResourceAllocations allocations) {
272 // Mock
273 }
274
275 @Override
276 public LinkResourceAllocations updateResources(LinkResourceRequest req,
277 LinkResourceAllocations oldAllocations) {
278 return null;
279 }
280
281 @Override
282 public Iterable<LinkResourceAllocations> getAllocations() {
Ray Milkeybb320482015-02-25 15:16:46 -0800283 return ImmutableSet.of(
284 new IntentTestsMocks.MockLinkResourceAllocations());
Ray Milkey8d3ce432014-11-07 16:21:10 -0800285 }
286
287 @Override
288 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
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 LinkResourceAllocations getAllocations(IntentId intentId) {
Ray Milkeybb320482015-02-25 15:16:46 -0800295 return new IntentTestsMocks.MockLinkResourceAllocations();
Ray Milkey8d3ce432014-11-07 16:21:10 -0800296 }
297
298 @Override
299 public Iterable<ResourceRequest> getAvailableResources(Link link) {
300 final List<ResourceRequest> result = new LinkedList<>();
301 if (availableBandwidth > 0.0) {
Ray Milkeycf590df2015-02-23 17:43:24 -0800302 result.add(new BandwidthResourceRequest(
303 Bandwidth.bps(availableBandwidth)));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800304 }
305 if (availableLambda > 0) {
306 result.add(new LambdaResourceRequest());
307 }
308 return result;
309 }
310
311 @Override
weibit00c94f52014-11-16 07:09:05 -0800312 public Iterable<ResourceRequest> getAvailableResources(Link link, LinkResourceAllocations allocations) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800313 return null;
314 }
Ray Milkeye97ede92014-11-20 10:43:12 -0800315
316 @Override
317 public void addListener(LinkResourceListener listener) {
318
319 }
320
321 @Override
322 public void removeListener(LinkResourceListener listener) {
323
324 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800325 }
326
Ray Milkey930fc662014-11-11 16:07:45 -0800327 private static final IntentTestsMocks.MockSelector SELECTOR =
328 new IntentTestsMocks.MockSelector();
329 private static final IntentTestsMocks.MockTreatment TREATMENT =
330 new IntentTestsMocks.MockTreatment();
331
332 public static class MockFlowRule implements FlowRule {
333
334 int priority;
sangho11c30ac2015-01-22 14:30:55 -0800335 Type type;
336
Ray Milkey930fc662014-11-11 16:07:45 -0800337 public MockFlowRule(int priority) {
338 this.priority = priority;
sangho11c30ac2015-01-22 14:30:55 -0800339 this.type = Type.DEFAULT;
Ray Milkey930fc662014-11-11 16:07:45 -0800340 }
341
342 @Override
343 public FlowId id() {
344 return FlowId.valueOf(1);
345 }
346
347 @Override
348 public short appId() {
349 return 0;
350 }
351
352 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800353 public GroupId groupId() {
354 return new DefaultGroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800355 }
356
357 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800358 public int priority() {
359 return priority;
360 }
361
362 @Override
363 public DeviceId deviceId() {
364 return did("1");
365 }
366
367 @Override
368 public TrafficSelector selector() {
369 return SELECTOR;
370 }
371
372 @Override
373 public TrafficTreatment treatment() {
374 return TREATMENT;
375 }
376
377 @Override
378 public int timeout() {
379 return 0;
380 }
381
382 @Override
383 public boolean isPermanent() {
384 return false;
385 }
386
Brian O'Connor427a1762014-11-19 18:40:32 -0800387 @Override
388 public int hashCode() {
389 return Objects.hash(priority);
390 }
Ray Milkey930fc662014-11-11 16:07:45 -0800391
Brian O'Connor427a1762014-11-19 18:40:32 -0800392 @Override
393 public boolean equals(Object obj) {
394 if (this == obj) {
395 return true;
396 }
397 if (obj == null || getClass() != obj.getClass()) {
398 return false;
399 }
400 final MockFlowRule other = (MockFlowRule) obj;
401 return Objects.equals(this.priority, other.priority);
402 }
sangho11c30ac2015-01-22 14:30:55 -0800403
404 @Override
405 public Type type() {
406 return type;
407 }
Ray Milkey930fc662014-11-11 16:07:45 -0800408 }
409
Ray Milkey5a7787a2015-02-23 11:44:30 -0800410 public static class MockIntent extends Intent {
411 private static AtomicLong counter = new AtomicLong(0);
412
413 private final Long number;
414
415 public MockIntent(Long number) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700416 super(NetTestTools.APP_ID, null, Collections.emptyList(),
417 Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey5a7787a2015-02-23 11:44:30 -0800418 this.number = number;
419 }
420
Ray Milkey43a28222015-02-23 13:57:58 -0800421 public MockIntent(Long number, Collection<NetworkResource> resources) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700422 super(NetTestTools.APP_ID, null, resources, Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey43a28222015-02-23 13:57:58 -0800423 this.number = number;
424 }
425
Ray Milkey5a7787a2015-02-23 11:44:30 -0800426 public Long number() {
427 return number;
428 }
429
430 public static Long nextId() {
431 return counter.getAndIncrement();
432 }
Ray Milkey43a28222015-02-23 13:57:58 -0800433
434 @Override
435 public String toString() {
436 return MoreObjects.toStringHelper(getClass())
437 .add("id", id())
438 .add("appId", appId())
439 .toString();
440 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800441 }
442
443 public static class MockTimestamp implements Timestamp {
444 final int value;
445
446 public MockTimestamp(int value) {
447 this.value = value;
448 }
449
450 @Override
451 public int compareTo(Timestamp o) {
452 if (!(o instanceof MockTimestamp)) {
453 return -1;
454 }
455 MockTimestamp that = (MockTimestamp) o;
Jonathan Hart72175c22015-03-24 18:55:58 -0700456 return this.value - that.value;
Ray Milkey5a7787a2015-02-23 11:44:30 -0800457 }
458 }
459
Ray Milkeye6684082014-10-16 16:59:47 -0700460}