From: Robert Varga Date: Mon, 6 Feb 2023 15:51:30 +0000 (+0100) Subject: Convert TlsAllowedDevicesMonitorImpl to OSGi DS X-Git-Tag: v5.0.2~43 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=netconf.git;a=commitdiff_plain;h=202f4eef4741a230897f029698863cb52b93d3f8 Convert TlsAllowedDevicesMonitorImpl to OSGi DS 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 --- diff --git a/apps/callhome-provider/pom.xml b/apps/callhome-provider/pom.xml index 778cec0ce5..1ea9a12483 100644 --- a/apps/callhome-provider/pom.xml +++ b/apps/callhome-provider/pom.xml @@ -18,12 +18,6 @@ org.opendaylight.mdsal mdsal-binding-api - - org.opendaylight.mdsal - mdsal-binding-dom-adapter - test-jar - test - org.opendaylight.netconf netconf-topology @@ -36,5 +30,27 @@ org.opendaylight.netconf callhome-model + + com.guicedee.services + javax.inject + true + + + jakarta.annotation + jakarta.annotation-api + provided + true + + + org.osgi + org.osgi.service.component.annotations + + + + org.opendaylight.mdsal + mdsal-binding-dom-adapter + test-jar + test + diff --git a/apps/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/tls/NetconfCallHomeTlsService.java b/apps/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/tls/NetconfCallHomeTlsService.java index 7465c6cf65..54a7a97ad0 100644 --- a/apps/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/tls/NetconfCallHomeTlsService.java +++ b/apps/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/tls/NetconfCallHomeTlsService.java @@ -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 diff --git a/apps/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/tls/TlsAllowedDevicesMonitorImpl.java b/apps/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/tls/TlsAllowedDevicesMonitorImpl.java index f0e5ac5ae2..f968442e77 100644 --- a/apps/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/tls/TlsAllowedDevicesMonitorImpl.java +++ b/apps/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/tls/TlsAllowedDevicesMonitorImpl.java @@ -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 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 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 { @Override public void onDataTreeChanged(@NonNull final Collection> changes) { diff --git a/apps/callhome-provider/src/main/resources/OSGI-INF/blueprint/callhome-topology.xml b/apps/callhome-provider/src/main/resources/OSGI-INF/blueprint/callhome-topology.xml index d74a3d34fe..09303953bd 100644 --- a/apps/callhome-provider/src/main/resources/OSGI-INF/blueprint/callhome-topology.xml +++ b/apps/callhome-provider/src/main/resources/OSGI-INF/blueprint/callhome-topology.xml @@ -37,6 +37,8 @@ + + diff --git a/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/tls/TlsAllowedDevicesMonitor.java b/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/tls/TlsAllowedDevicesMonitor.java index 5c71dde4e9..a5661bad6d 100644 --- a/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/tls/TlsAllowedDevicesMonitor.java +++ b/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/tls/TlsAllowedDevicesMonitor.java @@ -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 findAllowedKeys(); - - @Override - void close(); - }