Eliminate OSGiTopologyDataChangeCounterDeployer 18/109918/3
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 23 Jan 2024 06:28:44 +0000 (07:28 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 23 Jan 2024 06:35:10 +0000 (07:35 +0100)
Use constructor injection on TopologyDataChangeCounterDeployer to
eliminate the need for a separate class.

While we are here, also make sure to shutdown individual counters.

Change-Id: I8485968c1e44c0187ef03367cd998fd5e2301894
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
data-change-counter/src/main/java/org/opendaylight/protocol/data/change/counter/OSGiTopologyDataChangeCounterDeployer.java [deleted file]
data-change-counter/src/main/java/org/opendaylight/protocol/data/change/counter/TopologyDataChangeCounterDeployer.java

diff --git a/data-change-counter/src/main/java/org/opendaylight/protocol/data/change/counter/OSGiTopologyDataChangeCounterDeployer.java b/data-change-counter/src/main/java/org/opendaylight/protocol/data/change/counter/OSGiTopologyDataChangeCounterDeployer.java
deleted file mode 100644 (file)
index 735ce7f..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * 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");
-    }
-}
index ada4b66e5481d18cf268701a011ca8038930cfc3..1f05000cd1ff6a09815513fccb8c1f7bdec1265d 100644 (file)
@@ -12,13 +12,12 @@ import static java.util.Objects.requireNonNull;
 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;
@@ -26,79 +25,81 @@ import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 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));
     }
 }