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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 17 | |
| 18 | import java.util.Collection; |
| 19 | |
| 20 | import org.hamcrest.Description; |
| 21 | import org.hamcrest.TypeSafeMatcher; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.net.Link; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Matcher to determine if a Collection of Links contains a path between a source |
| 26 | * and a destination. |
| 27 | */ |
| 28 | public class LinksHaveEntryWithSourceDestinationPairMatcher extends |
| 29 | TypeSafeMatcher<Collection<Link>> { |
| 30 | private final String source; |
| 31 | private final String destination; |
| 32 | |
| 33 | /** |
| 34 | * Creates a matcher for a given path represented by a source and |
| 35 | * a destination. |
| 36 | * |
| 37 | * @param source string identifier for the source of the path |
| 38 | * @param destination string identifier for the destination of the path |
| 39 | */ |
| 40 | LinksHaveEntryWithSourceDestinationPairMatcher(String source, |
| 41 | String destination) { |
| 42 | this.source = source; |
| 43 | this.destination = destination; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public boolean matchesSafely(Collection<Link> links) { |
| 48 | for (Link link : links) { |
| 49 | if (link.src().elementId().toString().endsWith(source) && |
| 50 | link.dst().elementId().toString().endsWith(destination)) { |
| 51 | return true; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void describeTo(Description description) { |
| 60 | description.appendText("link lookup for source \""); |
| 61 | description.appendText(source); |
| 62 | description.appendText(" and destination "); |
| 63 | description.appendText(destination); |
| 64 | description.appendText("\""); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void describeMismatchSafely(Collection<Link> links, |
| 69 | Description mismatchDescription) { |
| 70 | mismatchDescription.appendText("was "). |
| 71 | appendText(links.toString()); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Creates a link has path matcher. |
| 76 | * |
| 77 | * @param source string identifier for the source of the path |
| 78 | * @param destination string identifier for the destination of the path |
| 79 | * @return matcher to match the path |
| 80 | */ |
| 81 | public static LinksHaveEntryWithSourceDestinationPairMatcher linksHasPath( |
| 82 | String source, |
| 83 | String destination) { |
| 84 | return new LinksHaveEntryWithSourceDestinationPairMatcher(source, |
| 85 | destination); |
| 86 | } |
| 87 | } |
| 88 | |