blob: c5125be2017aa60cdbbb92bd470b1b7583621f5f [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent;
Ray Milkeye6684082014-10-16 16:59:47 -070017
18import java.util.Collection;
19
20import org.hamcrest.Description;
21import org.hamcrest.TypeSafeMatcher;
Ray Milkey6e0fb302015-04-16 14:44:12 -070022import org.onosproject.net.EdgeLink;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.Link;
Ray Milkeye6684082014-10-16 16:59:47 -070024
25/**
26 * Matcher to determine if a Collection of Links contains a path between a source
27 * and a destination.
28 */
29public 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 Milkey6e0fb302015-04-16 14:44:12 -070050 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 Milkeye6684082014-10-16 16:59:47 -070058 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