blob: 700df0eee5c4117ce6488bedc196f9db3f318de2 [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;
48import org.onosproject.net.resource.BandwidthResourceRequest;
49import org.onosproject.net.resource.LambdaResourceRequest;
50import org.onosproject.net.resource.LinkResourceAllocations;
51import org.onosproject.net.resource.LinkResourceListener;
52import org.onosproject.net.resource.LinkResourceRequest;
53import org.onosproject.net.resource.LinkResourceService;
54import org.onosproject.net.resource.ResourceAllocation;
55import org.onosproject.net.resource.ResourceRequest;
56import org.onosproject.net.resource.ResourceType;
57import org.onosproject.net.topology.DefaultTopologyEdge;
58import org.onosproject.net.topology.DefaultTopologyVertex;
59import org.onosproject.net.topology.LinkWeight;
60import org.onosproject.net.topology.PathService;
61import org.onosproject.net.topology.TopologyVertex;
Ray Milkey5a7787a2015-02-23 11:44:30 -080062import org.onosproject.store.Timestamp;
Ray Milkeye6684082014-10-16 16:59:47 -070063
Ray Milkey43a28222015-02-23 13:57:58 -080064import com.google.common.base.MoreObjects;
65
Ray Milkeye6684082014-10-16 16:59:47 -070066/**
67 * Common mocks used by the intent framework tests.
68 */
69public class IntentTestsMocks {
70 /**
71 * Mock traffic selector class used for satisfying API requirements.
72 */
73 public static class MockSelector implements TrafficSelector {
74 @Override
75 public Set<Criterion> criteria() {
76 return new HashSet<>();
77 }
Jonathan Hart936c49d2014-10-23 16:38:59 -070078
79 @Override
80 public Criterion getCriterion(Type type) {
81 return null;
82 }
Ray Milkeye6684082014-10-16 16:59:47 -070083 }
84
85 /**
86 * Mock traffic treatment class used for satisfying API requirements.
87 */
88 public static class MockTreatment implements TrafficTreatment {
89 @Override
90 public List<Instruction> instructions() {
Ayaka Koshibeb1224ab2015-01-31 00:48:58 +000091 return new ArrayList<>();
Ray Milkeye6684082014-10-16 16:59:47 -070092 }
93 }
94
95 /**
96 * Mock path service for creating paths within the test.
97 */
98 public static class MockPathService implements PathService {
99
100 final String[] pathHops;
101 final String[] reversePathHops;
102
103 /**
104 * Constructor that provides a set of hops to mock.
105 *
106 * @param pathHops path hops to mock
107 */
108 public MockPathService(String[] pathHops) {
109 this.pathHops = pathHops;
110 String[] reversed = pathHops.clone();
111 Collections.reverse(Arrays.asList(reversed));
112 reversePathHops = reversed;
113 }
114
115 @Override
116 public Set<Path> getPaths(ElementId src, ElementId dst) {
117 Set<Path> result = new HashSet<>();
118
119 String[] allHops = new String[pathHops.length];
120
121 if (src.toString().endsWith(pathHops[0])) {
122 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
123 } else {
124 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
125 }
126
127 result.add(createPath(allHops));
128 return result;
129 }
130
131 @Override
132 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800133 final Set<Path> paths = getPaths(src, dst);
134
135 for (Path path : paths) {
136 final DeviceId srcDevice = path.src().deviceId();
137 final DeviceId dstDevice = path.dst().deviceId();
138 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
139 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
140 final Link link = link(src.toString(), 1, dst.toString(), 1);
141
142 final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
143 if (weightValue < 0) {
144 return new HashSet<>();
145 }
146 }
147 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700148 }
149 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800150
151 public static class MockLinkResourceAllocations implements LinkResourceAllocations {
152 @Override
153 public Set<ResourceAllocation> getResourceAllocation(Link link) {
154 return null;
155 }
156
157 @Override
158 public IntentId intendId() {
159 return null;
160 }
161
162 @Override
163 public Collection<Link> links() {
164 return null;
165 }
166
167 @Override
168 public Set<ResourceRequest> resources() {
169 return null;
170 }
171
172 @Override
173 public ResourceType type() {
174 return null;
175 }
176 }
177
178 public static class MockedAllocationFailure extends RuntimeException { }
179
180 public static class MockResourceService implements LinkResourceService {
181
182 double availableBandwidth = -1.0;
183 int availableLambda = -1;
184
185 /**
186 * Allocates a resource service that will allow bandwidth allocations
187 * up to a limit.
188 *
189 * @param bandwidth available bandwidth limit
190 * @return resource manager for bandwidth requests
191 */
192 public static MockResourceService makeBandwidthResourceService(double bandwidth) {
193 final MockResourceService result = new MockResourceService();
194 result.availableBandwidth = bandwidth;
195 return result;
196 }
197
198 /**
199 * Allocates a resource service that will allow lambda allocations.
200 *
201 * @param lambda Lambda to return for allocation requests. Currently unused
202 * @return resource manager for lambda requests
203 */
204 public static MockResourceService makeLambdaResourceService(int lambda) {
205 final MockResourceService result = new MockResourceService();
206 result.availableLambda = lambda;
207 return result;
208 }
209
210 public void setAvailableBandwidth(double availableBandwidth) {
211 this.availableBandwidth = availableBandwidth;
212 }
213
214 public void setAvailableLambda(int availableLambda) {
215 this.availableLambda = availableLambda;
216 }
217
218
219 @Override
220 public LinkResourceAllocations requestResources(LinkResourceRequest req) {
221 int lambda = -1;
222 double bandwidth = -1.0;
223
224 for (ResourceRequest resourceRequest : req.resources()) {
225 if (resourceRequest.type() == ResourceType.BANDWIDTH) {
226 final BandwidthResourceRequest brr = (BandwidthResourceRequest) resourceRequest;
227 bandwidth = brr.bandwidth().toDouble();
228 } else if (resourceRequest.type() == ResourceType.LAMBDA) {
229 lambda = 1;
230 }
231 }
232
233 if (availableBandwidth < bandwidth) {
234 throw new MockedAllocationFailure();
235 }
236 if (lambda > 0 && availableLambda == 0) {
237 throw new MockedAllocationFailure();
238 }
239
240 return new IntentTestsMocks.MockLinkResourceAllocations();
241 }
242
243 @Override
244 public void releaseResources(LinkResourceAllocations allocations) {
245 // Mock
246 }
247
248 @Override
249 public LinkResourceAllocations updateResources(LinkResourceRequest req,
250 LinkResourceAllocations oldAllocations) {
251 return null;
252 }
253
254 @Override
255 public Iterable<LinkResourceAllocations> getAllocations() {
256 return null;
257 }
258
259 @Override
260 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
261 return null;
262 }
263
264 @Override
265 public LinkResourceAllocations getAllocations(IntentId intentId) {
266 return null;
267 }
268
269 @Override
270 public Iterable<ResourceRequest> getAvailableResources(Link link) {
271 final List<ResourceRequest> result = new LinkedList<>();
272 if (availableBandwidth > 0.0) {
273 result.add(new BandwidthResourceRequest(availableBandwidth));
274 }
275 if (availableLambda > 0) {
276 result.add(new LambdaResourceRequest());
277 }
278 return result;
279 }
280
281 @Override
weibit00c94f52014-11-16 07:09:05 -0800282 public Iterable<ResourceRequest> getAvailableResources(Link link, LinkResourceAllocations allocations) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800283 return null;
284 }
Ray Milkeye97ede92014-11-20 10:43:12 -0800285
286 @Override
287 public void addListener(LinkResourceListener listener) {
288
289 }
290
291 @Override
292 public void removeListener(LinkResourceListener listener) {
293
294 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800295 }
296
Ray Milkey930fc662014-11-11 16:07:45 -0800297 private static final IntentTestsMocks.MockSelector SELECTOR =
298 new IntentTestsMocks.MockSelector();
299 private static final IntentTestsMocks.MockTreatment TREATMENT =
300 new IntentTestsMocks.MockTreatment();
301
302 public static class MockFlowRule implements FlowRule {
303
304 int priority;
sangho11c30ac2015-01-22 14:30:55 -0800305 Type type;
306
Ray Milkey930fc662014-11-11 16:07:45 -0800307 public MockFlowRule(int priority) {
308 this.priority = priority;
sangho11c30ac2015-01-22 14:30:55 -0800309 this.type = Type.DEFAULT;
Ray Milkey930fc662014-11-11 16:07:45 -0800310 }
311
312 @Override
313 public FlowId id() {
314 return FlowId.valueOf(1);
315 }
316
317 @Override
318 public short appId() {
319 return 0;
320 }
321
322 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800323 public GroupId groupId() {
324 return new DefaultGroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800325 }
326
327 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800328 public int priority() {
329 return priority;
330 }
331
332 @Override
333 public DeviceId deviceId() {
334 return did("1");
335 }
336
337 @Override
338 public TrafficSelector selector() {
339 return SELECTOR;
340 }
341
342 @Override
343 public TrafficTreatment treatment() {
344 return TREATMENT;
345 }
346
347 @Override
348 public int timeout() {
349 return 0;
350 }
351
352 @Override
353 public boolean isPermanent() {
354 return false;
355 }
356
Brian O'Connor427a1762014-11-19 18:40:32 -0800357 @Override
358 public int hashCode() {
359 return Objects.hash(priority);
360 }
Ray Milkey930fc662014-11-11 16:07:45 -0800361
Brian O'Connor427a1762014-11-19 18:40:32 -0800362 @Override
363 public boolean equals(Object obj) {
364 if (this == obj) {
365 return true;
366 }
367 if (obj == null || getClass() != obj.getClass()) {
368 return false;
369 }
370 final MockFlowRule other = (MockFlowRule) obj;
371 return Objects.equals(this.priority, other.priority);
372 }
sangho11c30ac2015-01-22 14:30:55 -0800373
374 @Override
375 public Type type() {
376 return type;
377 }
Ray Milkey930fc662014-11-11 16:07:45 -0800378 }
379
Ray Milkey5a7787a2015-02-23 11:44:30 -0800380 public static class MockIntent extends Intent {
381 private static AtomicLong counter = new AtomicLong(0);
382
383 private final Long number;
384
385 public MockIntent(Long number) {
386 super(NetTestTools.APP_ID, Collections.emptyList());
387 this.number = number;
388 }
389
Ray Milkey43a28222015-02-23 13:57:58 -0800390 public MockIntent(Long number, Collection<NetworkResource> resources) {
391 super(NetTestTools.APP_ID, resources);
392 this.number = number;
393 }
394
Ray Milkey5a7787a2015-02-23 11:44:30 -0800395 public Long number() {
396 return number;
397 }
398
399 public static Long nextId() {
400 return counter.getAndIncrement();
401 }
Ray Milkey43a28222015-02-23 13:57:58 -0800402
403 @Override
404 public String toString() {
405 return MoreObjects.toStringHelper(getClass())
406 .add("id", id())
407 .add("appId", appId())
408 .toString();
409 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800410 }
411
412 public static class MockTimestamp implements Timestamp {
413 final int value;
414
415 public MockTimestamp(int value) {
416 this.value = value;
417 }
418
419 @Override
420 public int compareTo(Timestamp o) {
421 if (!(o instanceof MockTimestamp)) {
422 return -1;
423 }
424 MockTimestamp that = (MockTimestamp) o;
425 return (this.value > that.value ? -1 : (this.value == that.value ? 0 : 1));
426 }
427 }
428
Ray Milkey930fc662014-11-11 16:07:45 -0800429
Ray Milkeye6684082014-10-16 16:59:47 -0700430}