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