blob: 0e22a7997cae74c885f85cb6973306f28e722d1a [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 */
Ray Milkeye6684082014-10-16 16:59:47 -070016package org.onlab.onos.net.intent;
17
Jonathan Hart936c49d2014-10-23 16:38:59 -070018import static org.onlab.onos.net.NetTestTools.createPath;
Ray Milkey930fc662014-11-11 16:07:45 -080019import static org.onlab.onos.net.NetTestTools.did;
Ray Milkey8d3ce432014-11-07 16:21:10 -080020import static org.onlab.onos.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;
31
Ray Milkey8d3ce432014-11-07 16:21:10 -080032import org.onlab.onos.net.DeviceId;
Ray Milkeye6684082014-10-16 16:59:47 -070033import org.onlab.onos.net.ElementId;
Ray Milkey8d3ce432014-11-07 16:21:10 -080034import org.onlab.onos.net.Link;
Ray Milkeye6684082014-10-16 16:59:47 -070035import org.onlab.onos.net.Path;
Ray Milkey930fc662014-11-11 16:07:45 -080036import org.onlab.onos.net.flow.FlowId;
37import org.onlab.onos.net.flow.FlowRule;
Ray Milkeye6684082014-10-16 16:59:47 -070038import org.onlab.onos.net.flow.TrafficSelector;
39import org.onlab.onos.net.flow.TrafficTreatment;
40import org.onlab.onos.net.flow.criteria.Criterion;
Jonathan Hart936c49d2014-10-23 16:38:59 -070041import org.onlab.onos.net.flow.criteria.Criterion.Type;
Ray Milkeye6684082014-10-16 16:59:47 -070042import org.onlab.onos.net.flow.instructions.Instruction;
Ray Milkey8d3ce432014-11-07 16:21:10 -080043import org.onlab.onos.net.resource.BandwidthResourceRequest;
44import org.onlab.onos.net.resource.LambdaResourceRequest;
45import org.onlab.onos.net.resource.LinkResourceAllocations;
Ray Milkeye97ede92014-11-20 10:43:12 -080046import org.onlab.onos.net.resource.LinkResourceListener;
Ray Milkey8d3ce432014-11-07 16:21:10 -080047import org.onlab.onos.net.resource.LinkResourceRequest;
48import org.onlab.onos.net.resource.LinkResourceService;
49import org.onlab.onos.net.resource.ResourceAllocation;
50import org.onlab.onos.net.resource.ResourceRequest;
51import org.onlab.onos.net.resource.ResourceType;
52import org.onlab.onos.net.topology.DefaultTopologyEdge;
53import org.onlab.onos.net.topology.DefaultTopologyVertex;
Ray Milkeye6684082014-10-16 16:59:47 -070054import org.onlab.onos.net.topology.LinkWeight;
55import org.onlab.onos.net.topology.PathService;
Ray Milkey8d3ce432014-11-07 16:21:10 -080056import org.onlab.onos.net.topology.TopologyVertex;
Ray Milkeye6684082014-10-16 16:59:47 -070057
Ray Milkeye6684082014-10-16 16:59:47 -070058/**
59 * Common mocks used by the intent framework tests.
60 */
61public class IntentTestsMocks {
62 /**
63 * Mock traffic selector class used for satisfying API requirements.
64 */
65 public static class MockSelector implements TrafficSelector {
66 @Override
67 public Set<Criterion> criteria() {
68 return new HashSet<>();
69 }
Jonathan Hart936c49d2014-10-23 16:38:59 -070070
71 @Override
72 public Criterion getCriterion(Type type) {
73 return null;
74 }
Ray Milkeye6684082014-10-16 16:59:47 -070075 }
76
77 /**
78 * Mock traffic treatment class used for satisfying API requirements.
79 */
80 public static class MockTreatment implements TrafficTreatment {
81 @Override
82 public List<Instruction> instructions() {
83 return new ArrayList<>();
84 }
85 }
86
87 /**
88 * Mock path service for creating paths within the test.
89 */
90 public static class MockPathService implements PathService {
91
92 final String[] pathHops;
93 final String[] reversePathHops;
94
95 /**
96 * Constructor that provides a set of hops to mock.
97 *
98 * @param pathHops path hops to mock
99 */
100 public MockPathService(String[] pathHops) {
101 this.pathHops = pathHops;
102 String[] reversed = pathHops.clone();
103 Collections.reverse(Arrays.asList(reversed));
104 reversePathHops = reversed;
105 }
106
107 @Override
108 public Set<Path> getPaths(ElementId src, ElementId dst) {
109 Set<Path> result = new HashSet<>();
110
111 String[] allHops = new String[pathHops.length];
112
113 if (src.toString().endsWith(pathHops[0])) {
114 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
115 } else {
116 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
117 }
118
119 result.add(createPath(allHops));
120 return result;
121 }
122
123 @Override
124 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800125 final Set<Path> paths = getPaths(src, dst);
126
127 for (Path path : paths) {
128 final DeviceId srcDevice = path.src().deviceId();
129 final DeviceId dstDevice = path.dst().deviceId();
130 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
131 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
132 final Link link = link(src.toString(), 1, dst.toString(), 1);
133
134 final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
135 if (weightValue < 0) {
136 return new HashSet<>();
137 }
138 }
139 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700140 }
141 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800142
143 public static class MockLinkResourceAllocations implements LinkResourceAllocations {
144 @Override
145 public Set<ResourceAllocation> getResourceAllocation(Link link) {
146 return null;
147 }
148
149 @Override
150 public IntentId intendId() {
151 return null;
152 }
153
154 @Override
155 public Collection<Link> links() {
156 return null;
157 }
158
159 @Override
160 public Set<ResourceRequest> resources() {
161 return null;
162 }
163
164 @Override
165 public ResourceType type() {
166 return null;
167 }
168 }
169
170 public static class MockedAllocationFailure extends RuntimeException { }
171
172 public static class MockResourceService implements LinkResourceService {
173
174 double availableBandwidth = -1.0;
175 int availableLambda = -1;
176
177 /**
178 * Allocates a resource service that will allow bandwidth allocations
179 * up to a limit.
180 *
181 * @param bandwidth available bandwidth limit
182 * @return resource manager for bandwidth requests
183 */
184 public static MockResourceService makeBandwidthResourceService(double bandwidth) {
185 final MockResourceService result = new MockResourceService();
186 result.availableBandwidth = bandwidth;
187 return result;
188 }
189
190 /**
191 * Allocates a resource service that will allow lambda allocations.
192 *
193 * @param lambda Lambda to return for allocation requests. Currently unused
194 * @return resource manager for lambda requests
195 */
196 public static MockResourceService makeLambdaResourceService(int lambda) {
197 final MockResourceService result = new MockResourceService();
198 result.availableLambda = lambda;
199 return result;
200 }
201
202 public void setAvailableBandwidth(double availableBandwidth) {
203 this.availableBandwidth = availableBandwidth;
204 }
205
206 public void setAvailableLambda(int availableLambda) {
207 this.availableLambda = availableLambda;
208 }
209
210
211 @Override
212 public LinkResourceAllocations requestResources(LinkResourceRequest req) {
213 int lambda = -1;
214 double bandwidth = -1.0;
215
216 for (ResourceRequest resourceRequest : req.resources()) {
217 if (resourceRequest.type() == ResourceType.BANDWIDTH) {
218 final BandwidthResourceRequest brr = (BandwidthResourceRequest) resourceRequest;
219 bandwidth = brr.bandwidth().toDouble();
220 } else if (resourceRequest.type() == ResourceType.LAMBDA) {
221 lambda = 1;
222 }
223 }
224
225 if (availableBandwidth < bandwidth) {
226 throw new MockedAllocationFailure();
227 }
228 if (lambda > 0 && availableLambda == 0) {
229 throw new MockedAllocationFailure();
230 }
231
232 return new IntentTestsMocks.MockLinkResourceAllocations();
233 }
234
235 @Override
236 public void releaseResources(LinkResourceAllocations allocations) {
237 // Mock
238 }
239
240 @Override
241 public LinkResourceAllocations updateResources(LinkResourceRequest req,
242 LinkResourceAllocations oldAllocations) {
243 return null;
244 }
245
246 @Override
247 public Iterable<LinkResourceAllocations> getAllocations() {
248 return null;
249 }
250
251 @Override
252 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
253 return null;
254 }
255
256 @Override
257 public LinkResourceAllocations getAllocations(IntentId intentId) {
258 return null;
259 }
260
261 @Override
262 public Iterable<ResourceRequest> getAvailableResources(Link link) {
263 final List<ResourceRequest> result = new LinkedList<>();
264 if (availableBandwidth > 0.0) {
265 result.add(new BandwidthResourceRequest(availableBandwidth));
266 }
267 if (availableLambda > 0) {
268 result.add(new LambdaResourceRequest());
269 }
270 return result;
271 }
272
273 @Override
weibit00c94f52014-11-16 07:09:05 -0800274 public Iterable<ResourceRequest> getAvailableResources(Link link, LinkResourceAllocations allocations) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800275 return null;
276 }
Ray Milkeye97ede92014-11-20 10:43:12 -0800277
278 @Override
279 public void addListener(LinkResourceListener listener) {
280
281 }
282
283 @Override
284 public void removeListener(LinkResourceListener listener) {
285
286 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800287 }
288
Ray Milkey930fc662014-11-11 16:07:45 -0800289 private static final IntentTestsMocks.MockSelector SELECTOR =
290 new IntentTestsMocks.MockSelector();
291 private static final IntentTestsMocks.MockTreatment TREATMENT =
292 new IntentTestsMocks.MockTreatment();
293
294 public static class MockFlowRule implements FlowRule {
295
296 int priority;
297 public MockFlowRule(int priority) {
298 this.priority = priority;
299 }
300
301 @Override
302 public FlowId id() {
303 return FlowId.valueOf(1);
304 }
305
306 @Override
307 public short appId() {
308 return 0;
309 }
310
311 @Override
alshabib28204e52014-11-12 18:29:45 -0800312 public short groupId() {
313 return 0;
314 }
315
316 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800317 public int priority() {
318 return priority;
319 }
320
321 @Override
322 public DeviceId deviceId() {
323 return did("1");
324 }
325
326 @Override
327 public TrafficSelector selector() {
328 return SELECTOR;
329 }
330
331 @Override
332 public TrafficTreatment treatment() {
333 return TREATMENT;
334 }
335
336 @Override
337 public int timeout() {
338 return 0;
339 }
340
341 @Override
342 public boolean isPermanent() {
343 return false;
344 }
345
Brian O'Connor427a1762014-11-19 18:40:32 -0800346 @Override
347 public int hashCode() {
348 return Objects.hash(priority);
349 }
Ray Milkey930fc662014-11-11 16:07:45 -0800350
Brian O'Connor427a1762014-11-19 18:40:32 -0800351 @Override
352 public boolean equals(Object obj) {
353 if (this == obj) {
354 return true;
355 }
356 if (obj == null || getClass() != obj.getClass()) {
357 return false;
358 }
359 final MockFlowRule other = (MockFlowRule) obj;
360 return Objects.equals(this.priority, other.priority);
361 }
Ray Milkey930fc662014-11-11 16:07:45 -0800362 }
363
364
Ray Milkeye6684082014-10-16 16:59:47 -0700365}