Remove GENIUS UTIL references in Elanmanager Module
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanInstanceManager.java
index e50f163c845d69701d9a7de06165bf78db3941d5..c4feb97f0485cc5fbbea9f010c99167208ea06f2 100644 (file)
@@ -8,48 +8,41 @@
 
 package org.opendaylight.netvirt.elan.internal;
 
-import static java.util.Collections.emptyList;
+import static org.opendaylight.mdsal.binding.util.Datastore.CONFIGURATION;
+import static org.opendaylight.mdsal.binding.util.Datastore.OPERATIONAL;
 
-import com.google.common.base.Optional;
-import com.google.common.util.concurrent.ListenableFuture;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.List;
-import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Singleton;
-
-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.infra.ManagedNewTransactionRunner;
-import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
-import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
-import org.opendaylight.genius.mdsalutil.MDSALUtil;
 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
-import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
-import org.opendaylight.netvirt.elan.ElanException;
+import org.opendaylight.infrautils.utils.concurrent.Executors;
+import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunner;
+import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunnerImpl;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.netvirt.elan.cache.ElanInterfaceCache;
 import org.opendaylight.netvirt.elan.utils.ElanConstants;
 import org.opendaylight.netvirt.elan.utils.ElanUtils;
+import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstance;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint32;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Singleton
-public class ElanInstanceManager extends AsyncDataTreeChangeListenerBase<ElanInstance, ElanInstanceManager> {
+public class ElanInstanceManager extends AbstractAsyncDataTreeChangeListener<ElanInstance> {
 
     private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceManager.class);
+    private static final Logger EVENT_LOGGER = LoggerFactory.getLogger("NetvirtEventLogger");
 
     private final DataBroker broker;
     private final ManagedNewTransactionRunner txRunner;
@@ -64,7 +57,9 @@ public class ElanInstanceManager extends AsyncDataTreeChangeListenerBase<ElanIns
                                final ElanInterfaceManager elanInterfaceManager,
                                final IInterfaceManager interfaceManager, final JobCoordinator jobCoordinator,
                                final ElanInterfaceCache elanInterfaceCache) {
-        super(ElanInstance.class, ElanInstanceManager.class);
+        super(dataBroker, LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(ElanInstances.class)
+                .child(ElanInstance.class),
+                Executors.newListeningSingleThreadExecutor("ElanInstanceManager", LOG));
         this.broker = dataBroker;
         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
         this.idManager = managerService;
@@ -74,55 +69,20 @@ public class ElanInstanceManager extends AsyncDataTreeChangeListenerBase<ElanIns
         this.elanInterfaceCache = elanInterfaceCache;
     }
 
-    @Override
-    @PostConstruct
     public void init() {
-        registerListener(LogicalDatastoreType.CONFIGURATION, broker);
+        LOG.info("{} registered", getClass().getSimpleName());
     }
 
     @Override
-    protected void remove(InstanceIdentifier<ElanInstance> identifier, ElanInstance deletedElan) {
+    public void remove(InstanceIdentifier<ElanInstance> identifier, ElanInstance deletedElan) {
         LOG.trace("Remove ElanInstance - Key: {}, value: {}", identifier, deletedElan);
         String elanName = deletedElan.getElanInstanceName();
-        // check the elan Instance present in the Operational DataStore
-        Elan existingElan = ElanUtils.getElanByName(broker, elanName);
-        long elanTag = deletedElan.getElanTag();
-        // Cleaning up the existing Elan Instance
-        if (existingElan != null) {
-            List<String> elanInterfaces = existingElan.getElanInterfaces();
-            if (elanInterfaces != null && !elanInterfaces.isEmpty()) {
-                List<ListenableFuture<Void>> futureList = new ArrayList<>();
-                elanInterfaces.forEach(elanInterfaceName -> {
-                    jobCoordinator.enqueueJob(ElanUtils.getElanInterfaceJobKey(elanInterfaceName), () -> {
-                        InstanceIdentifier<ElanInterface> elanInterfaceId = ElanUtils
-                                .getElanInterfaceConfigurationDataPathId(elanInterfaceName);
-                        InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(elanInterfaceName);
-                        futureList.addAll(elanInterfaceManager.removeElanInterface(deletedElan, elanInterfaceName,
-                                interfaceInfo));
-                        ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
-                                elanInterfaceId);
-                        return futureList;
-                    },ElanConstants.JOB_MAX_RETRIES);
-                });
-            }
-            ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL,
-                    ElanUtils.getElanInstanceOperationalDataPath(elanName));
-            Optional<ElanDpnInterfacesList> elanDpnInterfaceList = MDSALUtil.read(broker,
-                    LogicalDatastoreType.OPERATIONAL,
-                    ElanUtils.getElanDpnOperationDataPath(elanName));
-            if (elanDpnInterfaceList.isPresent()) {
-                ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL,
-                        getElanDpnOperationDataPath(elanName));
-            }
-            ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL,
-                    ElanUtils.getElanInfoEntriesOperationalDataPath(elanTag));
-        }
+        EVENT_LOGGER.debug("ELAN-Instance, REMOVE {}",elanName);
         elanInterfaceCache.getInterfaceNames(elanName).forEach(
             elanInterfaceName -> jobCoordinator.enqueueJob(ElanUtils.getElanInterfaceJobKey(elanInterfaceName),
-                () -> Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
+                () -> Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
                     LOG.info("Deleting the elanInterface present under ConfigDS:{}", elanInterfaceName);
-                    ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
-                            ElanUtils.getElanInterfaceConfigurationDataPathId(elanInterfaceName));
+                    tx.delete(ElanUtils.getElanInterfaceConfigurationDataPathId(elanInterfaceName));
                     elanInterfaceManager.unbindService(elanInterfaceName, tx);
                     LOG.info("unbind the Interface:{} service bounded to Elan:{}", elanInterfaceName, elanName);
                 })), ElanConstants.JOB_MAX_RETRIES));
