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 7c065b0e131bb7f99f54b61f348278b2194e5683..2447241e6b10d0ff99acb9341fc428ff80e0e20d 100644 (file)
@@ -8,49 +8,60 @@
 
 package org.opendaylight.genius.idmanager.jobs;
 
-import java.util.ArrayList;
+import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
+
+import com.google.common.util.concurrent.ListenableFuture;
+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.LogicalDatastoreType;
+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 com.google.common.util.concurrent.ListenableFuture;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class UpdateIdEntryJob implements Callable<List<ListenableFuture<Void>>> {
 
-    String parentPoolName;
-    String localPoolName;
-    String idKey;
-    List<Long> newIdValues;
-    DataBroker broker;
+    private static final Logger LOG = LoggerFactory.getLogger(UpdateIdEntryJob.class);
+    private final String parentPoolName;
+    private final String localPoolName;
+    private final String idKey;
+    private final List<Long> newIdValues;
+    private final DataBroker broker;
+    private final IdUtils idUtils;
 
     public UpdateIdEntryJob(String parentPoolName, String localPoolName,
-            String idKey, List<Long> newIdValues, DataBroker broker) {
-        super();
+            String idKey, List<Long> newIdValues, DataBroker broker, IdUtils idUtils) {
         this.parentPoolName = parentPoolName;
         this.localPoolName = localPoolName;
         this.idKey = idKey;
         this.newIdValues = newIdValues;
         this.broker = broker;
+        this.idUtils = idUtils;
     }
 
     @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);
+        idUtils.updateChildPool(tx, parentPoolName, localPoolName);
         if (newIdValues != null && !newIdValues.isEmpty()) {
-            IdEntries newIdEntry = IdUtils.createIdEntries(idKey, newIdValues);
-            tx.merge(LogicalDatastoreType.CONFIGURATION, IdUtils.getIdEntriesInstanceIdentifier(parentPoolName, idKey), newIdEntry);
-            futures.add(tx.submit());
-            return futures;
+            IdEntries newIdEntry = idUtils.createIdEntries(idKey, newIdValues);
+            tx.merge(CONFIGURATION, idUtils.getIdEntriesInstanceIdentifier(parentPoolName, idKey), newIdEntry);
+        } else {
+            tx.delete(CONFIGURATION, idUtils.getIdEntriesInstanceIdentifier(parentPoolName, idKey));
+        }
+        tx.submit().checkedGet();
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Updated id entry with idValues {}, idKey {}, pool {}", newIdValues, idKey, localPoolName);
         }
-        tx.delete(LogicalDatastoreType.CONFIGURATION, IdUtils.getIdEntriesInstanceIdentifier(parentPoolName, idKey));
-        futures.add(tx.submit());
-        return futures;
+        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();
     }
-}
\ No newline at end of file
+}