Boxing cleanup
[netvirt.git] / vpnservice / aclservice / impl / src / test / java / org / opendaylight / netvirt / aclservice / StatelessEgressAclServiceImplTest.java
1 /*
2  * Copyright © 2016, 2017 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netvirt.aclservice;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.when;
16
17 import com.google.common.base.Optional;
18 import com.google.common.util.concurrent.Futures;
19 import java.math.BigInteger;
20 import java.util.Arrays;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mock;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
28 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.genius.mdsalutil.FlowEntity;
31 import org.opendaylight.genius.mdsalutil.NwConstants;
32 import org.opendaylight.genius.mdsalutil.NxMatchFieldType;
33 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
34 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
35 import org.opendaylight.genius.mdsalutil.matches.MatchTcpFlags;
36 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
37 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
38 import org.opendaylight.netvirt.aclservice.utils.AclServiceTestUtils;
39 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
40 import org.opendaylight.netvirt.aclservice.utils.MethodInvocationParamSaver;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.Ipv4Acl;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.AclBuilder;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.AclKey;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.AccessListEntriesBuilder;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.AceBuilder;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.MatchesBuilder;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIpBuilder;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.ace.ip.version.AceIpv4Builder;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160218.acl.transport.header.fields.DestinationPortRangeBuilder;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.config.rev160806.AclserviceConfig;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionEgress;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.IpPrefixOrAddress;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttrBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairsBuilder;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62
63 @RunWith(MockitoJUnitRunner.class)
64 public class StatelessEgressAclServiceImplTest {
65
66     StatelessEgressAclServiceImpl testedService;
67
68     @Mock DataBroker dataBroker;
69     @Mock IMdsalApiManager mdsalManager;
70     @Mock WriteTransaction mockWriteTx;
71     @Mock ReadOnlyTransaction mockReadTx;
72     @Mock AclserviceConfig config;
73
74     MethodInvocationParamSaver<Void> installFlowValueSaver = null;
75     MethodInvocationParamSaver<Void> removeFlowValueSaver = null;
76
77     @Before
78     public void setUp() {
79         AclDataUtil aclDataUtil = new AclDataUtil();
80         AclServiceUtils aclServiceUtils = new AclServiceUtils(aclDataUtil, config);
81         testedService = new StatelessEgressAclServiceImpl(dataBroker, mdsalManager, aclDataUtil, aclServiceUtils);
82         doReturn(Futures.immediateCheckedFuture(null)).when(mockWriteTx).submit();
83         doReturn(mockReadTx).when(dataBroker).newReadOnlyTransaction();
84         doReturn(mockWriteTx).when(dataBroker).newWriteOnlyTransaction();
85         installFlowValueSaver = new MethodInvocationParamSaver<>(null);
86         doAnswer(installFlowValueSaver).when(mdsalManager).installFlow(any(FlowEntity.class));
87         removeFlowValueSaver = new MethodInvocationParamSaver<>(null);
88         doAnswer(removeFlowValueSaver).when(mdsalManager).removeFlow(any(FlowEntity.class));
89     }
90
91     @Test
92     public void addAcl__NullInterface() {
93         assertEquals(false, testedService.applyAcl(null));
94     }
95
96     @Test
97     public void addAcl__MissingInterfaceStateShouldFail() throws Exception {
98         AclInterface ai = new AclInterface();
99         ai.setPortSecurityEnabled(true);
100         ai.setDpId(BigInteger.ONE);
101         assertEquals(false, testedService.applyAcl(ai));
102     }
103
104     @Test
105     public void addAcl__SinglePort() throws Exception {
106         Uuid sgUuid = new Uuid("12345678-1234-1234-1234-123456789012");
107         AclInterface ai = stubTcpAclInterface(sgUuid, "if_name", "1.1.1.1/32", 80, 80);
108         assertEquals(true, testedService.applyAcl(ai));
109         assertEquals(10, installFlowValueSaver.getNumOfInvocations());
110
111         FlowEntity firstRangeFlow = (FlowEntity) installFlowValueSaver.getInvocationParams(9).get(0);
112         AclServiceTestUtils.verifyMatchInfo(firstRangeFlow.getMatchInfoList(),
113                 NxMatchFieldType.nx_tcp_dst_with_mask, "80", "65535");
114         assertTrue(firstRangeFlow.getMatchInfoList().contains(new MatchTcpFlags(2)));
115         AclServiceTestUtils.verifyActionInfo(firstRangeFlow.getInstructionInfoList().get(0),
116                 new ActionNxResubmit(NwConstants.LPORT_DISPATCHER_TABLE));
117
118     }
119
120     @Test
121     public void addAcl__AllowAll() throws Exception {
122         Uuid sgUuid = new Uuid("12345678-1234-1234-1234-123456789012");
123         AclInterface ai = stubAllowAllInterface(sgUuid, "if_name");
124         assertEquals(true, testedService.applyAcl(ai));
125         assertEquals(10, installFlowValueSaver.getNumOfInvocations());
126
127         FlowEntity firstRangeFlow = (FlowEntity) installFlowValueSaver.getInvocationParams(9).get(0);
128         AclServiceTestUtils.verifyActionInfo(firstRangeFlow.getInstructionInfoList().get(0),
129                 new ActionNxResubmit(NwConstants.LPORT_DISPATCHER_TABLE));
130     }
131
132     @Test
133     public void addAcl__MultipleRanges() throws Exception {
134         Uuid sgUuid = new Uuid("12345678-1234-1234-1234-123456789012");
135         AclInterface ai = stubTcpAclInterface(sgUuid, "if_name", "1.1.1.1/32", 80, 84);
136         assertEquals(true, testedService.applyAcl(ai));
137         assertEquals(11, installFlowValueSaver.getNumOfInvocations());
138         FlowEntity firstRangeFlow = (FlowEntity) installFlowValueSaver.getInvocationParams(9).get(0);
139         AclServiceTestUtils.verifyMatchInfo(firstRangeFlow.getMatchInfoList(),
140                 NxMatchFieldType.nx_tcp_dst_with_mask, "80", "65532");
141         assertTrue(firstRangeFlow.getMatchInfoList().contains(new MatchTcpFlags(2)));
142
143         FlowEntity secondRangeFlow = (FlowEntity) installFlowValueSaver.getInvocationParams(10).get(0);
144         AclServiceTestUtils.verifyMatchInfo(secondRangeFlow.getMatchInfoList(),
145                 NxMatchFieldType.nx_tcp_dst_with_mask, "84", "65535");
146         assertTrue(secondRangeFlow.getMatchInfoList().contains(new MatchTcpFlags(2)));
147     }
148
149     @Test
150     public void addAcl__UdpSinglePortShouldNotCreateSynRule() throws Exception {
151         Uuid sgUuid = new Uuid("12345678-1234-1234-1234-123456789012");
152         AclInterface ai = stubUdpAclInterface(sgUuid, "if_name", "1.1.1.1/32", 80, 80);
153         assertEquals(true, testedService.applyAcl(ai));
154         assertEquals(9, installFlowValueSaver.getNumOfInvocations());
155     }
156
157     @Test
158     public void removeAcl__SinglePort() throws Exception {
159         Uuid sgUuid = new Uuid("12345678-1234-1234-1234-123456789012");
160         AclInterface ai = stubTcpAclInterface(sgUuid, "if_name", "1.1.1.1/32", 80, 80);
161         assertEquals(true, testedService.removeAcl(ai));
162         assertEquals(10, removeFlowValueSaver.getNumOfInvocations());
163         FlowEntity firstRangeFlow = (FlowEntity) removeFlowValueSaver.getInvocationParams(9).get(0);
164         assertTrue(firstRangeFlow.getMatchInfoList().contains(new MatchTcpFlags(2)));
165         AclServiceTestUtils.verifyMatchInfo(firstRangeFlow.getMatchInfoList(),
166                 NxMatchFieldType.nx_tcp_dst_with_mask, "80", "65535");
167
168     }
169
170     private AclInterface stubUdpAclInterface(Uuid sgUuid, String ifName, String ipv4PrefixStr,
171             int tcpPortLower, int tcpPortUpper) {
172         AclInterface ai = new AclInterface();
173         ai.setPortSecurityEnabled(true);
174         ai.setSecurityGroups(Arrays.asList(sgUuid));
175         ai.setDpId(BigInteger.ONE);
176         ai.setLPortTag(2);
177         stubInterfaceAcl(ifName, ai);
178
179         stubAccessList(sgUuid, ipv4PrefixStr, tcpPortLower, tcpPortUpper, (short)NwConstants.IP_PROT_UDP);
180         return ai;
181     }
182
183     private AclInterface stubTcpAclInterface(Uuid sgUuid, String ifName, String ipv4PrefixStr,
184             int tcpPortLower, int tcpPortUpper) {
185         AclInterface ai = new AclInterface();
186         ai.setPortSecurityEnabled(true);
187         ai.setDpId(BigInteger.ONE);
188         ai.setLPortTag(2);
189         ai.setSecurityGroups(Arrays.asList(sgUuid));
190         stubInterfaceAcl(ifName, ai);
191
192         stubAccessList(sgUuid, ipv4PrefixStr, tcpPortLower, tcpPortUpper, (short)NwConstants.IP_PROT_TCP);
193         return ai;
194     }
195
196     private AclInterface stubAllowAllInterface(Uuid sgUuid, String ifName) {
197         AclInterface ai = new AclInterface();
198         ai.setPortSecurityEnabled(true);
199         ai.setSecurityGroups(Arrays.asList(sgUuid));
200         ai.setDpId(BigInteger.ONE);
201         ai.setLPortTag(2);
202         stubInterfaceAcl(ifName, ai);
203
204         stubAccessList(sgUuid, "0.0.0.0/0", -1, -1, (short)-1);
205         return ai;
206     }
207
208     private void stubInterfaceAcl(String ifName, AclInterface ai) {
209         AllowedAddressPairsBuilder aapb = new AllowedAddressPairsBuilder();
210         aapb.setIpAddress(new IpPrefixOrAddress("1.1.1.1/32".toCharArray()));
211         aapb.setMacAddress(new MacAddress("AA:BB:CC:DD:EE:FF"));
212         ai.setAllowedAddressPairs(Arrays.asList(aapb.build()));
213     }
214
215     private void stubAccessList(Uuid sgUuid, String ipv4PrefixStr, int portLower, int portUpper, short protocol) {
216         AclBuilder ab = new AclBuilder();
217         ab.setAclName("AAA");
218         ab.setKey(new AclKey(sgUuid.getValue(),Ipv4Acl.class));
219
220         AceIpBuilder aceIpBuilder = new AceIpBuilder();
221         if (portLower != -1 && portUpper != -1) {
222             DestinationPortRangeBuilder dprb = new DestinationPortRangeBuilder();
223             dprb.setLowerPort(new PortNumber(portLower));
224             dprb.setUpperPort(new PortNumber(portUpper));
225             aceIpBuilder.setDestinationPortRange(dprb.build());
226         }
227         if (ipv4PrefixStr != null) {
228             AceIpv4Builder aceIpv4Builder = new AceIpv4Builder();
229             Ipv4Prefix ipv4Prefix = new Ipv4Prefix(ipv4PrefixStr);
230             aceIpv4Builder.setSourceIpv4Network(ipv4Prefix);
231             aceIpBuilder.setAceIpVersion(aceIpv4Builder.build());
232         }
233         if (protocol != -1) {
234             aceIpBuilder.setProtocol(protocol);
235         }
236         MatchesBuilder matches = new MatchesBuilder();
237         matches.setAceType(aceIpBuilder.build());
238         AceBuilder aceBuilder = new AceBuilder();
239         aceBuilder.setMatches(matches.build());
240         SecurityRuleAttrBuilder securityRuleAttrBuilder = new SecurityRuleAttrBuilder();
241         securityRuleAttrBuilder.setDirection(DirectionEgress.class);
242         aceBuilder.addAugmentation(SecurityRuleAttr.class, securityRuleAttrBuilder.build());
243         AccessListEntriesBuilder aleb = new AccessListEntriesBuilder();
244         aleb.setAce(Arrays.asList(aceBuilder.build()));
245         ab.setAccessListEntries(aleb.build());
246
247         InstanceIdentifier<Acl> aclKey = AclServiceUtils.getAclInstanceIdentifier(sgUuid.getValue());
248         when(mockReadTx.read(LogicalDatastoreType.CONFIGURATION, aclKey))
249             .thenReturn(Futures.immediateCheckedFuture(Optional.of(ab.build())));
250     }
251 }