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