2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
8 package org.opendaylight.protocol.data.change.counter;
10 import static java.util.Objects.requireNonNull;
12 import java.util.Collection;
13 import java.util.HashMap;
15 import javax.annotation.PostConstruct;
16 import javax.annotation.PreDestroy;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.checkerframework.checker.lock.qual.GuardedBy;
20 import org.opendaylight.mdsal.binding.api.DataBroker;
21 import org.opendaylight.mdsal.binding.api.DataObjectModification;
22 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
23 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
24 import org.opendaylight.mdsal.binding.api.DataTreeModification;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgpcep.data.change.counter.config.rev170424.DataChangeCounterConfig;
27 import org.opendaylight.yangtools.concepts.ListenerRegistration;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 public class TopologyDataChangeCounterDeployer implements DataTreeChangeListener<DataChangeCounterConfig>,
35 private static final Logger LOG = LoggerFactory.getLogger(TopologyDataChangeCounterDeployer.class);
36 private static final InstanceIdentifier<DataChangeCounterConfig> DATA_CHANGE_COUNTER_IID =
37 InstanceIdentifier.builder(DataChangeCounterConfig.class).build();
38 private final DataBroker dataBroker;
40 private final Map<String, TopologyDataChangeCounter> counters = new HashMap<>();
41 private ListenerRegistration<TopologyDataChangeCounterDeployer> registration;
44 public TopologyDataChangeCounterDeployer(final DataBroker dataBroker) {
45 this.dataBroker = requireNonNull(dataBroker);
49 public synchronized void register() {
50 this.registration = this.dataBroker.registerDataTreeChangeListener(
51 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, DATA_CHANGE_COUNTER_IID), this);
52 LOG.info("Data change counter Deployer initiated");
56 public synchronized void onDataTreeChanged(
57 final Collection<DataTreeModification<DataChangeCounterConfig>> changes) {
58 for (final DataTreeModification<DataChangeCounterConfig> dataTreeModification : changes) {
59 final DataObjectModification<DataChangeCounterConfig> rootNode = dataTreeModification.getRootNode();
60 switch (dataTreeModification.getRootNode().getModificationType()) {
62 deleteCounterChange(rootNode.getDataBefore().getCounterId());
64 case SUBTREE_MODIFIED:
66 final DataChangeCounterConfig change = rootNode.getDataAfter();
67 chandleCounterChange(change.getCounterId(), change.getTopologyName());
70 LOG.error("Unhandled modification Type: {}",
71 dataTreeModification.getRootNode().getModificationType());
77 private synchronized void deleteCounterChange(final String counterId) {
78 final TopologyDataChangeCounter oldCounter = this.counters.remove(counterId);
79 if (oldCounter != null) {
80 LOG.info("Data change counter Deployer deleted: {}", counterId);
85 private synchronized void chandleCounterChange(final String counterId, final String topologyName) {
86 deleteCounterChange(counterId);
87 LOG.info("Data change counter Deployer created: {} / {}", counterId, topologyName);
89 final TopologyDataChangeCounter counter = new TopologyDataChangeCounter(this.dataBroker,
90 counterId, topologyName);
91 this.counters.put(counterId, counter);
96 public synchronized void close() {
97 LOG.info("Closing Data change counter Deployer");
99 if (this.registration != null) {
100 this.registration.close();
101 this.registration = null;