Move interface acls to separate yang module
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / acl / ingress / 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.ingress;
18
19 import static io.fd.honeycomb.translate.v3po.interfaces.acl.ingress.AbstractAceWriter.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 io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
26 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.Mock;
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.actions.PacketHandling;
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.actions.packet.handling.DenyBuilder;
32 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;
33 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;
34 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;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Dscp;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.InterfaceMode;
38
39 public class AceIp4WriterTest {
40
41     @Mock
42     private FutureJVppCore jvpp;
43     private AceIp4Writer writer;
44     private PacketHandling action;
45     private AceIp aceIp;
46
47     @Before
48     public void setUp() throws Exception {
49         initMocks(this);
50         writer = new AceIp4Writer(jvpp);
51         action = new DenyBuilder().setDeny(true).build();
52         aceIp = new AceIpBuilder()
53             .setProtocol((short) 4)
54             .setDscp(new Dscp((short) 11))
55             .setAceIpVersion(new AceIpv4Builder()
56                 .setSourceIpv4Network(new Ipv4Prefix("1.2.3.4/32"))
57                 .setDestinationIpv4Network(new Ipv4Prefix("1.2.4.5/24"))
58                 .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             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0xf0, (byte) 0xfc,
74             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1,
75             -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
76         };
77
78         if (isL2) {
79             expectedMask[12] = (byte) 0xff;
80             expectedMask[13] = (byte) 0xff;
81         }
82         AceIpWriterTestUtils.assertArrayEqualsWithOffset(expectedMask, request.mask, vlanTags * VLAN_TAG_LEN);
83
84     }
85
86     private static void verifySessionRequest(final ClassifyAddDelSession request, final int tableIndex,
87                                              final int vlanTags, final boolean isL2) {
88         assertEquals(1, request.isAdd);
89         assertEquals(tableIndex, request.tableIndex);
90         assertEquals(0, request.hitNextIndex);
91
92         byte[] expectedMatch = new byte[] {
93             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0x40, (byte) 0x2c,
94             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 1, 2,
95             4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
96         };
97
98         if (isL2) {
99             expectedMatch[12] = (byte) 0x08;
100             expectedMatch[13] = (byte) 0x00;
101         }
102         AceIpWriterTestUtils.assertArrayEqualsWithOffset(expectedMatch, request.match, vlanTags * VLAN_TAG_LEN);
103
104     }
105
106     @Test
107     public void testCreateClassifyTable() throws Exception {
108         final int nextTableIndex = 42;
109         final ClassifyAddDelTable request = writer.createClassifyTable(aceIp, InterfaceMode.L3, nextTableIndex, 0);
110         verifyTableRequest(request, nextTableIndex, 0, false);
111     }
112
113     @Test
114     public void testCreateClassifyTableForL2Interface() throws Exception {
115         final int nextTableIndex = 42;
116         final ClassifyAddDelTable request = writer.createClassifyTable(aceIp, InterfaceMode.L2, nextTableIndex, 0);
117         verifyTableRequest(request, nextTableIndex, 0, true);
118     }
119
120     @Test
121     public void testCreateClassifyTable1VlanTag() throws Exception {
122         final int nextTableIndex = 42;
123         final int vlanTags = 1;
124         final ClassifyAddDelTable request = writer.createClassifyTable(aceIp, InterfaceMode.L3, nextTableIndex, vlanTags);
125         verifyTableRequest(request, nextTableIndex, vlanTags, false);
126     }
127
128     @Test
129     public void testCreateClassifyTable2VlanTags() throws Exception {
130         final int nextTableIndex = 42;
131         final int vlanTags = 2;
132         final ClassifyAddDelTable request = writer.createClassifyTable(aceIp, InterfaceMode.L3, nextTableIndex, vlanTags);
133         verifyTableRequest(request, nextTableIndex, vlanTags, false);
134     }
135
136     @Test
137     public void testCreateClassifySession() throws Exception {
138         final int tableIndex = 123;
139         final ClassifyAddDelSession request = writer.createClassifySession(action, aceIp, InterfaceMode.L3, tableIndex, 0);
140         verifySessionRequest(request, tableIndex, 0, false);
141     }
142
143     @Test
144     public void testCreateClassifySessionForL2Interface() throws Exception {
145         final int tableIndex = 123;
146         final ClassifyAddDelSession request = writer.createClassifySession(action, aceIp, InterfaceMode.L2, tableIndex, 0);
147         verifySessionRequest(request, tableIndex, 0, true);
148     }
149
150     @Test
151     public void testCreateClassifySession1VlanTag() throws Exception {
152         final int tableIndex = 123;
153         final int vlanTags = 1;
154         final ClassifyAddDelSession request = writer.createClassifySession(action, aceIp, InterfaceMode.L3, tableIndex, vlanTags);
155         verifySessionRequest(request, tableIndex, vlanTags, false);
156     }
157
158     @Test
159     public void testCreateClassifySession2VlanTags() throws Exception {
160         final int tableIndex = 123;
161         final int vlanTags = 2;
162         final ClassifyAddDelSession request = writer.createClassifySession(action, aceIp, InterfaceMode.L3, tableIndex, vlanTags);
163
164         verifySessionRequest(request, tableIndex, vlanTags, false);
165     }
166
167     @Test
168     public void testSetClassifyTable() throws Exception {
169         final int tableIndex = 321;
170         final InputAclSetInterface request = new InputAclSetInterface();
171         writer.setClassifyTable(request, tableIndex);
172         assertEquals(tableIndex, request.ip4TableIndex);
173     }
174 }