0e2df38e5fce0038081d037e7d66cc606ea2d1de
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / handlers / TransactionChainHandler.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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 package org.opendaylight.restconf.nb.rfc8040.handlers;
9
10 import java.util.Objects;
11 import javax.annotation.PreDestroy;
12 import javax.inject.Inject;
13 import javax.inject.Singleton;
14 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeTransaction;
16 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
17 import org.opendaylight.mdsal.dom.api.DOMTransactionChainListener;
18 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Implementation of {@link TransactionChainHandler}.
24  */
25 @Singleton
26 public class TransactionChainHandler implements Handler<DOMTransactionChain>, AutoCloseable {
27     private static final Logger LOG = LoggerFactory.getLogger(TransactionChainHandler.class);
28
29     private final DOMTransactionChainListener transactionChainListener = new DOMTransactionChainListener() {
30         @Override
31         public void onTransactionChainFailed(final DOMTransactionChain chain, final DOMDataTreeTransaction transaction,
32                 final Throwable cause) {
33             LOG.warn("TransactionChain({}) {} FAILED!", chain, transaction.getIdentifier(), cause);
34             reset();
35             throw new RestconfDocumentedException("TransactionChain(" + chain + ") not committed correctly", cause);
36         }
37
38         @Override
39         public void onTransactionChainSuccessful(final DOMTransactionChain chain) {
40             LOG.trace("TransactionChain({}) SUCCESSFUL", chain);
41         }
42     };
43
44     private final DOMDataBroker dataBroker;
45
46     private volatile DOMTransactionChain transactionChain;
47
48     /**
49      * Prepare transaction chain service for Restconf services.
50      */
51     @Inject
52     public TransactionChainHandler(final DOMDataBroker dataBroker) {
53         this.dataBroker = Objects.requireNonNull(dataBroker);
54         transactionChain = Objects.requireNonNull(dataBroker.createTransactionChain(transactionChainListener));
55     }
56
57     @Override
58     public DOMTransactionChain get() {
59         return this.transactionChain;
60     }
61
62     public synchronized void reset() {
63         LOG.trace("Resetting TransactionChain({})", transactionChain);
64         transactionChain.close();
65         transactionChain = dataBroker.createTransactionChain(transactionChainListener);
66     }
67
68     @Override
69     @PreDestroy
70     public synchronized void close() {
71         transactionChain.close();
72     }
73 }