Convert TlsAllowedDevicesMonitorImpl to OSGi DS 76/104276/4
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 6 Feb 2023 15:51:30 +0000 (16:51 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 7 Feb 2023 09:44:27 +0000 (10:44 +0100)
This is a simple component, only depending on DataBroker. Use
declarative services to manage its lifecycle. Also eliminate close()
from the public contract of TlsAllowedDevicesMonitor to ensure it is
properly isolated.

JIRA: NETCONF-949
Change-Id: I4cc51ee5ea807f2b4db664b18b2f81888bb20a34
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
apps/callhome-provider/pom.xml
apps/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/tls/NetconfCallHomeTlsService.java
apps/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/tls/TlsAllowedDevicesMonitorImpl.java
apps/callhome-provider/src/main/resources/OSGI-INF/blueprint/callhome-topology.xml
netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/tls/TlsAllowedDevicesMonitor.java

index 778cec0ce50dc8f0ce48f5f5e3e29accd399a47c..1ea9a124832282358a041baa31b1b55e00a4bc89 100644 (file)
             <groupId>org.opendaylight.mdsal</groupId>
             <artifactId>mdsal-binding-api</artifactId>
         </dependency>
-        <dependency>
-            <groupId>org.opendaylight.mdsal</groupId>
-            <artifactId>mdsal-binding-dom-adapter</artifactId>
-            <type>test-jar</type>
-            <scope>test</scope>
-        </dependency>
         <dependency>
             <groupId>org.opendaylight.netconf</groupId>
             <artifactId>netconf-topology</artifactId>
             <groupId>org.opendaylight.netconf</groupId>
             <artifactId>callhome-model</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.guicedee.services</groupId>
+            <artifactId>javax.inject</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>jakarta.annotation</groupId>
+            <artifactId>jakarta.annotation-api</artifactId>
+            <scope>provided</scope>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.service.component.annotations</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.opendaylight.mdsal</groupId>
+            <artifactId>mdsal-binding-dom-adapter</artifactId>
+            <type>test-jar</type>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
index 7465c6cf65b1236f4827aad07ab6e45b8732e4da..54a7a97ad0d5d9fd0ad3babb0db73ef80f470950 100644 (file)
@@ -33,6 +33,7 @@ public class NetconfCallHomeTlsService implements AutoCloseable {
 
     public NetconfCallHomeTlsService(final Configuration config,
                                      final DataBroker dataBroker,
+                                     final TlsAllowedDevicesMonitor allowedDevicesMonitor,
                                      final CallHomeNetconfSubsystemListener subsystemListener,
                                      final EventLoopGroup bossGroup,
                                      final EventLoopGroup workerGroup) {
@@ -40,8 +41,8 @@ public class NetconfCallHomeTlsService implements AutoCloseable {
         this.subsystemListener = requireNonNull(subsystemListener);
         this.bossGroup = requireNonNull(bossGroup);
         this.workerGroup = requireNonNull(workerGroup);
-        this.allowedDevicesMonitor = new TlsAllowedDevicesMonitorImpl(dataBroker);
-        this.sslHandlerFactory = new SslHandlerFactoryAdapter(dataBroker, allowedDevicesMonitor);
+        this.allowedDevicesMonitor = requireNonNull(allowedDevicesMonitor);
+        sslHandlerFactory = new SslHandlerFactoryAdapter(dataBroker, allowedDevicesMonitor);
     }
 
     public void init() {
@@ -66,6 +67,5 @@ public class NetconfCallHomeTlsService implements AutoCloseable {
     @Override
     public void close() {
         server.stop();
-        allowedDevicesMonitor.close();
     }
 }
\ No newline at end of file
index f0e5ac5ae2b055a73d5009040cf98230bca41421..f968442e77a6b1e5e585cd14f57c5f75fada77d8 100644 (file)
@@ -23,6 +23,9 @@ import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.stream.Collectors;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
 import org.opendaylight.mdsal.binding.api.DataBroker;
@@ -39,10 +42,16 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev201015.netconf.callhome.server.allowed.devices.device.transport.Tls;
 import org.opendaylight.yangtools.concepts.Registration;
 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;
 
-public class TlsAllowedDevicesMonitorImpl implements TlsAllowedDevicesMonitor, AutoCloseable {
+@Singleton
+@Component(service = TlsAllowedDevicesMonitor.class, immediate = true, property = "type=default")
+public final class TlsAllowedDevicesMonitorImpl implements TlsAllowedDevicesMonitor, AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(TlsAllowedDevicesMonitorImpl.class);
 
     private final ConcurrentMap<String, String> deviceToPrivateKey = new ConcurrentHashMap<>();
@@ -51,7 +60,9 @@ public class TlsAllowedDevicesMonitorImpl implements TlsAllowedDevicesMonitor, A
     private final Registration allowedDevicesReg;
     private final Registration certificatesReg;
 
-    public TlsAllowedDevicesMonitorImpl(final DataBroker dataBroker) {
+    @Inject
+    @Activate
+    public TlsAllowedDevicesMonitorImpl(@Reference final DataBroker dataBroker) {
         allowedDevicesReg = dataBroker.registerDataTreeChangeListener(
             DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
                 InstanceIdentifier.create(NetconfCallhomeServer.class).child(AllowedDevices.class).child(Device.class)),
@@ -61,6 +72,14 @@ public class TlsAllowedDevicesMonitorImpl implements TlsAllowedDevicesMonitor, A
             new CertificatesMonitor());
     }
 
+    @PreDestroy
+    @Deactivate
+    @Override
+    public void close() {
+        allowedDevicesReg.close();
+        certificatesReg.close();
+    }
+
     @Override
     public Optional<String> findDeviceIdByPublicKey(final PublicKey key) {
         // Find certificate names by the public key
@@ -92,12 +111,6 @@ public class TlsAllowedDevicesMonitorImpl implements TlsAllowedDevicesMonitor, A
         return Set.copyOf(deviceToPrivateKey.values());
     }
 
-    @Override
-    public void close() {
-        allowedDevicesReg.close();
-        certificatesReg.close();
-    }
-
     private final class CertificatesMonitor implements ClusteredDataTreeChangeListener<Keystore> {
         @Override
         public void onDataTreeChanged(@NonNull final Collection<DataTreeModification<Keystore>> changes) {
index d74a3d34fef8faf06cac4229d930f0eb633004d3..09303953bd4c11ad6c0cf12e67ab2edfa33b76a2 100644 (file)
@@ -37,6 +37,8 @@
     <reference id="globalWorkerGroup"
                interface="io.netty.channel.EventLoopGroup"
                odl:type="global-worker-group"/>
+    <reference id="allowedDevicesMonitor"
+               interface="org.opendaylight.netconf.callhome.protocol.tls.TlsAllowedDevicesMonitor"/>
 
     <bean id="callhomeProvider" class="org.opendaylight.netconf.callhome.mount.IetfZeroTouchCallHomeServerProvider"
           init-method="init"
@@ -72,6 +74,7 @@
           destroy-method="close">
         <argument ref="netconfCallHomeTlsConfiguration" />
         <argument ref="dataBroker" />
+        <argument ref="allowedDevicesMonitor" />
         <argument ref="callhomeDispatcher" />
         <argument ref="globalBossGroup"/>
         <argument ref="globalWorkerGroup"/>
index 5c71dde4e9ecf25c44a1685468d0a3886a2f3b02..a5661bad6d2b49fdfd634473669656dbd59da132 100644 (file)
@@ -11,8 +11,7 @@ import java.security.PublicKey;
 import java.util.Optional;
 import java.util.Set;
 
-public interface TlsAllowedDevicesMonitor extends AutoCloseable {
-
+public interface TlsAllowedDevicesMonitor {
     /**
      * Returns a Call-Home Device ID by the public key.
      */
@@ -22,8 +21,4 @@ public interface TlsAllowedDevicesMonitor extends AutoCloseable {
      * Returns a set of IDs for the keys associated with Call-Home devices.
      */
     Set<String> findAllowedKeys();
-
-    @Override
-    void close();
-
 }