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.Nonnull;
16 import javax.annotation.concurrent.GuardedBy;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgpcep.data.change.counter.config.rev170424.DataChangeCounterConfig;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class TopologyDataChangeCounterDeployer implements DataTreeChangeListener<DataChangeCounterConfig>,
31 private static final Logger LOG = LoggerFactory.getLogger(TopologyDataChangeCounterDeployer.class);
32 private static final InstanceIdentifier<DataChangeCounterConfig> DATA_CHANGE_COUNTER_IID =
33 InstanceIdentifier.builder(DataChangeCounterConfig.class).build();
34 private final DataBroker dataBroker;
36 private final Map<String, TopologyDataChangeCounter> counters = new HashMap<>();
37 private ListenerRegistration<TopologyDataChangeCounterDeployer> registration;
39 public TopologyDataChangeCounterDeployer(final DataBroker dataBroker) {
40 this.dataBroker = requireNonNull(dataBroker);
43 public synchronized void register() {
44 this.registration = this.dataBroker.registerDataTreeChangeListener(
45 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, DATA_CHANGE_COUNTER_IID), this);
46 LOG.info("Data change counter Deployer initiated");
51 public synchronized void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<DataChangeCounterConfig>> changes) {
52 for (final DataTreeModification<DataChangeCounterConfig> dataTreeModification : changes) {
53 final DataObjectModification<DataChangeCounterConfig> rootNode = dataTreeModification.getRootNode();
54 switch (dataTreeModification.getRootNode().getModificationType()) {
56 deleteCounterChange(rootNode.getDataBefore().getCounterId());
58 case SUBTREE_MODIFIED:
60 final DataChangeCounterConfig change = rootNode.getDataAfter();
61 chandleCounterChange(change.getCounterId(), change.getTopologyName());
67 public synchronized void deleteCounterChange(final String counterId) {
68 final TopologyDataChangeCounter oldCounter = this.counters.remove(counterId);
69 if (oldCounter != null) {
70 LOG.info("Data change counter Deployer deleted: {}", counterId);
75 public synchronized void chandleCounterChange(final String counterId, final String topologyName) {
76 deleteCounterChange(counterId);
77 LOG.info("Data change counter Deployer created: {} / {}", counterId, topologyName);
79 final TopologyDataChangeCounter counter = new TopologyDataChangeCounter(this.dataBroker, counterId, topologyName);
80 this.counters.put(counterId, counter);
84 public synchronized void close() throws Exception {
85 LOG.info("Closing Data change counter Deployer");
87 if (this.registration != null) {
88 this.registration.close();
89 this.registration = null;