+++ /dev/null
-/*
- * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.protocol.data.change.counter;
-
-import org.opendaylight.mdsal.binding.api.DataBroker;
-import org.osgi.service.component.annotations.Activate;
-import org.osgi.service.component.annotations.Component;
-import org.osgi.service.component.annotations.Deactivate;
-import org.osgi.service.component.annotations.Reference;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-@Component(immediate = true)
-public final class OSGiTopologyDataChangeCounterDeployer {
- private static final Logger LOG = LoggerFactory.getLogger(OSGiTopologyDataChangeCounterDeployer.class);
-
- @Reference
- DataBroker dataBroker;
-
- private TopologyDataChangeCounterDeployer deployer;
-
- @Activate
- void activate() {
- deployer = new TopologyDataChangeCounterDeployer(dataBroker);
- deployer.register();
- LOG.info("Topolocy data change counter activated");
-
- }
-
- @Deactivate
- void deactivate() {
- deployer.close();
- LOG.info("Topolocy data change counter deactivated");
- }
-}
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
-import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.checkerframework.checker.lock.qual.GuardedBy;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.mdsal.binding.api.DataBroker;
-import org.opendaylight.mdsal.binding.api.DataObjectModification;
import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
import org.opendaylight.mdsal.binding.api.DataTreeModification;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgpcep.data.change.counter.config.rev170424.DataChangeCounterConfig;
import org.opendaylight.yangtools.concepts.ListenerRegistration;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
-public class TopologyDataChangeCounterDeployer implements DataTreeChangeListener<DataChangeCounterConfig>,
+@Component(service = { })
+public final class TopologyDataChangeCounterDeployer implements DataTreeChangeListener<DataChangeCounterConfig>,
AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(TopologyDataChangeCounterDeployer.class);
private static final InstanceIdentifier<DataChangeCounterConfig> DATA_CHANGE_COUNTER_IID =
InstanceIdentifier.builder(DataChangeCounterConfig.class).build();
- private final DataBroker dataBroker;
+
+ private final @NonNull DataBroker dataBroker;
@GuardedBy("this")
private final Map<String, TopologyDataChangeCounter> counters = new HashMap<>();
+ @GuardedBy("this")
private ListenerRegistration<TopologyDataChangeCounterDeployer> registration;
@Inject
- public TopologyDataChangeCounterDeployer(final DataBroker dataBroker) {
+ @Activate
+ public TopologyDataChangeCounterDeployer(@Reference final DataBroker dataBroker) {
this.dataBroker = requireNonNull(dataBroker);
+ registration = dataBroker.registerDataTreeChangeListener(
+ DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, DATA_CHANGE_COUNTER_IID), this);
+ LOG.info("Data change counter Deployer started");
}
- @PostConstruct
- public synchronized void register() {
- this.registration = this.dataBroker.registerDataTreeChangeListener(
- DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, DATA_CHANGE_COUNTER_IID), this);
- LOG.info("Data change counter Deployer initiated");
+ @Deactivate
+ @PreDestroy
+ @Override
+ public synchronized void close() {
+ if (registration != null) {
+ registration.close();
+ registration = null;
+ }
+ counters.values().forEach(TopologyDataChangeCounter::close);
+ counters.clear();
+ LOG.info("Data change counter Deployer stopped");
}
@Override
public synchronized void onDataTreeChanged(
final Collection<DataTreeModification<DataChangeCounterConfig>> changes) {
- for (final DataTreeModification<DataChangeCounterConfig> dataTreeModification : changes) {
- final DataObjectModification<DataChangeCounterConfig> rootNode = dataTreeModification.getRootNode();
- switch (dataTreeModification.getRootNode().getModificationType()) {
+ for (var change : changes) {
+ final var rootNode = change.getRootNode();
+ switch (rootNode.getModificationType()) {
case DELETE:
deleteCounterChange(rootNode.getDataBefore().getCounterId());
break;
case SUBTREE_MODIFIED:
case WRITE:
- final DataChangeCounterConfig change = rootNode.getDataAfter();
- chandleCounterChange(change.getCounterId(), change.getTopologyName());
+ final var counterConfig = rootNode.getDataAfter();
+ handleCounterChange(counterConfig.getCounterId(), counterConfig.getTopologyName());
break;
default:
- LOG.error("Unhandled modification Type: {}",
- dataTreeModification.getRootNode().getModificationType());
+ LOG.error("Unhandled modification Type: {}", rootNode.getModificationType());
break;
}
}
}
private synchronized void deleteCounterChange(final String counterId) {
- final TopologyDataChangeCounter oldCounter = this.counters.remove(counterId);
+ final var oldCounter = counters.remove(counterId);
if (oldCounter != null) {
LOG.info("Data change counter Deployer deleted: {}", counterId);
oldCounter.close();
}
}
- private synchronized void chandleCounterChange(final String counterId, final String topologyName) {
+ private synchronized void handleCounterChange(final String counterId, final String topologyName) {
deleteCounterChange(counterId);
LOG.info("Data change counter Deployer created: {} / {}", counterId, topologyName);
-
- final TopologyDataChangeCounter counter = new TopologyDataChangeCounter(this.dataBroker,
- counterId, topologyName);
- this.counters.put(counterId, counter);
- }
-
- @PreDestroy
- @Override
- public synchronized void close() {
- LOG.info("Closing Data change counter Deployer");
-
- if (this.registration != null) {
- this.registration.close();
- this.registration = null;
- }
+ counters.put(counterId, new TopologyDataChangeCounter(dataBroker, counterId, topologyName));
}
}