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.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.rev160315.DataChangeCounter;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.data.change.counter.rev160315.data.change.counter.Counter;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.data.change.counter.rev160315.data.change.counter.CounterBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.data.change.counter.rev160315.data.change.counter.CounterKey;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class TopologyDataChangeCounter implements DataChangeListener, TransactionChainListener {
33 private static final Logger LOG = LoggerFactory.getLogger(TopologyDataChangeCounter.class);
35 private final DataBroker dataBroker;
36 private final String counterId;
37 private final InstanceIdentifier<Counter> counterInstanceId;
38 private final BindingTransactionChain chain;
39 private final AtomicLong count;
41 public TopologyDataChangeCounter(final DataBroker dataBroker, final String counterId) {
42 this.dataBroker = dataBroker;
43 this.chain = this.dataBroker.createTransactionChain(this);
44 this.counterId = counterId;
45 this.counterInstanceId = InstanceIdentifier.builder(DataChangeCounter.class)
46 .child(Counter.class, new CounterKey(this.counterId)).build();
47 this.count = new AtomicLong(0);
48 putCount(this.count.get());
49 LOG.debug("Data change counter {} initiated", this.counterId);
53 public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
54 putCount(this.count.incrementAndGet());
55 LOG.debug("Data change #{} for counter {}", this.count.get(), this.counterId);
59 final WriteTransaction wTx = this.dataBroker.newWriteOnlyTransaction();
60 wTx.delete(LogicalDatastoreType.OPERATIONAL, this.counterInstanceId);
62 wTx.submit().checkedGet();
63 } catch (TransactionCommitFailedException except) {
64 LOG.warn("Error on remove data change counter {}", this.counterId, except);
67 LOG.debug("Data change counter {} removed", this.counterId);
70 private void putCount(final long count) {
71 final WriteTransaction wTx = this.chain.newWriteOnlyTransaction();
72 Counter counter = new CounterBuilder().setId(this.counterId).setCount(count).build();
73 wTx.put(LogicalDatastoreType.OPERATIONAL, this.counterInstanceId, counter);
78 public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction, final Throwable cause) {
80 LOG.warn("Transaction chain failure. Transaction: {}", transaction, cause);
84 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
85 LOG.debug("Transaction chain successful. {}", chain);