blob: ac4b44b92456d0fdc5e857935652d78fb99bc3d3 [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;
50import org.onosproject.net.resource.LambdaResourceRequest;
51import org.onosproject.net.resource.LinkResourceAllocations;
52import org.onosproject.net.resource.LinkResourceListener;
53import org.onosproject.net.resource.LinkResourceRequest;
54import org.onosproject.net.resource.LinkResourceService;
Ray Milkeybb320482015-02-25 15:16:46 -080055import org.onosproject.net.resource.MplsLabel;
56import org.onosproject.net.resource.MplsLabelResourceAllocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080057import org.onosproject.net.resource.ResourceAllocation;
58import org.onosproject.net.resource.ResourceRequest;
59import org.onosproject.net.resource.ResourceType;
60import org.onosproject.net.topology.DefaultTopologyEdge;
61import org.onosproject.net.topology.DefaultTopologyVertex;
62import org.onosproject.net.topology.LinkWeight;
63import org.onosproject.net.topology.PathService;
64import org.onosproject.net.topology.TopologyVertex;
Ray Milkey5a7787a2015-02-23 11:44:30 -080065import org.onosproject.store.Timestamp;
Ray Milkeye6684082014-10-16 16:59:47 -070066
Ray Milkey43a28222015-02-23 13:57:58 -080067import com.google.common.base.MoreObjects;
Ray Milkeybb320482015-02-25 15:16:46 -080068import com.google.common.collect.ImmutableSet;
Ray Milkey43a28222015-02-23 13:57:58 -080069
Ray Milkeye6684082014-10-16 16:59:47 -070070/**
71 * Common mocks used by the intent framework tests.
72 */
73public class IntentTestsMocks {
74 /**
75 * Mock traffic selector class used for satisfying API requirements.
76 */
77 public static class MockSelector implements TrafficSelector {
78 @Override
79 public Set<Criterion> criteria() {
80 return new HashSet<>();
81 }
Jonathan Hart936c49d2014-10-23 16:38:59 -070082
83 @Override
84 public Criterion getCriterion(Type type) {
85 return null;
86 }
Ray Milkeye6684082014-10-16 16:59:47 -070087 }
88
89 /**
90 * Mock traffic treatment class used for satisfying API requirements.
91 */
92 public static class MockTreatment implements TrafficTreatment {
93 @Override
94 public List<Instruction> instructions() {
Ayaka Koshibeb1224ab2015-01-31 00:48:58 +000095 return new ArrayList<>();
Ray Milkeye6684082014-10-16 16:59:47 -070096 }
97 }
98
99 /**
100 * Mock path service for creating paths within the test.
101 */
102 public static class MockPathService implements PathService {
103
104 final String[] pathHops;
105 final String[] reversePathHops;
106
107 /**
108 * Constructor that provides a set of hops to mock.
109 *
110 * @param pathHops path hops to mock
111 */
112 public MockPathService(String[] pathHops) {
113 this.pathHops = pathHops;
114 String[] reversed = pathHops.clone();
115 Collections.reverse(Arrays.asList(reversed));
116 reversePathHops = reversed;
117 }
118
119 @Override
120 public Set<Path> getPaths(ElementId src, ElementId dst) {
121 Set<Path> result = new HashSet<>();
122
123 String[] allHops = new String[pathHops.length];
124
125 if (src.toString().endsWith(pathHops[0])) {
126 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
127 } else {
128 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
129 }
130
131 result.add(createPath(allHops));
132 return result;
133 }
134
135 @Override
136 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800137 final Set<Path> paths = getPaths(src, dst);
138
139 for (Path path : paths) {
140 final DeviceId srcDevice = path.src().deviceId();
141 final DeviceId dstDevice = path.dst().deviceId();
142 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
143 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
144 final Link link = link(src.toString(), 1, dst.toString(), 1);
145
146 final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
147 if (weightValue < 0) {
148 return new HashSet<>();
149 }
150 }
151 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700152 }
153 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800154
155 public static class MockLinkResourceAllocations implements LinkResourceAllocations {
156 @Override
157 public Set<ResourceAllocation> getResourceAllocation(Link link) {
Ray Milkeybb320482015-02-25 15:16:46 -0800158 return ImmutableSet.of(new MplsLabelResourceAllocation(MplsLabel.valueOf(10)));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800159 }
160
161 @Override
162 public IntentId intendId() {
163 return null;
164 }
165
166 @Override
167 public Collection<Link> links() {
168 return null;
169 }
170
171 @Override
172 public Set<ResourceRequest> resources() {
173 return null;
174 }
175
176 @Override
177 public ResourceType type() {
178 return null;
179 }
180 }
181
182 public static class MockedAllocationFailure extends RuntimeException { }
183
184 public static class MockResourceService implements LinkResourceService {
185
186 double availableBandwidth = -1.0;
187 int availableLambda = -1;
188
189 /**
190 * Allocates a resource service that will allow bandwidth allocations
191 * up to a limit.
192 *
193 * @param bandwidth available bandwidth limit
194 * @return resource manager for bandwidth requests
195 */
196 public static MockResourceService makeBandwidthResourceService(double bandwidth) {
197 final MockResourceService result = new MockResourceService();
198 result.availableBandwidth = bandwidth;
199 return result;
200 }
201
202 /**
203 * Allocates a resource service that will allow lambda allocations.
204 *
205 * @param lambda Lambda to return for allocation requests. Currently unused
206 * @return resource manager for lambda requests
207 */
208 public static MockResourceService makeLambdaResourceService(int lambda) {
209 final MockResourceService result = new MockResourceService();
210 result.availableLambda = lambda;
211 return result;
212 }
213
214 public void setAvailableBandwidth(double availableBandwidth) {
215 this.availableBandwidth = availableBandwidth;
216 }
217
218 public void setAvailableLambda(int availableLambda) {
219 this.availableLambda = availableLambda;
220 }
221
222
223 @Override
224 public LinkResourceAllocations requestResources(LinkResourceRequest req) {
225 int lambda = -1;
226 double bandwidth = -1.0;
227
228 for (ResourceRequest resourceRequest : req.resources()) {
229 if (resourceRequest.type() == ResourceType.BANDWIDTH) {
230 final BandwidthResourceRequest brr = (BandwidthResourceRequest) resourceRequest;
231 bandwidth = brr.bandwidth().toDouble();
232 } else if (resourceRequest.type() == ResourceType.LAMBDA) {
233 lambda = 1;
234 }
235 }
236
237 if (availableBandwidth < bandwidth) {
238 throw new MockedAllocationFailure();
239 }
240 if (lambda > 0 && availableLambda == 0) {
241 throw new MockedAllocationFailure();
242 }
243
244 return new IntentTestsMocks.MockLinkResourceAllocations();
245 }
246
247 @Override
248 public void releaseResources(LinkResourceAllocations allocations) {
249 // Mock
250 }
251
252 @Override
253 public LinkResourceAllocations updateResources(LinkResourceRequest req,
254 LinkResourceAllocations oldAllocations) {
255 return null;
256 }
257
258 @Override
259 public Iterable<LinkResourceAllocations> getAllocations() {
Ray Milkeybb320482015-02-25 15:16:46 -0800260 return ImmutableSet.of(
261 new IntentTestsMocks.MockLinkResourceAllocations());
Ray Milkey8d3ce432014-11-07 16:21:10 -0800262 }
263
264 @Override
265 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
Ray Milkeybb320482015-02-25 15:16:46 -0800266 return ImmutableSet.of(
267 new IntentTestsMocks.MockLinkResourceAllocations());
Ray Milkey8d3ce432014-11-07 16:21:10 -0800268 }
269
270 @Override
271 public LinkResourceAllocations getAllocations(IntentId intentId) {
Ray Milkeybb320482015-02-25 15:16:46 -0800272 return new IntentTestsMocks.MockLinkResourceAllocations();
Ray Milkey8d3ce432014-11-07 16:21:10 -0800273 }
274
275 @Override
276 public Iterable<ResourceRequest> getAvailableResources(Link link) {
277 final List<ResourceRequest> result = new LinkedList<>();
278 if (availableBandwidth > 0.0) {
Ray Milkeycf590df2015-02-23 17:43:24 -0800279 result.add(new BandwidthResourceRequest(
280 Bandwidth.bps(availableBandwidth)));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800281 }
282 if (availableLambda > 0) {
283 result.add(new LambdaResourceRequest());
284 }
285 return result;
286 }
287
288 @Override
weibit00c94f52014-11-16 07:09:05 -0800289 public Iterable<ResourceRequest> getAvailableResources(Link link, LinkResourceAllocations allocations) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800290 return null;
291 }
Ray Milkeye97ede92014-11-20 10:43:12 -0800292
293 @Override
294 public void addListener(LinkResourceListener listener) {
295
296 }
297
298 @Override
299 public void removeListener(LinkResourceListener listener) {
300
301 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800302 }
303
Ray Milkey930fc662014-11-11 16:07:45 -0800304 private static final IntentTestsMocks.MockSelector SELECTOR =
305 new IntentTestsMocks.MockSelector();
306 private static final IntentTestsMocks.MockTreatment TREATMENT =
307 new IntentTestsMocks.MockTreatment();
308
309 public static class MockFlowRule implements FlowRule {
310
311 int priority;
sangho11c30ac2015-01-22 14:30:55 -0800312 Type type;
313
Ray Milkey930fc662014-11-11 16:07:45 -0800314 public MockFlowRule(int priority) {
315 this.priority = priority;
sangho11c30ac2015-01-22 14:30:55 -0800316 this.type = Type.DEFAULT;
Ray Milkey930fc662014-11-11 16:07:45 -0800317 }
318
319 @Override
320 public FlowId id() {
321 return FlowId.valueOf(1);
322 }
323
324 @Override
325 public short appId() {
326 return 0;
327 }
328
329 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800330 public GroupId groupId() {
331 return new DefaultGroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800332 }
333
334 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800335 public int priority() {
336 return priority;
337 }
338
339 @Override
340 public DeviceId deviceId() {
341 return did("1");
342 }
343
344 @Override
345 public TrafficSelector selector() {
346 return SELECTOR;
347 }
348
349 @Override
350 public TrafficTreatment treatment() {
351 return TREATMENT;
352 }
353
354 @Override
355 public int timeout() {
356 return 0;
357 }
358
359 @Override
360 public boolean isPermanent() {
361 return false;
362 }
363
Brian O'Connor427a1762014-11-19 18:40:32 -0800364 @Override
365 public int hashCode() {
366 return Objects.hash(priority);
367 }
Ray Milkey930fc662014-11-11 16:07:45 -0800368
Brian O'Connor427a1762014-11-19 18:40:32 -0800369 @Override
370 public boolean equals(Object obj) {
371 if (this == obj) {
372 return true;
373 }
374 if (obj == null || getClass() != obj.getClass()) {
375 return false;
376 }
377 final MockFlowRule other = (MockFlowRule) obj;
378 return Objects.equals(this.priority, other.priority);
379 }
sangho11c30ac2015-01-22 14:30:55 -0800380
381 @Override
382 public Type type() {
383 return type;
384 }
Ray Milkey930fc662014-11-11 16:07:45 -0800385 }
386
Ray Milkey5a7787a2015-02-23 11:44:30 -0800387 public static class MockIntent extends Intent {
388 private static AtomicLong counter = new AtomicLong(0);
389
390 private final Long number;
391
392 public MockIntent(Long number) {
393 super(NetTestTools.APP_ID, Collections.emptyList());
394 this.number = number;
395 }
396
Ray Milkey43a28222015-02-23 13:57:58 -0800397 public MockIntent(Long number, Collection<NetworkResource> resources) {
398 super(NetTestTools.APP_ID, resources);
399 this.number = number;
400 }
401
Ray Milkey5a7787a2015-02-23 11:44:30 -0800402 public Long number() {
403 return number;
404 }
405
406 public static Long nextId() {
407 return counter.getAndIncrement();
408 }
Ray Milkey43a28222015-02-23 13:57:58 -0800409
410 @Override
411 public String toString() {
412 return MoreObjects.toStringHelper(getClass())
413 .add("id", id())
414 .add("appId", appId())
415 .toString();
416 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800417 }
418
419 public static class MockTimestamp implements Timestamp {
420 final int value;
421
422 public MockTimestamp(int value) {
423 this.value = value;
424 }
425
426 @Override
427 public int compareTo(Timestamp o) {
428 if (!(o instanceof MockTimestamp)) {
429 return -1;
430 }
431 MockTimestamp that = (MockTimestamp) o;
432 return (this.value > that.value ? -1 : (this.value == that.value ? 0 : 1));
433 }
434 }
435
Ray Milkey930fc662014-11-11 16:07:45 -0800436
Ray Milkeye6684082014-10-16 16:59:47 -0700437}