Move interface acls to separate yang module
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / acl / ingress / AceIp6WriterTest.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.AceIpv6Builder;
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.Ipv6FlowLabel;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.InterfaceMode;
39
40 public class AceIp6WriterTest {
41
42     @Mock
43     private FutureJVppCore jvpp;
44     private AceIp6Writer writer;
45     private PacketHandling action;
46     private AceIp aceIp;
47
48     @Before
49     public void setUp() {
50         initMocks(this);
51         writer = new AceIp6Writer(jvpp);
52         action = new DenyBuilder().setDeny(true).build();
53         aceIp = new AceIpBuilder()
54             .setProtocol((short) 6)
55             .setDscp(new Dscp((short) 11))
56             .setAceIpVersion(new AceIpv6Builder()
57                 .setFlowLabel(new Ipv6FlowLabel(123L))
58                 .setSourceIpv6Network(new Ipv6Prefix("2001:db8:85a3:8d3:1319:8a2e:370:7348/128"))
59                 .setDestinationIpv6Network(new Ipv6Prefix("fe80:1234:5678:abcd:ef01::/64"))
60                 .build())
61             .build();
62     }
63
64
65     private static void verifyTableRequest(final ClassifyAddDelTable request, final int nextTableIndex,
66                                            final int vlanTags, final boolean isL2) {
67         assertEquals(1, request.isAdd);
68         assertEquals(-1, request.tableIndex);
69         assertEquals(1, request.nbuckets);
70         assertEquals(nextTableIndex, request.nextTableIndex);
71         assertEquals(0, request.skipNVectors);
72         assertEquals(AceIp6Writer.MATCH_N_VECTORS, request.matchNVectors);
73         assertEquals(AceIp6Writer.TABLE_MEM_SIZE, request.memorySize);
74
75         byte[] expectedMask = new byte[] {
76             // L2:
77             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78             // version, dscp, flow:
79             (byte) 0xff, (byte) 0xcf, (byte) 0xff, (byte) 0xff,
80             0, 0, 0, 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             // padding to multiple of 16B:
88             0, 0, 0, 0, 0, 0, 0, 0, 0, 0
89         };
90
91         if (isL2) {
92             expectedMask[12] = (byte) 0xff;
93             expectedMask[13] = (byte) 0xff;
94         }
95         AceIpWriterTestUtils.assertArrayEqualsWithOffset(expectedMask, request.mask, vlanTags * VLAN_TAG_LEN);
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             // version(6), dscp(11), flow(123):
108             (byte) 0x62, (byte) 0xc0, (byte) 0x00, (byte) 0x7b,
109             0, 0, 0, 0,
110             // source address:
111             (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, (byte) 0x85, (byte) 0xa3, (byte) 0x08, (byte) 0xd3,
112             (byte) 0x13, (byte) 0x19, (byte) 0x8a, (byte) 0x2e, (byte) 0x03, (byte) 0x70, (byte) 0x73, (byte) 0x48,
113             // destination address:
114             (byte) 0xfe, (byte) 0x80, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0xab, (byte) 0xcd,
115             0, 0, 0, 0, 0, 0, 0, 0,
116             // padding to multiple of 16B:
117             0, 0, 0, 0, 0, 0, 0, 0, 0, 0
118         };
119
120         if (isL2) {
121             expectedMatch[12] = (byte) 0x86;
122             expectedMatch[13] = (byte) 0xdd;
123         }
124         AceIpWriterTestUtils.assertArrayEqualsWithOffset(expectedMatch, request.match, vlanTags * VLAN_TAG_LEN);
125
126     }
127
128     @Test
129     public void testCreateClassifyTable() {
130         final int nextTableIndex = 42;
131         final ClassifyAddDelTable request =
132             writer.createClassifyTable(aceIp, InterfaceMode.L3, nextTableIndex, 0);
133         verifyTableRequest(request, nextTableIndex, 0, false);
134     }
135
136     @Test
137     public void testCreateClassifyTableForL2Interface() {
138         final int nextTableIndex = 42;
139         final ClassifyAddDelTable request =
140             writer.createClassifyTable(aceIp, InterfaceMode.L2, nextTableIndex, 0);
141         verifyTableRequest(request, nextTableIndex, 0, true);
142     }
143
144     @Test
145     public void testCreateClassifyTable1VlanTag() {
146         final int nextTableIndex = 42;
147         final int vlanTags = 1;
148         final ClassifyAddDelTable request =
149             writer.createClassifyTable(aceIp, InterfaceMode.L3, nextTableIndex, vlanTags);
150         verifyTableRequest(request, nextTableIndex, vlanTags, false);
151     }
152
153     @Test
154     public void testCreateClassifyTable2VlanTags() {
155         final int nextTableIndex = 42;
156         final int vlanTags = 2;
157         final ClassifyAddDelTable request =
158             writer.createClassifyTable(aceIp, InterfaceMode.L3, nextTableIndex, vlanTags);
159         verifyTableRequest(request, nextTableIndex, vlanTags, false);
160     }
161
162     @Test
163     public void testCreateClassifySession() {
164         final int tableIndex = 123;
165         final ClassifyAddDelSession request =
166             writer.createClassifySession(action, aceIp, InterfaceMode.L3, tableIndex, 0);
167         verifySessionRequest(request, tableIndex, 0, false);
168     }
169
170     @Test
171     public void testCreateClassifySessionForL2Interface() {
172         final int tableIndex = 123;
173         final ClassifyAddDelSession request =
174             writer.createClassifySession(action, aceIp, InterfaceMode.L2, tableIndex, 0);
175         verifySessionRequest(request, tableIndex, 0, true);
176     }
177
178     @Test
179     public void testCreateClassifySession1VlanTag() {
180         final int tableIndex = 123;
181         final int vlanTags = 1;
182         final ClassifyAddDelSession request =
183             writer.createClassifySession(action, aceIp, InterfaceMode.L3, tableIndex, vlanTags);
184         verifySessionRequest(request, tableIndex, vlanTags, false);
185     }
186
187     @Test
188     public void testCreateClassifySession2VlanTags() {
189         final int tableIndex = 123;
190         final int vlanTags = 2;
191         final ClassifyAddDelSession request =
192             writer.createClassifySession(action, aceIp, InterfaceMode.L3, tableIndex, vlanTags);
193         verifySessionRequest(request, tableIndex, vlanTags, false);
194     }
195
196     @Test
197     public void testSetClassifyTable() {
198         final int tableIndex = 321;
199         final InputAclSetInterface request = new InputAclSetInterface();
200         writer.setClassifyTable(request, tableIndex);
201         assertEquals(tableIndex, request.ip6TableIndex);
202     }
203 }