@@ -144,51 +104,45 @@ public class ElanInstanceManager extends AsyncDataTreeChangeListenerBase<ElanIns
     }
 
     @Override
-    protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
-        Long existingElanTag = original.getElanTag();
+    public void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
+        EVENT_LOGGER.debug("ELAN-Instance, UPDATE {}", original.getElanInstanceName());
+        Uint32 existingElanTag = original.getElanTag();
         String elanName = update.getElanInstanceName();
         if (existingElanTag == null || !existingElanTag.equals(update.getElanTag())) {
-            if (update.getElanTag() == null  || update.getElanTag() == 0L) {
+            if (update.getElanTag() == null  || update.getElanTag().longValue() == 0L) {
                 // update the elan-Instance with new properties
-                ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
-                    tx -> ElanUtils.updateOperationalDataStore(idManager, update, new ArrayList<>(), tx)),
-                    LOG, "Error updating ELAN tag in ELAN instance");
+                LoggingFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
+                    operTx -> LoggingFutures.addErrorLogging(
+                        txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
+                            confTx -> ElanUtils.updateOperationalDataStore(idManager, update, new ArrayList<>(), confTx,
+                                operTx)), LOG, "Error updating ELAN tag in ELAN instance for config "
+                                    + "transaction")), LOG,
+                    "Error updating ELAN tag in ELAN instance for operational transaction");
             } else {
-                jobCoordinator.enqueueJob(elanName, () -> {
-                    try {
-                        return elanInterfaceManager.handleunprocessedElanInterfaces(update);
-                    } catch (ElanException e) {
-                        LOG.error("update() failed for ElanInstance: {}", identifier.toString(), e);
-                        return emptyList();
-                    }
-                }, ElanConstants.JOB_MAX_RETRIES);
+                jobCoordinator.enqueueJob(elanName, () -> elanInterfaceManager.handleunprocessedElanInterfaces(update),
+                    ElanConstants.JOB_MAX_RETRIES);
             }
         }
     }
 
     @Override
-    protected void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance elanInstanceAdded) {
-        ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
+    public void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance elanInstanceAdded) {
+        LoggingFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, operTx -> {
             String elanInstanceName  = elanInstanceAdded.getElanInstanceName();
-            Elan elanInfo = ElanUtils.getElanByName(tx, elanInstanceName);
+            EVENT_LOGGER.debug("ELAN-Instance, ADD {}", elanInstanceName);
+            Elan elanInfo = ElanUtils.getElanByName(operTx, elanInstanceName);
             if (elanInfo == null) {
-                ElanUtils.updateOperationalDataStore(idManager, elanInstanceAdded, new ArrayList<>(), tx);
+                LoggingFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
+                    confTx -> ElanUtils.updateOperationalDataStore(idManager, elanInstanceAdded, new ArrayList<>(),
+                        confTx, operTx)), LOG, "Error adding an ELAN instance for config transaction");
             }
-        }), LOG, "Error adding an ELAN instance");
-    }
-
-    private static InstanceIdentifier<ElanDpnInterfacesList> getElanDpnOperationDataPath(String elanInstanceName) {
-        return InstanceIdentifier.builder(ElanDpnInterfaces.class)
-                .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
-    }
-
-    @Override
-    protected InstanceIdentifier<ElanInstance> getWildCardPath() {
-        return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
+        }), LOG, "Error adding an ELAN instance for operational transaction");
     }
 
     @Override
-    protected ElanInstanceManager getDataTreeChangeListener() {
-        return this;
+    @PreDestroy
+    public void close() {
+        super.close();
+        Executors.shutdownAndAwaitTermination(getExecutorService());
     }
 }