e8d9367e898ded73aa412d63165df257aa26b636
[genius.git] / idmanager / idmanager-impl / src / main / java / org / opendaylight / genius / idmanager / jobs / UpdateIdEntryJob.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.genius.idmanager.jobs;
10
11 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
12
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Optional;
17 import java.util.concurrent.Callable;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
20 import org.opendaylight.genius.idmanager.IdUtils;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.IdEntries;
22
23 public class UpdateIdEntryJob implements Callable<List<ListenableFuture<Void>>> {
24
25     private final String parentPoolName;
26     private final String localPoolName;
27     private final String idKey;
28     private final List<Long> newIdValues;
29     private final DataBroker broker;
30     private final IdUtils idUtils;
31
32     public UpdateIdEntryJob(String parentPoolName, String localPoolName,
33             String idKey, List<Long> newIdValues, DataBroker broker, IdUtils idUtils) {
34         this.parentPoolName = parentPoolName;
35         this.localPoolName = localPoolName;
36         this.idKey = idKey;
37         this.newIdValues = newIdValues;
38         this.broker = broker;
39         this.idUtils = idUtils;
40     }
41
42     @Override
43     public List<ListenableFuture<Void>> call() throws Exception {
44         WriteTransaction tx = broker.newWriteOnlyTransaction();
45         idUtils.updateChildPool(tx, parentPoolName, localPoolName);
46         if (newIdValues != null && !newIdValues.isEmpty()) {
47             IdEntries newIdEntry = idUtils.createIdEntries(idKey, newIdValues);
48             tx.merge(CONFIGURATION, idUtils.getIdEntriesInstanceIdentifier(parentPoolName, idKey), newIdEntry);
49         } else {
50             tx.delete(CONFIGURATION, idUtils.getIdEntriesInstanceIdentifier(parentPoolName, idKey));
51         }
52         tx.submit().checkedGet();
53         Optional.ofNullable(idUtils.releaseIdLatchMap.get(parentPoolName + idKey))
54             .ifPresent(latch -> latch.countDown());
55         return Collections.emptyList();
56     }
57 }