Fixup Augmentable and Identifiable methods changing
[netvirt.git] / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / listeners / AclInterfaceListener.java
index 797b34de18ec10d9592ad62e0b52acb5b2a046ab..ce8dd8d966beeeccc3ed786930274eb9fd8c4fb3 100644 (file)
@@ -19,6 +19,8 @@ import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeLis
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
+import org.opendaylight.genius.srm.RecoverableListener;
+import org.opendaylight.genius.srm.ServiceRecoveryRegistry;
 import org.opendaylight.netvirt.aclservice.api.AclInterfaceCache;
 import org.opendaylight.netvirt.aclservice.api.AclServiceManager;
 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
@@ -39,7 +41,7 @@ import org.slf4j.LoggerFactory;
 
 @Singleton
 public class AclInterfaceListener extends AsyncDataTreeChangeListenerBase<Interface, AclInterfaceListener>
-        implements ClusteredDataTreeChangeListener<Interface> {
+        implements ClusteredDataTreeChangeListener<Interface>, RecoverableListener {
     private static final Logger LOG = LoggerFactory.getLogger(AclInterfaceListener.class);
 
     private final AclServiceManager aclServiceManager;
@@ -52,7 +54,7 @@ public class AclInterfaceListener extends AsyncDataTreeChangeListenerBase<Interf
     @Inject
     public AclInterfaceListener(AclServiceManager aclServiceManager, AclClusterUtil aclClusterUtil,
             DataBroker dataBroker, AclDataUtil aclDataUtil, AclInterfaceCache aclInterfaceCache,
-            AclServiceUtils aclServicUtils) {
+            AclServiceUtils aclServicUtils, ServiceRecoveryRegistry serviceRecoveryRegistry) {
         super(Interface.class, AclInterfaceListener.class);
         this.aclServiceManager = aclServiceManager;
         this.aclClusterUtil = aclClusterUtil;
@@ -60,46 +62,53 @@ public class AclInterfaceListener extends AsyncDataTreeChangeListenerBase<Interf
         this.aclDataUtil = aclDataUtil;
         this.aclInterfaceCache = aclInterfaceCache;
         this.aclServiceUtils = aclServicUtils;
+        serviceRecoveryRegistry.addRecoverableListener(AclServiceUtils.getRecoverServiceRegistryKey(), this);
     }
 
     @Override
     @PostConstruct
     public void init() {
         LOG.info("{} start", getClass().getSimpleName());
+        registerListener();
+    }
+
+    @Override
+    public void registerListener() {
         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
     }
 
     @Override
     protected InstanceIdentifier<Interface> getWildCardPath() {
-        return InstanceIdentifier
-                .create(Interfaces.class)
-                .child(Interface.class);
+        return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
     }
 
     @Override
-    protected void remove(InstanceIdentifier<Interface> key, Interface port) {
+    public void remove(InstanceIdentifier<Interface> key, Interface port) {
         LOG.trace("Received AclInterface remove event, port={}", port);
         String interfaceId = port.getName();
         AclInterface aclInterface = aclInterfaceCache.remove(interfaceId);
         if (AclServiceUtils.isOfInterest(aclInterface)) {
             if (aclClusterUtil.isEntityOwner()) {
-                LOG.debug("On remove event, notify ACL service manager to unbind ACL from interface: {}", port);
+                LOG.debug("On remove event, notify ACL unbind/remove for interface: {}", interfaceId);
                 aclServiceManager.notify(aclInterface, null, Action.UNBIND);
-                AclServiceUtils.deleteSubnetIpPrefixes(dataBroker, interfaceId);
+                if (aclInterface.getDpId() != null) {
+                    aclServiceManager.notify(aclInterface, null, Action.REMOVE);
+                }
+                aclServiceUtils.deleteSubnetInfo(interfaceId);
             }
         }
     }
 
     @Override
-    protected void update(InstanceIdentifier<Interface> key, Interface portBefore, Interface portAfter) {
-        if (portBefore.getAugmentation(ParentRefs.class) == null
-                && portAfter.getAugmentation(ParentRefs.class) != null) {
+    public void update(InstanceIdentifier<Interface> key, Interface portBefore, Interface portAfter) {
+        if (portBefore.augmentation(ParentRefs.class) == null
+                && portAfter.augmentation(ParentRefs.class) != null) {
             LOG.trace("Ignoring event for update in ParentRefs for {} ", portAfter.getName());
             return;
         }
         LOG.trace("Received AclInterface update event, portBefore={}, portAfter={}", portBefore, portAfter);
-        InterfaceAcl aclInPortAfter = portAfter.getAugmentation(InterfaceAcl.class);
-        InterfaceAcl aclInPortBefore = portBefore.getAugmentation(InterfaceAcl.class);
+        InterfaceAcl aclInPortAfter = portAfter.augmentation(InterfaceAcl.class);
+        InterfaceAcl aclInPortBefore = portBefore.augmentation(InterfaceAcl.class);
 
         String interfaceId = portAfter.getName();
         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
@@ -113,8 +122,11 @@ public class AclInterfaceListener extends AsyncDataTreeChangeListenerBase<Interf
         }
         if (aclInPortAfter != null && aclInPortAfter.isPortSecurityEnabled()
                 || aclInPortBefore != null && aclInPortBefore.isPortSecurityEnabled()) {
-            boolean isSgChanged =
-                    isSecurityGroupsChanged(aclInPortBefore.getSecurityGroups(), aclInPortAfter.getSecurityGroups());
+            List<Uuid> sgsBefore = null;
+            if (aclInPortBefore != null) {
+                sgsBefore = aclInPortBefore.getSecurityGroups();
+            }
+            boolean isSgChanged = isSecurityGroupsChanged(sgsBefore, aclInPortAfter.getSecurityGroups());
             AclInterface aclInterfaceAfter =
                     addOrUpdateAclInterfaceCache(interfaceId, aclInPortAfter, isSgChanged, interfaceState);
 
@@ -122,19 +134,21 @@ public class AclInterfaceListener extends AsyncDataTreeChangeListenerBase<Interf
                 // Handle bind/unbind service irrespective of interface state (up/down)
                 boolean isPortSecurityEnable = aclInterfaceAfter.isPortSecurityEnabled();
                 boolean isPortSecurityEnableBefore = aclInterfaceBefore.isPortSecurityEnabled();
-                // if port security enable is changed, bind/unbind ACL service
-                if (isPortSecurityEnableBefore != isPortSecurityEnable) {
-                    LOG.debug("Notify bind/unbind ACL service for interface={}, isPortSecurityEnable={}", interfaceId,
+                // if port security enable is changed and is disabled, unbind ACL service
+                if (isPortSecurityEnableBefore != isPortSecurityEnable && !isPortSecurityEnable) {
+                    LOG.debug("Notify unbind ACL service for interface={}, isPortSecurityEnable={}", interfaceId,
                             isPortSecurityEnable);
-                    if (isPortSecurityEnable) {
-                        aclServiceManager.notify(aclInterfaceAfter, null, Action.BIND);
-                    } else {
-                        aclServiceManager.notify(aclInterfaceAfter, null, Action.UNBIND);
-                    }
+                    aclServiceManager.notify(aclInterfaceAfter, null, Action.UNBIND);
                 }
                 if (interfaceState != null && interfaceState.getOperStatus().equals(
                         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
                             .state.Interface.OperStatus.Up)) {
+                    // if port security enable is changed and is enabled, bind ACL service
+                    if (isPortSecurityEnableBefore != isPortSecurityEnable && isPortSecurityEnable) {
+                        LOG.debug("Notify bind ACL service for interface={}, isPortSecurityEnable={}", interfaceId,
+                                isPortSecurityEnable);
+                        aclServiceManager.notify(aclInterfaceAfter, null, Action.BIND);
+                    }
                     LOG.debug("On update event, notify ACL service manager to update ACL for interface: {}",
                             interfaceId);
                     // handle add for AclPortsLookup before processing update
@@ -210,9 +224,7 @@ public class AclInterfaceListener extends AsyncDataTreeChangeListenerBase<Interf
                         .lPortTag(interfaceState.getIfIndex()).isMarkedForDelete(false);
             }
 
-            if (prevAclInterface == null) {
-                builder.subnetIpPrefixes(AclServiceUtils.getSubnetIpPrefixes(dataBroker, interfaceId));
-            }
+            builder.subnetInfo(aclServiceUtils.getSubnetInfo(interfaceId));
             if (prevAclInterface == null || prevAclInterface.getElanId() == null) {
                 builder.elanId(AclServiceUtils.getElanIdFromInterface(interfaceId, dataBroker));
             }
@@ -226,16 +238,18 @@ public class AclInterfaceListener extends AsyncDataTreeChangeListenerBase<Interf
     }
 
     @Override
-    protected void add(InstanceIdentifier<Interface> key, Interface port) {
+    public void add(InstanceIdentifier<Interface> key, Interface port) {
         LOG.trace("Received AclInterface add event, port={}", port);
-        InterfaceAcl aclInPort = port.getAugmentation(InterfaceAcl.class);
+        InterfaceAcl aclInPort = port.augmentation(InterfaceAcl.class);
         if (aclInPort != null && aclInPort.isPortSecurityEnabled()) {
             String interfaceId = port.getName();
             AclInterface aclInterface = addOrUpdateAclInterfaceCache(interfaceId, aclInPort);
 
-            if (aclClusterUtil.isEntityOwner()) {
-                LOG.debug("On add event, notify ACL service manager to bind ACL for interface: {}", port);
+            // if interface state event comes first followed by interface config event.
+            if (aclInterface.getDpId() != null && aclInterface.getElanId() != null && aclClusterUtil.isEntityOwner()) {
+                LOG.debug("On add event, notify ACL bind/add for interface: {}", interfaceId);
                 aclServiceManager.notify(aclInterface, null, Action.BIND);
+                aclServiceManager.notify(aclInterface, null, Action.ADD);
             }
         }
     }