Switch to JDT annotations for Nullable and NonNull
[netvirt.git] / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / listeners / AclNodeListener.java
index fd8233a70ab73e1c0d5d7f4f1bc957a9614818be..cf02d2d241678ef945c457f2d0203e399be7766b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
+ * Copyright (c) 2016, 2018 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -8,21 +8,27 @@
 
 package org.opendaylight.netvirt.aclservice.listeners;
 
+import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
+
 import java.math.BigInteger;
 import java.util.Collections;
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
 import javax.inject.Singleton;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
 import org.opendaylight.netvirt.aclservice.utils.AclNodeDefaultFlowsTxBuilder;
 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
+import org.opendaylight.serviceutils.srm.RecoverableListener;
+import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
@@ -38,28 +44,34 @@ import org.slf4j.LoggerFactory;
  * during when node is discovered.
  */
 @Singleton
-public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapableNode, AclNodeListener> {
+public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapableNode, AclNodeListener>
+        implements RecoverableListener {
 
     private static final Logger LOG = LoggerFactory.getLogger(AclNodeListener.class);
 
     private final IMdsalApiManager mdsalManager;
     private final AclserviceConfig config;
     private final DataBroker dataBroker;
+    private final ManagedNewTransactionRunner txRunner;
     private final AclServiceUtils aclServiceUtils;
     private final JobCoordinator jobCoordinator;
 
+    @Nullable
     private SecurityGroupMode securityGroupMode = null;
 
     @Inject
     public AclNodeListener(final IMdsalApiManager mdsalManager, DataBroker dataBroker, AclserviceConfig config,
-            AclServiceUtils aclServiceUtils, JobCoordinator jobCoordinator) {
+            AclServiceUtils aclServiceUtils, JobCoordinator jobCoordinator,
+            ServiceRecoveryRegistry serviceRecoveryRegistry) {
         super(FlowCapableNode.class, AclNodeListener.class);
 
         this.mdsalManager = mdsalManager;
         this.dataBroker = dataBroker;
+        this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
         this.config = config;
         this.aclServiceUtils = aclServiceUtils;
         this.jobCoordinator = jobCoordinator;
+        serviceRecoveryRegistry.addRecoverableListener(AclServiceUtils.getRecoverServiceRegistryKey(), this);
     }
 
     @Override
@@ -69,9 +81,20 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
         if (config != null) {
             this.securityGroupMode = config.getSecurityGroupMode();
         }
+        registerListener();
+        LOG.info("AclserviceConfig: {}", this.config);
+    }
+
+    @Override
+    public void registerListener() {
         this.aclServiceUtils.createRemoteAclIdPool();
         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
-        LOG.info("AclserviceConfig: {}", this.config);
+    }
+
+    @Override
+    public  void deregisterListener() {
+        super.deregisterListener();
+        this.aclServiceUtils.deleteRemoteAclIdPool();
     }
 
     @Override
@@ -107,13 +130,12 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
                     dpId);
             return;
         }
-        jobCoordinator.enqueueJob(String.valueOf(dpId), () -> {
-            WriteTransaction tx = this.dataBroker.newWriteOnlyTransaction();
-            new AclNodeDefaultFlowsTxBuilder(dpId, mdsalManager, config, tx).build();
+        jobCoordinator.enqueueJob(String.valueOf(dpId),
+            () -> Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
+                new AclNodeDefaultFlowsTxBuilder(dpId, mdsalManager, config, tx).build();
 
-            LOG.info("Adding default ACL flows for dpId={}", dpId);
-            return Collections.singletonList(tx.submit());
-        }, AclConstants.JOB_MAX_RETRIES);
+                LOG.info("Adding default ACL flows for dpId={}", dpId);
+            })), AclConstants.JOB_MAX_RETRIES);
 
         LOG.trace("FlowCapableNode (dpid: {}) add event is processed.", dpId);
     }