De-static-ify idmanager IdUtils
[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.ArrayList;
15 import java.util.List;
16 import java.util.concurrent.Callable;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
19 import org.opendaylight.genius.idmanager.IdUtils;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.IdEntries;
21
22 public class UpdateIdEntryJob implements Callable<List<ListenableFuture<Void>>> {
23
24     private final String parentPoolName;
25     private final String localPoolName;
26     private final String idKey;
27     private final List<Long> newIdValues;
28     private final DataBroker broker;
29     private final IdUtils idUtils;
30
31     public UpdateIdEntryJob(String parentPoolName, String localPoolName,
32             String idKey, List<Long> newIdValues, DataBroker broker, IdUtils idUtils) {
33         this.parentPoolName = parentPoolName;
34         this.localPoolName = localPoolName;
35         this.idKey = idKey;
36         this.newIdValues = newIdValues;
37         this.broker = broker;
38         this.idUtils = idUtils;
39     }
40
41     @Override
42     public List<ListenableFuture<Void>> call() throws Exception {
43         List<ListenableFuture<Void>> futures = new ArrayList<>();
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         futures.add(tx.submit());
53         return futures;
54     }
55 }