Bug 7864: Specified Id key does not exist in id pool vpnservices
[genius.git] / idmanager / idmanager-impl / src / main / java / org / opendaylight / genius / idmanager / jobs / UpdateIdEntryJob.java
index 8d6621412a3315c7dc1ff592cb878cb520d92ac8..2447241e6b10d0ff99acb9341fc428ff80e0e20d 100644 (file)
@@ -11,16 +11,21 @@ package org.opendaylight.genius.idmanager.jobs;
 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
 
 import com.google.common.util.concurrent.ListenableFuture;
-import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
+import java.util.Optional;
 import java.util.concurrent.Callable;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
 import org.opendaylight.genius.idmanager.IdUtils;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.IdEntries;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class UpdateIdEntryJob implements Callable<List<ListenableFuture<Void>>> {
 
+    private static final Logger LOG = LoggerFactory.getLogger(UpdateIdEntryJob.class);
     private final String parentPoolName;
     private final String localPoolName;
     private final String idKey;
@@ -39,8 +44,7 @@ public class UpdateIdEntryJob implements Callable<List<ListenableFuture<Void>>>
     }
 
     @Override
-    public List<ListenableFuture<Void>> call() throws Exception {
-        List<ListenableFuture<Void>> futures = new ArrayList<>();
+    public List<ListenableFuture<Void>> call() throws TransactionCommitFailedException {
         WriteTransaction tx = broker.newWriteOnlyTransaction();
         idUtils.updateChildPool(tx, parentPoolName, localPoolName);
         if (newIdValues != null && !newIdValues.isEmpty()) {
@@ -49,7 +53,15 @@ public class UpdateIdEntryJob implements Callable<List<ListenableFuture<Void>>>
         } else {
             tx.delete(CONFIGURATION, idUtils.getIdEntriesInstanceIdentifier(parentPoolName, idKey));
         }
-        futures.add(tx.submit());
-        return futures;
+        tx.submit().checkedGet();
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Updated id entry with idValues {}, idKey {}, pool {}", newIdValues, idKey, localPoolName);
+        }
+        String uniqueIdKey = idUtils.getUniqueKey(parentPoolName, idKey);
+        Optional.ofNullable(idUtils.releaseIdLatchMap.get(uniqueIdKey))
+            .ifPresent(latch -> latch.countDown());
+        // Once the id is written to DS, removing the id value from map.
+        idUtils.allocatedIdMap.remove(uniqueIdKey);
+        return Collections.emptyList();
     }
 }