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