3f3c6f7bc5357305766ee361434bb2dc818be099
[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.AceIpv6Builder;
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.Ipv6FlowLabel;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
34 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
35 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
36 import io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
37 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
38
39 public class AceIp6WriterTest {
40
41     @Mock
42     private FutureJVppCore jvpp;
43     private AceIp6Writer writer;
44     private PacketHandling action;
45     private AceIp aceIp;
46
47     @Before
48     public void setUp() throws Exception {
49         initMocks(this);
50         writer = new AceIp6Writer(jvpp);
51         action = new DenyBuilder().setDeny(true).build();
52         aceIp = new AceIpBuilder()
53             .setProtocol((short) 6)
54             .setDscp(new Dscp((short) 11))
55             .setAceIpVersion(new AceIpv6Builder()
56                 .setFlowLabel(new Ipv6FlowLabel(123L))
57                 .setSourceIpv6Network(new Ipv6Prefix("2001:db8:85a3:8d3:1319:8a2e:370:7348/128"))
58                 .setDestinationIpv6Network(new Ipv6Prefix("fe80:1234:5678:abcd:ef01::/64"))
59                 .build())
60             .build();
61     }
62
63
64     private static void verifyTableRequest(final ClassifyAddDelTable request, final int nextTableIndex,
65                                            final int vlanTags) {
66         assertEquals(1, request.isAdd);
67         assertEquals(-1, request.tableIndex);
68         assertEquals(1, request.nbuckets);
69         assertEquals(-1, request.missNextIndex);
70         assertEquals(nextTableIndex, request.nextTableIndex);
71         assertEquals(0, request.skipNVectors);
72         assertEquals(AceIp6Writer.MATCH_N_VECTORS, 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             // version, dscp, flow:
79             (byte) 0xff, (byte) 0xcf, (byte) 0xff, (byte) 0xff,
80             0, 0, 0, 0,
81             // source address:
82             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
83             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
84             // destination address:
85             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
86             0, 0, 0, 0, 0, 0, 0, 0,
87             // padding to multiple of 16B:
88             0, 0, 0, 0, 0, 0, 0, 0, 0, 0
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) {
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             // version(6), dscp(11), flow(123):
104             (byte) 0x62, (byte) 0xc0, (byte) 0x00, (byte) 0x7b,
105             0, 0, 0, 0,
106             // source address:
107             (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, (byte) 0x85, (byte) 0xa3, (byte) 0x08, (byte) 0xd3,
108             (byte) 0x13, (byte) 0x19, (byte) 0x8a, (byte) 0x2e, (byte) 0x03, (byte) 0x70, (byte) 0x73, (byte) 0x48,
109             // destination address:
110             (byte) 0xfe, (byte) 0x80, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0xab, (byte) 0xcd,
111             0, 0, 0, 0, 0, 0, 0, 0,
112             // padding to multiple of 16B:
113             0, 0, 0, 0, 0, 0, 0, 0, 0, 0
114         };
115         AceIpWriterTestUtils.assertArrayEqualsWithOffset(expectedMatch, request.match, vlanTags * VLAN_TAG_LEN);
116
117     }
118
119     @Test
120     public void testGetClassifyAddDelTableRequest() throws Exception {
121         final int nextTableIndex = 42;
122         final ClassifyAddDelTable request = writer.createClassifyTable(action, aceIp, nextTableIndex, 0);
123         verifyTableRequest(request, nextTableIndex, 0);
124     }
125
126     @Test
127     public void testGetClassifyAddDelTableRequest1VlanTag() throws Exception {
128         final int nextTableIndex = 42;
129         final int vlanTags = 1;
130         final ClassifyAddDelTable request = writer.createClassifyTable(action, aceIp, nextTableIndex, vlanTags);
131         verifyTableRequest(request, nextTableIndex, vlanTags);
132     }
133
134     @Test
135     public void testGetClassifyAddDelTableRequest2VlanTag() throws Exception {
136         final int nextTableIndex = 42;
137         final int vlanTags = 2;
138         final ClassifyAddDelTable request = writer.createClassifyTable(action, aceIp, nextTableIndex, vlanTags);
139         verifyTableRequest(request, nextTableIndex, vlanTags);
140     }
141
142     @Test
143     public void testGetClassifyAddDelSessionRequest() throws Exception {
144         final int tableIndex = 123;
145         final ClassifyAddDelSession request = writer.createClassifySession(action, aceIp, tableIndex, 0);
146         verifySessionRequest(request, tableIndex, 0);
147     }
148
149     @Test
150     public void testGetClassifyAddDelSessionRequest1VlanTag() throws Exception {
151         final int tableIndex = 123;
152         final int vlanTags = 1;
153         final ClassifyAddDelSession request = writer.createClassifySession(action, aceIp, tableIndex, vlanTags);
154         verifySessionRequest(request, tableIndex, vlanTags);
155     }
156
157     @Test
158     public void testGetClassifyAddDelSessionRequest2VlanTag() throws Exception {
159         final int tableIndex = 123;
160         final int vlanTags = 2;
161         final ClassifyAddDelSession request = writer.createClassifySession(action, aceIp, tableIndex, vlanTags);
162         verifySessionRequest(request, tableIndex, vlanTags);
163     }
164
165     @Test
166     public void testSetClassifyTable() throws Exception {
167         final int tableIndex = 321;
168         final InputAclSetInterface request = new InputAclSetInterface();
169         writer.setClassifyTable(request, tableIndex);
170         assertEquals(tableIndex, request.ip6TableIndex);
171     }
172 }