2 * Copyright (c) 2016 Hewlett Packard Enterprise, Co. and others. All rights reserved.
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
9 package org.opendaylight.netvirt.aclservice.listeners;
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.opendaylight.netvirt.aclservice.utils.AclServiceTestUtils.clearStaticData;
16 import static org.opendaylight.netvirt.aclservice.utils.AclServiceTestUtils.prepareAcl;
17 import static org.opendaylight.netvirt.aclservice.utils.AclServiceTestUtils.prepareAclClusterUtil;
18 import static org.opendaylight.netvirt.aclservice.utils.AclServiceTestUtils.prepareAclDataUtil;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.ArgumentCaptor;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.netvirt.aclservice.api.AclServiceManager;
26 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
27 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 public class AclEventListenerTest {
34 private AclEventListener aclEventListener;
35 private AclServiceManager aclServiceManager;
37 private InstanceIdentifier<Acl> mockInstanceId;
38 private AclInterface aclInterfaceMock;
40 private ArgumentCaptor<AclInterface> aclInterfaceValueSaver;
41 private ArgumentCaptor<Action> actionValueSaver;
42 private ArgumentCaptor<Ace> aceValueSaver;
43 private String aclName;
45 @SuppressWarnings("unchecked")
49 mockInstanceId = mock(InstanceIdentifier.class);
50 aclInterfaceMock = mock(AclInterface.class);
51 aclServiceManager = mock(AclServiceManager.class);
52 aclEventListener = new AclEventListener(aclServiceManager, mock(DataBroker.class));
54 aclInterfaceValueSaver = ArgumentCaptor.forClass(AclInterface.class);
55 actionValueSaver = ArgumentCaptor.forClass(AclServiceManager.Action.class);
56 aceValueSaver = ArgumentCaptor.forClass(Ace.class);
57 prepareAclClusterUtil("netvirt-acl");
60 aclName = "00000000-0000-0000-0000-000000000001";
64 public void tearDown() {
65 clearStaticData(aclInterfaceMock, aclName);
69 public void testUpdate_singleInterface_addNewAce() {
70 prepareAclDataUtil(aclInterfaceMock, aclName);
72 Acl previousAcl = prepareAcl(aclName, "AllowUDP");
73 Acl updatedAcl = prepareAcl(aclName, "AllowICMP", "AllowUDP");
75 aclEventListener.update(mockInstanceId, previousAcl, updatedAcl);
77 verify(aclServiceManager).notifyAce(aclInterfaceValueSaver.capture(), actionValueSaver.capture(),
78 aceValueSaver.capture());
80 assertEquals(Action.ADD, actionValueSaver.getValue());
81 assertEquals("AllowICMP", aceValueSaver.getValue().getRuleName());
85 public void testUpdate_singleInterface_removeOldAce() {
86 prepareAclDataUtil(aclInterfaceMock, aclName);
88 Acl previousAcl = prepareAcl(aclName, "AllowICMP", "AllowUDP");
89 Acl updatedAcl = prepareAcl(aclName, "AllowUDP");
91 aclEventListener.update(mockInstanceId, previousAcl, updatedAcl);
93 verify(aclServiceManager).notifyAce(aclInterfaceValueSaver.capture(), actionValueSaver.capture(),
94 aceValueSaver.capture());
96 assertEquals(Action.REMOVE, actionValueSaver.getValue());
97 assertEquals("AllowICMP", aceValueSaver.getValue().getRuleName());
101 public void testUpdate_singleInterface_addNewAceAndRemoveOldAce() {
102 prepareAclDataUtil(aclInterfaceMock, aclName);
104 Acl previousAcl = prepareAcl(aclName, "AllowICMP", "AllowUDP");
105 Acl updatedAcl = prepareAcl(aclName, "AllowTCP", "AllowUDP");
107 aclEventListener.update(mockInstanceId, previousAcl, updatedAcl);
109 verify(aclServiceManager, times(2)).notifyAce(aclInterfaceValueSaver.capture(), actionValueSaver.capture(),
110 aceValueSaver.capture());
112 assertEquals(Action.ADD, actionValueSaver.getAllValues().get(0));
113 assertEquals("AllowTCP", aceValueSaver.getAllValues().get(0).getRuleName());
115 assertEquals(Action.REMOVE, actionValueSaver.getAllValues().get(1));
116 assertEquals("AllowICMP", aceValueSaver.getAllValues().get(1).getRuleName());