HONEYCOMB-58 - Routing Api
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / acl / common / 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.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.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.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.acl.transport.header.fields.DestinationPortRangeBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.acl.transport.header.fields.SourcePortRangeBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.InterfaceMode;
39
40 public class AceIp6WriterTest {
41
42     private AceIp6Writer writer;
43     private PacketHandling action;
44     private AceIp aceIp;
45
46     @Before
47     public void setUp() {
48         initMocks(this);
49         writer = new AceIp6Writer();
50         action = new DenyBuilder().setDeny(true).build();
51         aceIp = new AceIpBuilder()
52             .setProtocol((short) 132)
53             .setDscp(new Dscp((short) 11))
54             .setAceIpVersion(new AceIpv6Builder()
55                 .setFlowLabel(new Ipv6FlowLabel(123L))
56                 .setSourceIpv6Network(new Ipv6Prefix("2001:db8:85a3:8d3:1319:8a2e:370:7348/128"))
57                 .setDestinationIpv6Network(new Ipv6Prefix("fe80:1234:5678:abcd:ef01::/64"))
58                 .build())
59             .setSourcePortRange(new SourcePortRangeBuilder().setLowerPort(new PortNumber(0x1111)).build())
60             .setDestinationPortRange(new DestinationPortRangeBuilder().setLowerPort(new PortNumber(0x2222)).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(vlanTags == 2 ? 5 : 4, 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             // dscp, flow:
79             (byte) 0x0f, (byte) 0xcf, (byte) 0xff, (byte) 0xff,
80             // protocol:
81             0, 0, (byte) 0xff, 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             // source and destination port:
89             -1, -1, -1, -1,
90             // padding to multiple of 16B:
91             0, 0, 0, 0, 0, 0
92         };
93
94         if (isL2) {
95             expectedMask[12] = (byte) 0xff;
96             expectedMask[13] = (byte) 0xff;
97         }
98         AceIpWriterTestUtils.assertArrayEqualsWithOffset(expectedMask, vlanTags == 2 ? 80 : 64, request.mask, vlanTags * VLAN_TAG_LEN);
99     }
100
101     private static void verifySessionRequest(final ClassifyAddDelSession request, final int tableIndex,
102                                              final int vlanTags, final boolean isL2) {
103         assertEquals(1, request.isAdd);
104         assertEquals(tableIndex, request.tableIndex);
105         assertEquals(0, request.hitNextIndex);
106
107         byte[] expectedMatch = new byte[] {
108             // L2:
109             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110             // dscp(11), flow(123):
111             (byte) 0x02, (byte) 0xc0, (byte) 0x00, (byte) 0x7b,
112             // protocol (132):
113             0, 0, (byte) 132, 0,
114             // source address:
115             (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, (byte) 0x85, (byte) 0xa3, (byte) 0x08, (byte) 0xd3,
116             (byte) 0x13, (byte) 0x19, (byte) 0x8a, (byte) 0x2e, (byte) 0x03, (byte) 0x70, (byte) 0x73, (byte) 0x48,
117             // destination address:
118             (byte) 0xfe, (byte) 0x80, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0xab, (byte) 0xcd,
119             0, 0, 0, 0, 0, 0, 0, 0,
120             // source and destination port:
121             0x11, 0x11, 0x22, 0x22,
122             // padding to multiple of 16B:
123             0, 0, 0, 0, 0, 0
124         };
125
126         if (isL2) {
127             expectedMatch[12] = (byte) 0x86;
128             expectedMatch[13] = (byte) 0xdd;
129         }
130         AceIpWriterTestUtils.assertArrayEqualsWithOffset(expectedMatch, vlanTags == 2 ? 80 : 64, request.match, vlanTags * VLAN_TAG_LEN);
131
132     }
133
134     @Test
135     public void testCreateTable() {
136         final int nextTableIndex = 42;
137         final ClassifyAddDelTable request =
138             writer.createTable(aceIp, InterfaceMode.L3, nextTableIndex, 0);
139         verifyTableRequest(request, nextTableIndex, 0, false);
140     }
141
142     @Test
143     public void testCreateTableForL2Interface() {
144         final int nextTableIndex = 42;
145         final ClassifyAddDelTable request =
146             writer.createTable(aceIp, InterfaceMode.L2, nextTableIndex, 0);
147         verifyTableRequest(request, nextTableIndex, 0, true);
148     }
149
150     @Test
151     public void testCreateTable1VlanTag() {
152         final int nextTableIndex = 42;
153         final int vlanTags = 1;
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 testCreateTable2VlanTags() {
161         final int nextTableIndex = 42;
162         final int vlanTags = 2;
163         final ClassifyAddDelTable request =
164             writer.createTable(aceIp, InterfaceMode.L3, nextTableIndex, vlanTags);
165         verifyTableRequest(request, nextTableIndex, vlanTags, false);
166     }
167
168     @Test
169     public void testCreateClassifySession() {
170         final int tableIndex = 123;
171         final ClassifyAddDelSession request =
172             writer.createSession(action, aceIp, InterfaceMode.L3, tableIndex, 0).get(0);
173         verifySessionRequest(request, tableIndex, 0, false);
174     }
175
176     @Test
177     public void testCreateClassifySessionForL2Interface() {
178         final int tableIndex = 123;
179         final ClassifyAddDelSession request =
180             writer.createSession(action, aceIp, InterfaceMode.L2, tableIndex, 0).get(0);
181         verifySessionRequest(request, tableIndex, 0, true);
182     }
183
184     @Test
185     public void testCreateClassifySession1VlanTag() {
186         final int tableIndex = 123;
187         final int vlanTags = 1;
188         final ClassifyAddDelSession request =
189             writer.createSession(action, aceIp, InterfaceMode.L3, tableIndex, vlanTags).get(0);
190         verifySessionRequest(request, tableIndex, vlanTags, false);
191     }
192
193     @Test
194     public void testCreateClassifySession2VlanTags() {
195         final int tableIndex = 123;
196         final int vlanTags = 2;
197         final ClassifyAddDelSession request =
198             writer.createSession(action, aceIp, InterfaceMode.L3, tableIndex, vlanTags).get(0);
199         verifySessionRequest(request, tableIndex, vlanTags, false);
200     }
201 }