Using MD-SAL .exists() API
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepSouthboundProvider.java
index d1a9d53b0f2f3c6aa619d704f234210bf82e485b..74cd4a5ee8e21c9c93bcd3f33df8d651cba306f6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright © 2015, 2017 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,
@@ -7,47 +7,56 @@
  */
 package org.opendaylight.ovsdb.hwvtepsouthbound;
 
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-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 org.opendaylight.controller.sal.core.api.model.SchemaService;
+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;
+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.MdsalUtils;
+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.NetworkTopologyBuilder;
 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;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Optional;
-import com.google.common.util.concurrent.CheckedFuture;
-
-public class HwvtepSouthboundProvider implements AutoCloseable {
+@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;
 
@@ -57,16 +66,23 @@ public class HwvtepSouthboundProvider implements AutoCloseable {
     private HwvtepsbPluginInstanceEntityOwnershipListener providerOwnershipChangeListener;
     private HwvtepDataChangeListener hwvtepDTListener;
     private HwvtepReconciliationManager hwvtepReconciliationManager;
+    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 SchemaService 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);
@@ -75,12 +91,33 @@ public class HwvtepSouthboundProvider implements AutoCloseable {
     /**
      * 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);
@@ -88,100 +125,136 @@ public class HwvtepSouthboundProvider implements AutoCloseable {
         //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);
-            if (ownershipStateOpt.isPresent()) {
-                EntityOwnershipState ownershipState = ownershipStateOpt.get();
-                if (ownershipState.hasOwner() && !ownershipState.isOwner()) {
-                    ovsdbConnection.registerConnectionListener(cm);
-                    ovsdbConnection.startOvsdbManager(HwvtepSouthboundConstants.DEFAULT_OVSDB_PORT);
-                }
-            }
         } catch (CandidateAlreadyRegisteredException e) {
             LOG.warn("HWVTEP Southbound Provider instance entity {} was already "
                     + "registered for ownership", instanceEntity, e);
         }
+        InstanceIdentifier<Topology> path = InstanceIdentifier
+                .create(NetworkTopology.class)
+                .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID));
+        DataTreeIdentifier<Topology> treeId =
+                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
+    @PreDestroy
+    @SuppressWarnings("checkstyle:IllegalCatch")
     public void close() throws Exception {
         LOG.info("HwvtepSouthboundProvider Closed");
-        if(cm != null){
+        if (txInvoker != null) {
+            try {
+                txInvoker.close();
+                txInvoker = null;
+            } catch (Exception e) {
+                LOG.error("HWVTEP Southbound Provider failed to close TransactionInvoker", e);
+            }
+        }
+        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;
         }
+        if (operTopologyRegistration != null) {
+            operTopologyRegistration.close();
+            operTopologyRegistration = null;
+        }
     }
 
-    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));
-        initializeTopology(type);
-        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());
-                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);
         }
     }
 
-    private void initializeTopology(LogicalDatastoreType type) {
-        ReadWriteTransaction transaction = db.newReadWriteTransaction();
-        InstanceIdentifier<NetworkTopology> path = InstanceIdentifier.create(NetworkTopology.class);
-        CheckedFuture<Optional<NetworkTopology>, ReadFailedException> topology = transaction.read(type,path);
-        try {
-            if (!topology.get().isPresent()) {
-                NetworkTopologyBuilder ntb = new NetworkTopologyBuilder();
-                transaction.put(type,path,ntb.build());
-                transaction.submit();
-            } else {
-                transaction.cancel();
-            }
-        } catch (Exception 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 {
             LOG.info("*This* instance of HWVTEP southbound provider is set as a SLAVE instance");
         }
-        ovsdbConnection.registerConnectionListener(cm);
-        ovsdbConnection.startOvsdbManager(HwvtepSouthboundConstants.DEFAULT_OVSDB_PORT);
     }
 
-    private class HwvtepsbPluginInstanceEntityOwnershipListener implements EntityOwnershipListener {
-        private HwvtepSouthboundProvider hsp;
-        private EntityOwnershipListenerRegistration listenerRegistration;
 
-        HwvtepsbPluginInstanceEntityOwnershipListener(HwvtepSouthboundProvider hsp,
-                EntityOwnershipService entityOwnershipService) {
+    @Override
+    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();
+        }
+    }
+
+    public void setShardStatusCheckRetryCount(int retryCount) {
+        this.shardStatusCheckRetryCount = retryCount;
+    }
+
+    private static class HwvtepsbPluginInstanceEntityOwnershipListener implements EntityOwnershipListener {
+        private final HwvtepSouthboundProvider hsp;
+        private final EntityOwnershipListenerRegistration listenerRegistration;
+
+        HwvtepsbPluginInstanceEntityOwnershipListener(final HwvtepSouthboundProvider hsp,
+                final EntityOwnershipService entityOwnershipService) {
             this.hsp = hsp;
             listenerRegistration = entityOwnershipService.registerListener(ENTITY_TYPE, this);
         }
@@ -189,10 +262,14 @@ public class HwvtepSouthboundProvider implements AutoCloseable {
         public void close() {
             this.listenerRegistration.close();
         }
+
         @Override
-        public void ownershipChanged(EntityOwnershipChange ownershipChange) {
+        public void ownershipChanged(final EntityOwnershipChange ownershipChange) {
             hsp.handleOwnershipChange(ownershipChange);
         }
     }
 
+    public HwvtepConnectionManager getHwvtepConnectionManager() {
+        return cm;
+    }
 }