85ee57f799cc011a108d28b2c34b23946197db60
[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 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.AceIpv4Builder;
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.Ipv4Prefix;
33 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
34 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
35 import io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
36 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
37
38 public class AceIp4WriterTest {
39
40     @Mock
41     private FutureJVppCore jvpp;
42     private AceIp4Writer writer;
43     private PacketHandling action;
44     private AceIp aceIp;
45
46     @Before
47     public void setUp() throws Exception {
48         initMocks(this);
49         writer = new AceIp4Writer(jvpp);
50         action = new DenyBuilder().setDeny(true).build();
51         aceIp = new AceIpBuilder()
52             .setProtocol((short) 4)
53             .setDscp(new Dscp((short) 11))
54             .setAceIpVersion(new AceIpv4Builder()
55                 .setSourceIpv4Network(new Ipv4Prefix("1.2.3.4/32"))
56                 .setDestinationIpv4Network(new Ipv4Prefix("1.2.4.5/24"))
57                 .build())
58             .build();
59     }
60
61     private static void verifyTableRequest(final ClassifyAddDelTable request, final int nextTableIndex,
62                                            final int vlanTags) {
63         assertEquals(1, request.isAdd);
64         assertEquals(-1, request.tableIndex);
65         assertEquals(1, request.nbuckets);
66         assertEquals(-1, request.missNextIndex);
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             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0xf0, (byte) 0xfc,
74             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1,
75             -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
76         };
77         AceIpWriterTestUtils.assertArrayEqualsWithOffset(expectedMask, request.mask, vlanTags * VLAN_TAG_LEN);
78
79     }
80
81     private static void verifySessionRequest(final ClassifyAddDelSession request, final int tableIndex,
82                                              final int vlanTags) {
83         assertEquals(1, request.isAdd);
84         assertEquals(tableIndex, request.tableIndex);
85         assertEquals(0, request.hitNextIndex);
86
87         byte[] expectedMatch = new byte[] {
88             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0x40, (byte) 0x2c,
89             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 1, 2,
90             4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
91         };
92         AceIpWriterTestUtils.assertArrayEqualsWithOffset(expectedMatch, request.match, vlanTags * VLAN_TAG_LEN);
93
94     }
95
96     @Test
97     public void testGetClassifyAddDelTableRequest() throws Exception {
98         final int nextTableIndex = 42;
99         final ClassifyAddDelTable request = writer.createClassifyTable(action, aceIp, nextTableIndex, 0);
100         verifyTableRequest(request, nextTableIndex, 0);
101     }
102
103     @Test
104     public void testGetClassifyAddDelTableRequest1VlanTag() throws Exception {
105         final int nextTableIndex = 42;
106         final int vlanTags = 1;
107         final ClassifyAddDelTable request = writer.createClassifyTable(action, aceIp, nextTableIndex, vlanTags);
108         verifyTableRequest(request, nextTableIndex, vlanTags);
109     }
110
111     @Test
112     public void testGetClassifyAddDelTableRequest2VlanTags() throws Exception {
113         final int nextTableIndex = 42;
114         final int vlanTags = 2;
115         final ClassifyAddDelTable request = writer.createClassifyTable(action, aceIp, nextTableIndex, vlanTags);
116         verifyTableRequest(request, nextTableIndex, vlanTags);
117     }
118
119     @Test
120     public void testGetClassifyAddDelSessionRequest() throws Exception {
121         final int tableIndex = 123;
122         final ClassifyAddDelSession request = writer.createClassifySession(action, aceIp, tableIndex, 0);
123         verifySessionRequest(request, tableIndex, 0);
124     }
125
126     @Test
127     public void testGetClassifyAddDelSessionRequest1VlanTag() throws Exception {
128         final int tableIndex = 123;
129         final int vlanTags = 1;
130         final ClassifyAddDelSession request = writer.createClassifySession(action, aceIp, tableIndex, vlanTags);
131         verifySessionRequest(request, tableIndex, vlanTags);
132     }
133
134     @Test
135     public void testGetClassifyAddDelSessionRequest2VlanTags() throws Exception {
136         final int tableIndex = 123;
137         final int vlanTags = 2;
138         final ClassifyAddDelSession request = writer.createClassifySession(action, aceIp, tableIndex, vlanTags);
139
140         verifySessionRequest(request, tableIndex, vlanTags);
141     }
142
143     @Test
144     public void testSetClassifyTable() throws Exception {
145         final int tableIndex = 321;
146         final InputAclSetInterface request = new InputAclSetInterface();
147         writer.setClassifyTable(request, tableIndex);
148         assertEquals(tableIndex, request.ip4TableIndex);
149     }
150 }