Using MD-SAL .exists() API
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepSouthboundProvider.java
index 9956f4ef6eab46826fe9f87bc8d74c04d1ef2a45..74cd4a5ee8e21c9c93bcd3f33df8d651cba306f6 100644 (file)
@@ -7,33 +7,39 @@
  */
 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 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.clustering.CandidateAlreadyRegisteredException;
-import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+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.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;
+import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
+import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
+import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
+import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
+import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
 import org.opendaylight.ovsdb.hwvtepsouthbound.reconciliation.configuration.HwvtepReconciliationManager;
 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;
@@ -43,16 +49,14 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+@Singleton
+@Service(classes = HwvtepSouthboundProvider.class) // only because HwvtepCacheDisplayCmd needs a @Reference to this
 public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener<Topology>, AutoCloseable {
 
     private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundProvider.class);
     private static final String ENTITY_TYPE = "ovsdb-hwvtepsouthbound-provider";
 
-    public static DataBroker getDb() {
-        return db;
-    }
-
-    private static DataBroker db;
+    private final DataBroker dataBroker;
     private final EntityOwnershipService entityOwnershipService;
     private final OvsdbConnection ovsdbConnection;
 
@@ -62,18 +66,23 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
     private HwvtepsbPluginInstanceEntityOwnershipListener providerOwnershipChangeListener;
     private HwvtepDataChangeListener hwvtepDTListener;
     private HwvtepReconciliationManager hwvtepReconciliationManager;
-    private AtomicBoolean registered = new AtomicBoolean(false);
+    private final AtomicBoolean registered = new AtomicBoolean(false);
     private ListenerRegistration<HwvtepSouthboundProvider> operTopologyRegistration;
+    private int shardStatusCheckRetryCount = 1000;
+    private UpgradeState upgradeState;
 
-    public HwvtepSouthboundProvider(final DataBroker dataBroker,
-            final EntityOwnershipService entityOwnershipServiceDependency,
-            final OvsdbConnection ovsdbConnection,
-            final DOMSchemaService schemaService,
-            final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer) {
-        this.db = dataBroker;
+    @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 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);
@@ -82,12 +91,33 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
     /**
      * Used by blueprint when starting the container.
      */
+    @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(db);
-        cm = new HwvtepConnectionManager(db, txInvoker, entityOwnershipService);
-        hwvtepDTListener = new HwvtepDataChangeListener(db, cm);
-        hwvtepReconciliationManager = new HwvtepReconciliationManager(db, cm);
+        txInvoker = new TransactionInvokerImpl(dataBroker);
+        cm = new HwvtepConnectionManager(dataBroker, txInvoker, entityOwnershipService, ovsdbConnection);
+        registerConfigListenerPostUpgrade();
         //Register listener for entityOnwership changes
         providerOwnershipChangeListener =
                 new HwvtepsbPluginInstanceEntityOwnershipListener(this,this.entityOwnershipService);
@@ -95,7 +125,6 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
         //register instance entity to get the ownership of the provider
         Entity instanceEntity = new Entity(ENTITY_TYPE, ENTITY_TYPE);
         try {
-            Optional<EntityOwnershipState> ownershipStateOpt = entityOwnershipService.getOwnershipState(instanceEntity);
             registration = entityOwnershipService.registerCandidate(instanceEntity);
         } catch (CandidateAlreadyRegisteredException e) {
             LOG.warn("HWVTEP Southbound Provider instance entity {} was already "
@@ -105,13 +134,32 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
                 .create(NetworkTopology.class)
                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID));
         DataTreeIdentifier<Topology> treeId =
-                new DataTreeIdentifier<Topology>(LogicalDatastoreType.OPERATIONAL, path);
+                DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, path);
 
         LOG.trace("Registering listener for path {}", treeId);
-        operTopologyRegistration = db.registerDataTreeChangeListener(treeId, this);
+        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
+    @PreDestroy
+    @SuppressWarnings("checkstyle:IllegalCatch")
     public void close() throws Exception {
         LOG.info("HwvtepSouthboundProvider Closed");
         if (txInvoker != null) {
@@ -122,19 +170,19 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
                 LOG.error("HWVTEP Southbound Provider failed to close TransactionInvoker", e);
             }
         }
-        if(cm != null){
+        if (cm != null) {
             cm.close();
             cm = null;
         }
-        if(registration != null) {
+        if (registration != null) {
             registration.close();
             registration = null;
         }
-        if(providerOwnershipChangeListener != null) {
+        if (providerOwnershipChangeListener != null) {
             providerOwnershipChangeListener.close();
             providerOwnershipChangeListener = null;
         }
-        if(hwvtepDTListener != null) {
+        if (hwvtepDTListener != null) {
             hwvtepDTListener.close();
             hwvtepDTListener = null;
         }
@@ -144,31 +192,31 @@ 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 = db.newReadWriteTransaction();
-        CheckedFuture<Optional<Topology>, ReadFailedException> hwvtepTp = transaction.read(type, path);
+        ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
+        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();
             }
-        } catch (Exception e) {
+        } catch (InterruptedException | ExecutionException e) {
             LOG.error("Error initializing hwvtep topology", e);
         }
     }
 
-    public void handleOwnershipChange(EntityOwnershipChange ownershipChange) {
-        if (ownershipChange.isOwner()) {
+    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"
-                    ,HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
+            LOG.info("Initialize HWVTEP topology {} in operational and config data store if not already present",
+                    HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
             initializeHwvtepTopology(LogicalDatastoreType.OPERATIONAL);
             initializeHwvtepTopology(LogicalDatastoreType.CONFIGURATION);
         } else {
@@ -178,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();
     }
 
-    private class HwvtepsbPluginInstanceEntityOwnershipListener implements EntityOwnershipListener {
-        private HwvtepSouthboundProvider hsp;
-        private EntityOwnershipListenerRegistration listenerRegistration;
+    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);
         }
@@ -206,8 +262,9 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
         public void close() {
             this.listenerRegistration.close();
         }
+
         @Override
-        public void ownershipChanged(EntityOwnershipChange ownershipChange) {
+        public void ownershipChanged(final EntityOwnershipChange ownershipChange) {
             hsp.handleOwnershipChange(ownershipChange);
         }
     }