NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / dhcpservice / impl / src / main / java / org / opendaylight / netvirt / dhcpservice / DhcpSubnetListener.java
index c69337a1afcb851c09a16a74f3845029a428a1ab..c371cd0e89a2536509a6d3a5ede7e9333f1f805c 100644 (file)
@@ -8,24 +8,25 @@
 
 package org.opendaylight.netvirt.dhcpservice;
 
-import com.google.common.base.Optional;
-import java.math.BigInteger;
+import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
+
 import java.util.List;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.concurrent.ExecutionException;
-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.binding.api.ReadOnlyTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
 import org.opendaylight.genius.mdsalutil.NwConstants;
+import org.opendaylight.infrautils.utils.concurrent.Executors;
 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.ReadTransaction;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.serviceutils.tools.listener.AbstractClusteredAsyncDataTreeChangeListener;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
@@ -41,11 +42,12 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.s
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.config.rev150710.DhcpserviceConfig;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Singleton
-public class DhcpSubnetListener extends AsyncClusteredDataTreeChangeListenerBase<Subnet, DhcpSubnetListener> {
+public class DhcpSubnetListener extends AbstractClusteredAsyncDataTreeChangeListener<Subnet> {
     private static final Logger LOG = LoggerFactory.getLogger(DhcpSubnetListener.class);
 
     private final DataBroker dataBroker;
@@ -57,38 +59,37 @@ public class DhcpSubnetListener extends AsyncClusteredDataTreeChangeListenerBase
     @Inject
     public DhcpSubnetListener(final DhcpManager dhcpManager, final DhcpExternalTunnelManager
             dhcpExternalTunnelManager, final DataBroker broker, final DhcpserviceConfig config) {
-        super(Subnet.class, DhcpSubnetListener.class);
+        super(broker, LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Neutron.class).child(Subnets.class)
+                .child(Subnet.class), Executors.newListeningSingleThreadExecutor("DhcpSubnetListener", LOG));
         this.dhcpManager = dhcpManager;
         this.dataBroker = broker;
         this.txRunner = new ManagedNewTransactionRunnerImpl(broker);
         this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
         this.config = config;
+        init();
     }
 
-    @PostConstruct
     public void init() {
         if (config.isControllerDhcpEnabled()) {
-            registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
+            LOG.info("{} init", getClass().getSimpleName());
         }
     }
 
     @Override
-    protected void add(InstanceIdentifier<Subnet> identifier, Subnet add) {
+    public void add(InstanceIdentifier<Subnet> identifier, Subnet add) {
 
     }
 
     @Override
-    protected void remove(InstanceIdentifier<Subnet> identifier, Subnet del) {
-
-    }
+    public void remove(InstanceIdentifier<Subnet> identifier, Subnet del) {
 
-    @Override
-    protected InstanceIdentifier<Subnet> getWildCardPath() {
-        return InstanceIdentifier.create(Neutron.class).child(Subnets.class).child(Subnet.class);
     }
 
     @Override
-    protected void update(InstanceIdentifier<Subnet> identifier, Subnet original, Subnet update) {
+    public void update(InstanceIdentifier<Subnet> identifier, Subnet original, Subnet update) {
+        if (!config.isControllerDhcpEnabled()) {
+            return;
+        }
         LOG.trace("DhcpSubnetListener Update : Original dhcpstatus: {}, Updated dhcpstatus {}", original.isEnableDhcp(),
                 update.isEnableDhcp());
 
@@ -124,18 +125,18 @@ public class DhcpSubnetListener extends AsyncClusteredDataTreeChangeListenerBase
         LOG.trace("DhcpSubnetListener installNeutronPortEntries : portList: {}", portList);
         for (Uuid portIntf : portList) {
             NodeConnectorId nodeConnectorId = getNodeConnectorIdForPortIntf(portIntf);
-            BigInteger dpId = BigInteger.valueOf(MDSALUtil.getDpnIdFromPortName(nodeConnectorId));
+            Uint64 dpId = DhcpServiceUtils.getDpnIdFromNodeConnectorId(nodeConnectorId);
             String interfaceName = portIntf.getValue();
             Port port = dhcpManager.getNeutronPort(interfaceName);
             String vmMacAddress = port.getMacAddress().getValue();
             //check whether any changes have happened
             LOG.trace("DhcpSubnetListener installNeutronPortEntries dpId: {} vmMacAddress : {}", dpId, vmMacAddress);
             //Bind the dhcp service when enabled
-            ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
+            ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
                 tx -> DhcpServiceUtils.bindDhcpService(interfaceName, NwConstants.DHCP_TABLE, tx)), LOG,
                 "Error writing to the datastore");
             //install the entries
-            ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
+            ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
                 tx -> dhcpManager.installDhcpEntries(dpId, vmMacAddress, tx)), LOG,
                 "Error writing to the datastore");
         }
@@ -145,7 +146,7 @@ public class DhcpSubnetListener extends AsyncClusteredDataTreeChangeListenerBase
         LOG.trace("DhcpSubnetListener uninstallNeutronPortEntries : portList: {}", portList);
         for (Uuid portIntf : portList) {
             NodeConnectorId nodeConnectorId = getNodeConnectorIdForPortIntf(portIntf);
-            BigInteger dpId = BigInteger.valueOf(MDSALUtil.getDpnIdFromPortName(nodeConnectorId));
+            Uint64 dpId = DhcpServiceUtils.getDpnIdFromNodeConnectorId(nodeConnectorId);
             String interfaceName = portIntf.getValue();
             Port port = dhcpManager.getNeutronPort(interfaceName);
             String vmMacAddress = port.getMacAddress().getValue();
@@ -153,12 +154,12 @@ public class DhcpSubnetListener extends AsyncClusteredDataTreeChangeListenerBase
             LOG.trace("DhcpSubnetListener uninstallNeutronPortEntries dpId: {} vmMacAddress : {}",
                     dpId, vmMacAddress);
             //Unbind the dhcp service when disabled
-            ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
+            ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
                 tx -> DhcpServiceUtils.unbindDhcpService(interfaceName, tx)), LOG,
                 "Error writing to the datastore");
 
             //uninstall the entries
