summaryrefslogtreecommitdiffstats
path: root/framework/src/onos/core/api/src/test/java/org/onosproject/net/flow/DefaultTrafficSelectorTest.java
blob: 10c5a63712e728dd6470cfe51eb56cdafc963ba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Copyright 2014-2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.net.flow;

import java.util.Set;

import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Test;
import org.onlab.packet.Ip6Address;
import org.onlab.packet.IpPrefix;
import org.onlab.packet.MacAddress;
import org.onlab.packet.MplsLabel;
import org.onlab.packet.TpPort;
import org.onlab.packet.VlanId;
import org.onosproject.net.IndexedLambda;
import org.onosproject.net.PortNumber;
import org.onosproject.net.flow.criteria.Criteria;
import org.onosproject.net.flow.criteria.Criterion;

import com.google.common.testing.EqualsTester;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.notNullValue;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
import static org.onosproject.net.flow.criteria.Criterion.Type;

/**
 * Unit tests for default traffic selector class.
 */
public class DefaultTrafficSelectorTest {

    /**
     * Checks that the DefaultFlowRule class is immutable.
     */
    @Test
    public void testImmutability() {
        assertThatClassIsImmutable(DefaultTrafficSelector.class);
    }

    /**
     * Tests equals(), hashCode() and toString() methods.
     */
    @Test
    public void testEquals() {
        final short one = 1;
        final short two = 2;

        final TrafficSelector selector1 = DefaultTrafficSelector.builder()
                .add(Criteria.matchLambda(new IndexedLambda(one)))
                .build();
        final TrafficSelector sameAsSelector1 = DefaultTrafficSelector.builder()
                .add(Criteria.matchLambda(new IndexedLambda(one)))
                .build();
        final TrafficSelector selector2 = DefaultTrafficSelector.builder()
                .add(Criteria.matchLambda(new IndexedLambda(two)))
                .build();

        new EqualsTester()
                .addEqualityGroup(selector1, sameAsSelector1)
                .addEqualityGroup(selector2)
                .testEquals();
    }

    /**
     * Hamcrest matcher to check that a selector contains a
     * Criterion with the specified type.
     */
    public static final class CriterionExistsMatcher
           extends TypeSafeMatcher<TrafficSelector> {
        private final Criterion.Type type;

        /**
         * Constructs a matcher for the given criterion type.
         *
         * @param typeValue criterion type to match
         */
        public CriterionExistsMatcher(Criterion.Type typeValue) {
            type = typeValue;
        }

        @Override
        public boolean matchesSafely(TrafficSelector selector) {
            final Set<Criterion> criteria = selector.criteria();

            return notNullValue().matches(criteria) &&
                   hasSize(1).matches(criteria) &&
                   notNullValue().matches(selector.getCriterion(type));
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("a criterion with type \" ").
                    appendText(type.toString()).
                    appendText("\"");
        }
    }


    /**
     * Creates a criterion type matcher.  Returns a matcher
     * for a criterion with the given type.
     *
     * @param type type of Criterion to match
     * @return Matcher object
     */
    @Factory
    public static Matcher<TrafficSelector> hasCriterionWithType(Criterion.Type type) {
        return new CriterionExistsMatcher(type);
    }


