blob: 4b046e0fbff371f3633d37159d3edae4667419ee [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;
31
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.core.DefaultGroupId;
33import org.onosproject.core.GroupId;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.ElementId;
36import org.onosproject.net.Link;
37import org.onosproject.net.Path;
38import org.onosproject.net.flow.FlowId;
39import org.onosproject.net.flow.FlowRule;
40import org.onosproject.net.flow.TrafficSelector;
41import org.onosproject.net.flow.TrafficTreatment;
42import org.onosproject.net.flow.criteria.Criterion;
43import org.onosproject.net.flow.criteria.Criterion.Type;
44import org.onosproject.net.flow.instructions.Instruction;
45import org.onosproject.net.resource.BandwidthResourceRequest;
46import org.onosproject.net.resource.LambdaResourceRequest;
47import org.onosproject.net.resource.LinkResourceAllocations;
48import org.onosproject.net.resource.LinkResourceListener;
49import org.onosproject.net.resource.LinkResourceRequest;
50import org.onosproject.net.resource.LinkResourceService;
51import org.onosproject.net.resource.ResourceAllocation;
52import org.onosproject.net.resource.ResourceRequest;
53import org.onosproject.net.resource.ResourceType;
54import org.onosproject.net.topology.DefaultTopologyEdge;
55import org.onosproject.net.topology.DefaultTopologyVertex;
56import org.onosproject.net.topology.LinkWeight;
57import org.onosproject.net.topology.PathService;
58import org.onosproject.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 {
Ayaka Koshibe38f8c232015-01-30 14:14:40 -080083
84 private List<Instruction> instructions = new ArrayList<>();
85
86 public MockTreatment() {
87 }
88
89 public MockTreatment(Instruction... insts) {
90 this.instructions.addAll(Arrays.asList(insts));
91 }
92
Ray Milkeye6684082014-10-16 16:59:47 -070093 @Override
94 public List<Instruction> instructions() {
Ayaka Koshibe38f8c232015-01-30 14:14:40 -080095 return this.instructions;
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) {
158 return null;
159 }
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() {
260 return null;
261 }
262
263 @Override
264 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
265 return null;
266 }
267
268 @Override
269 public LinkResourceAllocations getAllocations(IntentId intentId) {
270 return null;
271 }
272
273 @Override
274 public Iterable<ResourceRequest> getAvailableResources(Link link) {
275 final List<ResourceRequest> result = new LinkedList<>();
276 if (availableBandwidth > 0.0) {
277 result.add(new BandwidthResourceRequest(availableBandwidth));
278 }
279 if (availableLambda > 0) {
280 result.add(new LambdaResourceRequest());
281 }
282 return result;
283 }
284
285 @Override
weibit00c94f52014-11-16 07:09:05 -0800286 public Iterable<ResourceRequest> getAvailableResources(Link link, LinkResourceAllocations allocations) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800287 return null;
288 }
Ray Milkeye97ede92014-11-20 10:43:12 -0800289
290 @Override
291 public void addListener(LinkResourceListener listener) {
292
293 }
294
295 @Override
296 public void removeListener(LinkResourceListener listener) {
297
298 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800299 }
300
Ray Milkey930fc662014-11-11 16:07:45 -0800301 private static final IntentTestsMocks.MockSelector SELECTOR =
302 new IntentTestsMocks.MockSelector();
303 private static final IntentTestsMocks.MockTreatment TREATMENT =
304 new IntentTestsMocks.MockTreatment();
305
306 public static class MockFlowRule implements FlowRule {
307
308 int priority;
sangho11c30ac2015-01-22 14:30:55 -0800309 Type type;
310
Ray Milkey930fc662014-11-11 16:07:45 -0800311 public MockFlowRule(int priority) {
312 this.priority = priority;
sangho11c30ac2015-01-22 14:30:55 -0800313 this.type = Type.DEFAULT;
Ray Milkey930fc662014-11-11 16:07:45 -0800314 }
315
316 @Override
317 public FlowId id() {
318 return FlowId.valueOf(1);
319 }
320
321 @Override
322 public short appId() {
323 return 0;
324 }
325
326 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800327 public GroupId groupId() {
328 return new DefaultGroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800329 }
330
331 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800332 public int priority() {
333 return priority;
334 }
335
336 @Override
337 public DeviceId deviceId() {
338 return did("1");
339 }
340
341 @Override
342 public TrafficSelector selector() {
343 return SELECTOR;
344 }
345
346 @Override
347 public TrafficTreatment treatment() {
348 return TREATMENT;
349 }
350
351 @Override
352 public int timeout() {
353 return 0;
354 }
355
356 @Override
357 public boolean isPermanent() {
358 return false;
359 }
360
Brian O'Connor427a1762014-11-19 18:40:32 -0800361 @Override
362 public int hashCode() {
363 return Objects.hash(priority);
364 }
Ray Milkey930fc662014-11-11 16:07:45 -0800365
Brian O'Connor427a1762014-11-19 18:40:32 -0800366 @Override
367 public boolean equals(Object obj) {
368 if (this == obj) {
369 return true;
370 }
371 if (obj == null || getClass() != obj.getClass()) {
372 return false;
373 }
374 final MockFlowRule other = (MockFlowRule) obj;
375 return Objects.equals(this.priority, other.priority);
376 }
sangho11c30ac2015-01-22 14:30:55 -0800377
378 @Override
379 public Type type() {
380 return type;
381 }
Ray Milkey930fc662014-11-11 16:07:45 -0800382 }
383
384
Ray Milkeye6684082014-10-16 16:59:47 -0700385}