5626352b3d23f76dce495d147bd8aaacc3562dbd
[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.hc2vpp.vpp.classifier.write.acl.common;
18
19 import static org.junit.Assert.assertArrayEquals;
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.inet.types.rev130715.Ipv4Prefix;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.acl.transport.header.fields.DestinationPortRangeBuilder;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.acl.transport.header.fields.SourcePortRangeBuilder;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.InterfaceMode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIpAndEth;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIpAndEthBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.and.eth.AceIpAndEthNodesBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170315.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.and.eth.ace.ip.and.eth.nodes.ace.ip.version.AceIpv4Builder;
39
40 public class AceIpAndEthWriterTest {
41
42     private AceIpAndEthWriter writer;
43     private PacketHandling action;
44     private AceIpAndEth ace;
45
46     @Before
47     public void setUp() {
48         initMocks(this);
49         writer = new AceIpAndEthWriter();
50         action = new DenyBuilder().setDeny(true).build();
51         ace = new AceIpAndEthBuilder().setAceIpAndEthNodes(new AceIpAndEthNodesBuilder()
52             .setDestinationMacAddress(new MacAddress("11:22:33:44:55:66"))
53             .setSourceMacAddress(new MacAddress("aa:bb:cc:dd:ee:ff"))
54             .setAceIpVersion(new AceIpv4Builder()
55                 .setSourceIpv4Network(new Ipv4Prefix("1.2.3.4/32")).build())
56             .setSourcePortRange(new SourcePortRangeBuilder().setLowerPort(new PortNumber(0x1111)).build())
57             .setDestinationPortRange(new DestinationPortRangeBuilder().setLowerPort(new PortNumber(0x2222)).build())
58             .build()).build();
59     }
60
61     @Test
62     public void testCreateTable() {
63         final int nextTableIndex = 42;
64         final ClassifyAddDelTable request = writer.createTable(ace, InterfaceMode.L2, nextTableIndex, 0);
65
66         assertEquals(1, request.isAdd);
67         assertEquals(-1, request.tableIndex);
68         assertEquals(1, request.nbuckets);
69         assertEquals(nextTableIndex, request.nextTableIndex);
70         assertEquals(0, request.skipNVectors);
71         assertEquals(3, request.matchNVectors);
72         assertEquals(AceEthWriter.TABLE_MEM_SIZE, request.memorySize);
73
74         byte[] expectedMask = new byte[] {
75             // destination MAC:
76             -1, -1, -1, -1, -1, -1,
77             // source MAC:
78             -1, -1, -1, -1, -1, -1,
79             // ether type:
80             -1, -1,
81             // IP header
82             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83             // source address:
84             -1, -1, -1, -1,
85             // destination address:
86             0, 0, 0, 0,
87             // source and destination port:
88             -1, -1, -1, -1,
89             // padding:
90             0, 0, 0, 0, 0, 0, 0, 0, 0, 0
91         };
92         assertArrayEquals(expectedMask, request.mask);
93     }
94
95     @Test
96     public void testCreateClassifySession() {
97         final int tableIndex = 123;
98         final ClassifyAddDelSession request = writer.createSession(action, ace, InterfaceMode.L2, tableIndex, 0).get(0);
99
100         assertEquals(1, request.isAdd);
101         assertEquals(tableIndex, request.tableIndex);
102         assertEquals(0, request.hitNextIndex);
103
104         byte[] expectedMatch = new byte[] {
105             // destination MAC:
106             (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66,
107             // source MAC:
108             (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd, (byte) 0xee, (byte) 0xff,
109             // ether type (IP4):
110             0x08, 0,
111             // IP header
112             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113             // source address:
114             1, 2, 3, 4,
115             // destination address:
116             0, 0, 0, 0,
117             // source and destination port:
118             0x11, 0x11, 0x22, 0x22,
119             // padding:
120             0, 0, 0, 0, 0, 0, 0, 0, 0, 0
121         };
122         assertArrayEquals(expectedMatch, request.match);
123     }
124 }