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