Using MD-SAL .exists() API
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepSouthboundProvider.java
index b8dd802751359b84fd2a57e36a7a4f3547358404..74cd4a5ee8e21c9c93bcd3f33df8d651cba306f6 100644 (file)
@@ -7,10 +7,10 @@
  */
 package org.opendaylight.ovsdb.hwvtepsouthbound;
 
-import com.google.common.base.Optional;
-import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.FluentFuture;
 import java.util.Collection;
 import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
@@ -18,14 +18,13 @@ import javax.inject.Inject;
 import javax.inject.Singleton;
 import org.apache.aries.blueprint.annotation.service.Reference;
 import org.apache.aries.blueprint.annotation.service.Service;
-import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
-import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
+import org.opendaylight.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.mdsal.eos.binding.api.Entity;
 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
@@ -38,6 +37,9 @@ import org.opendaylight.ovsdb.hwvtepsouthbound.reconciliation.configuration.Hwvt
 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.TransactionInvoker;
 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.TransactionInvokerImpl;
 import org.opendaylight.ovsdb.lib.OvsdbConnection;
+import org.opendaylight.ovsdb.utils.mdsal.utils.Scheduler;
+import org.opendaylight.ovsdb.utils.mdsal.utils.ShardStatusMonitor;
+import org.opendaylight.serviceutils.upgrade.UpgradeState;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
@@ -66,17 +68,21 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
     private HwvtepReconciliationManager hwvtepReconciliationManager;
     private final AtomicBoolean registered = new AtomicBoolean(false);
     private ListenerRegistration<HwvtepSouthboundProvider> operTopologyRegistration;
+    private int shardStatusCheckRetryCount = 1000;
+    private UpgradeState upgradeState;
 
     @Inject
     public HwvtepSouthboundProvider(@Reference final DataBroker dataBroker,
             @Reference final EntityOwnershipService entityOwnershipServiceDependency,
             @Reference final OvsdbConnection ovsdbConnection,
             @Reference final DOMSchemaService schemaService,
-            @Reference final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer) {
+            @Reference final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer,
+            @Reference final UpgradeState upgradeState) {
         this.dataBroker = dataBroker;
         this.entityOwnershipService = entityOwnershipServiceDependency;
         registration = null;
         this.ovsdbConnection = ovsdbConnection;
+        this.upgradeState = upgradeState;
         HwvtepSouthboundUtil.setInstanceIdentifierCodec(new InstanceIdentifierCodec(schemaService,
                 bindingNormalizedNodeSerializer));
         LOG.info("HwvtepSouthboundProvider ovsdbConnectionService: {}", ovsdbConnection);
@@ -87,11 +93,31 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
      */
     @PostConstruct
     public void init() {
+        boolean isDatastoreAvailable = false;
+        int retryCount = 0;
+        try {
+            while (retryCount < shardStatusCheckRetryCount) {
+                isDatastoreAvailable = ShardStatusMonitor.getShardStatus(ShardStatusMonitor.TOPOLOGY_SHARDS);
+                if (isDatastoreAvailable) {
+                    break;
+                }
+                LOG.warn("Hwvtep: retrying shard status check for the {} time", ++retryCount);
+                Thread.sleep(2000);
+            }
+            if (isDatastoreAvailable) {
+                LOG.info("Hwvtep is UP");
+                init2();
+            }
+        } catch (InterruptedException e) {
+            LOG.error("Error in intializing the Hwvtep Southbound ", e);
+        }
+    }
+
+    private void init2() {
         LOG.info("HwvtepSouthboundProvider Session Initiated");
         txInvoker = new TransactionInvokerImpl(dataBroker);
         cm = new HwvtepConnectionManager(dataBroker, txInvoker, entityOwnershipService, ovsdbConnection);
-        hwvtepDTListener = new HwvtepDataChangeListener(dataBroker, cm);
-        hwvtepReconciliationManager = new HwvtepReconciliationManager(dataBroker, cm);
+        registerConfigListenerPostUpgrade();
         //Register listener for entityOnwership changes
         providerOwnershipChangeListener =
                 new HwvtepsbPluginInstanceEntityOwnershipListener(this,this.entityOwnershipService);
@@ -108,10 +134,27 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
                 .create(NetworkTopology.class)
                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID));
         DataTreeIdentifier<Topology> treeId =
-                new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, path);
+                DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, path);
 
         LOG.trace("Registering listener for path {}", treeId);
         operTopologyRegistration = dataBroker.registerDataTreeChangeListener(treeId, this);
