BUG-4569: Data-change-counter reconfiguration failure
[bgpcep.git] / data-change-counter / src / main / java / org / opendaylight / protocol / data / change / counter / TopologyDataChangeCounter.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.protocol.data.change.counter;
10
11 import java.util.concurrent.atomic.AtomicLong;
12 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.data.change.counter.rev140815.DataChangeCounter;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.data.change.counter.rev140815.DataChangeCounterBuilder;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class TopologyDataChangeCounter implements DataChangeListener, TransactionChainListener {
30
31     private static final Logger LOG = LoggerFactory.getLogger(TopologyDataChangeCounter.class);
32
33     protected static final InstanceIdentifier<DataChangeCounter> IID = InstanceIdentifier
34             .builder(DataChangeCounter.class).build();
35
36     private final DataBroker dataBroker;
37     private final BindingTransactionChain chain;
38     private final AtomicLong count;
39
40     public TopologyDataChangeCounter(final DataBroker dataBroker) {
41         this.dataBroker = dataBroker;
42         this.chain = this.dataBroker.createTransactionChain(this);
43         this.count = new AtomicLong(0);
44         putCount(this.count.get());
45         LOG.debug("Data change counter initiated");
46     }
47
48     @Override
49     public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
50         putCount(this.count.incrementAndGet());
51         LOG.debug("Data change #{}", this.count.get());
52     }
53
54     public void close() {
55         final WriteTransaction wTx = this.dataBroker.newWriteOnlyTransaction();
56         wTx.delete(LogicalDatastoreType.OPERATIONAL, IID);
57         try {
58             wTx.submit().checkedGet();
59         } catch (TransactionCommitFailedException except) {
60             LOG.warn("Error on remove  data change counter{}", IID.toString(), except);
61         }
62         this.chain.close();
63         LOG.debug("Data change counter removed");
64     }
65
66     private void putCount(final long count) {
67         final WriteTransaction wTx = this.chain.newWriteOnlyTransaction();
68         wTx.put(LogicalDatastoreType.OPERATIONAL, IID, new DataChangeCounterBuilder().setCount(count).build());
69         wTx.submit();
70     }
71
72     @Override
73     public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction, final Throwable cause) {
74         chain.close();
75         LOG.warn("Transaction chain failure. Transaction: {}", transaction, cause);
76     }
77
78     @Override
79     public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
80         LOG.debug("Transaction chain successful. {}", chain);
81     }
82
83 }