blob: e0fe09eb86c3dd1ac1eca43a9045ccbfb611cdaf [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 Milkey8d3ce432014-11-07 16:21:10 -080019import static org.onlab.onos.net.NetTestTools.link;
Jonathan Hart936c49d2014-10-23 16:38:59 -070020
Ray Milkeye6684082014-10-16 16:59:47 -070021import java.util.ArrayList;
22import java.util.Arrays;
Ray Milkey8d3ce432014-11-07 16:21:10 -080023import java.util.Collection;
Ray Milkeye6684082014-10-16 16:59:47 -070024import java.util.Collections;
25import java.util.HashSet;
Ray Milkey8d3ce432014-11-07 16:21:10 -080026import java.util.LinkedList;
Ray Milkeye6684082014-10-16 16:59:47 -070027import java.util.List;
28import java.util.Set;
29
Ray Milkey8d3ce432014-11-07 16:21:10 -080030import org.onlab.onos.net.DeviceId;
Ray Milkeye6684082014-10-16 16:59:47 -070031import org.onlab.onos.net.ElementId;
Ray Milkey8d3ce432014-11-07 16:21:10 -080032import org.onlab.onos.net.Link;
Ray Milkeye6684082014-10-16 16:59:47 -070033import org.onlab.onos.net.Path;
34import org.onlab.onos.net.flow.TrafficSelector;
35import org.onlab.onos.net.flow.TrafficTreatment;
36import org.onlab.onos.net.flow.criteria.Criterion;
Jonathan Hart936c49d2014-10-23 16:38:59 -070037import org.onlab.onos.net.flow.criteria.Criterion.Type;
Ray Milkeye6684082014-10-16 16:59:47 -070038import org.onlab.onos.net.flow.instructions.Instruction;
Ray Milkey8d3ce432014-11-07 16:21:10 -080039import org.onlab.onos.net.resource.BandwidthResourceRequest;
40import org.onlab.onos.net.resource.LambdaResourceRequest;
41import org.onlab.onos.net.resource.LinkResourceAllocations;
42import org.onlab.onos.net.resource.LinkResourceRequest;
43import org.onlab.onos.net.resource.LinkResourceService;
44import org.onlab.onos.net.resource.ResourceAllocation;
45import org.onlab.onos.net.resource.ResourceRequest;
46import org.onlab.onos.net.resource.ResourceType;
47import org.onlab.onos.net.topology.DefaultTopologyEdge;
48import org.onlab.onos.net.topology.DefaultTopologyVertex;
Ray Milkeye6684082014-10-16 16:59:47 -070049import org.onlab.onos.net.topology.LinkWeight;
50import org.onlab.onos.net.topology.PathService;
Ray Milkey8d3ce432014-11-07 16:21:10 -080051import org.onlab.onos.net.topology.TopologyVertex;
Ray Milkeye6684082014-10-16 16:59:47 -070052
Ray Milkeye6684082014-10-16 16:59:47 -070053/**
54 * Common mocks used by the intent framework tests.
55 */
56public class IntentTestsMocks {
57 /**
58 * Mock traffic selector class used for satisfying API requirements.
59 */
60 public static class MockSelector implements TrafficSelector {
61 @Override
62 public Set<Criterion> criteria() {
63 return new HashSet<>();
64 }
Jonathan Hart936c49d2014-10-23 16:38:59 -070065
66 @Override
67 public Criterion getCriterion(Type type) {
68 return null;
69 }
Ray Milkeye6684082014-10-16 16:59:47 -070070 }
71
72 /**
73 * Mock traffic treatment class used for satisfying API requirements.
74 */
75 public static class MockTreatment implements TrafficTreatment {
76 @Override
77 public List<Instruction> instructions() {
78 return new ArrayList<>();
79 }
80 }
81
82 /**
83 * Mock path service for creating paths within the test.
84 */
85 public static class MockPathService implements PathService {
86
87 final String[] pathHops;
88 final String[] reversePathHops;
89
90 /**
91 * Constructor that provides a set of hops to mock.
92 *
93 * @param pathHops path hops to mock
94 */
95 public MockPathService(String[] pathHops) {
96 this.pathHops = pathHops;
97 String[] reversed = pathHops.clone();
98 Collections.reverse(Arrays.asList(reversed));
99 reversePathHops = reversed;
100 }
101
102 @Override
103 public Set<Path> getPaths(ElementId src, ElementId dst) {
104 Set<Path> result = new HashSet<>();
105
106 String[] allHops = new String[pathHops.length];
107
108 if (src.toString().endsWith(pathHops[0])) {
109 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
110 } else {
111 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
112 }
113
114 result.add(createPath(allHops));
115 return result;
116 }
117
118 @Override
119 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800120 final Set<Path> paths = getPaths(src, dst);
121
122 for (Path path : paths) {
123 final DeviceId srcDevice = path.src().deviceId();
124 final DeviceId dstDevice = path.dst().deviceId();
125 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
126 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
127 final Link link = link(src.toString(), 1, dst.toString(), 1);
128
129 final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
130 if (weightValue < 0) {
131 return new HashSet<>();
132 }
133 }
134 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700135 }
136 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800137
138 public static class MockLinkResourceAllocations implements LinkResourceAllocations {
139 @Override
140 public Set<ResourceAllocation> getResourceAllocation(Link link) {
141 return null;
142 }
143
144 @Override
145 public IntentId intendId() {
146 return null;
147 }
148
149 @Override
150 public Collection<Link> links() {
151 return null;
152 }
153
154 @Override
155 public Set<ResourceRequest> resources() {
156 return null;
157 }
158
159 @Override
160 public ResourceType type() {
161 return null;
162 }
163 }
164
165 public static class MockedAllocationFailure extends RuntimeException { }
166
167 public static class MockResourceService implements LinkResourceService {
168
169 double availableBandwidth = -1.0;
170 int availableLambda = -1;
171
172 /**
173 * Allocates a resource service that will allow bandwidth allocations
174 * up to a limit.
175 *
176 * @param bandwidth available bandwidth limit
177 * @return resource manager for bandwidth requests
178 */
179 public static MockResourceService makeBandwidthResourceService(double bandwidth) {
180 final MockResourceService result = new MockResourceService();
181 result.availableBandwidth = bandwidth;
182 return result;
183 }
184
185 /**
186 * Allocates a resource service that will allow lambda allocations.
187 *
188 * @param lambda Lambda to return for allocation requests. Currently unused
189 * @return resource manager for lambda requests
190 */
191 public static MockResourceService makeLambdaResourceService(int lambda) {
192 final MockResourceService result = new MockResourceService();
193 result.availableLambda = lambda;
194 return result;
195 }
196
197 public void setAvailableBandwidth(double availableBandwidth) {
198 this.availableBandwidth = availableBandwidth;
199 }
200
201 public void setAvailableLambda(int availableLambda) {
202 this.availableLambda = availableLambda;
203 }
204
205
206 @Override
207 public LinkResourceAllocations requestResources(LinkResourceRequest req) {
208 int lambda = -1;
209 double bandwidth = -1.0;
210
211 for (ResourceRequest resourceRequest : req.resources()) {
212 if (resourceRequest.type() == ResourceType.BANDWIDTH) {
213 final BandwidthResourceRequest brr = (BandwidthResourceRequest) resourceRequest;
214 bandwidth = brr.bandwidth().toDouble();
215 } else if (resourceRequest.type() == ResourceType.LAMBDA) {
216 lambda = 1;
217 }
218 }
219
220 if (availableBandwidth < bandwidth) {
221 throw new MockedAllocationFailure();
222 }
223 if (lambda > 0 && availableLambda == 0) {
224 throw new MockedAllocationFailure();
225 }
226
227 return new IntentTestsMocks.MockLinkResourceAllocations();
228 }
229
230 @Override
231 public void releaseResources(LinkResourceAllocations allocations) {
232 // Mock
233 }
234
235 @Override
236 public LinkResourceAllocations updateResources(LinkResourceRequest req,
237 LinkResourceAllocations oldAllocations) {
238 return null;
239 }
240
241 @Override
242 public Iterable<LinkResourceAllocations> getAllocations() {
243 return null;
244 }
245
246 @Override
247 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
248 return null;
249 }
250
251 @Override
252 public LinkResourceAllocations getAllocations(IntentId intentId) {
253 return null;
254 }
255
256 @Override
257 public Iterable<ResourceRequest> getAvailableResources(Link link) {
258 final List<ResourceRequest> result = new LinkedList<>();
259 if (availableBandwidth > 0.0) {
260 result.add(new BandwidthResourceRequest(availableBandwidth));
261 }
262 if (availableLambda > 0) {
263 result.add(new LambdaResourceRequest());
264 }
265 return result;
266 }
267
268 @Override
269 public ResourceRequest getAvailableResources(Link link, LinkResourceAllocations allocations) {
270 return null;
271 }
272 }
273
Ray Milkeye6684082014-10-16 16:59:47 -0700274}