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