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