948e5f95b60e141a3dcc96e1907cbc5aa5509ae3
[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.honeycomb.translate.v3po.interfaces.acl.ingress;
18
19 import static io.fd.honeycomb.translate.v3po.interfaces.acl.ingress.AclTranslator.VLAN_TAG_LEN;
20 import static org.junit.Assert.assertEquals;
21 import static org.mockito.MockitoAnnotations.initMocks;
22
23 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
24 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
25 import org.junit.Before;
26 import org.junit.Test;
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.PacketHandling;
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.actions.packet.handling.DenyBuilder;
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.AceIp;
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.AceIpBuilder;
31 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.AceIpv4Builder;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Dscp;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
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.acl.rev161214.InterfaceMode;
38
39 public class AceIp4WriterTest {
40
41     private AceIp4Writer writer;
42     private PacketHandling action;
43     private AceIp aceIp;
44
45     @Before
46     public void setUp() throws Exception {
47         initMocks(this);
48         writer = new AceIp4Writer();
49         action = new DenyBuilder().setDeny(true).build();
50         aceIp = new AceIpBuilder()
51             .setProtocol((short) 132)
52             .setDscp(new Dscp((short) 11))
53             .setAceIpVersion(new AceIpv4Builder()
54                 .setSourceIpv4Network(new Ipv4Prefix("1.2.3.4/32"))
55                 .setDestinationIpv4Network(new Ipv4Prefix("1.2.4.5/24"))
56                 .build())
57             .setSourcePortRange(new SourcePortRangeBuilder().setLowerPort(new PortNumber(0x1111)).build())
58             .setDestinationPortRange(new DestinationPortRangeBuilder().setLowerPort(new PortNumber(0x2222)).build())
59             .build();
60     }
61
62     private static void verifyTableRequest(final ClassifyAddDelTable request, final int nextTableIndex,
63                                            final int vlanTags, final boolean isL2) {
64         assertEquals(1, request.isAdd);
65         assertEquals(-1, request.tableIndex);
66         assertEquals(1, request.nbuckets);
67         assertEquals(nextTableIndex, request.nextTableIndex);
68         assertEquals(0, request.skipNVectors);
69         assertEquals(AceIp4Writer.MATCH_N_VECTORS, request.matchNVectors);
70         assertEquals(AceIp4Writer.TABLE_MEM_SIZE, request.memorySize);
71
72         byte[] expectedMask = new byte[] {
73             // L2:
74             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75             // dscp:
76             (byte) 0x00, (byte) 0xfc,
77             // protocol:
78             0, 0, 0, 0, 0, 0, 0, (byte) 0xff, 0, 0,
79             // source address:
80             -1, -1, -1, -1,
81             // destination address:
82             -1, -1, -1, 0,
83             // source and destination port:
84             -1, -1, -1, -1,
85             // padding:
86             0, 0, 0, 0, 0, 0, 0, 0, 0, 0
87         };
88
89         if (isL2) {
90             expectedMask[12] = (byte) 0xff;
91             expectedMask[13] = (byte) 0xff;
92         }
93         AceIpWriterTestUtils.assertArrayEqualsWithOffset(expectedMask, expectedMask.length, request.mask, vlanTags * VLAN_TAG_LEN);
94
95     }
96
97     private static void verifySessionRequest(final ClassifyAddDelSession request, final int tableIndex,
98                                              final int vlanTags, final boolean isL2) {
99         assertEquals(1, request.isAdd);
100         assertEquals(tableIndex, request.tableIndex);
101         assertEquals(0, request.hitNextIndex);
102
103         byte[] expectedMatch = new byte[] {
104             // L2:
105             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106             // dscp:
107             0, (byte) 0x2c,
108             // protocol (132):
109             0, 0, 0, 0, 0, 0, 0, (byte) 132, 0, 0,
110             // source address:
111             1, 2, 3, 4,
112             // destination address:
113             1, 2, 4, 0,
114             // source and destination port:
115             0x11, 0x11, 0x22, 0x22,
116             // padding:
117             0, 0, 0, 0, 0, 0, 0, 0, 0, 0
118         };
119
120         if (isL2) {
121             expectedMatch[12] = (byte) 0x08;
122             expectedMatch[13] = (byte) 0x00;
123         }
124         AceIpWriterTestUtils.assertArrayEqualsWithOffset(expectedMatch, expectedMatch.length, request.match, vlanTags * VLAN_TAG_LEN);
125
126     }
127
128     @Test
129     public void testCreateTable() throws Exception {
130         final int nextTableIndex = 42;
131         final ClassifyAddDelTable request = writer.createTable(aceIp, InterfaceMode.L3, nextTableIndex, 0);
132         verifyTableRequest(request, nextTableIndex, 0, false);
133     }
134
135     @Test
136     public void testCreateTableForL2Interface() throws Exception {
137         final int nextTableIndex = 42;
138         final ClassifyAddDelTable request = writer.createTable(aceIp, InterfaceMode.L2, nextTableIndex, 0);
139         verifyTableRequest(request, nextTableIndex, 0, true);
140     }
141
142     @Test
143     public void testCreateTable1VlanTag() throws Exception {
144         final int nextTableIndex = 42;
145         final int vlanTags = 1;
146         final ClassifyAddDelTable request = writer.createTable(aceIp, InterfaceMode.L3, nextTableIndex, vlanTags);
147         verifyTableRequest(request, nextTableIndex, vlanTags, false);
148     }
149
150     @Test
151     public void testCreateTable2VlanTags() throws Exception {
152         final int nextTableIndex = 42;
153         final int vlanTags = 2;
154         final ClassifyAddDelTable request = writer.createTable(aceIp, InterfaceMode.L3, nextTableIndex, vlanTags);
155         verifyTableRequest(request, nextTableIndex, vlanTags, false);
156     }
157
158     @Test
159     public void testCreateClassifySession() throws Exception {
160         final int tableIndex = 123;
161         final ClassifyAddDelSession request = writer.createSession(action, aceIp, InterfaceMode.L3, tableIndex, 0).get(0);
162         verifySessionRequest(request, tableIndex, 0, false);
163     }
164
165     @Test
166     public void testCreateClassifySessionForL2Interface() throws Exception {
167         final int tableIndex = 123;
168         final ClassifyAddDelSession request = writer.createSession(action, aceIp, InterfaceMode.L2, tableIndex, 0).get(0);
169         verifySessionRequest(request, tableIndex, 0, true);
170     }
171
172     @Test
173     public void testCreateClassifySession1VlanTag() throws Exception {
174         final int tableIndex = 123;
175         final int vlanTags = 1;
176         final ClassifyAddDelSession request = writer.createSession(action, aceIp, InterfaceMode.L3, tableIndex, vlanTags).get(0);
177         verifySessionRequest(request, tableIndex, vlanTags, false);
178     }
179
180     @Test
181     public void testCreateClassifySession2VlanTags() throws Exception {
182         final int tableIndex = 123;
183         final int vlanTags = 2;
184         final ClassifyAddDelSession request = writer.createSession(action, aceIp, InterfaceMode.L3, tableIndex, vlanTags).get(0);
185
186         verifySessionRequest(request, tableIndex, vlanTags, false);
187     }
188 }