blob: c090faed3f31fa892e64458690b057eebbf9204d [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;
Murat Parlakisikc6759e82016-06-29 03:22:22 -070029import org.onosproject.net.flow.FlowRule.FlowRemoveReason;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.flow.FlowId;
31import org.onosproject.net.flow.FlowRule;
jcc3d4e14a2015-04-21 11:32:05 +080032import org.onosproject.net.flow.FlowRuleExtPayLoad;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.flow.TrafficSelector;
34import org.onosproject.net.flow.TrafficTreatment;
35import org.onosproject.net.flow.criteria.Criterion;
36import org.onosproject.net.flow.criteria.Criterion.Type;
37import org.onosproject.net.flow.instructions.Instruction;
alshabib346b5b32015-03-06 00:42:16 -080038import org.onosproject.net.flow.instructions.Instructions;
Saurav Das86af8f12015-05-25 23:55:33 -070039import org.onosproject.net.flow.instructions.Instructions.MetadataInstruction;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080040import org.onosproject.net.resource.DiscreteResourceId;
41import org.onosproject.net.resource.Resource;
42import org.onosproject.net.resource.ResourceAllocation;
43import org.onosproject.net.resource.ResourceConsumer;
44import org.onosproject.net.resource.ResourceId;
45import org.onosproject.net.resource.ResourceListener;
46import org.onosproject.net.resource.ResourceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.topology.DefaultTopologyEdge;
48import org.onosproject.net.topology.DefaultTopologyVertex;
49import org.onosproject.net.topology.LinkWeight;
Thomas Vachuska48e64e42015-09-22 15:32:55 -070050import org.onosproject.net.topology.PathServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import org.onosproject.net.topology.TopologyVertex;
Ray Milkey5a7787a2015-02-23 11:44:30 -080052import org.onosproject.store.Timestamp;
Ray Milkeye6684082014-10-16 16:59:47 -070053
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070054import java.util.Arrays;
55import java.util.Collection;
56import java.util.Collections;
57import java.util.HashSet;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070058import java.util.List;
59import java.util.Objects;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080060import java.util.Optional;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070061import java.util.Set;
62import java.util.concurrent.atomic.AtomicLong;
63
Thomas Vachuska48e64e42015-09-22 15:32:55 -070064import static org.onosproject.net.NetTestTools.*;
Ray Milkey43a28222015-02-23 13:57:58 -080065
Ray Milkeye6684082014-10-16 16:59:47 -070066/**
67 * Common mocks used by the intent framework tests.
68 */
69public class IntentTestsMocks {
70 /**
71 * Mock traffic selector class used for satisfying API requirements.
72 */
73 public static class MockSelector implements TrafficSelector {
74 @Override
75 public Set<Criterion> criteria() {
76 return new HashSet<>();
77 }
Jonathan Hart936c49d2014-10-23 16:38:59 -070078
79 @Override
80 public Criterion getCriterion(Type type) {
81 return null;
82 }
Ray Milkeye6684082014-10-16 16:59:47 -070083 }
84
85 /**
86 * Mock traffic treatment class used for satisfying API requirements.
87 */
88 public static class MockTreatment implements TrafficTreatment {
89 @Override
alshabib346b5b32015-03-06 00:42:16 -080090 public List<Instruction> deferred() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070091 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -080092 }
93
94 @Override
95 public List<Instruction> immediate() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -070096 return Collections.emptyList();
alshabib346b5b32015-03-06 00:42:16 -080097 }
98
99 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700100 public List<Instruction> allInstructions() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700101 return Collections.emptyList();
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700102 }
103
104 @Override
alshabib346b5b32015-03-06 00:42:16 -0800105 public Instructions.TableTypeTransition tableTransition() {
106 return null;
107 }
108
109 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700110 public boolean clearedDeferred() {
111 return false;
alshabib346b5b32015-03-06 00:42:16 -0800112 }
Saurav Das86af8f12015-05-25 23:55:33 -0700113
114 @Override
115 public MetadataInstruction writeMetadata() {
116 return null;
117 }
alshabib10c810b2015-08-18 16:59:04 -0700118
119 @Override
120 public Instructions.MeterInstruction metered() {
121 return null;
122 }
Ray Milkeye6684082014-10-16 16:59:47 -0700123 }
124
125 /**
126 * Mock path service for creating paths within the test.
127 */
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700128 public static class MockPathService extends PathServiceAdapter {
Ray Milkeye6684082014-10-16 16:59:47 -0700129
130 final String[] pathHops;
131 final String[] reversePathHops;
132
133 /**
134 * Constructor that provides a set of hops to mock.
135 *
136 * @param pathHops path hops to mock
137 */
138 public MockPathService(String[] pathHops) {
139 this.pathHops = pathHops;
140 String[] reversed = pathHops.clone();
141 Collections.reverse(Arrays.asList(reversed));
142 reversePathHops = reversed;
143 }
144
145 @Override
146 public Set<Path> getPaths(ElementId src, ElementId dst) {
147 Set<Path> result = new HashSet<>();
148
149 String[] allHops = new String[pathHops.length];
150
151 if (src.toString().endsWith(pathHops[0])) {
152 System.arraycopy(pathHops, 0, allHops, 0, pathHops.length);
153 } else {
154 System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length);
155 }
156
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700157 result.add(createPath(src instanceof HostId, dst instanceof HostId, allHops));
Ray Milkeye6684082014-10-16 16:59:47 -0700158 return result;
159 }
160
161 @Override
162 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800163 final Set<Path> paths = getPaths(src, dst);
164
165 for (Path path : paths) {
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700166 final DeviceId srcDevice = path.src().elementId() instanceof DeviceId ? path.src().deviceId() : null;
167 final DeviceId dstDevice = path.dst().elementId() instanceof DeviceId ? path.dst().deviceId() : null;
168 if (srcDevice != null && dstDevice != null) {
169 final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
170 final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
171 final Link link = link(src.toString(), 1, dst.toString(), 1);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800172
Thomas Vachuskadc91b552016-03-29 14:02:47 -0700173 final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
174 if (weightValue < 0) {
175 return new HashSet<>();
176 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800177 }
178 }
179 return paths;
Ray Milkeye6684082014-10-16 16:59:47 -0700180 }
181 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800182
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800183 public static final class MockResourceService implements ResourceService {
184
185 private final double bandwidth;
186
187 public static ResourceService makeBandwidthResourceService(double bandwidth) {
188 return new MockResourceService(bandwidth);
189 }
190
191 private MockResourceService(double bandwidth) {
192 this.bandwidth = bandwidth;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800193 }
194
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700195 @Override
Sho SHIMIZUef835c92016-08-08 13:51:17 -0700196 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<? extends Resource> resources) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800197 return null;
198 }
199
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700200 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800201 public boolean release(List<ResourceAllocation> allocations) {
202 return false;
203 }
204
205 @Override
206 public boolean release(ResourceConsumer consumer) {
207 return false;
208 }
209
210 @Override
211 public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800212 return null;
213 }
214
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700215 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800216 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800217 return null;
218 }
219
220 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800221 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800222 return null;
223 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800224
Ray Milkey8d3ce432014-11-07 16:21:10 -0800225 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800226 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
227 return null;
228 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800229
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800230 @Override
231 public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
232 return null;
233 }
234
235 @Override
236 public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
237 return null;
238 }
239
240 @Override
241 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
242 return null;
243 }
244
245 @Override
246 public boolean isAvailable(Resource resource) {
247 if (!resource.isTypeOf(Bandwidth.class)) {
248 return false;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800249 }
250
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800251 Optional<Double> value = resource.valueAs(Double.class);
252 return value.filter(requested -> requested <= bandwidth).isPresent();
Ray Milkey8d3ce432014-11-07 16:21:10 -0800253 }
254
255 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800256 public void addListener(ResourceListener listener) {
Ray Milkey8d3ce432014-11-07 16:21:10 -0800257 }
258
259 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800260 public void removeListener(ResourceListener listener) {
Ray Milkeye97ede92014-11-20 10:43:12 -0800261 }
Ray Milkey8d3ce432014-11-07 16:21:10 -0800262 }
263
Ray Milkey930fc662014-11-11 16:07:45 -0800264 private static final IntentTestsMocks.MockSelector SELECTOR =
265 new IntentTestsMocks.MockSelector();
266 private static final IntentTestsMocks.MockTreatment TREATMENT =
267 new IntentTestsMocks.MockTreatment();
268
269 public static class MockFlowRule implements FlowRule {
Ray Milkey77a455f2015-03-27 10:08:17 -0700270 static int nextId = 0;
Ray Milkey930fc662014-11-11 16:07:45 -0800271
272 int priority;
alshabib08d98982015-04-21 16:25:50 -0700273 int tableId;
Ray Milkey77a455f2015-03-27 10:08:17 -0700274 long timestamp;
275 int id;
jcc3d4e14a2015-04-21 11:32:05 +0800276 FlowRuleExtPayLoad payLoad;
sangho11c30ac2015-01-22 14:30:55 -0800277
Ray Milkey930fc662014-11-11 16:07:45 -0800278 public MockFlowRule(int priority) {
279 this.priority = priority;
alshabib08d98982015-04-21 16:25:50 -0700280 this.tableId = 0;
Ray Milkey77a455f2015-03-27 10:08:17 -0700281 this.timestamp = System.currentTimeMillis();
282 this.id = nextId++;
jcc3d4e14a2015-04-21 11:32:05 +0800283 this.payLoad = null;
284 }
285
286 public MockFlowRule(int priority, FlowRuleExtPayLoad payLoad) {
287 this.priority = priority;
288 this.timestamp = System.currentTimeMillis();
289 this.id = nextId++;
290 this.payLoad = payLoad;
Ray Milkey930fc662014-11-11 16:07:45 -0800291 }
292
293 @Override
294 public FlowId id() {
Ray Milkey77a455f2015-03-27 10:08:17 -0700295 return FlowId.valueOf(id);
Ray Milkey930fc662014-11-11 16:07:45 -0800296 }
297
298 @Override
299 public short appId() {
300 return 0;
301 }
302
303 @Override
Sho SHIMIZU75a5bd92014-11-25 11:22:56 -0800304 public GroupId groupId() {
305 return new DefaultGroupId(0);
alshabib28204e52014-11-12 18:29:45 -0800306 }
307
308 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800309 public int priority() {
310 return priority;
311 }
312
313 @Override
314 public DeviceId deviceId() {
315 return did("1");
316 }
317
318 @Override
319 public TrafficSelector selector() {
320 return SELECTOR;
321 }
322
323 @Override
324 public TrafficTreatment treatment() {
325 return TREATMENT;
326 }
327
328 @Override
329 public int timeout() {
330 return 0;
331 }
332
333 @Override
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700334 public int hardTimeout() {
335 return 0;
336 }
337
338 @Override
339 public FlowRemoveReason reason() {
340 return FlowRemoveReason.NO_REASON;
341 }
342
343 @Override
Ray Milkey930fc662014-11-11 16:07:45 -0800344 public boolean isPermanent() {
345 return false;
346 }
347
Brian O'Connor427a1762014-11-19 18:40:32 -0800348 @Override
349 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700350 return priority;
Brian O'Connor427a1762014-11-19 18:40:32 -0800351 }
Ray Milkey930fc662014-11-11 16:07:45 -0800352
Brian O'Connor427a1762014-11-19 18:40:32 -0800353 @Override
354 public boolean equals(Object obj) {
355 if (this == obj) {
356 return true;
357 }
358 if (obj == null || getClass() != obj.getClass()) {
359 return false;
360 }
361 final MockFlowRule other = (MockFlowRule) obj;
Ray Milkey77a455f2015-03-27 10:08:17 -0700362 return Objects.equals(this.timestamp, other.timestamp) &&
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700363 this.id == other.id;
Brian O'Connor427a1762014-11-19 18:40:32 -0800364 }
sangho11c30ac2015-01-22 14:30:55 -0800365
366 @Override
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700367 public boolean exactMatch(FlowRule rule) {
368 return this.equals(rule);
369 }
370
371 @Override
alshabibdb774072015-04-20 13:13:51 -0700372 public int tableId() {
alshabib08d98982015-04-21 16:25:50 -0700373 return tableId;
alshabibdb774072015-04-20 13:13:51 -0700374 }
jcc3d4e14a2015-04-21 11:32:05 +0800375
376 @Override
377 public FlowRuleExtPayLoad payLoad() {
378 return payLoad;
379 }
Ray Milkey930fc662014-11-11 16:07:45 -0800380 }
381
Ray Milkey5a7787a2015-02-23 11:44:30 -0800382 public static class MockIntent extends Intent {
383 private static AtomicLong counter = new AtomicLong(0);
384
385 private final Long number;
386
387 public MockIntent(Long number) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700388 super(NetTestTools.APP_ID, null, Collections.emptyList(),
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700389 Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey5a7787a2015-02-23 11:44:30 -0800390 this.number = number;
391 }
392
Ray Milkey43a28222015-02-23 13:57:58 -0800393 public MockIntent(Long number, Collection<NetworkResource> resources) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700394 super(NetTestTools.APP_ID, null, resources, Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkey43a28222015-02-23 13:57:58 -0800395 this.number = number;
396 }
397
Ray Milkey5a7787a2015-02-23 11:44:30 -0800398 public Long number() {
399 return number;
400 }
401
402 public static Long nextId() {
403 return counter.getAndIncrement();
404 }
Ray Milkey43a28222015-02-23 13:57:58 -0800405
406 @Override
407 public String toString() {
408 return MoreObjects.toStringHelper(getClass())
409 .add("id", id())
410 .add("appId", appId())
411 .toString();
412 }
Ray Milkey5a7787a2015-02-23 11:44:30 -0800413 }
414
415 public static class MockTimestamp implements Timestamp {
416 final int value;
417
418 public MockTimestamp(int value) {
419 this.value = value;
420 }
421
422 @Override
423 public int compareTo(Timestamp o) {
424 if (!(o instanceof MockTimestamp)) {
425 return -1;
426 }
427 MockTimestamp that = (MockTimestamp) o;
Jonathan Hart72175c22015-03-24 18:55:58 -0700428 return this.value - that.value;
Ray Milkey5a7787a2015-02-23 11:44:30 -0800429 }
430 }
431
Ray Milkeye6684082014-10-16 16:59:47 -0700432}