Remove ovsdb related in resources
[netvirt.git] / openstack / net-virt-providers / src / test / java / org / opendaylight / netvirt / openstack / netvirt / providers / openflow13 / services / IngressAclServiceTest.java
1 /*
2  * Copyright (c) 2015 - 2016 Inocybe 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
9 package org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.services;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.concurrent.atomic.AtomicBoolean;
21
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.invocation.InvocationOnMock;
29 import org.mockito.stubbing.Answer;
30 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
31 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
34 import org.opendaylight.netvirt.openstack.netvirt.api.SecurityServicesManager;
35 import org.opendaylight.netvirt.openstack.netvirt.providers.NetvirtProvidersProvider;
36 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.PipelineOrchestrator;
37 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.Service;
38 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSecurityGroup;
39 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSecurityRule;
40 import org.opendaylight.netvirt.openstack.netvirt.translator.Neutron_IPs;
41 import org.opendaylight.netvirt.openstack.netvirt.api.SecurityGroupCacheManger;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4Match;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv6Match;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.powermock.api.mockito.PowerMockito;
53 import org.powermock.api.support.membermodification.MemberModifier;
54 import org.powermock.modules.junit4.PowerMockRunner;
55
56 import com.google.common.util.concurrent.CheckedFuture;
57
58 /**
59  * Unit test fort {@link IngressAclService}
60  */
61 @RunWith(PowerMockRunner.class)
62 @SuppressWarnings("unchecked")
63 public class IngressAclServiceTest {
64
65     @InjectMocks private IngressAclService ingressAclService = new IngressAclService();
66     private IngressAclService ingressAclServiceSpy;
67
68     @Mock private DataBroker dataBroker;
69     @Mock private PipelineOrchestrator orchestrator;
70
71     @Mock private WriteTransaction writeTransaction;
72     @Mock private CheckedFuture<Void, TransactionCommitFailedException> commitFuture;
73
74     @Mock private NeutronSecurityGroup securityGroup;
75     @Mock private NeutronSecurityRule portSecurityRule;
76     @Mock private NeutronSecurityGroup securityGroupIpv6;
77     @Mock private NeutronSecurityRule portSecurityIpv6Rule;
78     @Mock private SecurityServicesManager securityServices;
79     @Mock private SecurityGroupCacheManger securityGroupCacheManger;
80
81     private List<Neutron_IPs> neutronSrcIpList = new ArrayList<>();
82     private List<Neutron_IPs> neutronDestIpList = new ArrayList<>();
83     private Neutron_IPs neutron_ip_src;
84     private Neutron_IPs neutron_ip_dest_1;
85     private Neutron_IPs neutron_ip_dest_2;
86     private Neutron_IPs neutron_ipv6_dest_1;
87     private Neutron_IPs neutron_ipv6_dest_2;
88
89     private static final String MAC_ADDRESS = "87:1D:5E:02:40:B8";
90     private static final String DHCP_MAC_ADDRESS = "87:1D:5E:02:40:B9";
91     private static final String SRC_IP = "192.168.0.1";
92     private static final String DEST_IP_1 = "192.169.0.1";
93     private static final String DEST_IP_2 = "192.169.0.2";
94     private static final String IPV6_DEST_IP_1 = "2001:db8:2::200";
95     private static final String IPV6_DEST_IP_2 = "2001:db8:2::201";
96     private static final Long IPV6_ETHER_TYPE = (long) 0x86DD;
97     private static final Long IPV4_ETHER_TYPE = (long) 0x0800;
98     private static final String SECURITY_GROUP_UUID = "85cc3048-abc3-43cc-89b3-377341426ac5";
99     private static final String PORT_UUID = "95cc3048-abc3-43cc-89b3-377341426ac5";
100     private static final String SEGMENT_ID = "2";
101     private static final Long DP_ID_LONG = (long) 1554;
102     private static final Long LOCAL_PORT = (long) 124;
103     private static final int PORT_RANGE_MIN = 1;
104     private static final int PORT_RANGE_MAX = 65535;
105     private static FlowBuilder flowBuilder;
106     private static NodeBuilder nodeBuilder;
107
108     private static Answer<Object> answer() {
109         return new Answer<Object>() {
110             @Override
111             public CheckedFuture<Void, TransactionCommitFailedException> answer(InvocationOnMock invocation)
112                     throws Throwable {
113                 flowBuilder = (FlowBuilder) invocation.getArguments()[0];
114                 nodeBuilder = (NodeBuilder) invocation.getArguments()[1];
115                 return null;
116             }
117         };
118     }
119
120     @Before
121     public void setUp() throws IllegalArgumentException, IllegalAccessException{
122         ingressAclServiceSpy = PowerMockito.spy(ingressAclService);
123
124         when(writeTransaction.submit()).thenReturn(commitFuture);
125
126         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
127
128         when(orchestrator.getNextServiceInPipeline(any(Service.class))).thenReturn(Service.ARP_RESPONDER);
129
130         portSecurityRule = mock(NeutronSecurityRule.class);
131         when(portSecurityRule.getSecurityRuleEthertype()).thenReturn(NeutronSecurityRule.ETHERTYPE_IPV4);
132         when(portSecurityRule.getSecurityRuleDirection()).thenReturn(NeutronSecurityRule.DIRECTION_INGRESS);
133
134         List<NeutronSecurityRule> portSecurityList = new ArrayList<>();
135         portSecurityList.add(portSecurityRule);
136
137         neutron_ip_src = new Neutron_IPs();
138         neutron_ip_src.setIpAddress(SRC_IP);
139         neutronSrcIpList.add(neutron_ip_src);
140
141         neutron_ip_dest_1 = new Neutron_IPs();
142         neutron_ip_dest_1.setIpAddress(DEST_IP_1);
143         neutronDestIpList.add(neutron_ip_dest_1);
144
145         neutron_ip_dest_2 = new Neutron_IPs();
146         neutron_ip_dest_2.setIpAddress(DEST_IP_2);
147         neutronDestIpList.add(neutron_ip_dest_2);
148
149         portSecurityIpv6Rule = mock(NeutronSecurityRule.class);
150         when(portSecurityIpv6Rule.getSecurityRuleEthertype()).thenReturn(NeutronSecurityRule.ETHERTYPE_IPV6);
151         when(portSecurityIpv6Rule.getSecurityRuleDirection()).thenReturn(NeutronSecurityRule.DIRECTION_INGRESS);
152
153         List<NeutronSecurityRule> portSecurityIpv6List = new ArrayList<>();
154         portSecurityIpv6List.add(portSecurityIpv6Rule);
155         when(securityGroupIpv6.getSecurityRules()).thenReturn(portSecurityIpv6List);
156
157         neutron_ipv6_dest_1 = new Neutron_IPs();
158         neutron_ipv6_dest_1.setIpAddress(IPV6_DEST_IP_1);
159         neutronDestIpList.add(neutron_ipv6_dest_1);
160
161         neutron_ipv6_dest_2 = new Neutron_IPs();
162         neutron_ipv6_dest_2.setIpAddress(IPV6_DEST_IP_2);
163         neutronDestIpList.add(neutron_ipv6_dest_2);
164
165         when(securityGroup.getSecurityRules()).thenReturn(portSecurityList);
166         when(securityServices.getVmListForSecurityGroup
167              (PORT_UUID, SECURITY_GROUP_UUID)).thenReturn(neutronDestIpList);
168         NetvirtProvidersProvider netvirtProvider = mock(NetvirtProvidersProvider.class);
169         MemberModifier.field(NetvirtProvidersProvider.class, "hasProviderEntityOwnership").set(netvirtProvider, new AtomicBoolean(true));
170
171     }
172     /**
173      * Test method {@link EgressAclService#programPortSecurityGroup(java.lang.Long, java.lang.String,
174      * java.lang.String, long, NeutronSecurityGroup,
175      * java.lang.String, boolean)} when portSecurityRule is incomplete
176      */
177     @Test
178     public void testProgramPortSecurityGroupWithIncompleteRule() throws Exception {
179         NeutronSecurityRule portSecurityRule1 = mock(NeutronSecurityRule.class);
180         when(portSecurityRule1.getSecurityRuleEthertype()).thenReturn(NeutronSecurityRule.ETHERTYPE_IPV4);
181         when(portSecurityRule1.getSecurityRuleDirection()).thenReturn("not_ingress");  // other direction
182
183         NeutronSecurityRule portSecurityRule2 = mock(NeutronSecurityRule.class);
184         when(portSecurityRule2.getSecurityRuleEthertype()).thenReturn(null);
185         when(portSecurityRule2.getSecurityRuleDirection()).thenReturn(NeutronSecurityRule.DIRECTION_INGRESS);
186
187         NeutronSecurityRule portSecurityRule3 = mock(NeutronSecurityRule.class);
188         when(portSecurityRule3.getSecurityRuleEthertype()).thenReturn(NeutronSecurityRule.ETHERTYPE_IPV4);
189         when(portSecurityRule3.getSecurityRuleDirection()).thenReturn(null);
190
191         NeutronSecurityRule portSecurityRule4 = mock(NeutronSecurityRule.class);
192         when(portSecurityRule4.getSecurityRuleEthertype()).thenReturn(null);
193         when(portSecurityRule4.getSecurityRuleDirection()).thenReturn(null);
194
195         List<NeutronSecurityRule> portSecurityList = new ArrayList<>();
196         portSecurityList.add(null);
197         portSecurityList.add(portSecurityRule1);
198         portSecurityList.add(portSecurityRule2);
199         portSecurityList.add(portSecurityRule3);
200         portSecurityList.add(portSecurityRule4);
201
202         NeutronSecurityGroup localSecurityGroup = mock(NeutronSecurityGroup.class);
203         when(localSecurityGroup.getSecurityRules()).thenReturn(portSecurityList);
204
205         ingressAclServiceSpy.programPortSecurityGroup(
206                 Long.valueOf(1554), "2", MAC_ADDRESS, 124, localSecurityGroup, PORT_UUID, false);
207     }
208
209     /**
210      *  Test IPv4 add test case.
211      */
212     @Test
213     public void testProgramPortSecurityACLRuleAddIpv4() throws Exception {
214         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(null);
215         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(null);
216         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(null);
217         when(portSecurityRule.getSecurityRuleRemoteIpPrefix()).thenReturn(null);
218
219         ingressAclServiceSpy.programPortSecurityGroup(Long.valueOf(1554), "2", MAC_ADDRESS, 124, securityGroup,PORT_UUID,true);
220
221         verify(writeTransaction, times(1)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), eq(true));
222         verify(writeTransaction, times(1)).submit();
223         verify(commitFuture, times(1)).checkedGet();
224     }
225
226     /**
227      *  Test IPv6 add test case.
228      */
229     @Test
230     public void testProgramPortSecurityACLRuleAddIpv6() throws Exception {
231         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(null);
232         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(null);
233         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(null);
234         when(portSecurityIpv6Rule.getSecurityRuleRemoteIpPrefix()).thenReturn(null);
235
236         ingressAclServiceSpy.programPortSecurityGroup(Long.valueOf(1554), "2", MAC_ADDRESS, 124, securityGroupIpv6, PORT_UUID, true);
237
238         verify(writeTransaction, times(1)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), eq(true));
239         verify(writeTransaction, times(1)).submit();
240         verify(commitFuture, times(1)).checkedGet();
241     }
242
243     /**
244      *  Test IPv4 remove test case.
245      */
246     @Test
247     public void testProgramPortSecurityACLRuleRemoveIpv4() throws Exception {
248         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(null);
249         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(null);
250         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(null);
251         when(portSecurityRule.getSecurityRuleRemoteIpPrefix()).thenReturn(null);
252
253         ingressAclServiceSpy.programPortSecurityGroup(Long.valueOf(1554), "2", MAC_ADDRESS, 124, securityGroup,PORT_UUID,false);
254
255         verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
256         verify(writeTransaction, times(1)).submit();
257         verify(commitFuture, times(1)).get();
258     }
259
260     /**
261      *  Test IPv6 remove test case.
262      */
263     @Test
264     public void testProgramPortSecurityACLRuleRemoveIpv6() throws Exception {
265         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(null);
266         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(null);
267         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(null);
268         when(portSecurityIpv6Rule.getSecurityRuleRemoteIpPrefix()).thenReturn(null);
269
270         ingressAclServiceSpy.programPortSecurityGroup(Long.valueOf(1554), "2", MAC_ADDRESS, 124, securityGroupIpv6, PORT_UUID, false);
271
272         verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
273         verify(writeTransaction, times(1)).submit();
274         verify(commitFuture, times(1)).get();
275     }
276
277     /**
278      *  Test IPv4 TCP add with port no and CIDR selected.
279      */
280     @Test
281     public void testProgramPortSecurityACLRuleAddTcp1() throws Exception {
282         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_TCP);
283         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(20);
284         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(20);
285         when(portSecurityRule.getSecurityRuleRemoteIpPrefix()).thenReturn("0.0.0.0/24");
286         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
287                                              any(NodeBuilder.class));
288
289         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroup,
290                                                       PORT_UUID, true);
291
292         Match match = flowBuilder.getMatch();
293         EthernetMatch ethMatch = match.getEthernetMatch();
294         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
295         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
296
297         Assert.assertTrue(match.getLayer4Match() instanceof TcpMatch);
298         TcpMatch layer4Match = (TcpMatch) match.getLayer4Match();
299         Assert.assertEquals(20, layer4Match.getTcpDestinationPort().getValue().intValue());
300         int port = portSecurityRule.getSecurityRulePortMin();
301         Assert.assertEquals("Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS +
302                             "_" + port + "_0.0.0.0/24_Permit", flowBuilder.getFlowName());
303     }
304
305     /**
306      *  Test IPv6 TCP add with port no and CIDR selected.
307      */
308     @Test
309     public void testProgramPortSecurityACLRuleIpv6AddTcp1() throws Exception {
310         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_TCP);
311         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(20);
312         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(20);
313         when(portSecurityIpv6Rule.getSecurityRuleRemoteIpPrefix()).thenReturn("::/64");
314         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
315                 any(NodeBuilder.class));
316
317         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6,
318                 PORT_UUID, true);
319
320         Match match = flowBuilder.getMatch();
321         EthernetMatch ethMatch = match.getEthernetMatch();
322         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
323         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
324
325         Assert.assertTrue(match.getLayer4Match() instanceof TcpMatch);
326         TcpMatch layer4Match = (TcpMatch) match.getLayer4Match();
327         Assert.assertEquals(20, layer4Match.getTcpDestinationPort().getValue().intValue());
328         int port = portSecurityIpv6Rule.getSecurityRulePortMin();
329         Assert.assertEquals("Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS +
330                 "_" + port + "_::/64_Permit", flowBuilder.getFlowName());
331     }
332
333     /**
334      *  Test IPv4 TCP remove with port no and CIDR selected.
335      */
336     @Test
337     public void testProgramPortSecurityACLRuleRemoveTcp1() throws Exception {
338         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_TCP);
339         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(15);
340         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(15);
341         when(portSecurityRule.getSecurityRuleRemoteIpPrefix()).thenReturn("0.0.0.0/24");
342         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class),
343                                              any(NodeBuilder.class));
344
345         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroup,
346                                                       PORT_UUID, false);
347
348         Match match = flowBuilder.getMatch();
349         EthernetMatch ethMatch = match.getEthernetMatch();
350         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
351         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
352
353         Assert.assertTrue(match.getLayer4Match() instanceof TcpMatch);
354         TcpMatch layer4Match = (TcpMatch) match.getLayer4Match();
355         Assert.assertEquals(15, layer4Match.getTcpDestinationPort().getValue().intValue());
356         int port = portSecurityRule.getSecurityRulePortMin();
357         Assert.assertEquals("Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS +
358                             "_" + port + "_0.0.0.0/24_Permit", flowBuilder.getFlowName());
359     }
360
361     /**
362      *  Test IPv6 TCP remove with port no and CIDR selected.
363      */
364     @Test
365     public void testProgramPortSecurityACLRuleIpv6RemoveTcp1() throws Exception {
366         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_TCP);
367         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(15);
368         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(15);
369         when(portSecurityIpv6Rule.getSecurityRuleRemoteIpPrefix()).thenReturn("::/64");
370         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class),
371                 any(NodeBuilder.class));
372
373         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6,
374                 PORT_UUID, false);
375
376         Match match = flowBuilder.getMatch();
377         EthernetMatch ethMatch = match.getEthernetMatch();
378         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
379         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
380
381         Assert.assertTrue(match.getLayer4Match() instanceof TcpMatch);
382         TcpMatch layer4Match = (TcpMatch) match.getLayer4Match();
383         Assert.assertEquals(15, layer4Match.getTcpDestinationPort().getValue().intValue());
384         int port = portSecurityIpv6Rule.getSecurityRulePortMin();
385         Assert.assertEquals("Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS +
386                 "_" + port + "_::/64_Permit", flowBuilder.getFlowName());
387     }
388
389     /**
390      *  Test IPv4 TCP add with port no and remote SG selected.
391      */
392     @Test
393     public void testProgramPortSecurityACLRuleAddTcp2() throws Exception {
394         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_TCP);
395         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(50);
396         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(50);
397         when(portSecurityRule.getSecurityRuleRemoteIpPrefix()).thenReturn("0.0.0.0/24");
398         when(portSecurityRule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
399         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
400                                              any(NodeBuilder.class));
401         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroup,
402                                                       PORT_UUID, true);
403
404         Match match = flowBuilder.getMatch();
405         EthernetMatch ethMatch = match.getEthernetMatch();
406         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
407         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
408
409         Assert.assertTrue(match.getLayer4Match() instanceof TcpMatch);
410         TcpMatch layer4Match = (TcpMatch) match.getLayer4Match();
411         Assert.assertEquals(50, layer4Match.getTcpDestinationPort().getValue().intValue());
412         int port = portSecurityRule.getSecurityRulePortMin();
413         String expectedFlowId1 = "Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + port + "_" + DEST_IP_1 +
414                 "_Permit";
415         String expectedFlowId2 = "Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + port + "_" + DEST_IP_2 +
416                 "_Permit";
417         String actualFlowId = flowBuilder.getFlowName();
418         if (actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
419             Assert.assertTrue(true);
420         } else {
421             Assert.assertTrue(false);
422         }
423     }
424
425     /**
426      *  Test IPv6 TCP add with port no and remote SG selected.
427      */
428     @Test
429     public void testProgramPortSecurityACLRuleIpv6AddTcp2() throws Exception {
430         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_TCP);
431         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(50);
432         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(50);
433         when(portSecurityIpv6Rule.getSecurityRuleRemoteIpPrefix()).thenReturn("::/64");
434         when(portSecurityIpv6Rule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
435         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
436                 any(NodeBuilder.class));
437         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6,
438                 PORT_UUID, true);
439
440         Match match = flowBuilder.getMatch();
441         EthernetMatch ethMatch = match.getEthernetMatch();
442         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
443         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
444
445         Assert.assertTrue(match.getLayer4Match() instanceof TcpMatch);
446         TcpMatch layer4Match = (TcpMatch) match.getLayer4Match();
447         Assert.assertEquals(50, layer4Match.getTcpDestinationPort().getValue().intValue());
448         int port = portSecurityIpv6Rule.getSecurityRulePortMin();
449         String expectedFlowId1 = "Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + port + "_" + IPV6_DEST_IP_1 +
450                 "_Permit";
451         String expectedFlowId2 = "Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + port + "_" + IPV6_DEST_IP_2 +
452                 "_Permit";
453         String actualFlowId = flowBuilder.getFlowName();
454         if (actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
455             Assert.assertTrue(true);
456         } else {
457             Assert.assertTrue(false);
458         }
459     }
460
461     /**
462      *  Test IPv4 TCP remove with port no and remote SG selected.
463      */
464     @Test
465     public void testProgramPortSecurityACLRuleRemoveTcp2() throws Exception {
466         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_TCP);
467         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(50);
468         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(50);
469         when(portSecurityRule.getSecurityRuleRemoteIpPrefix()).thenReturn("0.0.0.0/24");
470         when(portSecurityRule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
471         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class),
472                                              any(NodeBuilder.class));
473
474         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroup,
475                                                       PORT_UUID, false);
476
477         Match match = flowBuilder.getMatch();
478         EthernetMatch ethMatch = match.getEthernetMatch();
479         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
480         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
481
482         Assert.assertTrue(match.getLayer4Match() instanceof TcpMatch);
483         TcpMatch layer4Match = (TcpMatch) match.getLayer4Match();
484         Assert.assertEquals(50, layer4Match.getTcpDestinationPort().getValue().intValue());
485         int port = portSecurityRule.getSecurityRulePortMin();
486         String expectedFlowId1 = "Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + port + "_" + DEST_IP_1 +
487                 "_Permit";
488         String expectedFlowId2 = "Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + port + "_" + DEST_IP_2 +
489                 "_Permit";
490         String actualFlowId = flowBuilder.getFlowName();
491         if (actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
492             Assert.assertTrue(true);
493         } else {
494             Assert.assertTrue(false);
495         }
496     }
497
498
499     /**
500      *  Test IPv6 TCP remove with port no and remote SG selected.
501      */
502     @Test
503     public void testProgramPortSecurityACLRuleIpv6RemoveTcp2() throws Exception {
504         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_TCP);
505         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(50);
506         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(50);
507         when(portSecurityIpv6Rule.getSecurityRuleRemoteIpPrefix()).thenReturn("::/64");
508         when(portSecurityIpv6Rule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
509         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class),
510                 any(NodeBuilder.class));
511
512         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6,
513                 PORT_UUID, false);
514
515         Match match = flowBuilder.getMatch();
516         EthernetMatch ethMatch = match.getEthernetMatch();
517         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
518         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
519
520         Assert.assertTrue(match.getLayer4Match() instanceof TcpMatch);
521         TcpMatch layer4Match = (TcpMatch) match.getLayer4Match();
522         Assert.assertEquals(50, layer4Match.getTcpDestinationPort().getValue().intValue());
523         int port = portSecurityIpv6Rule.getSecurityRulePortMin();
524         String expectedFlowId1 = "Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + port + "_" + IPV6_DEST_IP_1 +
525                 "_Permit";
526         String expectedFlowId2 = "Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + port + "_" + IPV6_DEST_IP_2 +
527                 "_Permit";
528         String actualFlowId = flowBuilder.getFlowName();
529         if (actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
530             Assert.assertTrue(true);
531         } else {
532             Assert.assertTrue(false);
533         }
534     }
535
536     /**
537      *  Test IPv4 TCP add with port (All TCP) and CIDR selected.
538      */
539     @Test
540     public void testProgramPortSecurityACLRuleAddTcpAll1() throws Exception {
541         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_TCP);
542         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(PORT_RANGE_MAX);
543         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(PORT_RANGE_MIN);
544         when(portSecurityRule.getSecurityRuleRemoteIpPrefix()).thenReturn("0.0.0.0/24");
545         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
546                                              any(NodeBuilder.class));
547
548         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroup,
549                                                       PORT_UUID, true);
550
551         Match match = flowBuilder.getMatch();
552         EthernetMatch ethMatch = match.getEthernetMatch();
553         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
554         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
555
556         Assert.assertTrue(match.getLayer4Match() instanceof TcpMatch);
557         Assert.assertEquals("Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS + "_" + PORT_RANGE_MIN + "_" +
558                             PORT_RANGE_MAX + "_0.0.0.0/24_Permit", flowBuilder.getFlowName());
559     }
560
561     /**
562      *  Test IPv6 TCP add with port (All TCP) and CIDR selected.
563      */
564     @Test
565     public void testProgramPortSecurityACLRuleIpv6AddTcpAll1() throws Exception {
566         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_TCP);
567         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(PORT_RANGE_MAX);
568         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(PORT_RANGE_MIN);
569         when(portSecurityIpv6Rule.getSecurityRuleRemoteIpPrefix()).thenReturn("::/64");
570         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
571                 any(NodeBuilder.class));
572
573         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6,
574                 PORT_UUID, true);
575
576         Match match = flowBuilder.getMatch();
577         EthernetMatch ethMatch = match.getEthernetMatch();
578         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
579         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
580
581         Assert.assertTrue(match.getLayer4Match() instanceof TcpMatch);
582         Assert.assertEquals("Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS + "_" + PORT_RANGE_MIN + "_" +
583                 PORT_RANGE_MAX + "_::/64_Permit", flowBuilder.getFlowName());
584     }
585
586     /**
587      *  Test IPv4 TCP remove with port (All TCP) and CIDR selected.
588      */
589     @Test
590     public void testProgramPortSecurityACLRuleRemoveTcpAll1() throws Exception {
591         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_TCP);
592         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(PORT_RANGE_MAX);
593         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(PORT_RANGE_MIN);
594         when(portSecurityRule.getSecurityRuleRemoteIpPrefix()).thenReturn("0.0.0.0/24");
595         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class),
596                                              any(NodeBuilder.class));
597
598         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroup,
599                                                       PORT_UUID, false);
600
601         Match match = flowBuilder.getMatch();
602         EthernetMatch ethMatch = match.getEthernetMatch();
603         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
604         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
605
606         Assert.assertTrue(match.getLayer4Match() instanceof TcpMatch);
607         Assert.assertEquals("Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS + "_" + PORT_RANGE_MIN + "_" +
608                             PORT_RANGE_MAX + "_0.0.0.0/24_Permit", flowBuilder.getFlowName());
609     }
610
611     /**
612      *  Test IPv6 TCP remove with port (All TCP) and CIDR selected.
613      */
614     @Test
615     public void testProgramPortSecurityACLRuleIpv6RemoveTcpAll1() throws Exception {
616         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_TCP);
617         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(PORT_RANGE_MAX);
618         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(PORT_RANGE_MIN);
619         when(portSecurityIpv6Rule.getSecurityRuleRemoteIpPrefix()).thenReturn("::/64");
620         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class),
621                 any(NodeBuilder.class));
622
623         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6,
624                 PORT_UUID, false);
625
626         Match match = flowBuilder.getMatch();
627         EthernetMatch ethMatch = match.getEthernetMatch();
628         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
629         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
630
631         Assert.assertTrue(match.getLayer4Match() instanceof TcpMatch);
632         Assert.assertEquals("Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS + "_" + PORT_RANGE_MIN + "_" +
633                 PORT_RANGE_MAX + "_::/64_Permit", flowBuilder.getFlowName());
634     }
635
636     /**
637      *  Test IPv4 TCP add with port (All TCP) and remote SG selected.
638      */
639     @Test
640     public void testProgramPortSecurityACLRuleAddTcpAll2() throws Exception {
641         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_TCP);
642         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(PORT_RANGE_MAX);
643         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(PORT_RANGE_MIN);
644         when(portSecurityRule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
645         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
646                                              any(NodeBuilder.class));
647         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroup,
648                                                       PORT_UUID, true);
649
650         Match match = flowBuilder.getMatch();
651         EthernetMatch ethMatch = match.getEthernetMatch();
652         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
653         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
654
655         Assert.assertTrue(match.getLayer4Match() instanceof TcpMatch);
656         String expectedFlowId1 = "Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS + "_" + PORT_RANGE_MIN + "_" +
657                             PORT_RANGE_MAX + "_" + DEST_IP_1 + "_Permit";
658         String expectedFlowId2 = "Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS + "_" + PORT_RANGE_MIN + "_" +
659                 PORT_RANGE_MAX + "_" + DEST_IP_2 + "_Permit";
660         String actualFlowId = flowBuilder.getFlowName();
661         if (actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
662             Assert.assertTrue(true);
663         } else {
664             Assert.assertTrue(false);
665         }
666     }
667
668     /**
669      *  Test IPv6 TCP add with port (All TCP) and remote SG selected.
670      */
671     @Test
672     public void testProgramPortSecurityACLRuleIpv6AddTcpAll2() throws Exception {
673         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_TCP);
674         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(PORT_RANGE_MAX);
675         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(PORT_RANGE_MIN);
676         when(portSecurityIpv6Rule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
677         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
678                 any(NodeBuilder.class));
679         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6,
680                 PORT_UUID, true);
681
682         Match match = flowBuilder.getMatch();
683         EthernetMatch ethMatch = match.getEthernetMatch();
684         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
685         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
686
687         Assert.assertTrue(match.getLayer4Match() instanceof TcpMatch);
688         String expectedFlowId1 = "Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS + "_" + PORT_RANGE_MIN + "_" +
689                 PORT_RANGE_MAX + "_" + IPV6_DEST_IP_1 + "_Permit";
690         String expectedFlowId2 = "Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS + "_" + PORT_RANGE_MIN + "_" +
691                 PORT_RANGE_MAX + "_" + IPV6_DEST_IP_2 + "_Permit";
692         String actualFlowId = flowBuilder.getFlowName();
693         if (actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
694             Assert.assertTrue(true);
695         } else {
696             Assert.assertTrue(false);
697         }
698     }
699
700     /**
701      *  Test IPv4 TCP remove with port (All TCP) and remote SG selected.
702      */
703     @Test
704     public void testProgramPortSecurityACLRuleRemoveTcpAll2() throws Exception {
705         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_TCP);
706         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(PORT_RANGE_MAX);
707         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(PORT_RANGE_MIN);
708         when(portSecurityRule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
709         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class),
710                                              any(NodeBuilder.class));
711
712         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroup,
713                                                       PORT_UUID, false);
714
715         Match match = flowBuilder.getMatch();
716         EthernetMatch ethMatch = match.getEthernetMatch();
717         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
718         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
719
720         Assert.assertTrue(match.getLayer4Match() instanceof TcpMatch);
721         TcpMatch layer4Match = (TcpMatch) match.getLayer4Match();
722         String expectedFlowId1 = "Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + PORT_RANGE_MIN + "_" +
723                                 PORT_RANGE_MAX + "_" + DEST_IP_1 + "_Permit";
724         String expectedFlowId2 = "Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + PORT_RANGE_MIN + "_" +
725                                 PORT_RANGE_MAX + "_" + DEST_IP_2 + "_Permit";
726         String actualFlowId = flowBuilder.getFlowName();
727         if (actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
728             Assert.assertTrue(true);
729         } else {
730             Assert.assertTrue(false);
731         }
732     }
733
734     /**
735      *  Test IPv6 TCP remove with port (All TCP) and remote SG selected.
736      */
737     @Test
738     public void testProgramPortSecurityACLRuleIpv6RemoveTcpAll2() throws Exception {
739         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_TCP);
740         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(PORT_RANGE_MAX);
741         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(PORT_RANGE_MIN);
742         when(portSecurityIpv6Rule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
743         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class),
744                 any(NodeBuilder.class));
745
746         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6,
747                 PORT_UUID, false);
748
749         Match match = flowBuilder.getMatch();
750         EthernetMatch ethMatch = match.getEthernetMatch();
751         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
752         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
753
754         Assert.assertTrue(match.getLayer4Match() instanceof TcpMatch);
755         TcpMatch layer4Match = (TcpMatch) match.getLayer4Match();
756         String expectedFlowId1 = "Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + PORT_RANGE_MIN + "_" +
757                 PORT_RANGE_MAX + "_" + IPV6_DEST_IP_1 + "_Permit";
758         String expectedFlowId2 = "Ingress_TCP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + PORT_RANGE_MIN + "_" +
759                 PORT_RANGE_MAX + "_" + IPV6_DEST_IP_2 + "_Permit";
760         String actualFlowId = flowBuilder.getFlowName();
761         if (actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
762             Assert.assertTrue(true);
763         } else {
764             Assert.assertTrue(false);
765         }
766     }
767
768     /**
769      *  Test IPv4 UDP add with port no and CIDR selected.
770      */
771     @Test
772     public void testProgramPortSecurityACLRuleAddUdp1() throws Exception {
773         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_UDP);
774         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(50);
775         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(50);
776         when(portSecurityRule.getSecurityRuleRemoteIpPrefix()).thenReturn("0.0.0.0/24");
777         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
778                                              any(NodeBuilder.class));
779
780         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroup,
781                                                       PORT_UUID, true);
782
783         Match match = flowBuilder.getMatch();
784         EthernetMatch ethMatch = match.getEthernetMatch();
785         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
786         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
787
788         Assert.assertTrue(match.getLayer4Match() instanceof UdpMatch);
789         UdpMatch layer4Match = (UdpMatch) match.getLayer4Match();
790         Assert.assertEquals(50, layer4Match.getUdpDestinationPort().getValue().intValue());
791         int port = portSecurityRule.getSecurityRulePortMin();
792         Assert.assertEquals("Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +
793                             "_" + port + "_0.0.0.0/24_Permit", flowBuilder.getFlowName());
794     }
795
796     /**
797      *  Test IPv6 UDP add with port no and CIDR selected.
798      */
799     @Test
800     public void testProgramPortSecurityACLRuleIpv6AddUdp1() throws Exception {
801         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_UDP);
802         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(50);
803         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(50);
804         when(portSecurityIpv6Rule.getSecurityRuleRemoteIpPrefix()).thenReturn("::/64");
805         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
806                 any(NodeBuilder.class));
807
808         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6,
809                 PORT_UUID, true);
810
811         Match match = flowBuilder.getMatch();
812         EthernetMatch ethMatch = match.getEthernetMatch();
813         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
814         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
815
816         Assert.assertTrue(match.getLayer4Match() instanceof UdpMatch);
817         UdpMatch layer4Match = (UdpMatch) match.getLayer4Match();
818         Assert.assertEquals(50, layer4Match.getUdpDestinationPort().getValue().intValue());
819         int port = portSecurityIpv6Rule.getSecurityRulePortMin();
820         Assert.assertEquals("Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +
821                 "_" + port + "_::/64_Permit", flowBuilder.getFlowName());
822     }
823
824     /**
825      *  Test IPv4 UDP remove with port no and CIDR selected.
826      */
827     @Test
828     public void testProgramPortSecurityACLRuleRemoveUdp1() throws Exception {
829         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_UDP);
830         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(50);
831         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(50);
832         when(portSecurityRule.getSecurityRuleRemoteIpPrefix()).thenReturn("0.0.0.0/24");
833         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class),
834                                              any(NodeBuilder.class));
835
836         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroup,
837                                                       PORT_UUID, false);
838
839         Match match = flowBuilder.getMatch();
840         EthernetMatch ethMatch = match.getEthernetMatch();
841         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
842         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
843
844         Assert.assertTrue(match.getLayer4Match() instanceof UdpMatch);
845         UdpMatch layer4Match = (UdpMatch) match.getLayer4Match();
846         Assert.assertEquals(50, layer4Match.getUdpDestinationPort().getValue().intValue());
847         int port = portSecurityRule.getSecurityRulePortMin();
848         Assert.assertEquals("Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +
849                             "_" + port + "_0.0.0.0/24_Permit", flowBuilder.getFlowName());
850     }
851
852     /**
853      *  Test IPv6 UDP remove with port no and CIDR selected.
854      */
855     @Test
856     public void testProgramPortSecurityACLRuleIpv6RemoveUdp1() throws Exception {
857         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_UDP);
858         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(50);
859         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(50);
860         when(portSecurityIpv6Rule.getSecurityRuleRemoteIpPrefix()).thenReturn("::/64");
861         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class),
862                 any(NodeBuilder.class));
863
864         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6,
865                 PORT_UUID, false);
866
867         Match match = flowBuilder.getMatch();
868         EthernetMatch ethMatch = match.getEthernetMatch();
869         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
870         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
871
872         Assert.assertTrue(match.getLayer4Match() instanceof UdpMatch);
873         UdpMatch layer4Match = (UdpMatch) match.getLayer4Match();
874         Assert.assertEquals(50, layer4Match.getUdpDestinationPort().getValue().intValue());
875         int port = portSecurityIpv6Rule.getSecurityRulePortMin();
876         Assert.assertEquals("Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +
877                 "_" + port + "_::/64_Permit", flowBuilder.getFlowName());
878     }
879
880     /**
881      *  Test IPv4 UDP add with port no and remote SG selected.
882      */
883     @Test
884     public void testProgramPortSecurityACLRuleAddUdp2() throws Exception {
885         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_UDP);
886         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(50);
887         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(50);
888         when(portSecurityRule.getSecurityRuleRemoteIpPrefix()).thenReturn("0.0.0.0/24");
889         when(portSecurityRule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
890         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
891                                              any(NodeBuilder.class));
892         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroup,
893                                                       PORT_UUID, true);
894
895         Match match = flowBuilder.getMatch();
896         EthernetMatch ethMatch = match.getEthernetMatch();
897         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
898         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
899
900         Assert.assertTrue(match.getLayer4Match() instanceof UdpMatch);
901         UdpMatch layer4Match = (UdpMatch) match.getLayer4Match();
902         Assert.assertEquals(50, layer4Match.getUdpDestinationPort().getValue().intValue());
903         int port = portSecurityRule.getSecurityRulePortMin();
904         String expectedFlowId1 = "Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + port + "_" + DEST_IP_1 +
905                 "_Permit";
906         String expectedFlowId2 = "Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + port + "_" + DEST_IP_2 +
907                 "_Permit";
908         String actualFlowId = flowBuilder.getFlowName();
909         if (actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
910             Assert.assertTrue(true);
911         } else {
912             Assert.assertTrue(false);
913         }
914     }
915
916     /**
917      *  Test IPv6 UDP add with port no and remote SG selected.
918      */
919     @Test
920     public void testProgramPortSecurityACLRuleIpv6AddUdp2() throws Exception {
921         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_UDP);
922         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(50);
923         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(50);
924         when(portSecurityIpv6Rule.getSecurityRuleRemoteIpPrefix()).thenReturn("::/64");
925         when(portSecurityIpv6Rule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
926         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
927                 any(NodeBuilder.class));
928         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6,
929                 PORT_UUID, true);
930
931         Match match = flowBuilder.getMatch();
932         EthernetMatch ethMatch = match.getEthernetMatch();
933         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
934         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
935
936         Assert.assertTrue(match.getLayer4Match() instanceof UdpMatch);
937         UdpMatch layer4Match = (UdpMatch) match.getLayer4Match();
938         Assert.assertEquals(50, layer4Match.getUdpDestinationPort().getValue().intValue());
939         int port = portSecurityIpv6Rule.getSecurityRulePortMin();
940         String expectedFlowId1 = "Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + port + "_" + IPV6_DEST_IP_1 +
941                 "_Permit";
942         String expectedFlowId2 = "Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + port + "_" + IPV6_DEST_IP_2 +
943                 "_Permit";
944         String actualFlowId = flowBuilder.getFlowName();
945         if (actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
946             Assert.assertTrue(true);
947         } else {
948             Assert.assertTrue(false);
949         }
950     }
951
952     /**
953      *  Test IPv4 UDP remove with port no and remote SG selected.
954      */
955     @Test
956     public void testProgramPortSecurityACLRuleRemoveUdp2() throws Exception {
957         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_UDP);
958         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(50);
959         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(50);
960         when(portSecurityRule.getSecurityRuleRemoteIpPrefix()).thenReturn("0.0.0.0/24");
961         when(portSecurityRule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
962         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class),
963                                              any(NodeBuilder.class));
964         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroup,
965                                                       PORT_UUID, false);
966
967         Match match = flowBuilder.getMatch();
968         EthernetMatch ethMatch = match.getEthernetMatch();
969         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
970         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
971
972         Assert.assertTrue(match.getLayer4Match() instanceof UdpMatch);
973         UdpMatch layer4Match = (UdpMatch) match.getLayer4Match();
974         Assert.assertEquals(50, layer4Match.getUdpDestinationPort().getValue().intValue());
975         int port = portSecurityRule.getSecurityRulePortMin();
976         String expectedFlowId1 = "Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + port + "_" + DEST_IP_1 +
977                 "_Permit";
978         String expectedFlowId2 = "Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + port + "_" + DEST_IP_2 +
979                 "_Permit";
980         String actualFlowId = flowBuilder.getFlowName();
981         if (actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
982             Assert.assertTrue(true);
983         } else {
984             Assert.assertTrue(false);
985         }
986     }
987
988     /**
989      *  Test IPv6 UDP remove with port no and remote SG selected.
990      */
991     @Test
992     public void testProgramPortSecurityACLRuleIpv6RemoveUdp2() throws Exception {
993         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_UDP);
994         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(50);
995         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(50);
996         when(portSecurityIpv6Rule.getSecurityRuleRemoteIpPrefix()).thenReturn("::/64");
997         when(portSecurityIpv6Rule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
998         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class),
999                 any(NodeBuilder.class));
1000         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6,
1001                 PORT_UUID, false);
1002
1003         Match match = flowBuilder.getMatch();
1004         EthernetMatch ethMatch = match.getEthernetMatch();
1005         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
1006         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
1007
1008         Assert.assertTrue(match.getLayer4Match() instanceof UdpMatch);
1009         UdpMatch layer4Match = (UdpMatch) match.getLayer4Match();
1010         Assert.assertEquals(50, layer4Match.getUdpDestinationPort().getValue().intValue());
1011         int port = portSecurityIpv6Rule.getSecurityRulePortMin();
1012         String expectedFlowId1 = "Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + port + "_" + IPV6_DEST_IP_1 +
1013                 "_Permit";
1014         String expectedFlowId2 = "Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + port + "_" + IPV6_DEST_IP_2 +
1015                 "_Permit";
1016         String actualFlowId = flowBuilder.getFlowName();
1017         if (actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
1018             Assert.assertTrue(true);
1019         } else {
1020             Assert.assertTrue(false);
1021         }
1022     }
1023
1024     /**
1025      *  Test IPv4 UDP add with ports (All UDP) and CIDR selected.
1026      */
1027     @Test
1028     public void testProgramPortSecurityACLRuleAddUdpAll1() throws Exception {
1029         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_UDP);
1030         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(PORT_RANGE_MAX);
1031         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(PORT_RANGE_MIN);
1032         when(portSecurityRule.getSecurityRuleRemoteIpPrefix()).thenReturn("0.0.0.0/24");
1033         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
1034                                              any(NodeBuilder.class));
1035
1036         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroup,
1037                                                       PORT_UUID, true);
1038
1039         Match match = flowBuilder.getMatch();
1040         EthernetMatch ethMatch = match.getEthernetMatch();
1041         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
1042         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
1043
1044         Assert.assertTrue(match.getLayer4Match() instanceof UdpMatch);
1045         Assert.assertEquals("Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS + "_" + PORT_RANGE_MIN  + "_" +
1046                             PORT_RANGE_MAX + "_0.0.0.0/24_Permit", flowBuilder.getFlowName());
1047     }
1048
1049     /**
1050      *  Test IPv6 UDP add with ports (All UDP) and CIDR selected.
1051      */
1052     @Test
1053     public void testProgramPortSecurityACLRuleIPv6AddUdpAll1() throws Exception {
1054         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_UDP);
1055         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(PORT_RANGE_MAX);
1056         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(PORT_RANGE_MIN);
1057         when(portSecurityIpv6Rule.getSecurityRuleRemoteIpPrefix()).thenReturn("::/64");
1058         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
1059                 any(NodeBuilder.class));
1060
1061         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6,
1062                 PORT_UUID, true);
1063
1064         Match match = flowBuilder.getMatch();
1065         EthernetMatch ethMatch = match.getEthernetMatch();
1066         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
1067         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
1068
1069         Assert.assertTrue(match.getLayer4Match() instanceof UdpMatch);
1070         Assert.assertEquals("Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS + "_" + PORT_RANGE_MIN  + "_" +
1071                 PORT_RANGE_MAX + "_::/64_Permit", flowBuilder.getFlowName());
1072     }
1073
1074     /**
1075      *  Test IPv4 UDP remove with ports (All UDP) and CIDR selected.
1076      */
1077     @Test
1078     public void testProgramPortSecurityACLRuleRemoveUdpAll1() throws Exception {
1079         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_UDP);
1080         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(PORT_RANGE_MAX);
1081         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(PORT_RANGE_MIN);
1082         when(portSecurityRule.getSecurityRuleRemoteIpPrefix()).thenReturn("0.0.0.0/24");
1083         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class),
1084                                              any(NodeBuilder.class));
1085
1086         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroup,
1087                                                       PORT_UUID, false);
1088
1089         Match match = flowBuilder.getMatch();
1090         EthernetMatch ethMatch = match.getEthernetMatch();
1091         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
1092         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
1093
1094         Assert.assertTrue(match.getLayer4Match() instanceof UdpMatch);
1095         Assert.assertEquals("Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS + "_" + PORT_RANGE_MIN  + "_" +
1096                 PORT_RANGE_MAX + "_0.0.0.0/24_Permit", flowBuilder.getFlowName());
1097     }
1098
1099     /**
1100      *  Test IPv6 UDP remove with ports (All UDP) and CIDR selected.
1101      */
1102     @Test
1103     public void testProgramPortSecurityACLRuleIpv6RemoveUdpAll1() throws Exception {
1104         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_UDP);
1105         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(PORT_RANGE_MAX);
1106         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(PORT_RANGE_MIN);
1107         when(portSecurityIpv6Rule.getSecurityRuleRemoteIpPrefix()).thenReturn("::/64");
1108         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class),
1109                 any(NodeBuilder.class));
1110
1111         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6,
1112                 PORT_UUID, false);
1113
1114         Match match = flowBuilder.getMatch();
1115         EthernetMatch ethMatch = match.getEthernetMatch();
1116         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
1117         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
1118
1119         Assert.assertTrue(match.getLayer4Match() instanceof UdpMatch);
1120         Assert.assertEquals("Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS + "_" + PORT_RANGE_MIN  + "_" +
1121                 PORT_RANGE_MAX + "_::/64_Permit", flowBuilder.getFlowName());
1122     }
1123
1124     /**
1125      *  Test IPv4 UDP add with ports (All UDP) and remote SG selected.
1126      */
1127     @Test
1128     public void testProgramPortSecurityACLRuleAddUdpAll2() throws Exception {
1129         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_UDP);
1130         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(PORT_RANGE_MAX);
1131         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(PORT_RANGE_MIN);
1132         when(portSecurityRule.getSecurityRuleRemoteIpPrefix()).thenReturn("0.0.0.0/24");
1133         when(portSecurityRule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
1134         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
1135                                              any(NodeBuilder.class));
1136         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroup,
1137                                                       PORT_UUID, true);
1138
1139         Match match = flowBuilder.getMatch();
1140         EthernetMatch ethMatch = match.getEthernetMatch();
1141         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
1142         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
1143
1144         Assert.assertTrue(match.getLayer4Match() instanceof UdpMatch);
1145         String expectedFlowId1 = "Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + PORT_RANGE_MIN + "_" +
1146                         PORT_RANGE_MAX + "_" + DEST_IP_1 + "_Permit";
1147         String expectedFlowId2 = "Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + PORT_RANGE_MIN + "_" +
1148                 PORT_RANGE_MAX + "_" + DEST_IP_2 + "_Permit";
1149         String actualFlowId = flowBuilder.getFlowName();
1150         if (actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
1151             Assert.assertTrue(true);
1152         } else {
1153             Assert.assertTrue(false);
1154         }
1155     }
1156
1157     /**
1158      *  Test IPv6 UDP add with ports (All UDP) and remote SG selected.
1159      */
1160     @Test
1161     public void testProgramPortSecurityACLRuleIpv6AddUdpAll2() throws Exception {
1162         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_UDP);
1163         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(PORT_RANGE_MAX);
1164         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(PORT_RANGE_MIN);
1165         when(portSecurityIpv6Rule.getSecurityRuleRemoteIpPrefix()).thenReturn("::/64");
1166         when(portSecurityIpv6Rule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
1167         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
1168                 any(NodeBuilder.class));
1169         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6,
1170                 PORT_UUID, true);
1171
1172         Match match = flowBuilder.getMatch();
1173         EthernetMatch ethMatch = match.getEthernetMatch();
1174         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
1175         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
1176
1177         Assert.assertTrue(match.getLayer4Match() instanceof UdpMatch);
1178         String expectedFlowId1 = "Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + PORT_RANGE_MIN + "_" +
1179                 PORT_RANGE_MAX + "_" + IPV6_DEST_IP_1 + "_Permit";
1180         String expectedFlowId2 = "Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + PORT_RANGE_MIN + "_" +
1181                 PORT_RANGE_MAX + "_" + IPV6_DEST_IP_2 + "_Permit";
1182         String actualFlowId = flowBuilder.getFlowName();
1183         if (actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
1184             Assert.assertTrue(true);
1185         } else {
1186             Assert.assertTrue(false);
1187         }
1188     }
1189
1190     /**
1191      *  Test IPv4 UDP remove with ports (All UDP) and remote SG selected.
1192      */
1193     @Test
1194     public void testProgramPortSecurityACLRuleRemoveUdpAll2() throws Exception {
1195         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_UDP);
1196         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(PORT_RANGE_MAX);
1197         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(PORT_RANGE_MIN);
1198         when(portSecurityRule.getSecurityRuleRemoteIpPrefix()).thenReturn("0.0.0.0/24");
1199         when(portSecurityRule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
1200         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class),
1201                                              any(NodeBuilder.class));
1202         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroup,
1203                                                       PORT_UUID, false);
1204
1205         Match match = flowBuilder.getMatch();
1206         EthernetMatch ethMatch = match.getEthernetMatch();
1207         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
1208         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
1209
1210         Assert.assertTrue(match.getLayer4Match() instanceof UdpMatch);
1211         String expectedFlowId1 = "Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + PORT_RANGE_MIN + "_" +
1212                         PORT_RANGE_MAX + "_" + DEST_IP_1 + "_Permit";
1213         String expectedFlowId2 = "Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + PORT_RANGE_MIN + "_" +
1214                 PORT_RANGE_MAX + "_" + DEST_IP_2 + "_Permit";
1215         String actualFlowId = flowBuilder.getFlowName();
1216         if (actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
1217             Assert.assertTrue(true);
1218         } else {
1219             Assert.assertTrue(false);
1220         }
1221     }
1222
1223     /**
1224      *  Test IPv6 UDP remove with ports (All UDP) and remote SG selected.
1225      */
1226     @Test
1227     public void testProgramPortSecurityACLRuleIpv6RemoveUdpAll2() throws Exception {
1228         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_UDP);
1229         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(PORT_RANGE_MAX);
1230         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(PORT_RANGE_MIN);
1231         when(portSecurityIpv6Rule.getSecurityRuleRemoteIpPrefix()).thenReturn("::/64");
1232         when(portSecurityIpv6Rule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
1233         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class),
1234                 any(NodeBuilder.class));
1235         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID, MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6,
1236                 PORT_UUID, false);
1237
1238         Match match = flowBuilder.getMatch();
1239         EthernetMatch ethMatch = match.getEthernetMatch();
1240         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
1241         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
1242
1243         Assert.assertTrue(match.getLayer4Match() instanceof UdpMatch);
1244         String expectedFlowId1 = "Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + PORT_RANGE_MIN + "_" +
1245                 PORT_RANGE_MAX + "_" + IPV6_DEST_IP_1 + "_Permit";
1246         String expectedFlowId2 = "Ingress_UDP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + PORT_RANGE_MIN + "_" +
1247                 PORT_RANGE_MAX + "_" + IPV6_DEST_IP_2 + "_Permit";
1248         String actualFlowId = flowBuilder.getFlowName();
1249         if (actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
1250             Assert.assertTrue(true);
1251         } else {
1252             Assert.assertTrue(false);
1253         }
1254     }
1255
1256     /**
1257      *  Test ICMP add with code, type and CIDR selected.
1258      */
1259     @Test
1260     public void testProgramPortSecurityACLRuleAddIcmp1() throws Exception {
1261         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_ICMP);
1262         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(10);
1263         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(10);
1264         when(portSecurityRule.getSecurityRuleRemoteIpPrefix()).thenReturn("0.0.0.0/24");
1265
1266         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
1267                                              any(NodeBuilder.class));
1268         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID,
1269                                                     MAC_ADDRESS, LOCAL_PORT, securityGroup, PORT_UUID, true);
1270         Match match = flowBuilder.getMatch();
1271         Icmpv4Match icmpv4Match = match.getIcmpv4Match();
1272         Assert.assertEquals(10, icmpv4Match.getIcmpv4Type().shortValue());
1273         Assert.assertEquals(10, icmpv4Match.getIcmpv4Code().shortValue());
1274         EthernetMatch ethMatch = match.getEthernetMatch();
1275         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
1276         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
1277         Short type = portSecurityRule.getSecurityRulePortMin().shortValue();
1278         Short code = portSecurityRule.getSecurityRulePortMax().shortValue();
1279         Assert.assertEquals("Ingress_ICMP_" + SEGMENT_ID + "_" + MAC_ADDRESS + "_" + type + "_" + code
1280                             + "_0.0.0.0/24_Permit",
1281                             flowBuilder.getFlowName());
1282     }
1283
1284     /**
1285      *  Test ICMPv6 add with code, type and CIDR selected.
1286      */
1287     @Test
1288     public void testProgramPortSecurityACLRuleIpv6AddIcmp1() throws Exception {
1289         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_ICMPV6);
1290         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(10);
1291         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(10);
1292         when(portSecurityIpv6Rule.getSecurityRuleRemoteIpPrefix()).thenReturn("::/64");
1293
1294         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
1295                 any(NodeBuilder.class));
1296         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID,
1297                 MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6, PORT_UUID, true);
1298
1299         Match match = flowBuilder.getMatch();
1300         Icmpv6Match icmpv6Match = match.getIcmpv6Match();
1301         Assert.assertEquals(10, icmpv6Match.getIcmpv6Type().shortValue());
1302         Assert.assertEquals(10, icmpv6Match.getIcmpv6Code().shortValue());
1303         EthernetMatch ethMatch = match.getEthernetMatch();
1304         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
1305         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
1306         Short type = portSecurityIpv6Rule.getSecurityRulePortMin().shortValue();
1307         Short code = portSecurityIpv6Rule.getSecurityRulePortMax().shortValue();
1308         Assert.assertEquals("Ingress_ICMP_" + SEGMENT_ID + "_" + MAC_ADDRESS + "_" + type + "_" + code
1309                         + "_::/64_Permit",
1310                 flowBuilder.getFlowName());
1311     }
1312
1313     /**
1314      *  Test ICMP remove with code, type and CIDR selected.
1315      */
1316     @Test
1317     public void testProgramPortSecurityACLRuleRemoveIcmp1() throws Exception {
1318         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_ICMP);
1319         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(20);
1320         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(20);
1321         when(portSecurityRule.getSecurityRuleRemoteIpPrefix()).thenReturn("0.0.0.0/24");
1322         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class),
1323                                              any(NodeBuilder.class));
1324
1325         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID,
1326                                                     MAC_ADDRESS, LOCAL_PORT, securityGroup, PORT_UUID, false);
1327         Match match = flowBuilder.getMatch();
1328         Icmpv4Match icmpv4Match = match.getIcmpv4Match();
1329         Assert.assertEquals(20, icmpv4Match.getIcmpv4Type().shortValue());
1330         Assert.assertEquals(20, icmpv4Match.getIcmpv4Code().shortValue());
1331         EthernetMatch ethMatch = match.getEthernetMatch();
1332         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
1333         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
1334         Short type = portSecurityRule.getSecurityRulePortMin().shortValue();
1335         Short code = portSecurityRule.getSecurityRulePortMax().shortValue();
1336         Assert.assertEquals("Ingress_ICMP_" + SEGMENT_ID + "_" + MAC_ADDRESS + "_" + type + "_" + code
1337                             + "_0.0.0.0/24_Permit",
1338                             flowBuilder.getFlowName());
1339     }
1340
1341     /**
1342      *  Test ICMPv6 remove with code, type and CIDR selected.
1343      */
1344     @Test
1345     public void testProgramPortSecurityACLRuleIpv6RemoveIcmp1() throws Exception {
1346         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_ICMPV6);
1347         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(20);
1348         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(20);
1349         when(portSecurityIpv6Rule.getSecurityRuleRemoteIpPrefix()).thenReturn("::/64");
1350         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class),
1351                 any(NodeBuilder.class));
1352
1353         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID,
1354                 MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6, PORT_UUID, false);
1355         Match match = flowBuilder.getMatch();
1356         Icmpv6Match icmpv6Match = match.getIcmpv6Match();
1357         Assert.assertEquals(20, icmpv6Match.getIcmpv6Type().shortValue());
1358         Assert.assertEquals(20, icmpv6Match.getIcmpv6Code().shortValue());
1359         EthernetMatch ethMatch = match.getEthernetMatch();
1360         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
1361         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
1362         Short type = portSecurityIpv6Rule.getSecurityRulePortMin().shortValue();
1363         Short code = portSecurityIpv6Rule.getSecurityRulePortMax().shortValue();
1364         Assert.assertEquals("Ingress_ICMP_" + SEGMENT_ID + "_" + MAC_ADDRESS + "_" + type + "_" + code
1365                         + "_::/64_Permit",
1366                 flowBuilder.getFlowName());
1367     }
1368
1369     /**
1370      *  Test ICMP add with code, type and remote SG selected.
1371      */
1372     @Test
1373     public void testProgramPortSecurityACLRuleAddIcmp2() throws Exception {
1374         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_ICMP);
1375         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(30);
1376         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(30);
1377         when(portSecurityRule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
1378         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
1379                                              any(NodeBuilder.class));
1380
1381         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID,
1382                                                     MAC_ADDRESS, LOCAL_PORT, securityGroup, PORT_UUID, true);
1383         Match match = flowBuilder.getMatch();
1384         Icmpv4Match icmpv4Match =match.getIcmpv4Match();
1385         Assert.assertEquals(30, icmpv4Match.getIcmpv4Type().shortValue());
1386         Assert.assertEquals(30, icmpv4Match.getIcmpv4Code().shortValue());
1387         EthernetMatch ethMatch = match.getEthernetMatch();
1388         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
1389         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
1390         Short type = portSecurityRule.getSecurityRulePortMin().shortValue();
1391         Short code = portSecurityRule.getSecurityRulePortMax().shortValue();
1392         String expectedFlowId1 = "Ingress_ICMP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + type + "_" + code + "_"
1393                                 + DEST_IP_1 + "_Permit";
1394         String expectedFlowId2 = "Ingress_ICMP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + type + "_" + code + "_"
1395                                 + DEST_IP_2 + "_Permit";
1396         String actualFlowId = flowBuilder.getFlowName();
1397         if(actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
1398             Assert.assertTrue(true);
1399         } else {
1400             Assert.assertTrue(false);
1401         }
1402     }
1403
1404     /**
1405      *  Test ICMPv6 add with code, type and remote SG selected.
1406      */
1407     @Test
1408     public void testProgramPortSecurityACLRuleIpv6AddIcmp2() throws Exception {
1409         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_ICMPV6);
1410         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(30);
1411         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(30);
1412         when(portSecurityIpv6Rule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
1413         PowerMockito.doAnswer(answer()).when(ingressAclServiceSpy, "writeFlow", any(FlowBuilder.class),
1414                 any(NodeBuilder.class));
1415
1416         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID,
1417                 MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6, PORT_UUID, true);
1418         Match match = flowBuilder.getMatch();
1419         Icmpv6Match icmpv6Match =match.getIcmpv6Match();
1420         Assert.assertEquals(30, icmpv6Match.getIcmpv6Type().shortValue());
1421         Assert.assertEquals(30, icmpv6Match.getIcmpv6Code().shortValue());
1422         EthernetMatch ethMatch = match.getEthernetMatch();
1423         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
1424         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
1425
1426         Short type = portSecurityIpv6Rule.getSecurityRulePortMin().shortValue();
1427         Short code = portSecurityIpv6Rule.getSecurityRulePortMax().shortValue();
1428         String expectedFlowId1 = "Ingress_ICMP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + type + "_" + code + "_"
1429                 + IPV6_DEST_IP_1 + "_Permit";
1430         String expectedFlowId2 = "Ingress_ICMP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + type + "_" + code + "_"
1431                 + IPV6_DEST_IP_2 + "_Permit";
1432         String actualFlowId = flowBuilder.getFlowName();
1433         if(actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
1434             Assert.assertTrue(true);
1435         } else {
1436             Assert.assertTrue(false);
1437         }
1438     }
1439
1440     /**
1441      *  Test ICMP remove with code, type and remote SG selected.
1442      */
1443     @Test
1444     public void testProgramPortSecurityACLRuleRemoveIcmp2() throws Exception {
1445         when(portSecurityRule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_ICMP);
1446         when(portSecurityRule.getSecurityRulePortMax()).thenReturn(40);
1447         when(portSecurityRule.getSecurityRulePortMin()).thenReturn(40);
1448         when(portSecurityRule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
1449         PowerMockito.doAnswer(answer())
1450         .when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class), any(NodeBuilder.class));
1451
1452         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID,
1453                                                     MAC_ADDRESS, LOCAL_PORT, securityGroup, PORT_UUID, false);
1454         Match match = flowBuilder.getMatch();
1455         Icmpv4Match icmpv4Match = match.getIcmpv4Match();
1456         Assert.assertEquals(40, icmpv4Match.getIcmpv4Type().shortValue());
1457         Assert.assertEquals(40, icmpv4Match.getIcmpv4Code().shortValue());
1458         EthernetMatch ethMatch = match.getEthernetMatch();
1459         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
1460         Assert.assertEquals((long) IPV4_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
1461         Short type = portSecurityRule.getSecurityRulePortMin().shortValue();
1462         Short code = portSecurityRule.getSecurityRulePortMax().shortValue();
1463         String expectedFlowId1 = "Ingress_ICMP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + type + "_" + code + "_"
1464                                 + DEST_IP_1 + "_Permit";
1465         String expectedFlowId2 = "Ingress_ICMP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + type + "_" + code + "_"
1466                                 + DEST_IP_2 + "_Permit";
1467         String actualFlowId = flowBuilder.getFlowName();
1468         if(actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
1469             Assert.assertTrue(true);
1470         } else {
1471             Assert.assertTrue(false);
1472         }
1473     }
1474
1475     /**
1476      *  Test ICMPv6 remove with code, type and remote SG selected.
1477      */
1478     @Test
1479     public void testProgramPortSecurityACLRuleIpv6RemoveIcmp2() throws Exception {
1480         when(portSecurityIpv6Rule.getSecurityRuleProtocol()).thenReturn(NeutronSecurityRule.PROTOCOL_ICMPV6);
1481         when(portSecurityIpv6Rule.getSecurityRulePortMax()).thenReturn(40);
1482         when(portSecurityIpv6Rule.getSecurityRulePortMin()).thenReturn(40);
1483         when(portSecurityIpv6Rule.getSecurityRemoteGroupID()).thenReturn("85cc3048-abc3-43cc-89b3-377341426ac5");
1484         PowerMockito.doAnswer(answer())
1485                 .when(ingressAclServiceSpy, "removeFlow", any(FlowBuilder.class), any(NodeBuilder.class));
1486
1487         ingressAclServiceSpy.programPortSecurityGroup(DP_ID_LONG, SEGMENT_ID,
1488                 MAC_ADDRESS, LOCAL_PORT, securityGroupIpv6, PORT_UUID, false);
1489         Match match = flowBuilder.getMatch();
1490         Icmpv6Match icmpv6Match = match.getIcmpv6Match();
1491         Assert.assertEquals(40, icmpv6Match.getIcmpv6Type().shortValue());
1492         Assert.assertEquals(40, icmpv6Match.getIcmpv6Code().shortValue());
1493         EthernetMatch ethMatch = match.getEthernetMatch();
1494         Assert.assertEquals(MAC_ADDRESS, ethMatch.getEthernetDestination().getAddress().getValue());
1495         Assert.assertEquals((long) IPV6_ETHER_TYPE, (long) ethMatch.getEthernetType().getType().getValue());
1496         Short type = portSecurityIpv6Rule.getSecurityRulePortMin().shortValue();
1497         Short code = portSecurityIpv6Rule.getSecurityRulePortMax().shortValue();
1498         String expectedFlowId1 = "Ingress_ICMP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + type + "_" + code + "_"
1499                 + IPV6_DEST_IP_1 + "_Permit";
1500         String expectedFlowId2 = "Ingress_ICMP_" + SEGMENT_ID + "_" + MAC_ADDRESS +"_" + type + "_" + code + "_"
1501                 + IPV6_DEST_IP_2 + "_Permit";
1502         String actualFlowId = flowBuilder.getFlowName();
1503         if(actualFlowId.equals(expectedFlowId1) || actualFlowId.equals(expectedFlowId2)) {
1504             Assert.assertTrue(true);
1505         } else {
1506             Assert.assertTrue(false);
1507         }
1508     }
1509
1510     /**
1511      *  Test IPv4 invalid ether type test case.
1512      */
1513     @Test
1514     public void testProgramPortSecurityACLRuleInvalidEther() throws Exception {
1515         when(portSecurityRule.getSecurityRuleEthertype()).thenReturn("IP");
1516
1517         ingressAclServiceSpy.programPortSecurityGroup(Long.valueOf(1554), "2", MAC_ADDRESS, 124, securityGroup,PORT_UUID,false);
1518
1519         verify(writeTransaction, times(0)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
1520         verify(writeTransaction, times(0)).submit();
1521         verify(commitFuture, times(0)).get();
1522     }
1523
1524     /**
1525      *  Test IPv4 invalid direction type test case.
1526      */
1527     @Test
1528     public void testProgramPortSecurityACLRuleInvalidDirection() throws Exception {
1529         when(portSecurityRule.getSecurityRuleDirection()).thenReturn("edgress");
1530
1531         ingressAclServiceSpy.programPortSecurityGroup(Long.valueOf(1554), "2", MAC_ADDRESS, 124, securityGroup,PORT_UUID,false);
1532
1533         verify(writeTransaction, times(0)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
1534         verify(writeTransaction, times(0)).submit();
1535         verify(commitFuture, times(0)).get();
1536     }
1537
1538     /**
1539      *  Test With isConntrackEnabled false isComputeNode false
1540      */
1541     @Test
1542     public void testProgramFixedSecurityACLAdd1() throws Exception {
1543         when(securityServices.isConntrackEnabled()).thenReturn(false);
1544
1545         ingressAclServiceSpy.programFixedSecurityGroup(Long.valueOf(1554), "2", DHCP_MAC_ADDRESS, 1, false, false, MAC_ADDRESS, true);
1546
1547         verify(writeTransaction, times(0)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), eq(true));
1548         verify(writeTransaction, times(0)).submit();
1549         verify(commitFuture, times(0)).get();
1550     }
1551     /**
1552      *  Test With isConntrackEnabled false isComputeNode false
1553      */
1554     @Test
1555     public void testProgramFixedSecurityACLRemove1() throws Exception {
1556         when(securityServices.isConntrackEnabled()).thenReturn(false);
1557
1558         ingressAclServiceSpy.programFixedSecurityGroup(Long.valueOf(1554), "2", DHCP_MAC_ADDRESS, 1, false, false, MAC_ADDRESS, false);
1559
1560         verify(writeTransaction, times(0)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
1561         verify(writeTransaction, times(0)).submit();
1562         verify(commitFuture, times(0)).get();
1563     }
1564     /**
1565      *  Test With isConntrackEnabled false isComputeNode false
1566      */
1567     @Test
1568     public void testProgramFixedSecurityACLAdd2() throws Exception {
1569         when(securityServices.isConntrackEnabled()).thenReturn(false);
1570
1571         ingressAclServiceSpy.programFixedSecurityGroup(Long.valueOf(1554), "2", DHCP_MAC_ADDRESS, 1, false, true, MAC_ADDRESS, true);
1572
1573         verify(writeTransaction, times(1)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), eq(true));
1574         verify(writeTransaction, times(1)).submit();
1575         verify(commitFuture, times(1)).checkedGet();
1576     }
1577     /**
1578      *  Test With isConntrackEnabled false isComputeNode false
1579      */
1580     @Test
1581     public void testProgramFixedSecurityACLRemove2() throws Exception {
1582         when(securityServices.isConntrackEnabled()).thenReturn(false);
1583
1584         ingressAclServiceSpy.programFixedSecurityGroup(Long.valueOf(1554), "2", DHCP_MAC_ADDRESS, 1, false, true, MAC_ADDRESS, false);
1585
1586         verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
1587         verify(writeTransaction, times(1)).submit();
1588         verify(commitFuture, times(1)).get();
1589     }
1590     /**
1591      *  Test With isConntrackEnabled true isComputeNode false
1592      */
1593     @Test
1594     public void testProgramFixedSecurityACLAdd3() throws Exception {
1595         when(securityServices.isConntrackEnabled()).thenReturn(true);
1596
1597         ingressAclServiceSpy.programFixedSecurityGroup(Long.valueOf(1554), "2", DHCP_MAC_ADDRESS, 1, false, false, MAC_ADDRESS, true);
1598
1599         verify(writeTransaction, times(0)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), eq(true));
1600         verify(writeTransaction, times(0)).submit();
1601         verify(commitFuture, times(0)).get();
1602     }
1603     /**
1604      *  Test With isConntrackEnabled true isComputeNode false
1605      */
1606     @Test
1607     public void testProgramFixedSecurityACLRemove3() throws Exception {
1608         when(securityServices.isConntrackEnabled()).thenReturn(true);
1609
1610         ingressAclServiceSpy.programFixedSecurityGroup(Long.valueOf(1554), "2", DHCP_MAC_ADDRESS, 1, false, false, MAC_ADDRESS, false);
1611
1612         verify(writeTransaction, times(0)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
1613         verify(writeTransaction, times(0)).submit();
1614         verify(commitFuture, times(0)).get();
1615     }
1616     /**
1617      *  Test With isConntrackEnabled true isComputeNode true
1618      */
1619     @Test
1620     public void testProgramFixedSecurityACLAdd4() throws Exception {
1621         when(securityServices.isConntrackEnabled()).thenReturn(true);
1622
1623         ingressAclServiceSpy.programFixedSecurityGroup(Long.valueOf(1554), "2", DHCP_MAC_ADDRESS, 1, false, true, MAC_ADDRESS, true);
1624
1625         verify(writeTransaction, times(6)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), eq(true));
1626         verify(writeTransaction, times(6)).submit();
1627         verify(commitFuture, times(6)).checkedGet();
1628     }
1629     /**
1630      *  Test With isConntrackEnabled true isComputeNode true
1631      */
1632     @Test
1633     public void testProgramFixedSecurityACLRemove4() throws Exception {
1634         when(securityServices.isConntrackEnabled()).thenReturn(true);
1635
1636         ingressAclServiceSpy.programFixedSecurityGroup(Long.valueOf(1554), "2", DHCP_MAC_ADDRESS, 1, false, true, MAC_ADDRESS, false);
1637
1638         verify(writeTransaction, times(6)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
1639         verify(writeTransaction, times(6)).submit();
1640         verify(commitFuture, times(6)).get();
1641     }
1642
1643 }