blob: 3951f0f18cf6d072e5ae6c26b5b56232b408baf2 [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
Brian O'Connorabafb502014-12-02 22:26:20 -080018import static org.onosproject.net.NetTestTools.createPath;
19import static org.onosproject.net.NetTestTools.did;
20import static org.onosproject.net.NetTestTools.link;
Jonathan Hart936c49d2014-10-23 16:38:59 -070021
Ray Milkeye6684082014-10-16 16:59:47 -070022import java.util.ArrayList;
23import java.util.Arrays;
Ray Milkey8d3ce432014-11-07 16:21:10 -080024import java.util.Collection;
Ray Milkeye6684082014-10-16 16:59:47 -070025import java.util.Collections;
26import java.util.HashSet;
Ray Milkey8d3ce432014-11-07 16:21:10 -080027import java.util.LinkedList;
Ray Milkeye6684082014-10-16 16:59:47 -070028import java.util.List;
Brian O'Connor427a1762014-11-19 18:40:32 -080029import java.util.Objects;
Ray Milkeye6684082014-10-16 16:59:47 -070030import java.util.Set;
Ray Milkey5a7787a2015-02-23 11:44:30 -080031import java.util.concurrent.atomic.AtomicLong;
Ray Milkeye6684082014-10-16 16:59:47 -070032
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.core.DefaultGroupId;
34import org.onosproject.core.GroupId;
35import org.onosproject.net.DeviceId;
36import org.onosproject.net.ElementId;
37import org.onosproject.net.Link;
Ray Milkey5a7787a2015-02-23 11:44:30 -080038import org.onosproject.net.NetTestTools;
Ray Milkey43a28222015-02-23 13:57:58 -080039import org.onosproject.net.NetworkResource;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.Path;
41import org.onosproject.net.flow.FlowId;
42import org.onosproject.net.flow.FlowRule;
43import org.onosproject.net.flow.TrafficSelector;
44import org.onosproject.net.flow.TrafficTreatment;
45import org.onosproject.net.flow.criteria.Criterion;
46import org.onosproject.net.flow.criteria.Criterion.Type;
47import org.onosproject.net.flow.instructions.Instruction;
Ray Milkeycf590df2015-02-23 17:43:24 -080048import org.onosproject.net.resource.Bandwidth;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.resource.BandwidthResourceRequest;
Ray Milkeyb1b66d32015-03-02 10:27:11 -080050import org.onosproject.net.resource.Lambda;
51import org.onosproject.net.resource.LambdaResourceAllocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080052import org.onosproject.net.resource.LambdaResourceRequest;
53import org.onosproject.net.resource.LinkResourceAllocations;
54import org.onosproject.net.resource.LinkResourceListener;
55import org.onosproject.net.resource.LinkResourceRequest;
56import org.onosproject.net.resource.LinkResourceService;
Ray Milkeybb320482015-02-25 15:16:46 -080057import org.onosproject.net.resource.MplsLabel;
58import org.onosproject.net.resource.MplsLabelResourceAllocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080059import org.onosproject.net.resource.ResourceAllocation;
60import org.onosproject.net.resource.ResourceRequest;
61import org.onosproject.net.resource.ResourceType;
62import org.onosproject.net.topology.DefaultTopologyEdge;
63import org.onosproject.net.topology.DefaultTopologyVertex;
64import org.onosproject.net.topology.LinkWeight;
65import org.onosproject.net.topology.PathService;
66import org.onosproject.net.topology.TopologyVertex;
Ray Milkey5a7787a2015-02-23 11:44:30 -080067import org.onosproject.store.Timestamp;
Ray Milkeye6684082014-10-16 16:59:47 -070068
Ray Milkey43a28222015-02-23 13:57:58 -080069import com.google.common.base.MoreObjects;
Ray Milkeybb320482015-02-25 15:16:46 -080070import com.google.common.collect.ImmutableSet;
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 }
99 }
100
101 /**
102 * Mock path service for creating paths within the test.
103 */
104 public static class MockPathService implements PathService {
105
106 final String[] pathHops;
107 final String[] reversePathHops;
108
109 /**
110 * Constructor that provides a set of hops to mock.
111 *
112 * @param pathHops path hops to mock
113 */
114 public MockPathService(String[] pathHops) {
115 this.pathHops = pathHops;
116 String[] reversed = pathHops.clone();
117 Collections.reverse(Arrays.asList(reversed));
118 reversePathHops = reversed;
119 }
120
121 @Override
122 public Set<Path> getPaths(ElementId src, ElementId dst) {
123 Set<Path> result = new HashSet<>();
124
125 String[] allHops = new String[pathHops.length];
126
127 if (src.toString().endsWith(pathHops[0])) {
128 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
129 } else {
130 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
131 }
132
133 result.add(createPath(allHops));
134 return result;
135 }
136
137 @Override
138 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800139 final Set<Path> paths = getPaths(src, dst);
140
141 for (Path path : paths) {
142 final DeviceId srcDevice = path.src().deviceId();
143 final DeviceId dstDevice = path.dst().deviceId();
144 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
145 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
146 final Link link = link(src.toString(), 1, dst.toString(), 1);
147
148 final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
149 if (weightValue < 0) {
150 return new HashSet<>();
151 }
152 }
153 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700154 }
155 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800156
157 public static class MockLinkResourceAllocations implements LinkResourceAllocations {
158 @Override
159 public Set<ResourceAllocation> getResourceAllocation(Link link) {
Ray Milkeyb1b66d32015-03-02 10:27:11 -0800160 return ImmutableSet.of(
161 new LambdaResourceAllocation(Lambda.valueOf(77)),
162 new MplsLabelResourceAllocation(MplsLabel.valueOf(10)));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800163 }
164
165 @Override
166 public IntentId intendId() {
167 return null;
168 }
169
170 @Override
171 public Collection<Link> links() {
172 return null;
173 }
174
175 @Override
176 public Set<ResourceRequest> resources() {
177 return null;
178 }
179
180 @Override
181 public ResourceType type() {
182 return null;
183 }
184 }
185
186 public static class MockedAllocationFailure extends RuntimeException { }
187
188 public static class MockResourceService implements LinkResourceService {
189
190 double availableBandwidth = -1.0;
191 int availableLambda = -1;
192
193 /**
194 * Allocates a resource service that will allow bandwidth allocations
195 * up to a limit.
196 *
197 * @param bandwidth available bandwidth limit
198 * @return resource manager for bandwidth requests
199 */
200 public static MockResourceService makeBandwidthResourceService(double bandwidth) {
201 final MockResourceService result = new MockResourceService();
202 result.availableBandwidth = bandwidth;
203 return result;
204 }
205
206 /**
207 * Allocates a resource service that will allow lambda allocations.
208 *
209 * @param lambda Lambda to return for allocation requests. Currently unused
210 * @return resource manager for lambda requests
211 */
212 public static MockResourceService makeLambdaResourceService(int lambda) {
213 final MockResourceService result = new MockResourceService();
214 result.availableLambda = lambda;
215 return result;
216 }
217
218 public void setAvailableBandwidth(double availableBandwidth) {
219 this.availableBandwidth = availableBandwidth;
220 }
221
222 public void setAvailableLambda(int availableLambda) {
223 this.availableLambda = availableLambda;
224 }
225
226
227 @Override
228 public LinkResourceAllocations requestResources(LinkResourceRequest req) {
229 int lambda = -1;
230 double bandwidth = -1.0;
231
232 for (ResourceRequest resourceRequest : req.resources()) {
233 if (resourceRequest.type() == ResourceType.BANDWIDTH) {
234 final BandwidthResourceRequest brr = (BandwidthResourceRequest) resourceRequest;
235 bandwidth = brr.bandwidth().toDouble();
236 } else if (resourceRequest.type() == ResourceType.LAMBDA) {
237 lambda = 1;
238 }
239 }
240
241 if (availableBandwidth < bandwidth) {
242 throw new MockedAllocationFailure();
243 }
244 if (lambda > 0 && availableLambda == 0) {
245 throw new MockedAllocationFailure();
246 }
247
248 return new IntentTestsMocks.MockLinkResourceAllocations();
249 }
250
251 @Override
252 public void releaseResources(LinkResourceAllocations allocations) {
253 // Mock
254 }
255
256 @Override
257 public LinkResourceAllocations updateResources(LinkResourceRequest req,
258 LinkResourceAllocations oldAllocations) {
259 return null;
260 }
261
262 @Override
263 public Iterable<LinkResourceAllocations> getAllocations() {
Ray Milkeybb320482015-02-25 15:16:46 -0800264 return ImmutableSet.of(
265 new IntentTestsMocks.MockLinkResourceAllocations());
Ray Milkey8d3ce432014-11-07 16:21:10 -0800266 }
267
268 @Override
269 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
Ray Milkeybb320482015-02-25 15:16:46 -0800270 return ImmutableSet.of(
271 new IntentTestsMocks.MockLinkResourceAllocations());
Ray Milkey8d3ce432014-11-07 16:21:10 -0800272 }
273
274 @Override
275 public LinkResourceAllocations getAllocations(IntentId intentId) {
Ray Milkeybb320482015-02-25 15:16:46 -0800276 return new IntentTestsMocks.MockLinkResourceAllocations();
Ray Milkey8d3ce432014-11-07 16:21:10 -0800277 }
278
279 @Override
280 public Iterable<ResourceRequest> getAvailableResources(Link link) {
281 final List<ResourceRequest> result = new LinkedList<>();
282 if (availableBandwidth > 0.0) {
Ray Milkeycf590df2015-02-23 17:43:24 -0800283 result.add(new BandwidthResourceRequest(
284 Bandwidth.bps(availableBandwidth)));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800285 }
286 if (availableLambda > 0) {
287 result.add(new LambdaResourceRequest());
288 }
289 return result;
290 }
291
292 @Override
weibit00c94f52014-11-16 07:09:05 -0800293 public Iterable<ResourceRequest> getAvailableResources(Link link, LinkResourceAllocations allocations) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800294 return null;
295 }
Ray Milkeye97ede92014-11-20 10:43:12 -0800296
297 @Override
298 public void addListener(LinkResourceListener listener) {
299
300 }
301
302 @Override
303 public void removeListener(LinkResourceListener listener) {
304
305 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800306 }
307
Ray Milkey930fc662014-11-11 16:07:45 -0800308 private static final IntentTestsMocks.MockSelector SELECTOR =
309 new IntentTestsMocks.MockSelector();
310 private static final IntentTestsMocks.MockTreatment TREATMENT =
311 new IntentTestsMocks.MockTreatment();
312
313 public static class MockFlowRule implements FlowRule {
314
315 int priority;
sangho11c30ac2015-01-22 14:30:55 -0800316 Type type;
317
Ray Milkey930fc662014-11-11 16:07:45 -0800318 public MockFlowRule(int priority) {
319 this.priority = priority;
sangho11c30ac2015-01-22 14:30:55 -0800320 this.type = Type.DEFAULT;
Ray Milkey930fc662014-11-11 16:07:45 -0800321 }
322
323 @Override
324 public FlowId id() {
325 return FlowId.valueOf(1);
326 }
327
328 @Override
329 public short appId() {
330 return 0;
331 }
332
333 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800334 public GroupId groupId() {
335 return new DefaultGroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800336 }
337
338 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800339 public int priority() {
340 return priority;
341 }
342
343 @Override
344 public DeviceId deviceId() {
345 return did("1");
346 }
347
348 @Override
349 public TrafficSelector selector() {
350 return SELECTOR;
351 }
352
353 @Override
354 public TrafficTreatment treatment() {
355 return TREATMENT;
356 }
357
358 @Override
359 public int timeout() {
360 return 0;
361 }
362
363 @Override
364 public boolean isPermanent() {
365 return false;
366 }
367
Brian O'Connor427a1762014-11-19 18:40:32 -0800368 @Override
369 public int hashCode() {
370 return Objects.hash(priority);
371 }
Ray Milkey930fc662014-11-11 16:07:45 -0800372
Brian O'Connor427a1762014-11-19 18:40:32 -0800373 @Override
374 public boolean equals(Object obj) {
375 if (this == obj) {
376 return true;
377 }
378 if (obj == null || getClass() != obj.getClass()) {
379 return false;
380 }
381 final MockFlowRule other = (MockFlowRule) obj;
382 return Objects.equals(this.priority, other.priority);
383 }
sangho11c30ac2015-01-22 14:30:55 -0800384
385 @Override
386 public Type type() {
387 return type;
388 }
Ray Milkey930fc662014-11-11 16:07:45 -0800389 }
390
Ray Milkey5a7787a2015-02-23 11:44:30 -0800391 public static class MockIntent extends Intent {
392 private static AtomicLong counter = new AtomicLong(0);
393
394 private final Long number;
395
396 public MockIntent(Long number) {
397 super(NetTestTools.APP_ID, Collections.emptyList());
398 this.number = number;
399 }
400
Ray Milkey43a28222015-02-23 13:57:58 -0800401 public MockIntent(Long number, Collection<NetworkResource> resources) {
402 super(NetTestTools.APP_ID, resources);
403 this.number = number;
404 }
405
Ray Milkey5a7787a2015-02-23 11:44:30 -0800406 public Long number() {
407 return number;
408 }
409
410 public static Long nextId() {
411 return counter.getAndIncrement();
412 }
Ray Milkey43a28222015-02-23 13:57:58 -0800413
414 @Override
415 public String toString() {
416 return MoreObjects.toStringHelper(getClass())
417 .add("id", id())
418 .add("appId", appId())
419 .toString();
420 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800421 }
422
423 public static class MockTimestamp implements Timestamp {
424 final int value;
425
426 public MockTimestamp(int value) {
427 this.value = value;
428 }
429
430 @Override
431 public int compareTo(Timestamp o) {
432 if (!(o instanceof MockTimestamp)) {
433 return -1;
434 }
435 MockTimestamp that = (MockTimestamp) o;
436 return (this.value > that.value ? -1 : (this.value == that.value ? 0 : 1));
437 }
438 }
439
Ray Milkey930fc662014-11-11 16:07:45 -0800440
Ray Milkeye6684082014-10-16 16:59:47 -0700441}