blob: 4fe53433743d320ff291a5330e9793bdeb29cec1 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070018import com.google.common.base.MoreObjects;
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070019import org.onlab.util.Bandwidth;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.core.DefaultGroupId;
21import org.onosproject.core.GroupId;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.ElementId;
24import org.onosproject.net.Link;
Ray Milkey5a7787a2015-02-23 11:44:30 -080025import org.onosproject.net.NetTestTools;
Ray Milkey43a28222015-02-23 13:57:58 -080026import org.onosproject.net.NetworkResource;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.Path;
28import org.onosproject.net.flow.FlowId;
29import org.onosproject.net.flow.FlowRule;
jcc3d4e14a2015-04-21 11:32:05 +080030import org.onosproject.net.flow.FlowRuleExtPayLoad;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.flow.TrafficSelector;
32import org.onosproject.net.flow.TrafficTreatment;
33import org.onosproject.net.flow.criteria.Criterion;
34import org.onosproject.net.flow.criteria.Criterion.Type;
35import org.onosproject.net.flow.instructions.Instruction;
alshabib346b5b32015-03-06 00:42:16 -080036import org.onosproject.net.flow.instructions.Instructions;
Saurav Das86af8f12015-05-25 23:55:33 -070037import org.onosproject.net.flow.instructions.Instructions.MetadataInstruction;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080038import org.onosproject.net.resource.DiscreteResourceId;
39import org.onosproject.net.resource.Resource;
40import org.onosproject.net.resource.ResourceAllocation;
41import org.onosproject.net.resource.ResourceConsumer;
42import org.onosproject.net.resource.ResourceId;
43import org.onosproject.net.resource.ResourceListener;
44import org.onosproject.net.resource.ResourceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import org.onosproject.net.topology.DefaultTopologyEdge;
46import org.onosproject.net.topology.DefaultTopologyVertex;
47import org.onosproject.net.topology.LinkWeight;
Thomas Vachuska48e64e42015-09-22 15:32:55 -070048import org.onosproject.net.topology.PathServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.topology.TopologyVertex;
Ray Milkey5a7787a2015-02-23 11:44:30 -080050import org.onosproject.store.Timestamp;
Ray Milkeye6684082014-10-16 16:59:47 -070051
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070052import java.util.Arrays;
53import java.util.Collection;
54import java.util.Collections;
55import java.util.HashSet;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070056import java.util.List;
57import java.util.Objects;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080058import java.util.Optional;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070059import java.util.Set;
60import java.util.concurrent.atomic.AtomicLong;
61
Thomas Vachuska48e64e42015-09-22 15:32:55 -070062import static org.onosproject.net.NetTestTools.*;
Ray Milkey43a28222015-02-23 13:57:58 -080063
Ray Milkeye6684082014-10-16 16:59:47 -070064/**
65 * Common mocks used by the intent framework tests.
66 */
67public class IntentTestsMocks {
68 /**
69 * Mock traffic selector class used for satisfying API requirements.
70 */
71 public static class MockSelector implements TrafficSelector {
72 @Override
73 public Set<Criterion> criteria() {
74 return new HashSet<>();
75 }
Jonathan Hart936c49d2014-10-23 16:38:59 -070076
77 @Override
78 public Criterion getCriterion(Type type) {
79 return null;
80 }
Ray Milkeye6684082014-10-16 16:59:47 -070081 }
82
83 /**
84 * Mock traffic treatment class used for satisfying API requirements.
85 */
86 public static class MockTreatment implements TrafficTreatment {
87 @Override
alshabib346b5b32015-03-06 00:42:16 -080088 public List<Instruction> deferred() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070089 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -080090 }
91
92 @Override
93 public List<Instruction> immediate() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070094 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -080095 }
96
97 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070098 public List<Instruction> allInstructions() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070099 return Collections.emptyList();
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700100 }
101
102 @Override
alshabib346b5b32015-03-06 00:42:16 -0800103 public Instructions.TableTypeTransition tableTransition() {
104 return null;
105 }
106
107 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700108 public boolean clearedDeferred() {
109 return false;
alshabib346b5b32015-03-06 00:42:16 -0800110 }
Saurav Das86af8f12015-05-25 23:55:33 -0700111
112 @Override
113 public MetadataInstruction writeMetadata() {
114 return null;
115 }
alshabib10c810b2015-08-18 16:59:04 -0700116
117 @Override
118 public Instructions.MeterInstruction metered() {
119 return null;
120 }
Ray Milkeye6684082014-10-16 16:59:47 -0700121 }
122
123 /**
124 * Mock path service for creating paths within the test.
125 */
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700126 public static class MockPathService extends PathServiceAdapter {
Ray Milkeye6684082014-10-16 16:59:47 -0700127
128 final String[] pathHops;
129 final String[] reversePathHops;
130
131 /**
132 * Constructor that provides a set of hops to mock.
133 *
134 * @param pathHops path hops to mock
135 */
136 public MockPathService(String[] pathHops) {
137 this.pathHops = pathHops;
138 String[] reversed = pathHops.clone();
139 Collections.reverse(Arrays.asList(reversed));
140 reversePathHops = reversed;
141 }
142
143 @Override
144 public Set<Path> getPaths(ElementId src, ElementId dst) {
145 Set<Path> result = new HashSet<>();
146
147 String[] allHops = new String[pathHops.length];
148
149 if (src.toString().endsWith(pathHops[0])) {
150 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
151 } else {
152 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
153 }
154
155 result.add(createPath(allHops));
156 return result;
157 }
158
159 @Override
160 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800161 final Set<Path> paths = getPaths(src, dst);
162
163 for (Path path : paths) {
164 final DeviceId srcDevice = path.src().deviceId();
165 final DeviceId dstDevice = path.dst().deviceId();
166 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
167 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
168 final Link link = link(src.toString(), 1, dst.toString(), 1);
169
170 final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
171 if (weightValue < 0) {
172 return new HashSet<>();
173 }
174 }
175 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700176 }
177 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800178
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800179 public static final class MockResourceService implements ResourceService {
180
181 private final double bandwidth;
182
183 public static ResourceService makeBandwidthResourceService(double bandwidth) {
184 return new MockResourceService(bandwidth);
185 }
186
187 private MockResourceService(double bandwidth) {
188 this.bandwidth = bandwidth;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800189 }
190
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700191 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800192 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<Resource> resources) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800193 return null;
194 }
195
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700196 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800197 public boolean release(List<ResourceAllocation> allocations) {
198 return false;
199 }
200
201 @Override
202 public boolean release(ResourceConsumer consumer) {
203 return false;
204 }
205
206 @Override
207 public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800208 return null;
209 }
210
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700211 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800212 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800213 return null;
214 }
215
216 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800217 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800218 return null;
219 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800220
Ray Milkey8d3ce432014-11-07 16:21:10 -0800221 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800222 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
223 return null;
224 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800225
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800226 @Override
227 public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
228 return null;
229 }
230
231 @Override
232 public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
233 return null;
234 }
235
236 @Override
237 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
238 return null;
239 }
240
241 @Override
242 public boolean isAvailable(Resource resource) {
243 if (!resource.isTypeOf(Bandwidth.class)) {
244 return false;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800245 }
246
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800247 Optional<Double> value = resource.valueAs(Double.class);
248 return value.filter(requested -> requested <= bandwidth).isPresent();
Ray Milkey8d3ce432014-11-07 16:21:10 -0800249 }
250
251 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800252 public void addListener(ResourceListener listener) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800253 }
254
255 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800256 public void removeListener(ResourceListener listener) {
Ray Milkeye97ede92014-11-20 10:43:12 -0800257 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800258 }
259
Ray Milkey930fc662014-11-11 16:07:45 -0800260 private static final IntentTestsMocks.MockSelector SELECTOR =
261 new IntentTestsMocks.MockSelector();
262 private static final IntentTestsMocks.MockTreatment TREATMENT =
263 new IntentTestsMocks.MockTreatment();
264
265 public static class MockFlowRule implements FlowRule {
Ray Milkey77a455f2015-03-27 10:08:17 -0700266 static int nextId = 0;
Ray Milkey930fc662014-11-11 16:07:45 -0800267
268 int priority;
alshabib08d98982015-04-21 16:25:50 -0700269 int tableId;
Ray Milkey77a455f2015-03-27 10:08:17 -0700270 long timestamp;
271 int id;
jcc3d4e14a2015-04-21 11:32:05 +0800272 FlowRuleExtPayLoad payLoad;
sangho11c30ac2015-01-22 14:30:55 -0800273
Ray Milkey930fc662014-11-11 16:07:45 -0800274 public MockFlowRule(int priority) {
275 this.priority = priority;
alshabib08d98982015-04-21 16:25:50 -0700276 this.tableId = 0;
Ray Milkey77a455f2015-03-27 10:08:17 -0700277 this.timestamp = System.currentTimeMillis();
278 this.id = nextId++;
jcc3d4e14a2015-04-21 11:32:05 +0800279 this.payLoad = null;
280 }
281
282 public MockFlowRule(int priority, FlowRuleExtPayLoad payLoad) {
283 this.priority = priority;
284 this.timestamp = System.currentTimeMillis();
285 this.id = nextId++;
286 this.payLoad = payLoad;
Ray Milkey930fc662014-11-11 16:07:45 -0800287 }
288
289 @Override
290 public FlowId id() {
Ray Milkey77a455f2015-03-27 10:08:17 -0700291 return FlowId.valueOf(id);
Ray Milkey930fc662014-11-11 16:07:45 -0800292 }
293
294 @Override
295 public short appId() {
296 return 0;
297 }
298
299 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800300 public GroupId groupId() {
301 return new DefaultGroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800302 }
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
Brian O'Connor427a1762014-11-19 18:40:32 -0800334 @Override
335 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700336 return priority;
Brian O'Connor427a1762014-11-19 18:40:32 -0800337 }
Ray Milkey930fc662014-11-11 16:07:45 -0800338
Brian O'Connor427a1762014-11-19 18:40:32 -0800339 @Override
340 public boolean equals(Object obj) {
341 if (this == obj) {
342 return true;
343 }
344 if (obj == null || getClass() != obj.getClass()) {
345 return false;
346 }
347 final MockFlowRule other = (MockFlowRule) obj;
Ray Milkey77a455f2015-03-27 10:08:17 -0700348 return Objects.equals(this.timestamp, other.timestamp) &&
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700349 this.id == other.id;
Brian O'Connor427a1762014-11-19 18:40:32 -0800350 }
sangho11c30ac2015-01-22 14:30:55 -0800351
352 @Override
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700353 public boolean exactMatch(FlowRule rule) {
354 return this.equals(rule);
355 }
356
357 @Override
alshabibdb774072015-04-20 13:13:51 -0700358 public int tableId() {
alshabib08d98982015-04-21 16:25:50 -0700359 return tableId;
alshabibdb774072015-04-20 13:13:51 -0700360 }
jcc3d4e14a2015-04-21 11:32:05 +0800361
362 @Override
363 public FlowRuleExtPayLoad payLoad() {
364 return payLoad;
365 }
Ray Milkey930fc662014-11-11 16:07:45 -0800366 }
367
Ray Milkey5a7787a2015-02-23 11:44:30 -0800368 public static class MockIntent extends Intent {
369 private static AtomicLong counter = new AtomicLong(0);
370
371 private final Long number;
372
373 public MockIntent(Long number) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700374 super(NetTestTools.APP_ID, null, Collections.emptyList(),
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700375 Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey5a7787a2015-02-23 11:44:30 -0800376 this.number = number;
377 }
378
Ray Milkey43a28222015-02-23 13:57:58 -0800379 public MockIntent(Long number, Collection<NetworkResource> resources) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700380 super(NetTestTools.APP_ID, null, resources, Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey43a28222015-02-23 13:57:58 -0800381 this.number = number;
382 }
383
Ray Milkey5a7787a2015-02-23 11:44:30 -0800384 public Long number() {
385 return number;
386 }
387
388 public static Long nextId() {
389 return counter.getAndIncrement();
390 }
Ray Milkey43a28222015-02-23 13:57:58 -0800391
392 @Override
393 public String toString() {
394 return MoreObjects.toStringHelper(getClass())
395 .add("id", id())
396 .add("appId", appId())
397 .toString();
398 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800399 }
400
401 public static class MockTimestamp implements Timestamp {
402 final int value;
403
404 public MockTimestamp(int value) {
405 this.value = value;
406 }
407
408 @Override
409 public int compareTo(Timestamp o) {
410 if (!(o instanceof MockTimestamp)) {
411 return -1;
412 }
413 MockTimestamp that = (MockTimestamp) o;
Jonathan Hart72175c22015-03-24 18:55:58 -0700414 return this.value - that.value;
Ray Milkey5a7787a2015-02-23 11:44:30 -0800415 }
416 }
417
Ray Milkeye6684082014-10-16 16:59:47 -0700418}