-            ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
+            ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
                 tx -> dhcpManager.unInstallDhcpEntries(dpId, vmMacAddress, tx)), LOG,
                 "Error writing to the datastore");
         }
@@ -171,16 +172,16 @@ public class DhcpSubnetListener extends AsyncClusteredDataTreeChangeListenerBase
             String vmMacAddress = port.getMacAddress().getValue();
             Uuid networkId = port.getNetworkId();
             //install the entries on designated dpnId
-            List<BigInteger> listOfDpns = DhcpServiceUtils.getListOfDpns(dataBroker);
+            List<Uint64> listOfDpns = DhcpServiceUtils.getListOfDpns(dataBroker);
             IpAddress tunnelIp = dhcpExternalTunnelManager.getTunnelIpBasedOnElan(networkId.getValue(), vmMacAddress);
             if (null == tunnelIp) {
                 LOG.warn("DhcpSubnetListener installDirectPortEntries tunnelIP is null for  port {}", portIntf);
                 continue;
             }
-            BigInteger designatedDpnId =
+            Uint64 designatedDpnId =
                     dhcpExternalTunnelManager.readDesignatedSwitchesForExternalTunnel(tunnelIp, networkId.getValue());
             LOG.trace("CR-DHCP DhcpSubnetListener update Install DIRECT vmMacAddress: {} tunnelIp: {} "
-                    + "designatedDpnId : {} ListOf Dpn:",
+                    + "designatedDpnId : {} ListOf Dpn: {}",
                     vmMacAddress, tunnelIp, designatedDpnId, listOfDpns);
             dhcpExternalTunnelManager.installDhcpFlowsForVms(tunnelIp, networkId.getValue(), listOfDpns,
                     designatedDpnId, vmMacAddress);
@@ -194,7 +195,7 @@ public class DhcpSubnetListener extends AsyncClusteredDataTreeChangeListenerBase
             Port port = dhcpManager.getNeutronPort(portIntf.getValue());
             String vmMacAddress = port.getMacAddress().getValue();
             Uuid networkId = port.getNetworkId();
-            List<BigInteger> listOfDpns = DhcpServiceUtils.getListOfDpns(dataBroker);
+            List<Uint64> listOfDpns = DhcpServiceUtils.getListOfDpns(dataBroker);
             LOG.trace("DhcpSubnetListener uninstallDirectPortEntries  vmMacAddress: {} networkId: {} ListOf Dpn: {}",
                     vmMacAddress, networkId, listOfDpns);
             dhcpExternalTunnelManager.unInstallDhcpFlowsForVms(networkId.getValue(), listOfDpns, vmMacAddress);
@@ -232,19 +233,21 @@ public class DhcpSubnetListener extends AsyncClusteredDataTreeChangeListenerBase
         SubnetmapBuilder builder = null ;
         InstanceIdentifier<Subnetmap> id = InstanceIdentifier.builder(Subnetmaps.class)
                 .child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
-        ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
+        ReadTransaction tx = broker.newReadOnlyTransaction();
 
         Optional<Subnetmap> sn ;
         try {
             sn = tx.read(LogicalDatastoreType.CONFIGURATION, id).get();
         } catch (InterruptedException | ExecutionException e) {
             throw new RuntimeException(e);
+        } finally {
+            tx.close();
         }
 
         if (sn.isPresent()) {
             builder = new SubnetmapBuilder(sn.get());
         } else {
-            builder = new SubnetmapBuilder().setKey(new SubnetmapKey(subnetId)).setId(subnetId);
+            builder = new SubnetmapBuilder().withKey(new SubnetmapKey(subnetId)).setId(subnetId);
         }
         return builder;
     }
@@ -253,11 +256,7 @@ public class DhcpSubnetListener extends AsyncClusteredDataTreeChangeListenerBase
     @PreDestroy
     public void close() {
         super.close();
+        Executors.shutdownAndAwaitTermination(getExecutorService());
         LOG.info("DhcpSubnetListener Closed");
     }
-
-    @Override
-    protected DhcpSubnetListener getDataTreeChangeListener() {
-        return DhcpSubnetListener.this;
-    }
 }