From 06bfd34c589c84d355fef6a36cf2e27c2f07958c Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 26 Sep 2023 14:36:39 +0200 Subject: [PATCH] Do not leak ClassToInstanceMap from NetconfSalKeystoreRpcs A public method here is completely superfluous, do not leak it. JIRA: NETCONF-1116 Change-Id: Ib2789ba44fd1ac00ace1d857a92f859c10b11e0f Signed-off-by: Robert Varga --- .../mdsal/impl/NetconfSalKeystoreRpcs.java | 28 +++++++++---------- .../impl/NetconfSalKeystoreRpcsTest.java | 6 ++-- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/plugins/netconf-client-mdsal/src/main/java/org/opendaylight/netconf/client/mdsal/impl/NetconfSalKeystoreRpcs.java b/plugins/netconf-client-mdsal/src/main/java/org/opendaylight/netconf/client/mdsal/impl/NetconfSalKeystoreRpcs.java index 975e3df5c7..4f4cb6db78 100644 --- a/plugins/netconf-client-mdsal/src/main/java/org/opendaylight/netconf/client/mdsal/impl/NetconfSalKeystoreRpcs.java +++ b/plugins/netconf-client-mdsal/src/main/java/org/opendaylight/netconf/client/mdsal/impl/NetconfSalKeystoreRpcs.java @@ -9,7 +9,7 @@ package org.opendaylight.netconf.client.mdsal.impl; import static java.util.Objects.requireNonNull; -import com.google.common.collect.ClassToInstanceMap; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableClassToInstanceMap; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.ListenableFuture; @@ -89,7 +89,14 @@ public final class NetconfSalKeystoreRpcs implements AutoCloseable { this.dataBroker = requireNonNull(dataBroker); this.encryptionService = requireNonNull(encryptionService); - reg = rpcProvider.registerRpcImplementations(getRpcClassToInstanceMap()); + reg = rpcProvider.registerRpcImplementations(ImmutableClassToInstanceMap.>builder() + .put(RemoveKeystoreEntry.class, this::removeKeystoreEntry) + .put(AddKeystoreEntry.class, this::addKeystoreEntry) + .put(AddTrustedCertificate.class, this::addTrustedCertificate) + .put(RemoveTrustedCertificate.class, this::removeTrustedCertificate) + .put(AddPrivateKey.class, this::addPrivateKey) + .put(RemovePrivateKey.class, this::removePrivateKey) + .build()); LOG.info("NETCONF keystore service started"); } @@ -166,7 +173,8 @@ public final class NetconfSalKeystoreRpcs implements AutoCloseable { return rpcResult; } - private ListenableFuture> addTrustedCertificate( + @VisibleForTesting + ListenableFuture> addTrustedCertificate( final AddTrustedCertificateInput input) { final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction(); @@ -222,7 +230,8 @@ public final class NetconfSalKeystoreRpcs implements AutoCloseable { return rpcResult; } - private ListenableFuture> addPrivateKey(final AddPrivateKeyInput input) { + @VisibleForTesting + ListenableFuture> addPrivateKey(final AddPrivateKeyInput input) { final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction(); for (PrivateKey key: input.nonnullPrivateKey().values()) { @@ -275,15 +284,4 @@ public final class NetconfSalKeystoreRpcs implements AutoCloseable { return rpcResult; } - - public ClassToInstanceMap> getRpcClassToInstanceMap() { - return ImmutableClassToInstanceMap.>builder() - .put(RemoveKeystoreEntry.class, this::removeKeystoreEntry) - .put(AddKeystoreEntry.class, this::addKeystoreEntry) - .put(AddTrustedCertificate.class, this::addTrustedCertificate) - .put(RemoveTrustedCertificate.class, this::removeTrustedCertificate) - .put(AddPrivateKey.class, this::addPrivateKey) - .put(RemovePrivateKey.class, this::removePrivateKey) - .build(); - } } diff --git a/plugins/netconf-client-mdsal/src/test/java/org/opendaylight/netconf/client/mdsal/impl/NetconfSalKeystoreRpcsTest.java b/plugins/netconf-client-mdsal/src/test/java/org/opendaylight/netconf/client/mdsal/impl/NetconfSalKeystoreRpcsTest.java index 2a5460252c..18e357e4c9 100644 --- a/plugins/netconf-client-mdsal/src/test/java/org/opendaylight/netconf/client/mdsal/impl/NetconfSalKeystoreRpcsTest.java +++ b/plugins/netconf-client-mdsal/src/test/java/org/opendaylight/netconf/client/mdsal/impl/NetconfSalKeystoreRpcsTest.java @@ -29,10 +29,8 @@ import org.opendaylight.mdsal.binding.api.RpcProviderService; import org.opendaylight.mdsal.binding.api.WriteTransaction; import org.opendaylight.mdsal.common.api.LogicalDatastoreType; import org.opendaylight.netconf.api.xml.XmlUtil; -import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddPrivateKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddPrivateKeyInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddPrivateKeyInputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddTrustedCertificate; import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddTrustedCertificateInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddTrustedCertificateInputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017._private.keys.PrivateKey; @@ -83,7 +81,7 @@ public class NetconfSalKeystoreRpcsTest { doReturn(emptyFluentFuture()).when(writeTx).commit(); try (var keystoreService = new NetconfSalKeystoreRpcs(dataBroker, encryptionService, rpcProvider)) { final AddPrivateKeyInput input = getPrivateKeyInput(); - keystoreService.getRpcClassToInstanceMap().getInstance(AddPrivateKey.class).invoke(input).get(); + keystoreService.addPrivateKey(input).get(); verify(writeTx, times(input.nonnullPrivateKey().size())) .merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(DataObject.class)); @@ -95,7 +93,7 @@ public class NetconfSalKeystoreRpcsTest { doReturn(emptyFluentFuture()).when(writeTx).commit(); try (var keystoreService = new NetconfSalKeystoreRpcs(dataBroker, encryptionService, rpcProvider)) { final var input = getTrustedCertificateInput(); - keystoreService.getRpcClassToInstanceMap().getInstance(AddTrustedCertificate.class).invoke(input).get(); + keystoreService.addTrustedCertificate(input).get(); verify(writeTx, times(input.nonnullTrustedCertificate().size())) .merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(DataObject.class)); -- 2.36.6