260d454871c3c7ce7eb131661e43e933ddb0fc7a
[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 org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mock;
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 io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
35 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
36 import io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
37 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.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(-1, request.missNextIndex);
71         assertEquals(nextTableIndex, request.nextTableIndex);
72         assertEquals(0, request.skipNVectors);
73         assertEquals(AceIp6Writer.MATCH_N_VECTORS, request.matchNVectors);
74         assertEquals(AceIp6Writer.TABLE_MEM_SIZE, request.memorySize);
75
76         byte[] expectedMask = new byte[] {
77             // L2:
78             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79             // version, dscp, flow:
80             (byte) 0xff, (byte) 0xcf, (byte) 0xff, (byte) 0xff,
81             0, 0, 0, 0,
82             // source address:
83             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
84             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
85             // destination address:
86             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
87             0, 0, 0, 0, 0, 0, 0, 0,
88             // padding to multiple of 16B:
89             0, 0, 0, 0, 0, 0, 0, 0, 0, 0
90         };
91
92         if (isL2) {
93             expectedMask[12] = (byte) 0xff;
94             expectedMask[13] = (byte) 0xff;
95         }
96         AceIpWriterTestUtils.assertArrayEqualsWithOffset(expectedMask, request.mask, vlanTags * VLAN_TAG_LEN);
97     }
98
99     private static void verifySessionRequest(final ClassifyAddDelSession request, final int tableIndex,
100                                              final int vlanTags, final boolean isL2) {
101         assertEquals(1, request.isAdd);
102         assertEquals(tableIndex, request.tableIndex);
103         assertEquals(0, request.hitNextIndex);
104
105         byte[] expectedMatch = new byte[] {
106             // L2:
107             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
108             // version(6), dscp(11), flow(123):
109             (byte) 0x62, (byte) 0xc0, (byte) 0x00, (byte) 0x7b,
110             0, 0, 0, 0,
111             // source address:
112             (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, (byte) 0x85, (byte) 0xa3, (byte) 0x08, (byte) 0xd3,
113             (byte) 0x13, (byte) 0x19, (byte) 0x8a, (byte) 0x2e, (byte) 0x03, (byte) 0x70, (byte) 0x73, (byte) 0x48,
114             // destination address:
115             (byte) 0xfe, (byte) 0x80, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0xab, (byte) 0xcd,
116             0, 0, 0, 0, 0, 0, 0, 0,
117             // padding to multiple of 16B:
118             0, 0, 0, 0, 0, 0, 0, 0, 0, 0
119         };
120
121         if (isL2) {
122             expectedMatch[12] = (byte) 0x86;
123             expectedMatch[13] = (byte) 0xdd;
124         }
125         AceIpWriterTestUtils.assertArrayEqualsWithOffset(expectedMatch, request.match, vlanTags * VLAN_TAG_LEN);
126
127     }
128
129     @Test
130     public void testCreateClassifyTable() {
131         final int nextTableIndex = 42;
132         final ClassifyAddDelTable request =
133             writer.createClassifyTable(action, aceIp, InterfaceMode.L3, nextTableIndex, 0);
134         verifyTableRequest(request, nextTableIndex, 0, false);
135     }
136
137     @Test
138     public void testCreateClassifyTableForL2Interface() {
139         final int nextTableIndex = 42;
140         final ClassifyAddDelTable request =
141             writer.createClassifyTable(action, aceIp, InterfaceMode.L2, nextTableIndex, 0);
142         verifyTableRequest(request, nextTableIndex, 0, true);
143     }
144
145     @Test
146     public void testCreateClassifyTable1VlanTag() {
147         final int nextTableIndex = 42;
148         final int vlanTags = 1;
149         final ClassifyAddDelTable request =
150             writer.createClassifyTable(action, aceIp, InterfaceMode.L3, nextTableIndex, vlanTags);
151         verifyTableRequest(request, nextTableIndex, vlanTags, false);
152     }
153
154     @Test
155     public void testCreateClassifyTable2VlanTags() {
156         final int nextTableIndex = 42;
157         final int vlanTags = 2;
158         final ClassifyAddDelTable request =
159             writer.createClassifyTable(action, aceIp, InterfaceMode.L3, nextTableIndex, vlanTags);
160         verifyTableRequest(request, nextTableIndex, vlanTags, false);
161     }
162
163     @Test
164     public void testCreateClassifySession() {
165         final int tableIndex = 123;
166         final ClassifyAddDelSession request =
167             writer.createClassifySession(action, aceIp, InterfaceMode.L3, tableIndex, 0);
168         verifySessionRequest(request, tableIndex, 0, false);
169     }
170
171     @Test
172     public void testCreateClassifySessionForL2Interface() {
173         final int tableIndex = 123;
174         final ClassifyAddDelSession request =
175             writer.createClassifySession(action, aceIp, InterfaceMode.L2, tableIndex, 0);
176         verifySessionRequest(request, tableIndex, 0, true);
177     }
178
179     @Test
180     public void testCreateClassifySession1VlanTag() {
181         final int tableIndex = 123;
182         final int vlanTags = 1;
183         final ClassifyAddDelSession request =
184             writer.createClassifySession(action, aceIp, InterfaceMode.L3, tableIndex, vlanTags);
185         verifySessionRequest(request, tableIndex, vlanTags, false);
186     }
187
188     @Test
189     public void testCreateClassifySession2VlanTags() {
190         final int tableIndex = 123;
191         final int vlanTags = 2;
192         final ClassifyAddDelSession request =
193             writer.createClassifySession(action, aceIp, InterfaceMode.L3, tableIndex, vlanTags);
194         verifySessionRequest(request, tableIndex, vlanTags, false);
195     }
196
197     @Test
198     public void testSetClassifyTable() {
199         final int tableIndex = 321;
200         final InputAclSetInterface request = new InputAclSetInterface();
201         writer.setClassifyTable(request, tableIndex);
202         assertEquals(tableIndex, request.ip6TableIndex);
203     }
204 }