Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.intent; |
| 2 | |
Jonathan Hart | 936c49d | 2014-10-23 16:38:59 -0700 | [diff] [blame] | 3 | import static org.onlab.onos.net.NetTestTools.createPath; |
| 4 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 5 | import java.util.ArrayList; |
| 6 | import java.util.Arrays; |
| 7 | import java.util.Collections; |
| 8 | import java.util.HashSet; |
| 9 | import java.util.List; |
| 10 | import java.util.Set; |
| 11 | |
| 12 | import org.onlab.onos.net.ElementId; |
| 13 | import org.onlab.onos.net.Path; |
| 14 | import org.onlab.onos.net.flow.TrafficSelector; |
| 15 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 16 | import org.onlab.onos.net.flow.criteria.Criterion; |
Jonathan Hart | 936c49d | 2014-10-23 16:38:59 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.flow.criteria.Criterion.Type; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.flow.instructions.Instruction; |
| 19 | import org.onlab.onos.net.topology.LinkWeight; |
| 20 | import org.onlab.onos.net.topology.PathService; |
| 21 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 22 | /** |
| 23 | * Common mocks used by the intent framework tests. |
| 24 | */ |
| 25 | public class IntentTestsMocks { |
| 26 | /** |
| 27 | * Mock traffic selector class used for satisfying API requirements. |
| 28 | */ |
| 29 | public static class MockSelector implements TrafficSelector { |
| 30 | @Override |
| 31 | public Set<Criterion> criteria() { |
| 32 | return new HashSet<>(); |
| 33 | } |
Jonathan Hart | 936c49d | 2014-10-23 16:38:59 -0700 | [diff] [blame] | 34 | |
| 35 | @Override |
| 36 | public Criterion getCriterion(Type type) { |
| 37 | return null; |
| 38 | } |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Mock traffic treatment class used for satisfying API requirements. |
| 43 | */ |
| 44 | public static class MockTreatment implements TrafficTreatment { |
| 45 | @Override |
| 46 | public List<Instruction> instructions() { |
| 47 | return new ArrayList<>(); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Mock path service for creating paths within the test. |
| 53 | */ |
| 54 | public static class MockPathService implements PathService { |
| 55 | |
| 56 | final String[] pathHops; |
| 57 | final String[] reversePathHops; |
| 58 | |
| 59 | /** |
| 60 | * Constructor that provides a set of hops to mock. |
| 61 | * |
| 62 | * @param pathHops path hops to mock |
| 63 | */ |
| 64 | public MockPathService(String[] pathHops) { |
| 65 | this.pathHops = pathHops; |
| 66 | String[] reversed = pathHops.clone(); |
| 67 | Collections.reverse(Arrays.asList(reversed)); |
| 68 | reversePathHops = reversed; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public Set<Path> getPaths(ElementId src, ElementId dst) { |
| 73 | Set<Path> result = new HashSet<>(); |
| 74 | |
| 75 | String[] allHops = new String[pathHops.length]; |
| 76 | |
| 77 | if (src.toString().endsWith(pathHops[0])) { |
| 78 | System.arraycopy(pathHops, 0, allHops, 0, pathHops.length); |
| 79 | } else { |
| 80 | System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length); |
| 81 | } |
| 82 | |
| 83 | result.add(createPath(allHops)); |
| 84 | return result; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) { |
| 89 | return getPaths(src, dst); |
| 90 | } |
| 91 | } |
| 92 | } |