2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.genius.utils.batching;
11 import java.util.List;
12 import org.opendaylight.mdsal.binding.api.DataBroker;
13 import org.opendaylight.mdsal.binding.api.WriteTransaction;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.yangtools.yang.binding.DataObject;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 public class DefaultBatchHandler implements ResourceHandler {
21 private final DataBroker dataBroker;
22 private final Integer batchSize;
23 private final Integer batchInterval;
24 private final LogicalDatastoreType datastoreType;
26 public DefaultBatchHandler(DataBroker dataBroker, LogicalDatastoreType dataStoreType, Integer batchSize,
27 Integer batchInterval) {
28 this.dataBroker = dataBroker;
29 this.batchSize = batchSize;
30 this.batchInterval = batchInterval;
31 this.datastoreType = dataStoreType;
35 public void update(WriteTransaction tx, LogicalDatastoreType logicalDatastoreType,
36 InstanceIdentifier identifier, Object original, Object update, List<SubTransaction> transactionObjects) {
37 if (update != null && !(update instanceof DataObject)) {
40 if (logicalDatastoreType != getDatastoreType()) {
44 SubTransaction subTransaction = new SubTransactionImpl();
45 subTransaction.setAction(SubTransaction.UPDATE);
46 subTransaction.setInstance(update);
47 subTransaction.setInstanceIdentifier(identifier);
48 transactionObjects.add(subTransaction);
50 tx.merge(logicalDatastoreType, identifier, (DataObject) update, true);
54 public void updateContainer(WriteTransaction tx, LogicalDatastoreType logicalDatastoreType,
55 InstanceIdentifier identifier, Object original, Object update,
56 List<SubTransaction> transactionObjects) {
57 if (update != null && !(update instanceof DataObject)) {
60 if (logicalDatastoreType != getDatastoreType()) {
64 SubTransaction subTransaction = new SubTransactionImpl();
65 subTransaction.setAction(SubTransaction.UPDATE);
66 subTransaction.setInstance(update);
67 subTransaction.setInstanceIdentifier(identifier);
68 transactionObjects.add(subTransaction);
70 tx.merge(logicalDatastoreType, identifier, (DataObject) update);
74 public void create(WriteTransaction tx, final LogicalDatastoreType logicalDatastoreType,
75 final InstanceIdentifier identifier, final Object data, List<SubTransaction> transactionObjects) {
76 if (data != null && !(data instanceof DataObject)) {
79 if (logicalDatastoreType != getDatastoreType()) {
83 SubTransaction subTransaction = new SubTransactionImpl();
84 subTransaction.setAction(SubTransaction.CREATE);
85 subTransaction.setInstance(data);
86 subTransaction.setInstanceIdentifier(identifier);
87 transactionObjects.add(subTransaction);
89 tx.put(logicalDatastoreType, identifier, (DataObject) data, true);
93 public void delete(WriteTransaction tx, final LogicalDatastoreType logicalDatastoreType,
94 final InstanceIdentifier identifier, final Object data, List<SubTransaction> transactionObjects) {
95 if (data != null && !(data instanceof DataObject)) {
98 if (logicalDatastoreType != getDatastoreType()) {
102 SubTransaction subTransaction = new SubTransactionImpl();
103 subTransaction.setAction(SubTransaction.DELETE);
104 subTransaction.setInstanceIdentifier(identifier);
105 transactionObjects.add(subTransaction);
107 tx.delete(logicalDatastoreType, identifier);
111 public DataBroker getResourceBroker() {
116 public int getBatchSize() {
121 public int getBatchInterval() {
122 return batchInterval;
126 public LogicalDatastoreType getDatastoreType() {
127 return datastoreType;