blob: b3e2acfbdc670bb4ba586c61f2b99853ab306db4 [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 */
16package org.onlab.onos.net.flow;
17
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;
25import org.onlab.onos.net.PortNumber;
26import org.onlab.onos.net.flow.criteria.Criteria;
27import org.onlab.onos.net.flow.criteria.Criterion;
28import 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;
38import static org.onlab.onos.net.flow.criteria.Criterion.Type;
39
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 /**
75 * Hamcrest matcher to check that a collection of Intents contains an
76 * Intent with the specified Intent Id.
77 */
78 public static final class CriterionExistsMatcher
79 extends TypeSafeMatcher<TrafficSelector> {
80 private final Criterion.Type type;
81
82 public CriterionExistsMatcher(Criterion.Type typeValue) {
83 type = typeValue;
84 }
85
86 @Override
87 public boolean matchesSafely(TrafficSelector selector) {
88 final Set<Criterion> criteria = selector.criteria();
89
90 return notNullValue().matches(criteria) &&
91 hasSize(1).matches(criteria) &&
92 notNullValue().matches(selector.getCriterion(type));
93 }
94
95 @Override
96 public void describeTo(Description description) {
97 description.appendText("a criterion with type \" ").
98 appendText(type.toString()).
99 appendText("\"");
100 }
101 }
102
103
104 /**
105 * Factory method to create a criterion type matcher. Returns a matcher
106 * for a criterion with the given type.
107 *
108 * @param type type of Criterion to match
109 * @return Matcher object
110 */
111 @Factory
112 public static Matcher<TrafficSelector> hasCriterionWithType(Criterion.Type type) {
113 return new CriterionExistsMatcher(type);
114 }
115
116
117 /**
118 * Tests the builder functions that add specific criteria.
119 */
120 @Test
121 public void testCriteriaCreation() {
122 TrafficSelector selector;
123
124 final short shortValue = 33;
125 final byte byteValue = 44;
126 final IpPrefix ipPrefixValue = IpPrefix.valueOf("192.168.1.0/24");
127
128 selector = DefaultTrafficSelector.builder()
129 .matchInport(PortNumber.portNumber(11)).build();
130 assertThat(selector, hasCriterionWithType(Type.IN_PORT));
131
132 selector = DefaultTrafficSelector.builder()
133 .matchEthSrc(MacAddress.BROADCAST).build();
134 assertThat(selector, hasCriterionWithType(Type.ETH_SRC));
135
136 selector = DefaultTrafficSelector.builder()
137 .matchEthDst(MacAddress.BROADCAST).build();
138 assertThat(selector, hasCriterionWithType(Type.ETH_DST));
139
140 selector = DefaultTrafficSelector.builder()
141 .matchEthType(shortValue).build();
142 assertThat(selector, hasCriterionWithType(Type.ETH_TYPE));
143
144 selector = DefaultTrafficSelector.builder()
145 .matchVlanId(VlanId.vlanId(shortValue)).build();
146 assertThat(selector, hasCriterionWithType(Type.VLAN_VID));
147
148 selector = DefaultTrafficSelector.builder()
149 .matchVlanPcp(byteValue).build();
150 assertThat(selector, hasCriterionWithType(Type.VLAN_PCP));
151
152 selector = DefaultTrafficSelector.builder()
153 .matchIPProtocol(byteValue).build();
154 assertThat(selector, hasCriterionWithType(Type.IP_PROTO));
155
156 selector = DefaultTrafficSelector.builder()
157 .matchIPSrc(ipPrefixValue).build();
158 assertThat(selector, hasCriterionWithType(Type.IPV4_SRC));
159
160 selector = DefaultTrafficSelector.builder()
161 .matchIPDst(ipPrefixValue).build();
162 assertThat(selector, hasCriterionWithType(Type.IPV4_DST));
163
164 selector = DefaultTrafficSelector.builder()
165 .matchTcpSrc(shortValue).build();
166 assertThat(selector, hasCriterionWithType(Type.TCP_SRC));
167
168 selector = DefaultTrafficSelector.builder()
169 .matchTcpDst(shortValue).build();
170 assertThat(selector, hasCriterionWithType(Type.TCP_DST));
171
172 selector = DefaultTrafficSelector.builder()
173 .matchMplsLabel(3).build();
174 assertThat(selector, hasCriterionWithType(Type.MPLS_LABEL));
175
176 selector = DefaultTrafficSelector.builder()
177 .matchLambda(shortValue).build();
178 assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
179
180 selector = DefaultTrafficSelector.builder()
181 .matchOpticalSignalType(shortValue).build();
182 assertThat(selector, hasCriterionWithType(Type.OCH_SIGTYPE));
183
184 final TrafficSelector baseSelector = DefaultTrafficSelector.builder()
185 .matchOpticalSignalType(shortValue).build();
186 selector = DefaultTrafficSelector.builder(baseSelector)
187 .build();
188 assertThat(selector, hasCriterionWithType(Type.OCH_SIGTYPE));
189
190 final Criterion criterion = Criteria.matchLambda(shortValue);
191 selector = DefaultTrafficSelector.builder()
192 .add(criterion).build();
193 assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
194 }
195}