1a7045529c42dddd8649de7c96f8bf15b6a9f401
[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.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.AceIpv4Builder;
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.Ipv4Prefix;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.InterfaceMode;
38
39 public class AceIp4WriterTest {
40
41     @Mock
42     private FutureJVppCore jvpp;
43     private AceIp4Writer writer;
44     private PacketHandling action;
45     private AceIp aceIp;
46
47     @Before
48     public void setUp() throws Exception {
49         initMocks(this);
50         writer = new AceIp4Writer(jvpp);
51         action = new DenyBuilder().setDeny(true).build();
52         aceIp = new AceIpBuilder()
53             .setProtocol((short) 132)
54             .setDscp(new Dscp((short) 11))
55             .setAceIpVersion(new AceIpv4Builder()
56                 .setSourceIpv4Network(new Ipv4Prefix("1.2.3.4/32"))
57                 .setDestinationIpv4Network(new Ipv4Prefix("1.2.4.5/24"))
58                 .build())
59             .build();
60     }
61
62     private static void verifyTableRequest(final ClassifyAddDelTable request, final int nextTableIndex,
63                                            final int vlanTags, final boolean isL2) {
64         assertEquals(1, request.isAdd);
65         assertEquals(-1, request.tableIndex);
66         assertEquals(1, request.nbuckets);
67         assertEquals(nextTableIndex, request.nextTableIndex);
68         assertEquals(0, request.skipNVectors);
69         assertEquals(AceIp4Writer.MATCH_N_VECTORS, request.matchNVectors);
70         assertEquals(AceIp4Writer.TABLE_MEM_SIZE, request.memorySize);
71
72         byte[] expectedMask = new byte[] {
73             // L2:
74             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75             // dscp:
76             (byte) 0x00, (byte) 0xfc,
77             // protocol:
78             0, 0, 0, 0, 0, 0, 0, (byte) 0xff, 0, 0,
79             // source address:
80             -1, -1, -1, -1,
81             // destination address:
82             -1, -1, -1, 0,
83             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
84         };
85
86         if (isL2) {
87             expectedMask[12] = (byte) 0xff;
88             expectedMask[13] = (byte) 0xff;
89         }
90         AceIpWriterTestUtils.assertArrayEqualsWithOffset(expectedMask, request.mask, vlanTags * VLAN_TAG_LEN);
91
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:
104             0, (byte) 0x2c,
105             // protocol (132):
106             0, 0, 0, 0, 0, 0, 0, (byte) 132, 0, 0,
107             // source address:
108             1, 2, 3, 4,
109             // destination address:
110             1, 2, 4, 0,
111             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
112         };
113
114         if (isL2) {
115             expectedMatch[12] = (byte) 0x08;
116             expectedMatch[13] = (byte) 0x00;
117         }
118         AceIpWriterTestUtils.assertArrayEqualsWithOffset(expectedMatch, request.match, vlanTags * VLAN_TAG_LEN);
119
120     }
121
122     @Test
123     public void testCreateClassifyTable() throws Exception {
124         final int nextTableIndex = 42;
125         final ClassifyAddDelTable request = writer.createClassifyTable(aceIp, InterfaceMode.L3, nextTableIndex, 0);
126         verifyTableRequest(request, nextTableIndex, 0, false);
127     }
128
129     @Test
130     public void testCreateClassifyTableForL2Interface() throws Exception {
131         final int nextTableIndex = 42;
132         final ClassifyAddDelTable request = writer.createClassifyTable(aceIp, InterfaceMode.L2, nextTableIndex, 0);
133         verifyTableRequest(request, nextTableIndex, 0, true);
134     }
135
136     @Test
137     public void testCreateClassifyTable1VlanTag() throws Exception {
138         final int nextTableIndex = 42;
139         final int vlanTags = 1;
140         final ClassifyAddDelTable request = writer.createClassifyTable(aceIp, InterfaceMode.L3, nextTableIndex, vlanTags);
141         verifyTableRequest(request, nextTableIndex, vlanTags, false);
142     }
143
144     @Test
145     public void testCreateClassifyTable2VlanTags() throws Exception {
146         final int nextTableIndex = 42;
147         final int vlanTags = 2;
148         final ClassifyAddDelTable request = writer.createClassifyTable(aceIp, InterfaceMode.L3, nextTableIndex, vlanTags);
149         verifyTableRequest(request, nextTableIndex, vlanTags, false);
150     }
151
152     @Test
153     public void testCreateClassifySession() throws Exception {
154         final int tableIndex = 123;
155         final ClassifyAddDelSession request = writer.createClassifySession(action, aceIp, InterfaceMode.L3, tableIndex, 0);
156         verifySessionRequest(request, tableIndex, 0, false);
157     }
158
159     @Test
160     public void testCreateClassifySessionForL2Interface() throws Exception {
161         final int tableIndex = 123;
162         final ClassifyAddDelSession request = writer.createClassifySession(action, aceIp, InterfaceMode.L2, tableIndex, 0);
163         verifySessionRequest(request, tableIndex, 0, true);
164     }
165
166     @Test
167     public void testCreateClassifySession1VlanTag() throws Exception {
168         final int tableIndex = 123;
169         final int vlanTags = 1;
170         final ClassifyAddDelSession request = writer.createClassifySession(action, aceIp, InterfaceMode.L3, tableIndex, vlanTags);
171         verifySessionRequest(request, tableIndex, vlanTags, false);
172     }
173
174     @Test
175     public void testCreateClassifySession2VlanTags() throws Exception {
176         final int tableIndex = 123;
177         final int vlanTags = 2;
178         final ClassifyAddDelSession request = writer.createClassifySession(action, aceIp, InterfaceMode.L3, tableIndex, vlanTags);
179
180         verifySessionRequest(request, tableIndex, vlanTags, false);
181     }
182
183     @Test
184     public void testSetClassifyTable() throws Exception {
185         final int tableIndex = 321;
186         final InputAclSetInterface request = new InputAclSetInterface();
187         writer.setClassifyTable(request, tableIndex);
188         assertEquals(tableIndex, request.ip4TableIndex);
189     }
190 }