2 * Copyright (c) 2016 Cisco Systems, Inc. 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.restconf.nb.rfc8040.handlers;
11 import java.util.Objects;
12 import javax.annotation.Nullable;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
15 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
17 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
18 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
24 * Implementation of {@link TransactionChainHandler}.
27 public class TransactionChainHandler implements Handler<DOMTransactionChain>, AutoCloseable {
28 private static final Logger LOG = LoggerFactory.getLogger(TransactionChainHandler.class);
30 private final TransactionChainListener transactionChainListener = new TransactionChainListener() {
32 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
33 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
34 LOG.warn("TransactionChain({}) {} FAILED!", chain, transaction.getIdentifier(), cause);
36 throw new RestconfDocumentedException("TransactionChain(" + chain + ") not committed correctly", cause);
40 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
41 LOG.trace("TransactionChain({}) SUCCESSFUL", chain);
46 private final DOMDataBroker dataBroker;
47 private volatile DOMTransactionChain transactionChain;
50 * Prepare transaction chain service for Restconf services.
52 public TransactionChainHandler(final DOMDataBroker dataBroker) {
53 this.dataBroker = Objects.requireNonNull(dataBroker);
54 transactionChain = Objects.requireNonNull(dataBroker.createTransactionChain(transactionChainListener));
58 public DOMTransactionChain get() {
59 return this.transactionChain;
62 public synchronized void reset() {
63 LOG.trace("Resetting TransactionChain({})", transactionChain);
64 transactionChain.close();
65 transactionChain = dataBroker.createTransactionChain(transactionChainListener);
69 public synchronized void close() {
70 transactionChain.close();