Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame^] | 1 | package org.onlab.onos.net.intent; |
| 2 | |
| 3 | import java.util.Collection; |
| 4 | |
| 5 | import org.hamcrest.Description; |
| 6 | import org.hamcrest.TypeSafeMatcher; |
| 7 | import org.onlab.onos.net.Link; |
| 8 | |
| 9 | /** |
| 10 | * Matcher to determine if a Collection of Links contains a path between a source |
| 11 | * and a destination. |
| 12 | */ |
| 13 | public class LinksHaveEntryWithSourceDestinationPairMatcher extends |
| 14 | TypeSafeMatcher<Collection<Link>> { |
| 15 | private final String source; |
| 16 | private final String destination; |
| 17 | |
| 18 | /** |
| 19 | * Creates a matcher for a given path represented by a source and |
| 20 | * a destination. |
| 21 | * |
| 22 | * @param source string identifier for the source of the path |
| 23 | * @param destination string identifier for the destination of the path |
| 24 | */ |
| 25 | LinksHaveEntryWithSourceDestinationPairMatcher(String source, |
| 26 | String destination) { |
| 27 | this.source = source; |
| 28 | this.destination = destination; |
| 29 | } |
| 30 | |
| 31 | @Override |
| 32 | public boolean matchesSafely(Collection<Link> links) { |
| 33 | for (Link link : links) { |
| 34 | if (link.src().elementId().toString().endsWith(source) && |
| 35 | link.dst().elementId().toString().endsWith(destination)) { |
| 36 | return true; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public void describeTo(Description description) { |
| 45 | description.appendText("link lookup for source \""); |
| 46 | description.appendText(source); |
| 47 | description.appendText(" and destination "); |
| 48 | description.appendText(destination); |
| 49 | description.appendText("\""); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void describeMismatchSafely(Collection<Link> links, |
| 54 | Description mismatchDescription) { |
| 55 | mismatchDescription.appendText("was "). |
| 56 | appendText(links.toString()); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Creates a link has path matcher. |
| 61 | * |
| 62 | * @param source string identifier for the source of the path |
| 63 | * @param destination string identifier for the destination of the path |
| 64 | * @return matcher to match the path |
| 65 | */ |
| 66 | public static LinksHaveEntryWithSourceDestinationPairMatcher linksHasPath( |
| 67 | String source, |
| 68 | String destination) { |
| 69 | return new LinksHaveEntryWithSourceDestinationPairMatcher(source, |
| 70 | destination); |
| 71 | } |
| 72 | } |
| 73 | |