+        Scheduler.getScheduledExecutorService().schedule(() -> {
+            if (!registered.get()) {
+                openOvsdbPort();
+                LOG.error("Timed out to get eos notification opening the port now");
+            }
+        }, HwvtepSouthboundConstants.PORT_OPEN_MAX_DELAY_IN_MINS, TimeUnit.MINUTES);
+    }
+
+    private void registerConfigListenerPostUpgrade() {
+        if (upgradeState.isUpgradeInProgress()) {
+            LOG.error("Upgrade is in progress delay config data change listener registration");
+            Scheduler.getScheduledExecutorService().schedule(() -> registerConfigListenerPostUpgrade(),
+                    60, TimeUnit.SECONDS);
+            return;
+        }
+        hwvtepDTListener = new HwvtepDataChangeListener(dataBroker, cm);
+        hwvtepReconciliationManager = new HwvtepReconciliationManager(dataBroker, cm);
     }
 
     @Override
@@ -149,18 +192,18 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
         }
     }
 
-    private void initializeHwvtepTopology(LogicalDatastoreType type) {
+    private void initializeHwvtepTopology(final LogicalDatastoreType type) {
         InstanceIdentifier<Topology> path = InstanceIdentifier
                 .create(NetworkTopology.class)
                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID));
         ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
-        CheckedFuture<Optional<Topology>, ReadFailedException> hwvtepTp = transaction.read(type, path);
+        FluentFuture<Boolean> hwvtepTp = transaction.exists(type, path);
         try {
-            if (!hwvtepTp.get().isPresent()) {
+            if (!hwvtepTp.get().booleanValue()) {
                 TopologyBuilder tpb = new TopologyBuilder();
                 tpb.setTopologyId(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
-                transaction.put(type, path, tpb.build(), true);
-                transaction.submit();
+                transaction.mergeParentStructurePut(type, path, tpb.build());
+                transaction.commit();
             } else {
                 transaction.cancel();
             }
@@ -169,7 +212,7 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
         }
     }
 
-    public void handleOwnershipChange(EntityOwnershipChange ownershipChange) {
+    public void handleOwnershipChange(final EntityOwnershipChange ownershipChange) {
         if (ownershipChange.getState().isOwner()) {
             LOG.info("*This* instance of HWVTEP southbound provider is set as a MASTER instance");
             LOG.info("Initialize HWVTEP topology {} in operational and config data store if not already present",
@@ -183,27 +226,35 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
 
 
     @Override
-    public void onDataTreeChanged(Collection<DataTreeModification<Topology>> collection) {
+    public void onDataTreeChanged(final Collection<DataTreeModification<Topology>> collection) {
+        openOvsdbPort();
+
+        if (operTopologyRegistration != null) {
+            operTopologyRegistration.close();
+            operTopologyRegistration = null;
+        }
+    }
+
+
+
+    private void openOvsdbPort() {
         if (!registered.getAndSet(true)) {
             LOG.info("Starting the ovsdb port");
             ovsdbConnection.registerConnectionListener(cm);
             ovsdbConnection.startOvsdbManager();
         }
-        //mdsal registration/deregistration in mdsal update callback should be avoided
-        new Thread(() -> {
-            if (operTopologyRegistration != null) {
-                operTopologyRegistration.close();
-                operTopologyRegistration = null;
-            }
-        }).start();
+    }
+
+    public void setShardStatusCheckRetryCount(int retryCount) {
+        this.shardStatusCheckRetryCount = retryCount;
     }
 
     private static class HwvtepsbPluginInstanceEntityOwnershipListener implements EntityOwnershipListener {
         private final HwvtepSouthboundProvider hsp;
         private final EntityOwnershipListenerRegistration listenerRegistration;
 
-        HwvtepsbPluginInstanceEntityOwnershipListener(HwvtepSouthboundProvider hsp,
-                EntityOwnershipService entityOwnershipService) {
+        HwvtepsbPluginInstanceEntityOwnershipListener(final HwvtepSouthboundProvider hsp,
+                final EntityOwnershipService entityOwnershipService) {
             this.hsp = hsp;
             listenerRegistration = entityOwnershipService.registerListener(ENTITY_TYPE, this);
         }
@@ -213,7 +264,7 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
         }
 
         @Override
-        public void ownershipChanged(EntityOwnershipChange ownershipChange) {
+        public void ownershipChanged(final EntityOwnershipChange ownershipChange) {
             hsp.handleOwnershipChange(ownershipChange);
         }
     }