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