blob: fdc8e66d99e6b2569636d489590af7a78f3ba45e [file] [log] [blame]
Carmelo Casconef1db2842017-07-13 17:54:04 -04001/*
2 * Copyright 2017-present 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 */
16
17package org.onosproject.net.pi.impl;
18
19import org.junit.Test;
20import org.onlab.packet.EthType;
21import org.onlab.packet.IpPrefix;
22import org.onlab.packet.MacAddress;
23import org.onosproject.net.flow.criteria.Criteria;
24import org.onosproject.net.flow.criteria.EthCriterion;
25import org.onosproject.net.flow.criteria.EthTypeCriterion;
26import org.onosproject.net.flow.criteria.IPCriterion;
27import org.onosproject.net.pi.runtime.PiExactFieldMatch;
28import org.onosproject.net.pi.runtime.PiHeaderFieldId;
29import org.onosproject.net.pi.runtime.PiLpmFieldMatch;
30import org.onosproject.net.pi.runtime.PiTernaryFieldMatch;
31
32import java.util.Random;
33
34import static org.hamcrest.CoreMatchers.is;
35import static org.hamcrest.MatcherAssert.assertThat;
36import static org.onosproject.net.pi.impl.CriterionTranslatorHelper.translateCriterion;
37import static org.onosproject.net.pi.model.PiMatchType.*;
38
39/**
40 * Tests for CriterionTranslators.
41 */
42public class PiCriterionTranslatorsTest {
43
44 private Random random = new Random();
45 private final PiHeaderFieldId fieldId = PiHeaderFieldId.of("foo", "bar");
46
47 @Test
48 public void testEthCriterion() throws Exception {
49 MacAddress value1 = MacAddress.valueOf(random.nextLong());
50 MacAddress value2 = MacAddress.valueOf(random.nextLong());
51 MacAddress mask = MacAddress.valueOf(random.nextLong());
52 int bitWidth = value1.toBytes().length * 8;
53
54 EthCriterion criterion = (EthCriterion) Criteria.matchEthDst(value1);
55 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
56
57 EthCriterion maskedCriterion = (EthCriterion) Criteria.matchEthDstMasked(value2, mask);
58 PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) translateCriterion(maskedCriterion, fieldId, TERNARY,
59 bitWidth);
60
61 assertThat(exactMatch.value().asArray(), is(criterion.mac().toBytes()));
62 assertThat(ternaryMatch.value().asArray(), is(maskedCriterion.mac().toBytes()));
63 assertThat(ternaryMatch.mask().asArray(), is(maskedCriterion.mask().toBytes()));
64 }
65
66 @Test
67 public void testEthTypeCriterion() throws Exception {
68 EthType ethType = new EthType(random.nextInt());
69 int bitWidth = 16;
70
71 EthTypeCriterion criterion = (EthTypeCriterion) Criteria.matchEthType(ethType);
72
73 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
74
75 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is(criterion.ethType().toShort()));
76 }
77
78 @Test
79 public void testIpCriterion() throws Exception {
80 IpPrefix prefix1 = IpPrefix.valueOf(random.nextInt(), random.nextInt(32));
81 int bitWidth = prefix1.address().toOctets().length * 8;
82
83 IPCriterion criterion = (IPCriterion) Criteria.matchIPDst(prefix1);
84
85 PiLpmFieldMatch lpmMatch = (PiLpmFieldMatch) translateCriterion(criterion, fieldId, LPM, bitWidth);
86
87 assertThat(lpmMatch.value().asArray(), is(criterion.ip().address().toOctets()));
88 assertThat(lpmMatch.prefixLength(), is(criterion.ip().prefixLength()));
89 }
90
91
92}