blob: 6511cfae50a4213941409c4bf789dcb240ec9a86 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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;
Thomas Vachuskadc91b552016-03-29 14:02:47 -070024import org.onosproject.net.HostId;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.Link;
Ray Milkey5a7787a2015-02-23 11:44:30 -080026import org.onosproject.net.NetTestTools;
Ray Milkey43a28222015-02-23 13:57:58 -080027import org.onosproject.net.NetworkResource;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.Path;
29import org.onosproject.net.flow.FlowId;
30import org.onosproject.net.flow.FlowRule;
jcc3d4e14a2015-04-21 11:32:05 +080031import org.onosproject.net.flow.FlowRuleExtPayLoad;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.flow.TrafficSelector;
33import org.onosproject.net.flow.TrafficTreatment;
34import org.onosproject.net.flow.criteria.Criterion;
35import org.onosproject.net.flow.criteria.Criterion.Type;
36import org.onosproject.net.flow.instructions.Instruction;
alshabib346b5b32015-03-06 00:42:16 -080037import org.onosproject.net.flow.instructions.Instructions;
Saurav Das86af8f12015-05-25 23:55:33 -070038import org.onosproject.net.flow.instructions.Instructions.MetadataInstruction;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080039import org.onosproject.net.resource.DiscreteResourceId;
40import org.onosproject.net.resource.Resource;
41import org.onosproject.net.resource.ResourceAllocation;
42import org.onosproject.net.resource.ResourceConsumer;
43import org.onosproject.net.resource.ResourceId;
44import org.onosproject.net.resource.ResourceListener;
45import org.onosproject.net.resource.ResourceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.topology.DefaultTopologyEdge;
47import org.onosproject.net.topology.DefaultTopologyVertex;
48import org.onosproject.net.topology.LinkWeight;
Thomas Vachuska48e64e42015-09-22 15:32:55 -070049import org.onosproject.net.topology.PathServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import org.onosproject.net.topology.TopologyVertex;
Ray Milkey5a7787a2015-02-23 11:44:30 -080051import org.onosproject.store.Timestamp;
Ray Milkeye6684082014-10-16 16:59:47 -070052
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070053import java.util.Arrays;
54import java.util.Collection;
55import java.util.Collections;
56import java.util.HashSet;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070057import java.util.List;
58import java.util.Objects;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080059import java.util.Optional;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070060import java.util.Set;
61import java.util.concurrent.atomic.AtomicLong;
62
Thomas Vachuska48e64e42015-09-22 15:32:55 -070063import static org.onosproject.net.NetTestTools.*;
Ray Milkey43a28222015-02-23 13:57:58 -080064
Ray Milkeye6684082014-10-16 16:59:47 -070065/**
66 * Common mocks used by the intent framework tests.
67 */
68public class IntentTestsMocks {
69 /**
70 * Mock traffic selector class used for satisfying API requirements.
71 */
72 public static class MockSelector implements TrafficSelector {
73 @Override
74 public Set<Criterion> criteria() {
75 return new HashSet<>();
76 }
Jonathan Hart936c49d2014-10-23 16:38:59 -070077
78 @Override
79 public Criterion getCriterion(Type type) {
80 return null;
81 }
Ray Milkeye6684082014-10-16 16:59:47 -070082 }
83
84 /**
85 * Mock traffic treatment class used for satisfying API requirements.
86 */
87 public static class MockTreatment implements TrafficTreatment {
88 @Override
alshabib346b5b32015-03-06 00:42:16 -080089 public List<Instruction> deferred() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070090 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -080091 }
92
93 @Override
94 public List<Instruction> immediate() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070095 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -080096 }
97
98 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070099 public List<Instruction> allInstructions() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700100 return Collections.emptyList();
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700101 }
102
103 @Override
alshabib346b5b32015-03-06 00:42:16 -0800104 public Instructions.TableTypeTransition tableTransition() {
105 return null;
106 }
107
108 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700109 public boolean clearedDeferred() {
110 return false;
alshabib346b5b32015-03-06 00:42:16 -0800111 }
Saurav Das86af8f12015-05-25 23:55:33 -0700112
113 @Override
114 public MetadataInstruction writeMetadata() {
115 return null;
116 }
alshabib10c810b2015-08-18 16:59:04 -0700117
118 @Override
119 public Instructions.MeterInstruction metered() {
120 return null;
121 }
Ray Milkeye6684082014-10-16 16:59:47 -0700122 }
123
124 /**
125 * Mock path service for creating paths within the test.
126 */
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700127 public static class MockPathService extends PathServiceAdapter {
Ray Milkeye6684082014-10-16 16:59:47 -0700128
129 final String[] pathHops;
130 final String[] reversePathHops;
131
132 /**
133 * Constructor that provides a set of hops to mock.
134 *
135 * @param pathHops path hops to mock
136 */
137 public MockPathService(String[] pathHops) {
138 this.pathHops = pathHops;
139 String[] reversed = pathHops.clone();
140 Collections.reverse(Arrays.asList(reversed));
141 reversePathHops = reversed;
142 }
143
144 @Override
145 public Set<Path> getPaths(ElementId src, ElementId dst) {
146 Set<Path> result = new HashSet<>();
147
148 String[] allHops = new String[pathHops.length];
149
150 if (src.toString().endsWith(pathHops[0])) {
151 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
152 } else {
153 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
154 }
155
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700156 result.add(createPath(src instanceof HostId, dst instanceof HostId, allHops));
Ray Milkeye6684082014-10-16 16:59:47 -0700157 return result;
158 }
159
160 @Override
161 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800162 final Set<Path> paths = getPaths(src, dst);
163
164 for (Path path : paths) {
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700165 final DeviceId srcDevice = path.src().elementId() instanceof DeviceId ? path.src().deviceId() : null;
166 final DeviceId dstDevice = path.dst().elementId() instanceof DeviceId ? path.dst().deviceId() : null;
167 if (srcDevice != null && dstDevice != null) {
168 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
169 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
170 final Link link = link(src.toString(), 1, dst.toString(), 1);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800171
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700172 final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
173 if (weightValue < 0) {
174 return new HashSet<>();
175 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800176 }
177 }
178 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700179 }
180 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800181
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800182 public static final class MockResourceService implements ResourceService {
183
184 private final double bandwidth;
185
186 public static ResourceService makeBandwidthResourceService(double bandwidth) {
187 return new MockResourceService(bandwidth);
188 }
189
190 private MockResourceService(double bandwidth) {
191 this.bandwidth = bandwidth;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800192 }
193
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700194 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800195 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<Resource> resources) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800196 return null;
197 }
198
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700199 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800200 public boolean release(List<ResourceAllocation> allocations) {
201 return false;
202 }
203
204 @Override
205 public boolean release(ResourceConsumer consumer) {
206 return false;
207 }
208
209 @Override
210 public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800211 return null;
212 }
213
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700214 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800215 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800216 return null;
217 }
218
219 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800220 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800221 return null;
222 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800223
Ray Milkey8d3ce432014-11-07 16:21:10 -0800224 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800225 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
226 return null;
227 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800228
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800229 @Override
230 public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
231 return null;
232 }
233
234 @Override
235 public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
236 return null;
237 }
238
239 @Override
240 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
241 return null;
242 }
243
244 @Override
245 public boolean isAvailable(Resource resource) {
246 if (!resource.isTypeOf(Bandwidth.class)) {
247 return false;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800248 }
249
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800250 Optional<Double> value = resource.valueAs(Double.class);
251 return value.filter(requested -> requested <= bandwidth).isPresent();
Ray Milkey8d3ce432014-11-07 16:21:10 -0800252 }
253
254 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800255 public void addListener(ResourceListener listener) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800256 }
257
258 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800259 public void removeListener(ResourceListener listener) {
Ray Milkeye97ede92014-11-20 10:43:12 -0800260 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800261 }
262
Ray Milkey930fc662014-11-11 16:07:45 -0800263 private static final IntentTestsMocks.MockSelector SELECTOR =
264 new IntentTestsMocks.MockSelector();
265 private static final IntentTestsMocks.MockTreatment TREATMENT =
266 new IntentTestsMocks.MockTreatment();
267
268 public static class MockFlowRule implements FlowRule {
Ray Milkey77a455f2015-03-27 10:08:17 -0700269 static int nextId = 0;
Ray Milkey930fc662014-11-11 16:07:45 -0800270
271 int priority;
alshabib08d98982015-04-21 16:25:50 -0700272 int tableId;
Ray Milkey77a455f2015-03-27 10:08:17 -0700273 long timestamp;
274 int id;
jcc3d4e14a2015-04-21 11:32:05 +0800275 FlowRuleExtPayLoad payLoad;
sangho11c30ac2015-01-22 14:30:55 -0800276
Ray Milkey930fc662014-11-11 16:07:45 -0800277 public MockFlowRule(int priority) {
278 this.priority = priority;
alshabib08d98982015-04-21 16:25:50 -0700279 this.tableId = 0;
Ray Milkey77a455f2015-03-27 10:08:17 -0700280 this.timestamp = System.currentTimeMillis();
281 this.id = nextId++;
jcc3d4e14a2015-04-21 11:32:05 +0800282 this.payLoad = null;
283 }
284
285 public MockFlowRule(int priority, FlowRuleExtPayLoad payLoad) {
286 this.priority = priority;
287 this.timestamp = System.currentTimeMillis();
288 this.id = nextId++;
289 this.payLoad = payLoad;
Ray Milkey930fc662014-11-11 16:07:45 -0800290 }
291
292 @Override
293 public FlowId id() {
Ray Milkey77a455f2015-03-27 10:08:17 -0700294 return FlowId.valueOf(id);
Ray Milkey930fc662014-11-11 16:07:45 -0800295 }
296
297 @Override
298 public short appId() {
299 return 0;
300 }
301
302 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800303 public GroupId groupId() {
304 return new DefaultGroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800305 }
306
307 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800308 public int priority() {
309 return priority;
310 }
311
312 @Override
313 public DeviceId deviceId() {
314 return did("1");
315 }
316
317 @Override
318 public TrafficSelector selector() {
319 return SELECTOR;
320 }
321
322 @Override
323 public TrafficTreatment treatment() {
324 return TREATMENT;
325 }
326
327 @Override
328 public int timeout() {
329 return 0;
330 }
331
332 @Override
333 public boolean isPermanent() {
334 return false;
335 }
336
Brian O'Connor427a1762014-11-19 18:40:32 -0800337 @Override
338 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700339 return priority;
Brian O'Connor427a1762014-11-19 18:40:32 -0800340 }
Ray Milkey930fc662014-11-11 16:07:45 -0800341
Brian O'Connor427a1762014-11-19 18:40:32 -0800342 @Override
343 public boolean equals(Object obj) {
344 if (this == obj) {
345 return true;
346 }
347 if (obj == null || getClass() != obj.getClass()) {
348 return false;
349 }
350 final MockFlowRule other = (MockFlowRule) obj;
Ray Milkey77a455f2015-03-27 10:08:17 -0700351 return Objects.equals(this.timestamp, other.timestamp) &&
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700352 this.id == other.id;
Brian O'Connor427a1762014-11-19 18:40:32 -0800353 }
sangho11c30ac2015-01-22 14:30:55 -0800354
355 @Override
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700356 public boolean exactMatch(FlowRule rule) {
357 return this.equals(rule);
358 }
359
360 @Override
alshabibdb774072015-04-20 13:13:51 -0700361 public int tableId() {
alshabib08d98982015-04-21 16:25:50 -0700362 return tableId;
alshabibdb774072015-04-20 13:13:51 -0700363 }
jcc3d4e14a2015-04-21 11:32:05 +0800364
365 @Override
366 public FlowRuleExtPayLoad payLoad() {
367 return payLoad;
368 }
Ray Milkey930fc662014-11-11 16:07:45 -0800369 }
370
Ray Milkey5a7787a2015-02-23 11:44:30 -0800371 public static class MockIntent extends Intent {
372 private static AtomicLong counter = new AtomicLong(0);
373
374 private final Long number;
375
376 public MockIntent(Long number) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700377 super(NetTestTools.APP_ID, null, Collections.emptyList(),
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700378 Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey5a7787a2015-02-23 11:44:30 -0800379 this.number = number;
380 }
381
Ray Milkey43a28222015-02-23 13:57:58 -0800382 public MockIntent(Long number, Collection<NetworkResource> resources) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700383 super(NetTestTools.APP_ID, null, resources, Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey43a28222015-02-23 13:57:58 -0800384 this.number = number;
385 }
386
Ray Milkey5a7787a2015-02-23 11:44:30 -0800387 public Long number() {
388 return number;
389 }
390
391 public static Long nextId() {
392 return counter.getAndIncrement();
393 }
Ray Milkey43a28222015-02-23 13:57:58 -0800394
395 @Override
396 public String toString() {
397 return MoreObjects.toStringHelper(getClass())
398 .add("id", id())
399 .add("appId", appId())
400 .toString();
401 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800402 }
403
404 public static class MockTimestamp implements Timestamp {
405 final int value;
406
407 public MockTimestamp(int value) {
408 this.value = value;
409 }
410
411 @Override
412 public int compareTo(Timestamp o) {
413 if (!(o instanceof MockTimestamp)) {
414 return -1;
415 }
416 MockTimestamp that = (MockTimestamp) o;
Jonathan Hart72175c22015-03-24 18:55:58 -0700417 return this.value - that.value;
Ray Milkey5a7787a2015-02-23 11:44:30 -0800418 }
419 }
420
Ray Milkeye6684082014-10-16 16:59:47 -0700421}