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