blob: a1ce3d9cd43f485cb477dba85c29b07064eb0431 [file] [log] [blame]
Ray Milkeyf19b7152014-11-21 10:56:52 -08001/*
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow;
Ray Milkeyf19b7152014-11-21 10:56:52 -080017
18import java.util.Set;
19
20import org.hamcrest.Description;
21import org.hamcrest.Factory;
22import org.hamcrest.Matcher;
23import org.hamcrest.TypeSafeMatcher;
24import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.PortNumber;
26import org.onosproject.net.flow.criteria.Criteria;
27import org.onosproject.net.flow.criteria.Criterion;
Ray Milkeyf19b7152014-11-21 10:56:52 -080028import org.onlab.packet.IpPrefix;
29import org.onlab.packet.MacAddress;
30import org.onlab.packet.VlanId;
31
32import com.google.common.testing.EqualsTester;
33
34import static org.hamcrest.MatcherAssert.assertThat;
35import static org.hamcrest.Matchers.hasSize;
36import static org.hamcrest.Matchers.notNullValue;
37import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import static org.onosproject.net.flow.criteria.Criterion.Type;
Ray Milkeyf19b7152014-11-21 10:56:52 -080039
40/**
41 * Unit tests for default traffic selector class.
42 */
43public class DefaultTrafficSelectorTest {
44
45 /**
46 * Checks that the DefaultFlowRule class is immutable.
47 */
48 @Test
49 public void testImmutability() {
50 assertThatClassIsImmutable(DefaultTrafficSelector.class);
51 }
52
53 /**
54 * Tests equals(), hashCode() and toString() methods.
55 */
56 @Test
57 public void testEquals() {
58 final short one = 1;
59 final short two = 2;
60
61 final TrafficSelector selector1 =
62 DefaultTrafficSelector.builder().matchLambda(one).build();
63 final TrafficSelector sameAsSelector1 =
64 DefaultTrafficSelector.builder().matchLambda(one).build();
65 final TrafficSelector selector2 =
66 DefaultTrafficSelector.builder().matchLambda(two).build();
67
68 new EqualsTester()
69 .addEqualityGroup(selector1, sameAsSelector1)
70 .addEqualityGroup(selector2)
71 .testEquals();
72 }
73
74 /**
Ray Milkeyff517732014-12-01 10:40:14 -080075 * Hamcrest matcher to check that a selector contains a
76 * Criterion with the specified type.
Ray Milkeyf19b7152014-11-21 10:56:52 -080077 */
78 public static final class CriterionExistsMatcher
79 extends TypeSafeMatcher<TrafficSelector> {
80 private final Criterion.Type type;
81
Ray Milkeyff517732014-12-01 10:40:14 -080082 /**
83 * Constructs a matcher for the given criterion type.
84 *
85 * @param typeValue criterion type to match
86 */
Ray Milkeyf19b7152014-11-21 10:56:52 -080087 public CriterionExistsMatcher(Criterion.Type typeValue) {
88 type = typeValue;
89 }
90
91 @Override
92 public boolean matchesSafely(TrafficSelector selector) {
93 final Set<Criterion> criteria = selector.criteria();
94
95 return notNullValue().matches(criteria) &&
96 hasSize(1).matches(criteria) &&
97 notNullValue().matches(selector.getCriterion(type));
98 }
99
100 @Override
101 public void describeTo(Description description) {
102 description.appendText("a criterion with type \" ").
103 appendText(type.toString()).
104 appendText("\"");
105 }
106 }
107
108
109 /**
Ray Milkeyff517732014-12-01 10:40:14 -0800110 * Creates a criterion type matcher. Returns a matcher
Ray Milkeyf19b7152014-11-21 10:56:52 -0800111 * for a criterion with the given type.
112 *
113 * @param type type of Criterion to match
114 * @return Matcher object
115 */
116 @Factory
117 public static Matcher<TrafficSelector> hasCriterionWithType(Criterion.Type type) {
118 return new CriterionExistsMatcher(type);
119 }
120
121
122 /**
123 * Tests the builder functions that add specific criteria.
124 */
125 @Test
126 public void testCriteriaCreation() {
127 TrafficSelector selector;
128
129 final short shortValue = 33;
130 final byte byteValue = 44;
131 final IpPrefix ipPrefixValue = IpPrefix.valueOf("192.168.1.0/24");
132
133 selector = DefaultTrafficSelector.builder()
134 .matchInport(PortNumber.portNumber(11)).build();
135 assertThat(selector, hasCriterionWithType(Type.IN_PORT));
136
137 selector = DefaultTrafficSelector.builder()
138 .matchEthSrc(MacAddress.BROADCAST).build();
139 assertThat(selector, hasCriterionWithType(Type.ETH_SRC));
140
141 selector = DefaultTrafficSelector.builder()
142 .matchEthDst(MacAddress.BROADCAST).build();
143 assertThat(selector, hasCriterionWithType(Type.ETH_DST));
144
145 selector = DefaultTrafficSelector.builder()
146 .matchEthType(shortValue).build();
147 assertThat(selector, hasCriterionWithType(Type.ETH_TYPE));
148
149 selector = DefaultTrafficSelector.builder()
150 .matchVlanId(VlanId.vlanId(shortValue)).build();
151 assertThat(selector, hasCriterionWithType(Type.VLAN_VID));
152
153 selector = DefaultTrafficSelector.builder()
154 .matchVlanPcp(byteValue).build();
155 assertThat(selector, hasCriterionWithType(Type.VLAN_PCP));
156
157 selector = DefaultTrafficSelector.builder()
158 .matchIPProtocol(byteValue).build();
159 assertThat(selector, hasCriterionWithType(Type.IP_PROTO));
160
161 selector = DefaultTrafficSelector.builder()
162 .matchIPSrc(ipPrefixValue).build();
163 assertThat(selector, hasCriterionWithType(Type.IPV4_SRC));
164
165 selector = DefaultTrafficSelector.builder()
166 .matchIPDst(ipPrefixValue).build();
167 assertThat(selector, hasCriterionWithType(Type.IPV4_DST));
168
169 selector = DefaultTrafficSelector.builder()
170 .matchTcpSrc(shortValue).build();
171 assertThat(selector, hasCriterionWithType(Type.TCP_SRC));
172
173 selector = DefaultTrafficSelector.builder()
174 .matchTcpDst(shortValue).build();
175 assertThat(selector, hasCriterionWithType(Type.TCP_DST));
176
177 selector = DefaultTrafficSelector.builder()
178 .matchMplsLabel(3).build();
179 assertThat(selector, hasCriterionWithType(Type.MPLS_LABEL));
180
181 selector = DefaultTrafficSelector.builder()
182 .matchLambda(shortValue).build();
183 assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
184
185 selector = DefaultTrafficSelector.builder()
186 .matchOpticalSignalType(shortValue).build();
187 assertThat(selector, hasCriterionWithType(Type.OCH_SIGTYPE));
188
189 final TrafficSelector baseSelector = DefaultTrafficSelector.builder()
190 .matchOpticalSignalType(shortValue).build();
191 selector = DefaultTrafficSelector.builder(baseSelector)
192 .build();
193 assertThat(selector, hasCriterionWithType(Type.OCH_SIGTYPE));
194
195 final Criterion criterion = Criteria.matchLambda(shortValue);
196 selector = DefaultTrafficSelector.builder()
197 .add(criterion).build();
198 assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
199 }
200}