f3a9d0f87fd289752c17b5f7dfc6cf3aae939d8c
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / acl / common / AceIp4WriterTest.java
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.common;
18
19 import static io.fd.honeycomb.translate.v3po.interfaces.acl.common.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
94             .assertArrayEqualsWithOffset(expectedMask, expectedMask.length, request.mask, vlanTags * VLAN_TAG_LEN);
95
96     }
97
98     private static void verifySessionRequest(final ClassifyAddDelSession request, final int tableIndex,
99                                              final int vlanTags, final boolean isL2) {
100         assertEquals(1, request.isAdd);
101         assertEquals(tableIndex, request.tableIndex);
102         assertEquals(0, request.hitNextIndex);
103
104         byte[] expectedMatch = new byte[] {
105             // L2:
106             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
107             // dscp:
108             0, (byte) 0x2c,
109             // protocol (132):
110             0, 0, 0, 0, 0, 0, 0, (byte) 132, 0, 0,
111             // source address:
112             1, 2, 3, 4,
113             // destination address:
114             1, 2, 4, 0,
115             // source and destination port:
116             0x11, 0x11, 0x22, 0x22,
117             // padding:
118             0, 0, 0, 0, 0, 0, 0, 0, 0, 0
119         };
120
121         if (isL2) {
122             expectedMatch[12] = (byte) 0x08;
123             expectedMatch[13] = (byte) 0x00;
124         }
125         AceIpWriterTestUtils.assertArrayEqualsWithOffset(expectedMatch, expectedMatch.length, request.match, vlanTags * VLAN_TAG_LEN);
126
127     }
128
129     @Test
130     public void testCreateTable() throws Exception {
131         final int nextTableIndex = 42;
132         final ClassifyAddDelTable request = writer.createTable(aceIp, InterfaceMode.L3, nextTableIndex, 0);
133         verifyTableRequest(request, nextTableIndex, 0, false);
134     }
135
136     @Test
137     public void testCreateTableForL2Interface() throws Exception {
138         final int nextTableIndex = 42;
139         final ClassifyAddDelTable request = writer.createTable(aceIp, InterfaceMode.L2, nextTableIndex, 0);
140         verifyTableRequest(request, nextTableIndex, 0, true);
141     }
142
143     @Test
144     public void testCreateTable1VlanTag() throws Exception {
145         final int nextTableIndex = 42;
146         final int vlanTags = 1;
147         final ClassifyAddDelTable request = writer.createTable(aceIp, InterfaceMode.L3, nextTableIndex, vlanTags);
148         verifyTableRequest(request, nextTableIndex, vlanTags, false);
149     }
150
151     @Test
152     public void testCreateTable2VlanTags() throws Exception {
153         final int nextTableIndex = 42;
154         final int vlanTags = 2;
155         final ClassifyAddDelTable request = writer.createTable(aceIp, InterfaceMode.L3, nextTableIndex, vlanTags);
156         verifyTableRequest(request, nextTableIndex, vlanTags, false);
157     }
158
159     @Test
160     public void testCreateClassifySession() throws Exception {
161         final int tableIndex = 123;
162         final ClassifyAddDelSession request = writer.createSession(action, aceIp, InterfaceMode.L3, tableIndex, 0).get(0);
163         verifySessionRequest(request, tableIndex, 0, false);
164     }
165
166     @Test
167     public void testCreateClassifySessionForL2Interface() throws Exception {
168         final int tableIndex = 123;
169         final ClassifyAddDelSession request = writer.createSession(action, aceIp, InterfaceMode.L2, tableIndex, 0).get(0);
170         verifySessionRequest(request, tableIndex, 0, true);
171     }
172
173     @Test
174     public void testCreateClassifySession1VlanTag() throws Exception {
175         final int tableIndex = 123;
176         final int vlanTags = 1;
177         final ClassifyAddDelSession request = writer.createSession(action, aceIp, InterfaceMode.L3, tableIndex, vlanTags).get(0);
178         verifySessionRequest(request, tableIndex, vlanTags, false);
179     }
180
181     @Test
182     public void testCreateClassifySession2VlanTags() throws Exception {
183         final int tableIndex = 123;
184         final int vlanTags = 2;
185         final ClassifyAddDelSession request = writer.createSession(action, aceIp, InterfaceMode.L3, tableIndex, vlanTags).get(0);
186
187         verifySessionRequest(request, tableIndex, vlanTags, false);
188     }
189 }