    /**
     * Tests the builder functions that add specific criteria.
     */
    @Test
    public void testCriteriaCreation() {
        TrafficSelector selector;

        final long longValue = 0x12345678;
        final int intValue = 22;
        final short shortValue = 33;
        final byte byteValue = 44;
        final byte dscpValue = 0xf;
        final byte ecnValue = 3;
        final MacAddress macValue = MacAddress.valueOf("11:22:33:44:55:66");
        final IpPrefix ipPrefixValue = IpPrefix.valueOf("192.168.1.0/24");
        final IpPrefix ipv6PrefixValue = IpPrefix.valueOf("fe80::1/64");
        final Ip6Address ipv6AddressValue = Ip6Address.valueOf("fe80::1");

        selector = DefaultTrafficSelector.builder()
                .matchInPort(PortNumber.portNumber(11)).build();
        assertThat(selector, hasCriterionWithType(Type.IN_PORT));

        selector = DefaultTrafficSelector.builder()
                .matchInPhyPort(PortNumber.portNumber(11)).build();
        assertThat(selector, hasCriterionWithType(Type.IN_PHY_PORT));

        selector = DefaultTrafficSelector.builder()
                .matchMetadata(longValue).build();
        assertThat(selector, hasCriterionWithType(Type.METADATA));

        selector = DefaultTrafficSelector.builder()
                .matchEthDst(macValue).build();
        assertThat(selector, hasCriterionWithType(Type.ETH_DST));

        selector = DefaultTrafficSelector.builder()
                .matchEthSrc(macValue).build();
        assertThat(selector, hasCriterionWithType(Type.ETH_SRC));

        selector = DefaultTrafficSelector.builder()
                .matchEthType(shortValue).build();
        assertThat(selector, hasCriterionWithType(Type.ETH_TYPE));

        selector = DefaultTrafficSelector.builder()
                .matchVlanId(VlanId.vlanId(shortValue)).build();
        assertThat(selector, hasCriterionWithType(Type.VLAN_VID));

        selector = DefaultTrafficSelector.builder()
                .matchVlanPcp(byteValue).build();
        assertThat(selector, hasCriterionWithType(Type.VLAN_PCP));

        selector = DefaultTrafficSelector.builder()
                .matchIPDscp(dscpValue).build();
        assertThat(selector, hasCriterionWithType(Type.IP_DSCP));

        selector = DefaultTrafficSelector.builder()
                .matchIPEcn(ecnValue).build();
        assertThat(selector, hasCriterionWithType(Type.IP_ECN));

        selector = DefaultTrafficSelector.builder()
                .matchIPProtocol(byteValue).build();
        assertThat(selector, hasCriterionWithType(Type.IP_PROTO));

        selector = DefaultTrafficSelector.builder()
                .matchIPSrc(ipPrefixValue).build();
        assertThat(selector, hasCriterionWithType(Type.IPV4_SRC));

        selector = DefaultTrafficSelector.builder()
                .matchIPDst(ipPrefixValue).build();
        assertThat(selector, hasCriterionWithType(Type.IPV4_DST));

        selector = DefaultTrafficSelector.builder()
                .matchTcpSrc(TpPort.tpPort(intValue)).build();
        assertThat(selector, hasCriterionWithType(Type.TCP_SRC));

        selector = DefaultTrafficSelector.builder()
                .matchTcpDst(TpPort.tpPort(intValue)).build();
        assertThat(selector, hasCriterionWithType(Type.TCP_DST));

        selector = DefaultTrafficSelector.builder()
                .matchUdpSrc(TpPort.tpPort(intValue)).build();
        assertThat(selector, hasCriterionWithType(Type.UDP_SRC));

        selector = DefaultTrafficSelector.builder()
                .matchUdpDst(TpPort.tpPort(intValue)).build();
        assertThat(selector, hasCriterionWithType(Type.UDP_DST));

        selector = DefaultTrafficSelector.builder()
                .matchSctpSrc(TpPort.tpPort(intValue)).build();
        assertThat(selector, hasCriterionWithType(Type.SCTP_SRC));

        selector = DefaultTrafficSelector.builder()
                .matchSctpDst(TpPort.tpPort(intValue)).build();
        assertThat(selector, hasCriterionWithType(Type.SCTP_DST));

        selector = DefaultTrafficSelector.builder()
                .matchIcmpType(byteValue).build();
        assertThat(selector, hasCriterionWithType(Type.ICMPV4_TYPE));

        selector = DefaultTrafficSelector.builder()
                .matchIcmpCode(byteValue).build();
        assertThat(selector, hasCriterionWithType(Type.ICMPV4_CODE));

        selector = DefaultTrafficSelector.builder()
                .matchIPv6Src(ipv6PrefixValue).build();
        assertThat(selector, hasCriterionWithType(Type.IPV6_SRC));

        selector = DefaultTrafficSelector.builder()
                .matchIPv6Dst(ipv6PrefixValue).build();
        assertThat(selector, hasCriterionWithType(Type.IPV6_DST));

        selector = DefaultTrafficSelector.builder()
                .matchIPv6FlowLabel(intValue).build();
        assertThat(selector, hasCriterionWithType(Type.IPV6_FLABEL));

        selector = DefaultTrafficSelector.builder()
                .matchIcmpv6Type(byteValue).build();
        assertThat(selector, hasCriterionWithType(Type.ICMPV6_TYPE));

        selector = DefaultTrafficSelector.builder()
                .matchIPv6NDTargetAddress(ipv6AddressValue).build();
        assertThat(selector, hasCriterionWithType(Type.IPV6_ND_TARGET));

        selector = DefaultTrafficSelector.builder()
                .matchIPv6NDSourceLinkLayerAddress(macValue).build();
        assertThat(selector, hasCriterionWithType(Type.IPV6_ND_SLL));

        selector = DefaultTrafficSelector.builder()
                .matchIPv6NDTargetLinkLayerAddress(macValue).build();
        assertThat(selector, hasCriterionWithType(Type.IPV6_ND_TLL));

        selector = DefaultTrafficSelector.builder()
                .matchMplsLabel(MplsLabel.mplsLabel(3)).build();
        assertThat(selector, hasCriterionWithType(Type.MPLS_LABEL));

        selector = DefaultTrafficSelector.builder()
                .matchIPv6ExthdrFlags(Criterion.IPv6ExthdrFlags.NONEXT.getValue()).build();
        assertThat(selector, hasCriterionWithType(Type.IPV6_EXTHDR));

        selector = DefaultTrafficSelector.builder()
                .add(Criteria.matchLambda(new IndexedLambda(shortValue))).build();
        assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
    }
}