2 * Copyright (c) 2016, 2017 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.mdsalutil.internal;
11 import java.util.List;
12 import org.opendaylight.genius.utils.batching.ResourceHandler;
13 import org.opendaylight.genius.utils.batching.SubTransaction;
14 import org.opendaylight.genius.utils.batching.SubTransactionImpl;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.binding.api.WriteTransaction;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 class MdSalUtilBatchHandler implements ResourceHandler {
22 private final DataBroker dataBroker;
23 private final int batchSize;
24 private final int batchInterval;
26 MdSalUtilBatchHandler(DataBroker dataBroker, int batchSize, int batchInterval) {
27 this.dataBroker = dataBroker;
28 this.batchSize = batchSize;
29 this.batchInterval = batchInterval;
33 public void update(WriteTransaction tx, LogicalDatastoreType datastoreType, InstanceIdentifier identifier,
34 Object original, Object update, List<SubTransaction> transactionObjects) {
35 if (update != null && !(update instanceof DataObject)) {
38 if (datastoreType != this.getDatastoreType()) {
41 tx.merge(datastoreType, identifier, (DataObject) update, true);
43 buildSubTransactions(transactionObjects, identifier, update, SubTransaction.UPDATE);
47 public void updateContainer(WriteTransaction tx, LogicalDatastoreType datastoreType,
48 InstanceIdentifier identifier, Object original, Object update,
49 List<SubTransaction> transactionObjects) {
54 public void create(WriteTransaction tx, LogicalDatastoreType datastoreType, InstanceIdentifier identifier,
55 Object data, List<SubTransaction> transactionObjects) {
56 if (data != null && !(data instanceof DataObject)) {
59 if (datastoreType != this.getDatastoreType()) {
62 tx.put(datastoreType, identifier, (DataObject) data, true);
64 buildSubTransactions(transactionObjects, identifier, data, SubTransaction.CREATE);
68 public void delete(WriteTransaction tx, LogicalDatastoreType datastoreType, InstanceIdentifier identifier,
69 Object data, List<SubTransaction> transactionObjects) {
70 if (data != null && !(data instanceof DataObject)) {
73 if (datastoreType != this.getDatastoreType()) {
76 tx.delete(datastoreType, identifier);
78 buildSubTransactions(transactionObjects, identifier, data, SubTransaction.DELETE);
82 public DataBroker getResourceBroker() {
87 public int getBatchSize() {
92 public int getBatchInterval() {
97 public LogicalDatastoreType getDatastoreType() {
98 return LogicalDatastoreType.CONFIGURATION;
101 private void buildSubTransactions(List<SubTransaction> transactionObjects, InstanceIdentifier identifier,
102 Object data, short subTransactionType) {
104 SubTransaction subTransaction = new SubTransactionImpl();
105 subTransaction.setInstanceIdentifier(identifier);
106 subTransaction.setInstance(data);
107 subTransaction.setAction(subTransactionType);
108 transactionObjects.add(subTransaction);