4fee862a7cf260718e54ebec51cc664b518ed511
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
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
17 package io.fd.hc2vpp.vpp.classifier.write.acl.common;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.MockitoAnnotations.initMocks;
21
22 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
23 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.PacketHandling;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.packet.handling.DenyBuilder;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIp;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIpBuilder;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.ace.ip.version.AceIpv6Builder;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Dscp;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6FlowLabel;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.acl.transport.header.fields.DestinationPortRangeBuilder;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.acl.transport.header.fields.SourcePortRangeBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev161214.InterfaceMode;
38
39 public class AceIp6WriterTest {
40
41     private AceIp6Writer writer;
42     private PacketHandling action;
43     private AceIp aceIp;
44
45     @Before
46     public void setUp() {
47         initMocks(this);
48         writer = new AceIp6Writer();
49         action = new DenyBuilder().setDeny(true).build();
50         aceIp = new AceIpBuilder()
51             .setProtocol((short) 132)
52             .setDscp(new Dscp((short) 11))
53             .setAceIpVersion(new AceIpv6Builder()
54                 .setFlowLabel(new Ipv6FlowLabel(123L))
55                 .setSourceIpv6Network(new Ipv6Prefix("2001:db8:85a3:8d3:1319:8a2e:370:7348/128"))
56                 .setDestinationIpv6Network(new Ipv6Prefix("fe80:1234:5678:abcd:ef01::/64"))
57                 .build())
58             .setSourcePortRange(new SourcePortRangeBuilder().setLowerPort(new PortNumber(0x1111)).build())
59             .setDestinationPortRange(new DestinationPortRangeBuilder().setLowerPort(new PortNumber(0x2222)).build())
60             .build();
61     }
62
63
64     private static void verifyTableRequest(final ClassifyAddDelTable request, final int nextTableIndex,
65                                            final int vlanTags, final boolean isL2) {
66         assertEquals(1, request.isAdd);
67         assertEquals(-1, request.tableIndex);
68         assertEquals(1, request.nbuckets);
69         assertEquals(nextTableIndex, request.nextTableIndex);
70         assertEquals(0, request.skipNVectors);
71         assertEquals(vlanTags == 2 ? 5 : 4, request.matchNVectors);
72         assertEquals(AceIp6Writer.TABLE_MEM_SIZE, request.memorySize);
73
74         byte[] expectedMask = new byte[] {
75             // L2:
76             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77             // dscp, flow:
78             (byte) 0x0f, (byte) 0xcf, (byte) 0xff, (byte) 0xff,
79             // protocol:
80             0, 0, (byte) 0xff, 0,
81             // source address:
82             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
83             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
84             // destination address:
85             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
86             0, 0, 0, 0, 0, 0, 0, 0,
87             // source and destination port:
88             -1, -1, -1, -1,
89             // padding to multiple of 16B:
90             0, 0, 0, 0, 0, 0
91         };
92
93         if (isL2) {
94             expectedMask[12] = (byte) 0xff;
95             expectedMask[13] = (byte) 0xff;
96         }
97         AceIpWriterTestUtils.assertArrayEqualsWithOffset(expectedMask, vlanTags == 2 ? 80 : 64, request.mask, vlanTags * AclTranslator.VLAN_TAG_LEN);
98     }
99
100     private static void verifySessionRequest(final ClassifyAddDelSession request, final int tableIndex,
101                                              final int vlanTags, final boolean isL2) {
102         assertEquals(1, request.isAdd);
103         assertEquals(tableIndex, request.tableIndex);
104         assertEquals(0, request.hitNextIndex);
105
106         byte[] expectedMatch = new byte[] {
107             // L2:
108             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
109             // dscp(11), flow(123):
110             (byte) 0x02, (byte) 0xc0, (byte) 0x00, (byte) 0x7b,
111             // protocol (132):
112             0, 0, (byte) 132, 0,
113             // source address:
114             (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, (byte) 0x85, (byte) 0xa3, (byte) 0x08, (byte) 0xd3,
115             (byte) 0x13, (byte) 0x19, (byte) 0x8a, (byte) 0x2e, (byte) 0x03, (byte) 0x70, (byte) 0x73, (byte) 0x48,
116             // destination address:
117             (byte) 0xfe, (byte) 0x80, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0xab, (byte) 0xcd,
118             0, 0, 0, 0, 0, 0, 0, 0,
119             // source and destination port:
120             0x11, 0x11, 0x22, 0x22,
121             // padding to multiple of 16B:
122             0, 0, 0, 0, 0, 0
123         };
124
125         if (isL2) {
126             expectedMatch[12] = (byte) 0x86;
127             expectedMatch[13] = (byte) 0xdd;
128         }
129         AceIpWriterTestUtils.assertArrayEqualsWithOffset(expectedMatch, vlanTags == 2 ? 80 : 64, request.match, vlanTags * AclTranslator.VLAN_TAG_LEN);
130
131     }
132
133     @Test
134     public void testCreateTable() {
135         final int nextTableIndex = 42;
136         final ClassifyAddDelTable request =
137             writer.createTable(aceIp, InterfaceMode.L3, nextTableIndex, 0);
138         verifyTableRequest(request, nextTableIndex, 0, false);
139     }
140
141     @Test
142     public void testCreateTableForL2Interface() {
143         final int nextTableIndex = 42;
144         final ClassifyAddDelTable request =
145             writer.createTable(aceIp, InterfaceMode.L2, nextTableIndex, 0);
146         verifyTableRequest(request, nextTableIndex, 0, true);
147     }
148
149     @Test
150     public void testCreateTable1VlanTag() {
151         final int nextTableIndex = 42;
152         final int vlanTags = 1;
153         final ClassifyAddDelTable request =
154             writer.createTable(aceIp, InterfaceMode.L3, nextTableIndex, vlanTags);
155         verifyTableRequest(request, nextTableIndex, vlanTags, false);
156     }
157
158     @Test
159     public void testCreateTable2VlanTags() {
160         final int nextTableIndex = 42;
161         final int vlanTags = 2;
162         final ClassifyAddDelTable request =
163             writer.createTable(aceIp, InterfaceMode.L3, nextTableIndex, vlanTags);
164         verifyTableRequest(request, nextTableIndex, vlanTags, false);
165     }
166
167     @Test
168     public void testCreateClassifySession() {
169         final int tableIndex = 123;
170         final ClassifyAddDelSession request =
171             writer.createSession(action, aceIp, InterfaceMode.L3, tableIndex, 0).get(0);
172         verifySessionRequest(request, tableIndex, 0, false);
173     }
174
175     @Test
176     public void testCreateClassifySessionForL2Interface() {
177         final int tableIndex = 123;
178         final ClassifyAddDelSession request =
179             writer.createSession(action, aceIp, InterfaceMode.L2, tableIndex, 0).get(0);
180         verifySessionRequest(request, tableIndex, 0, true);
181     }
182
183     @Test
184     public void testCreateClassifySession1VlanTag() {
185         final int tableIndex = 123;
186         final int vlanTags = 1;
187         final ClassifyAddDelSession request =
188             writer.createSession(action, aceIp, InterfaceMode.L3, tableIndex, vlanTags).get(0);
189         verifySessionRequest(request, tableIndex, vlanTags, false);
190     }
191
192     @Test
193     public void testCreateClassifySession2VlanTags() {
194         final int tableIndex = 123;
195         final int vlanTags = 2;
196         final ClassifyAddDelSession request =
197             writer.createSession(action, aceIp, InterfaceMode.L3, tableIndex, vlanTags).get(0);
198         verifySessionRequest(request, tableIndex, vlanTags, false);
199     }
200 }