b5d3d471f5705d4de14a623dd1265ca80ffe3b75
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / acl / 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;
18
19 import static io.fd.honeycomb.translate.v3po.interfaces.acl.AbstractAceWriter.VLAN_TAG_LEN;
20 import static io.fd.honeycomb.translate.v3po.interfaces.acl.AceIpWriterTestUtils.assertArrayEqualsWithOffset;
21 import static org.junit.Assert.assertEquals;
22 import static org.mockito.MockitoAnnotations.initMocks;
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mock;
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 io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
36 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
37 import io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
38 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
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() throws Exception {
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) {
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         assertArrayEqualsWithOffset(expectedMask, request.mask, vlanTags * VLAN_TAG_LEN);
92
93     }
94
95     private static void verifySessionRequest(final ClassifyAddDelSession request, final int tableIndex,
96                                              final int vlanTags) {
97         assertEquals(1, request.isAdd);
98         assertEquals(tableIndex, request.tableIndex);
99         assertEquals(0, request.hitNextIndex);
100
101         byte[] expectedMatch = new byte[] {
102             // L2:
103             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104             // version(6), dscp(11), flow(123):
105             (byte) 0x62, (byte) 0xc0, (byte) 0x00, (byte) 0x7b,
106             0, 0, 0, 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         assertArrayEqualsWithOffset(expectedMatch, request.match, vlanTags * VLAN_TAG_LEN);
117
118     }
119
120     @Test
121     public void testGetClassifyAddDelTableRequest() throws Exception {
122         final int nextTableIndex = 42;
123         final ClassifyAddDelTable request = writer.createClassifyTable(action, aceIp, nextTableIndex, 0);
124         verifyTableRequest(request, nextTableIndex, 0);
125     }
126
127     @Test
128     public void testGetClassifyAddDelTableRequest1VlanTag() throws Exception {
129         final int nextTableIndex = 42;
130         final int vlanTags = 1;
131         final ClassifyAddDelTable request = writer.createClassifyTable(action, aceIp, nextTableIndex, vlanTags);
132         verifyTableRequest(request, nextTableIndex, vlanTags);
133     }
134
135     @Test
136     public void testGetClassifyAddDelTableRequest2VlanTag() throws Exception {
137         final int nextTableIndex = 42;
138         final int vlanTags = 2;
139         final ClassifyAddDelTable request = writer.createClassifyTable(action, aceIp, nextTableIndex, vlanTags);
140         verifyTableRequest(request, nextTableIndex, vlanTags);
141     }
142
143     @Test
144     public void testGetClassifyAddDelSessionRequest() throws Exception {
145         final int tableIndex = 123;
146         final ClassifyAddDelSession request = writer.createClassifySession(action, aceIp, tableIndex, 0);
147         verifySessionRequest(request, tableIndex, 0);
148     }
149
150     @Test
151     public void testGetClassifyAddDelSessionRequest1VlanTag() throws Exception {
152         final int tableIndex = 123;
153         final int vlanTags = 1;
154         final ClassifyAddDelSession request = writer.createClassifySession(action, aceIp, tableIndex, vlanTags);
155         verifySessionRequest(request, tableIndex, vlanTags);
156     }
157
158     @Test
159     public void testGetClassifyAddDelSessionRequest2VlanTag() throws Exception {
160         final int tableIndex = 123;
161         final int vlanTags = 2;
162         final ClassifyAddDelSession request = writer.createClassifySession(action, aceIp, tableIndex, vlanTags);
163         verifySessionRequest(request, tableIndex, vlanTags);
164     }
165
166     @Test
167     public void testSetClassifyTable() throws Exception {
168         final int tableIndex = 321;
169         final InputAclSetInterface request = new InputAclSetInterface();
170         writer.setClassifyTable(request, tableIndex);
171         assertEquals(tableIndex, request.ip6TableIndex);
172     }
173 }