Bump upstreams
[lispflowmapping.git] / mappingservice / dsbackend / src / main / java / org / opendaylight / lispflowmapping / dsbackend / DataStoreBackEnd.java
index 2ce6aafdf3c1ce86d4697f3185244ff8c48fbd0c..93375c4cd7453ab526957f0b10b80791b6a8b118 100644 (file)
@@ -7,10 +7,12 @@
  */
 package org.opendaylight.lispflowmapping.dsbackend;
 
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.MoreExecutors;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -42,7 +44,6 @@ import org.slf4j.LoggerFactory;
  * Stores data coming from the mapping database RPCs into the MD-SAL datastore.
  *
  * @author Lorand Jakab
- *
  */
 public class DataStoreBackEnd implements TransactionChainListener {
     private static final Logger LOG = LoggerFactory.getLogger(DataStoreBackEnd.class);
@@ -50,17 +51,15 @@ public class DataStoreBackEnd implements TransactionChainListener {
             InstanceIdentifier.create(MappingDatabase.class);
     private static final InstanceIdentifier<LastUpdated> LAST_UPDATED =
             InstanceIdentifier.create(MappingDatabase.class).child(LastUpdated.class);
-    private final DataBroker broker;
-    private TransactionChain txChain;
 
-    public DataStoreBackEnd(DataBroker broker) {
-        this.broker = broker;
-        createTransactionChain();
-    }
+    private final TransactionChain configTxChain;
+    private final TransactionChain operTxChain;
 
-    public void createTransactionChain() {
+    @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR", justification = "Non-final for mocking")
+    public DataStoreBackEnd(DataBroker broker) {
         LOG.debug("Creating DataStoreBackEnd transaction chain...");
-        txChain = broker.createMergingTransactionChain(this);
+        configTxChain = broker.createMergingTransactionChain(this);
+        operTxChain = broker.createMergingTransactionChain(this);
     }
 
     public void addAuthenticationKey(AuthenticationKey authenticationKey) {
@@ -91,8 +90,7 @@ public class DataStoreBackEnd implements TransactionChainListener {
     // This method assumes that it is only called for southbound originated Map-Registers
     public void addXtrIdMapping(XtrIdMapping mapping) {
         XtrId xtrId = mapping.getMappingRecord().getXtrId();
-        Preconditions.checkNotNull(xtrId, "Make sure you only call addXtrIdMapping when the MappingRecord "
-                + "contains an xTR-ID");
+        requireNonNull(xtrId, "Make sure you only call addXtrIdMapping when the MappingRecord contains an xTR-ID");
         if (LOG.isDebugEnabled()) {
             LOG.debug("MD-SAL: Adding mapping for {}, xTR-ID {}",
                     LispAddressStringifier.getString(mapping.getMappingRecord().getEid()), xtrId);
@@ -129,8 +127,7 @@ public class DataStoreBackEnd implements TransactionChainListener {
 
     public void removeXtrIdMapping(XtrIdMapping mapping) {
         XtrId xtrId = mapping.getMappingRecord().getXtrId();
-        Preconditions.checkNotNull(xtrId, "Make sure you only call addXtrIdMapping when the MappingRecord "
-                + "contains an xTR-ID");
+        requireNonNull(xtrId, "Make sure you only call addXtrIdMapping when the MappingRecord contains an xTR-ID");
         if (LOG.isDebugEnabled()) {
             LOG.debug("MD-SAL: Removing mapping for {}, xTR-ID {}",
                     LispAddressStringifier.getString(mapping.getMappingRecord().getEid()), xtrId);
@@ -196,8 +193,8 @@ public class DataStoreBackEnd implements TransactionChainListener {
         MappingDatabase mdb = readTransaction(DATABASE_ROOT, logicalDataStore);
 
         if (mdb != null && mdb.getVirtualNetworkIdentifier() != null) {
-            for (VirtualNetworkIdentifier id : mdb.getVirtualNetworkIdentifier()) {
-                List<Mapping> ms = id.getMapping();
+            for (VirtualNetworkIdentifier id : mdb.nonnullVirtualNetworkIdentifier().values()) {
+                List<Mapping> ms = new ArrayList<>(id.nonnullMapping().values());
                 if (ms != null) {
                     mappings.addAll(ms);
                 }
@@ -213,8 +210,8 @@ public class DataStoreBackEnd implements TransactionChainListener {
         MappingDatabase mdb = readTransaction(DATABASE_ROOT, LogicalDatastoreType.CONFIGURATION);
 
         if (mdb != null && mdb.getVirtualNetworkIdentifier() != null) {
-            for (VirtualNetworkIdentifier id : mdb.getVirtualNetworkIdentifier()) {
-                List<AuthenticationKey> keys = id.getAuthenticationKey();
+            for (VirtualNetworkIdentifier id : mdb.nonnullVirtualNetworkIdentifier().values()) {
+                List<AuthenticationKey> keys = new ArrayList<>(id.nonnullAuthenticationKey().values());
                 if (keys != null) {
                     authKeys.addAll(keys);
                 }
@@ -255,10 +252,18 @@ public class DataStoreBackEnd implements TransactionChainListener {
                 : LogicalDatastoreType.CONFIGURATION;
     }
 
+    private TransactionChain getChain(LogicalDatastoreType logicalDatastoreType) {
+        return switch (logicalDatastoreType) {
+            case CONFIGURATION -> configTxChain;
+            case OPERATIONAL -> operTxChain;
+        };
+    }
+
     private <U extends org.opendaylight.yangtools.yang.binding.DataObject> void writePutTransaction(
             InstanceIdentifier<U> addIID, U data, LogicalDatastoreType logicalDatastoreType, String errMsg) {
-        WriteTransaction writeTx = txChain.newWriteOnlyTransaction();
-        writeTx.put(logicalDatastoreType, addIID, data, true);
+        WriteTransaction writeTx = getChain(logicalDatastoreType).newWriteOnlyTransaction();
+        // TODO: is is a utility method, hence we do not have enough lifecycle knowledge to use plain put()
+        writeTx.mergeParentStructurePut(logicalDatastoreType, addIID, data);
         writeTx.commit().addCallback(new FutureCallback<CommitInfo>() {
 
             @Override
@@ -275,13 +280,13 @@ public class DataStoreBackEnd implements TransactionChainListener {
     private <U extends org.opendaylight.yangtools.yang.binding.DataObject> U readTransaction(
             InstanceIdentifier<U> readIID, LogicalDatastoreType logicalDatastoreType) {
         final ListenableFuture<Optional<U>> readFuture;
-        try (ReadTransaction readTx = txChain.newReadOnlyTransaction()) {
+        try (ReadTransaction readTx = getChain(logicalDatastoreType).newReadOnlyTransaction()) {
             readFuture = readTx.read(logicalDatastoreType, readIID);
         }
         try {
             Optional<U> optionalDataObject = readFuture.get();
             if (optionalDataObject != null && optionalDataObject.isPresent()) {
-                return optionalDataObject.get();
+                return optionalDataObject.orElseThrow();
             } else {
                 LOG.debug("{}: Failed to read", Thread.currentThread().getStackTrace()[1]);
             }
@@ -293,8 +298,7 @@ public class DataStoreBackEnd implements TransactionChainListener {
 
     private <U extends org.opendaylight.yangtools.yang.binding.DataObject> void deleteTransaction(
             InstanceIdentifier<U> deleteIID, LogicalDatastoreType logicalDatastoreType, String errMsg) {
-
-        WriteTransaction writeTx = txChain.newWriteOnlyTransaction();
+        WriteTransaction writeTx = getChain(logicalDatastoreType).newWriteOnlyTransaction();
         writeTx.delete(logicalDatastoreType, deleteIID);
         writeTx.commit().addCallback(new FutureCallback<CommitInfo>() {
             @Override
@@ -311,7 +315,7 @@ public class DataStoreBackEnd implements TransactionChainListener {
     @Override
     public void onTransactionChainFailed(TransactionChain chain, Transaction transaction, Throwable cause) {
         LOG.error("Broken chain {} in DataStoreBackEnd, transaction {}, cause {}", chain, transaction.getIdentifier(),
-                cause);
+                cause.getMessage());
     }
 
     @Override
@@ -321,6 +325,7 @@ public class DataStoreBackEnd implements TransactionChainListener {
 
     public void closeTransactionChain() {
         LOG.debug("Closing DataStoreBackEnd transaction chain...");
-        txChain.close();
+        configTxChain.close();
+        operTxChain.close();
     }
 }