Boxing cleanup
[netvirt.git] / vpnservice / aclservice / impl / src / test / java / org / opendaylight / netvirt / aclservice / StatelessIngressAclServiceImplTest.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.DirectionIngress;
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 StatelessIngressAclServiceImplTest {
65
66     StatelessIngressAclServiceImpl 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 StatelessIngressAclServiceImpl(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(7, installFlowValueSaver.getNumOfInvocations());
110
111         FlowEntity firstRangeFlow = (FlowEntity) installFlowValueSaver.getInvocationParams(6).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.EGRESS_LPORT_DISPATCHER_TABLE));
117     }
118
119     @Test
120     public void addAcl__AllowAll() throws Exception {
121         Uuid sgUuid = new Uuid("12345678-1234-1234-1234-123456789012");
122         AclInterface ai = stubAllowAllInterface(sgUuid, "if_name");
123         assertEquals(true, testedService.applyAcl(ai));
124         assertEquals(7, installFlowValueSaver.getNumOfInvocations());
125
126         FlowEntity firstRangeFlow = (FlowEntity) installFlowValueSaver.getInvocationParams(6).get(0);
127         AclServiceTestUtils.verifyActionInfo(firstRangeFlow.getInstructionInfoList().get(0),
128                 new ActionNxResubmit(NwConstants.EGRESS_LPORT_DISPATCHER_TABLE));
129     }
130
131     @Test
132     public void addAcl__MultipleRanges() throws Exception {
133         Uuid sgUuid = new Uuid("12345678-1234-1234-1234-123456789012");
134         AclInterface ai = stubTcpAclInterface(sgUuid, "if_name", "1.1.1.1/32", 80, 84);
135         assertEquals(true, testedService.applyAcl(ai));
136         assertEquals(8, installFlowValueSaver.getNumOfInvocations());
137         FlowEntity firstRangeFlow = (FlowEntity) installFlowValueSaver.getInvocationParams(6).get(0);
138         // should have been 80-83 will be fixed as part of the port range support
139         // https://bugs.opendaylight.org/show_bug.cgi?id=6200
140         AclServiceTestUtils.verifyMatchInfo(firstRangeFlow.getMatchInfoList(),
141                 NxMatchFieldType.nx_tcp_dst_with_mask, "80", "65532");
142         assertTrue(firstRangeFlow.getMatchInfoList().contains(new MatchTcpFlags(2)));
143
144         FlowEntity secondRangeFlow = (FlowEntity) installFlowValueSaver.getInvocationParams(7).get(0);
145         AclServiceTestUtils.verifyMatchInfo(secondRangeFlow.getMatchInfoList(),
146                 NxMatchFieldType.nx_tcp_dst_with_mask, "84", "65535");
147         assertTrue(secondRangeFlow.getMatchInfoList().contains(new MatchTcpFlags(2)));
148     }
149
150     @Test
151     public void addAcl__UdpSinglePortShouldDoNothing() throws Exception {
152         Uuid sgUuid = new Uuid("12345678-1234-1234-1234-123456789012");
153         AclInterface ai = stubUdpAclInterface(sgUuid, "if_name", "1.1.1.1/32", 80, 80);
154         assertEquals(true, testedService.applyAcl(ai));
155         assertEquals(6, installFlowValueSaver.getNumOfInvocations());
156     }
157
158     @Test
159     public void removeAcl__SinglePort() throws Exception {
160         Uuid sgUuid = new Uuid("12345678-1234-1234-1234-123456789012");
161         AclInterface ai = stubTcpAclInterface(sgUuid, "if_name", "1.1.1.1/32", 80, 80);
162         assertEquals(true, testedService.removeAcl(ai));
163         assertEquals(7, removeFlowValueSaver.getNumOfInvocations());
164         FlowEntity firstSynFlow = (FlowEntity) removeFlowValueSaver.getInvocationParams(6).get(0);
165         AclServiceTestUtils.verifyMatchInfo(firstSynFlow.getMatchInfoList(),
166                 NxMatchFieldType.nx_tcp_dst_with_mask, "80", "65535");
167         assertTrue(firstSynFlow.getMatchInfoList().contains(MatchTcpFlags.SYN));
168
169     }
170
171     private AclInterface stubUdpAclInterface(Uuid sgUuid, String ifName, String ipv4PrefixStr,
172             int tcpPortLower, int tcpPortUpper) {
173         AclInterface ai = new AclInterface();
174         ai.setPortSecurityEnabled(true);
175         ai.setSecurityGroups(Arrays.asList(sgUuid));
176         ai.setDpId(BigInteger.ONE);
177         ai.setLPortTag(2);
178         stubInterfaceAcl(ifName, ai);
179
180         stubAccessList(sgUuid, ipv4PrefixStr, tcpPortLower, tcpPortUpper, (short)NwConstants.IP_PROT_UDP);
181         return ai;
182     }
183
184     private AclInterface stubTcpAclInterface(Uuid sgUuid, String ifName, String ipv4PrefixStr,
185             int tcpPortLower, int tcpPortUpper) {
186         AclInterface ai = new AclInterface();
187         ai.setPortSecurityEnabled(true);
188         ai.setDpId(BigInteger.ONE);
189         ai.setLPortTag(2);
190         ai.setSecurityGroups(Arrays.asList(sgUuid));
191         stubInterfaceAcl(ifName, ai);
192
193         stubAccessList(sgUuid, ipv4PrefixStr, tcpPortLower, tcpPortUpper, (short)NwConstants.IP_PROT_TCP);
194         return ai;
195     }
196
197     private AclInterface stubAllowAllInterface(Uuid sgUuid, String ifName) {
198         AclInterface ai = new AclInterface();
199         ai.setPortSecurityEnabled(true);
200         ai.setSecurityGroups(Arrays.asList(sgUuid));
201         ai.setDpId(BigInteger.ONE);
202         ai.setLPortTag(2);
203         stubInterfaceAcl(ifName, ai);
204
205         stubAccessList(sgUuid, "0.0.0.0/0", -1, -1, (short)-1);
206         return ai;
207     }
208
209     private void stubInterfaceAcl(String ifName, AclInterface ai) {
210         AllowedAddressPairsBuilder aapb = new AllowedAddressPairsBuilder();
211         aapb.setIpAddress(new IpPrefixOrAddress("1.1.1.1/32".toCharArray()));
212         aapb.setMacAddress(new MacAddress("AA:BB:CC:DD:EE:FF"));
213         ai.setAllowedAddressPairs(Arrays.asList(aapb.build()));
214     }
215
216     private void stubAccessList(Uuid sgUuid, String ipv4PrefixStr, int portLower, int portUpper, short protocol) {
217         AclBuilder ab = new AclBuilder();
218         ab.setAclName("AAA");
219         ab.setKey(new AclKey(sgUuid.getValue(),Ipv4Acl.class));
220
221         AceIpBuilder aceIpBuilder = new AceIpBuilder();
222         if (portLower != -1 && portUpper != -1) {
223             DestinationPortRangeBuilder dprb = new DestinationPortRangeBuilder();
224             dprb.setLowerPort(new PortNumber(portLower));
225             dprb.setUpperPort(new PortNumber(portUpper));
226             aceIpBuilder.setDestinationPortRange(dprb.build());
227         }
228         AceIpv4Builder aceIpv4Builder = new AceIpv4Builder();
229         if (ipv4PrefixStr != null) {
230             Ipv4Prefix ipv4Prefix = new Ipv4Prefix(ipv4PrefixStr);
231             aceIpv4Builder.setDestinationIpv4Network(ipv4Prefix);
232             aceIpBuilder.setAceIpVersion(aceIpv4Builder.build());
233         }
234         if (protocol != -1) {
235             aceIpBuilder.setProtocol(protocol);
236         }
237         MatchesBuilder matches = new MatchesBuilder();
238         matches.setAceType(aceIpBuilder.build());
239         AceBuilder aceBuilder = new AceBuilder();
240         aceBuilder.setMatches(matches.build());
241         SecurityRuleAttrBuilder securityRuleAttrBuilder = new SecurityRuleAttrBuilder();
242         securityRuleAttrBuilder.setDirection(DirectionIngress.class);
243         aceBuilder.addAugmentation(SecurityRuleAttr.class, securityRuleAttrBuilder.build());
244         AccessListEntriesBuilder aleb = new AccessListEntriesBuilder();
245         aleb.setAce(Arrays.asList(aceBuilder.build()));
246         ab.setAccessListEntries(aleb.build());
247
248         InstanceIdentifier<Acl> aclKey = AclServiceUtils.getAclInstanceIdentifier(sgUuid.getValue());
249         when(mockReadTx.read(LogicalDatastoreType.CONFIGURATION, aclKey))
250             .thenReturn(Futures.immediateCheckedFuture(Optional.of(ab.build())));
251     }
252 }