2 * Copyright (c) 2014 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.protocol.data.change.counter;
11 import java.util.Collection;
12 import java.util.concurrent.atomic.AtomicLong;
13 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
14 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.data.change.counter.rev160315.DataChangeCounter;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.data.change.counter.rev160315.data.change.counter.Counter;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.data.change.counter.rev160315.data.change.counter.CounterBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.data.change.counter.rev160315.data.change.counter.CounterKey;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class TopologyDataChangeCounter implements ClusteredDataTreeChangeListener<Topology>, TransactionChainListener {
34 private static final Logger LOG = LoggerFactory.getLogger(TopologyDataChangeCounter.class);
36 private final DataBroker dataBroker;
37 private final String counterId;
38 private final InstanceIdentifier<Counter> counterInstanceId;
39 private final BindingTransactionChain chain;
40 private final AtomicLong count;
42 public TopologyDataChangeCounter(final DataBroker dataBroker, final String counterId) {
43 this.dataBroker = dataBroker;
44 this.chain = this.dataBroker.createTransactionChain(this);
45 this.counterId = counterId;
46 this.counterInstanceId = InstanceIdentifier.builder(DataChangeCounter.class)
47 .child(Counter.class, new CounterKey(this.counterId)).build();
48 this.count = new AtomicLong(0);
49 putCount(this.count.get());
50 LOG.debug("Data change counter {} initiated", this.counterId);
54 public void onDataTreeChanged(final Collection<DataTreeModification<Topology>> changes) {
55 putCount(this.count.incrementAndGet());
56 LOG.debug("Data change #{} for counter {}", this.count.get(), this.counterId);
60 final WriteTransaction wTx = this.dataBroker.newWriteOnlyTransaction();
61 wTx.delete(LogicalDatastoreType.OPERATIONAL, this.counterInstanceId);
63 wTx.submit().checkedGet();
64 } catch (TransactionCommitFailedException except) {
65 LOG.warn("Error on remove data change counter {}", this.counterId, except);
68 LOG.debug("Data change counter {} removed", this.counterId);
71 private void putCount(final long count) {
72 final WriteTransaction wTx = this.chain.newWriteOnlyTransaction();
73 Counter counter = new CounterBuilder().setId(this.counterId).setCount(count).build();
74 wTx.put(LogicalDatastoreType.OPERATIONAL, this.counterInstanceId, counter);
79 public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction, final Throwable cause) {
81 LOG.warn("Transaction chain failure. Transaction: {}", transaction, cause);
85 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
86 LOG.debug("Transaction chain successful. {}", chain);