Return Registration on registerCapabilityListener() 82/104082/5
authorRobert Varga <[email protected]>
Thu, 19 Jan 2023 21:05:13 +0000 (22:05 +0100)
committerRobert Varga <[email protected]>
Fri, 20 Jan 2023 08:28:02 +0000 (09:28 +0100)
Registrations, as a concept resulting via architecture-level fiat,
cannot throw exceptions. Codify this in registerCapabilityListener(),
which should previously have returned ListenerRegistration. Also clarify
nullness of return types.

Change-Id: I9f9cd1638568280ee4ae64ea104a665aeaca19e6
Signed-off-by: Robert Varga <[email protected]>
18 files changed:
netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/netconf/mdsal/connector/CurrentSchemaContext.java
netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/netconf/mdsal/connector/MdsalNetconfOperationServiceFactory.java
netconf/mdsal-netconf-monitoring/src/main/java/org/opendaylight/controller/config/yang/netconf/mdsal/monitoring/MdsalMonitoringMapperFactory.java
netconf/mdsal-netconf-notification/src/main/java/org/opendaylight/netconf/mdsal/notification/impl/NetconfNotificationOperationServiceFactory.java
netconf/netconf-mapping-api/src/main/java/org/opendaylight/netconf/mapping/api/NetconfOperationService.java
netconf/netconf-mapping-api/src/main/java/org/opendaylight/netconf/mapping/api/NetconfOperationServiceFactory.java
netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/MdsalOperationProvider.java
netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/SimulatedOperationProvider.java
netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/customrpc/SettableOperationProvider.java
netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/monitoring/Get.java
netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/monitoring/NetconfMonitoringOperationService.java
netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/monitoring/NetconfMonitoringOperationServiceFactory.java
netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/operations/OperationsProvider.java
netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/rpchandler/SettableOperationRpcProvider.java
protocol/netconf-impl/src/main/java/org/opendaylight/netconf/impl/osgi/AggregatedNetconfOperationServiceFactory.java
protocol/netconf-impl/src/test/java/org/opendaylight/netconf/impl/ConcurrentClientsTest.java
protocol/netconf-impl/src/test/java/org/opendaylight/netconf/impl/mapping/operations/DefaultCloseSessionTest.java
protocol/netconf-impl/src/test/java/org/opendaylight/netconf/impl/osgi/AggregatedNetconfOperationServiceFactoryTest.java

index c46dfb51d90b6bcf0b5c899e35ad7df74999a896..3c1c79c776d2a86a6a2ae31dd13e1ffb22354267 100644 (file)
@@ -73,7 +73,7 @@ public class CurrentSchemaContext implements EffectiveModelContextListener, Auto
         currentContext.set(null);
     }
 
-    public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
+    public Registration registerCapabilityListener(final CapabilityListener listener) {
         listener.onCapabilitiesChanged(MdsalNetconfOperationServiceFactory.transformCapabilities(currentContext.get(),
                 rootSchemaSourceProvider), Collections.emptySet());
         listeners1.add(listener);
index d8b5ad86d30be2e5644421ae9774ff3f6d41c8b4..6d2483b6854ba281abe78ebb71d4b93dcdf99fd5 100644 (file)
@@ -30,6 +30,7 @@ import org.opendaylight.netconf.api.capability.YangModuleCapability;
 import org.opendaylight.netconf.api.monitoring.CapabilityListener;
 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.ModuleLike;
@@ -166,7 +167,7 @@ public final class MdsalNetconfOperationServiceFactory implements NetconfOperati
     }
 
     @Override
-    public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
+    public Registration registerCapabilityListener(final CapabilityListener listener) {
         // Advertise validate capability only if DOMDataBroker provides DOMDataTransactionValidator
         if (dataBroker.getExtensions().get(DOMDataTransactionValidator.class) != null) {
             listener.onCapabilitiesChanged(Collections.singleton(VALIDATE_CAPABILITY), Collections.emptySet());
index 56db7411dea5863f50abffd746fb29e51cdc54bd..a4fef8b3f552fd827c6f10fe50c7e1b0efc291c7 100644 (file)
@@ -15,6 +15,7 @@ import org.opendaylight.netconf.mapping.api.NetconfOperation;
 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
+import org.opendaylight.yangtools.concepts.Registration;
 
 public final class MdsalMonitoringMapperFactory implements NetconfOperationServiceFactory, AutoCloseable {
     private final MonitoringToMdsalWriter monitoringToMdsalWriter;
@@ -56,7 +57,7 @@ public final class MdsalMonitoringMapperFactory implements NetconfOperationServi
     }
 
     @Override
-    public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
+    public Registration registerCapabilityListener(final CapabilityListener listener) {
         return () -> { };
     }
 
index fc01a5fd3fd1a3da7041c7985d3cefe18a4ae5af..1f37ccf5c36288e0fbe078c312fc8b344112040f 100644 (file)
@@ -16,6 +16,7 @@ import org.opendaylight.netconf.mapping.api.NetconfOperationService;
 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
 import org.opendaylight.netconf.notifications.NetconfNotificationRegistry;
+import org.opendaylight.yangtools.concepts.Registration;
 
 public final class NetconfNotificationOperationServiceFactory implements NetconfOperationServiceFactory, AutoCloseable {
     private final NetconfNotificationRegistry netconfNotificationRegistry;
@@ -46,7 +47,7 @@ public final class NetconfNotificationOperationServiceFactory implements Netconf
     }
 
     @Override
-    public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
+    public Registration registerCapabilityListener(final CapabilityListener listener) {
         return () -> { };
     }
 
index f7f633de19e5f49b83579590aef4d25b4c811f42..e7a903a66097f49acb86ef59c235098f907cd45e 100644 (file)
@@ -5,7 +5,6 @@
  * 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.netconf.mapping.api;
 
 import java.util.Set;
@@ -14,7 +13,6 @@ import java.util.Set;
  * Service of netconf operations.
  */
 public interface NetconfOperationService extends AutoCloseable {
-
     /**
      * Get set of netconf operations that are handled by this service.
      */
@@ -25,5 +23,4 @@ public interface NetconfOperationService extends AutoCloseable {
      */
     @Override
     void close();
-
 }
index 9458476e83943142be07e67d83dc27255921e73f..f38c059b590075779d74787ad8dcbc2ae73b1166 100644 (file)
@@ -5,12 +5,13 @@
  * 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.netconf.mapping.api;
 
 import java.util.Set;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.netconf.api.capability.Capability;
 import org.opendaylight.netconf.api.monitoring.CapabilityListener;
+import org.opendaylight.yangtools.concepts.Registration;
 
 /**
  * Factory that must be registered in OSGi service registry in order to be used
@@ -18,18 +19,16 @@ import org.opendaylight.netconf.api.monitoring.CapabilityListener;
  * {@link NetconfOperationService}.
  */
 public interface NetconfOperationServiceFactory {
-
     /**
      * Get capabilities supported by current operation service.
      */
-    Set<Capability> getCapabilities();
+    @NonNull Set<Capability> getCapabilities();
 
     /**
      * Supported capabilities may change over time, registering a listener allows for push based information
      * retrieval about current notifications.
      */
-    AutoCloseable registerCapabilityListener(CapabilityListener listener);
-
-    NetconfOperationService createService(String netconfSessionIdForReporting);
+    @NonNull Registration registerCapabilityListener(CapabilityListener listener);
 
+    @NonNull NetconfOperationService createService(String netconfSessionIdForReporting);
 }
index e8452afd570bdd57c77b3ffe1a4dd81bb63a9a85..3685f2172b37eb6dc6d36afd13b393b38b04def5 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.netconf.test.tool;
 
-import com.google.common.collect.Sets;
 import com.google.common.util.concurrent.MoreExecutors;
 import java.util.EnumMap;
 import java.util.HashMap;
@@ -43,6 +42,7 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.mon
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -85,10 +85,9 @@ class MdsalOperationProvider implements NetconfOperationServiceFactory {
     }
 
     @Override
-    public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
+    public Registration registerCapabilityListener(final CapabilityListener listener) {
         listener.onCapabilitiesChanged(caps, Set.of());
-        return () -> {
-        };
+        return () -> { };
     }
 
     @Override
@@ -143,7 +142,7 @@ class MdsalOperationProvider implements NetconfOperationServiceFactory {
             final Unlock unLock = new Unlock(currentSessionId);
             final DiscardChanges discardChanges = new DiscardChanges(currentSessionId, transactionProvider);
 
-            return Sets.newHashSet(get, getConfig, editConfig, commit, lock, unLock, discardChanges);
+            return Set.of(get, getConfig, editConfig, commit, lock, unLock, discardChanges);
         }
 
         @Override
index a84268625bd56e3dc17695a48b9f36099407079d..38aabec19f0ea60a164229527111a4ed337e9ea1 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.netconf.test.tool;
 
 import com.google.common.collect.Sets;
 import java.io.File;
-import java.util.Collections;
 import java.util.Optional;
 import java.util.Set;
 import org.opendaylight.netconf.api.capability.Capability;
@@ -27,6 +26,7 @@ import org.opendaylight.netconf.test.tool.rpc.SimulatedGet;
 import org.opendaylight.netconf.test.tool.rpc.SimulatedGetConfig;
 import org.opendaylight.netconf.test.tool.rpc.SimulatedLock;
 import org.opendaylight.netconf.test.tool.rpc.SimulatedUnLock;
+import org.opendaylight.yangtools.concepts.Registration;
 
 class SimulatedOperationProvider implements NetconfOperationServiceFactory {
     private final Set<Capability> caps;
@@ -47,11 +47,9 @@ class SimulatedOperationProvider implements NetconfOperationServiceFactory {
     }
 
     @Override
-    public AutoCloseable registerCapabilityListener(
-            final CapabilityListener listener) {
-        listener.onCapabilitiesChanged(caps, Collections.emptySet());
-        return () -> {
-        };
+    public Registration registerCapabilityListener(final CapabilityListener listener) {
+        listener.onCapabilitiesChanged(caps, Set.of());
+        return () -> { };
     }
 
     @Override
index 480643c6743efe0ccef4491c997e0ac4e62fc902..84f2fbeaa4897cf7ea5edbed2dfad12436a5bc75 100644 (file)
@@ -5,20 +5,18 @@
  * 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.netconf.test.tool.customrpc;
 
 import java.io.File;
-import java.util.Collections;
 import java.util.Set;
 import org.opendaylight.netconf.api.capability.Capability;
 import org.opendaylight.netconf.api.monitoring.CapabilityListener;
 import org.opendaylight.netconf.mapping.api.NetconfOperation;
 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
+import org.opendaylight.yangtools.concepts.Registration;
 
 public class SettableOperationProvider implements NetconfOperationServiceFactory {
-
     private final File rpcConfig;
 
     public SettableOperationProvider(final File rpcConfig) {
@@ -27,14 +25,12 @@ public class SettableOperationProvider implements NetconfOperationServiceFactory
 
     @Override
     public Set<Capability> getCapabilities() {
-        return Collections.emptySet();
+        return Set.of();
     }
 
     @Override
-    public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
-        return () -> {
-            //no op
-        };
+    public Registration registerCapabilityListener(final CapabilityListener listener) {
+        return () -> { };
     }
 
     @Override
@@ -43,16 +39,15 @@ public class SettableOperationProvider implements NetconfOperationServiceFactory
     }
 
     private static class SettableOperationService implements NetconfOperationService {
-
         private final SettableRpc rpc;
 
         SettableOperationService(final File rpcConfig) {
-            this.rpc = new SettableRpc(rpcConfig);
+            rpc = new SettableRpc(rpcConfig);
         }
 
         @Override
         public Set<NetconfOperation> getNetconfOperations() {
-            return Collections.singleton(rpc);
+            return Set.of(rpc);
         }
 
         @Override
index b47b9e74443b9cd1a4916a161ece1af723b9778b..ae504ace8187ee9773ee2a378c5ec8dc0ae2a811 100644 (file)
@@ -33,14 +33,6 @@ public class Get extends AbstractNetconfOperation {
         this.netconfMonitor = netconfMonitor;
     }
 
-    private Element getPlaceholder(final Document innerResult)
-            throws DocumentedException {
-        final XmlElement rootElement = XmlElement.fromDomElementWithExpected(
-                innerResult.getDocumentElement(), XmlNetconfConstants.RPC_REPLY_KEY,
-                XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
-        return rootElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY).getDomElement();
-    }
-
     @Override
     protected String getOperationName() {
         return XmlNetconfConstants.GET;
@@ -87,4 +79,11 @@ public class Get extends AbstractNetconfOperation {
                              final NetconfOperationChainedExecution subsequentOperation) {
         throw new UnsupportedOperationException("Never gets called");
     }
+
+    private static Element getPlaceholder(final Document innerResult) throws DocumentedException {
+        return XmlElement.fromDomElementWithExpected(innerResult.getDocumentElement(),
+            XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0)
+            .getOnlyChildElement(XmlNetconfConstants.DATA_KEY)
+            .getDomElement();
+    }
 }
index 668cf763842cc7883540e2ede0cb5d240ebad741..ee89d8b95d43644ef55dc52840ef47f3dd4e73c8 100644 (file)
@@ -5,10 +5,8 @@
  * 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.netconf.test.tool.monitoring;
 
-import com.google.common.collect.Sets;
 import java.util.Set;
 import org.opendaylight.controller.config.yang.netconf.mdsal.monitoring.GetSchema;
 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
@@ -16,9 +14,6 @@ import org.opendaylight.netconf.mapping.api.NetconfOperation;
 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
 
 public class NetconfMonitoringOperationService implements NetconfOperationService {
-
-    private static final String TESTTOOL_SESSION = "testtool-session";
-
     private final NetconfMonitoringService monitor;
 
     public NetconfMonitoringOperationService(final NetconfMonitoringService monitor) {
@@ -27,11 +22,12 @@ public class NetconfMonitoringOperationService implements NetconfOperationServic
 
     @Override
     public Set<NetconfOperation> getNetconfOperations() {
-        return Sets.newHashSet(new Get(monitor), new GetSchema(TESTTOOL_SESSION, monitor));
+        return Set.of(new Get(monitor), new GetSchema("testtool-session", monitor));
     }
 
     @Override
     public void close() {
+        // No-op
     }
 
 }
index 51820f4750c1d3466cbb97054d8df78e419f7585..7f3513a806cf467ce739a6fa37be89b5eedc8d4f 100644 (file)
@@ -5,27 +5,23 @@
  * 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.netconf.test.tool.monitoring;
 
-import java.util.Collections;
+import static java.util.Objects.requireNonNull;
+
 import java.util.Set;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.netconf.api.capability.Capability;
 import org.opendaylight.netconf.api.monitoring.CapabilityListener;
 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
+import org.opendaylight.yangtools.concepts.Registration;
 
-public class NetconfMonitoringOperationServiceFactory implements NetconfOperationServiceFactory,
-        AutoCloseable {
-
-    private final NetconfMonitoringOperationService operationService;
-
-    private static final AutoCloseable AUTO_CLOSEABLE = () -> {
-        // NOOP
-    };
+public class NetconfMonitoringOperationServiceFactory implements NetconfOperationServiceFactory, AutoCloseable {
+    private final @NonNull NetconfMonitoringOperationService operationService;
 
     public NetconfMonitoringOperationServiceFactory(final NetconfMonitoringOperationService operationService) {
-        this.operationService = operationService;
+        this.operationService = requireNonNull(operationService);
     }
 
     @Override
@@ -35,16 +31,17 @@ public class NetconfMonitoringOperationServiceFactory implements NetconfOperatio
 
     @Override
     public Set<Capability> getCapabilities() {
-        return Collections.emptySet();
+        return Set.of();
     }
 
     @Override
-    public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
-        return AUTO_CLOSEABLE;
+    public Registration registerCapabilityListener(final CapabilityListener listener) {
+        return () -> { };
     }
 
     @Override
     public void close() {
+        // No-op
     }
 }
 
index 9c46c40bbc6eee7aaa0117ed34b19a45ed43a9a9..6e56e3d5f014928425edc2ba50a507b17e5f109e 100644 (file)
@@ -13,6 +13,7 @@ import org.opendaylight.netconf.api.monitoring.CapabilityListener;
 import org.opendaylight.netconf.impl.SessionIdProvider;
 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
+import org.opendaylight.yangtools.concepts.Registration;
 
 
 public class OperationsProvider implements NetconfOperationServiceFactory {
@@ -37,7 +38,7 @@ public class OperationsProvider implements NetconfOperationServiceFactory {
     }
 
     @Override
-    public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
+    public Registration registerCapabilityListener(final CapabilityListener listener) {
         listener.onCapabilitiesChanged(caps, Set.of());
         return () -> { };
     }
index dc74c9d604df9c497e689a3e743850d0134daf85..bfc4818397f2c730e021599dcd52ace802797f8e 100644 (file)
@@ -8,32 +8,30 @@
 
 package org.opendaylight.netconf.test.tool.rpchandler;
 
-import java.util.Collections;
 import java.util.Set;
 import org.opendaylight.netconf.api.capability.Capability;
 import org.opendaylight.netconf.api.monitoring.CapabilityListener;
 import org.opendaylight.netconf.mapping.api.NetconfOperation;
 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
+import org.opendaylight.yangtools.concepts.Registration;
 
 public class SettableOperationRpcProvider implements NetconfOperationServiceFactory {
 
     private final RpcHandler rpcHandler;
 
-    public SettableOperationRpcProvider(RpcHandler rpcHandler) {
+    public SettableOperationRpcProvider(final RpcHandler rpcHandler) {
         this.rpcHandler = rpcHandler;
     }
 
     @Override
     public Set<Capability> getCapabilities() {
-        return Collections.emptySet();
+        return Set.of();
     }
 
     @Override
-    public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
-        return () -> {
-            //no op
-        };
+    public Registration registerCapabilityListener(final CapabilityListener listener) {
+        return () -> { };
     }
 
     @Override
@@ -42,16 +40,15 @@ public class SettableOperationRpcProvider implements NetconfOperationServiceFact
     }
 
     private static class SettableOperationService implements NetconfOperationService {
-
         private final SettableRpc rpc;
 
-        SettableOperationService(RpcHandler rpcHandler) {
-            this.rpc = new SettableRpc(rpcHandler);
+        SettableOperationService(final RpcHandler rpcHandler) {
+            rpc = new SettableRpc(rpcHandler);
         }
 
         @Override
         public Set<NetconfOperation> getNetconfOperations() {
-            return Collections.singleton(rpc);
+            return Set.of(rpc);
         }
 
         @Override
index 9166cc41f62cf2591029435f5c907234cd8b2ab8..d1dc45124a71bcfa5745214faea9200a24cea03a 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.netconf.impl.osgi;
 
 import com.google.common.collect.HashMultimap;
 import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.ImmutableSet.Builder;
 import com.google.common.collect.Multimap;
 import com.google.common.collect.Multimaps;
 import java.util.HashMap;
@@ -25,19 +24,16 @@ import org.opendaylight.netconf.mapping.api.NetconfOperationService;
 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
 import org.opendaylight.netconf.util.CloseableUtil;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.opendaylight.yangtools.concepts.AbstractRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
 
 /**
  * NetconfOperationService aggregator. Makes a collection of operation services accessible as one.
  */
 public final class AggregatedNetconfOperationServiceFactory
         implements NetconfOperationServiceFactory, NetconfOperationServiceFactoryListener, AutoCloseable {
-
-    private static final Logger LOG = LoggerFactory.getLogger(AggregatedNetconfOperationServiceFactory.class);
-
     private final Set<NetconfOperationServiceFactory> factories = ConcurrentHashMap.newKeySet();
-    private final Multimap<NetconfOperationServiceFactory, AutoCloseable> registrations =
+    private final Multimap<NetconfOperationServiceFactory, Registration> registrations =
             Multimaps.synchronizedMultimap(HashMultimap.create());
     private final Set<CapabilityListener> listeners = ConcurrentHashMap.newKeySet();
 
@@ -53,25 +49,14 @@ public final class AggregatedNetconfOperationServiceFactory
         factories.add(service);
 
         for (final CapabilityListener listener : listeners) {
-            AutoCloseable reg = service.registerCapabilityListener(listener);
-            registrations.put(service, reg);
+            registrations.put(service, service.registerCapabilityListener(listener));
         }
     }
 
-    @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
     public synchronized void onRemoveNetconfOperationServiceFactory(final NetconfOperationServiceFactory service) {
         factories.remove(service);
-
-        for (final AutoCloseable autoCloseable : registrations.get(service)) {
-            try {
-                autoCloseable.close();
-            } catch (Exception e) {
-                LOG.warn("Unable to close listener registration", e);
-            }
-        }
-
-        registrations.removeAll(service);
+        registrations.removeAll(service).forEach(Registration::close);
     }
 
     @Override
@@ -84,21 +69,24 @@ public final class AggregatedNetconfOperationServiceFactory
     }
 
     @Override
-    public synchronized AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
-        final Map<NetconfOperationServiceFactory, AutoCloseable> regs = new HashMap<>();
+    public synchronized Registration registerCapabilityListener(final CapabilityListener listener) {
+        final Map<NetconfOperationServiceFactory, Registration> regs = new HashMap<>();
 
         for (final NetconfOperationServiceFactory factory : factories) {
-            final AutoCloseable reg = factory.registerCapabilityListener(listener);
-            regs.put(factory, reg);
+            regs.put(factory, factory.registerCapabilityListener(listener));
         }
         listeners.add(listener);
 
-        return () -> {
-            synchronized (AggregatedNetconfOperationServiceFactory.this) {
-                listeners.remove(listener);
-                CloseableUtil.closeAll(regs.values());
-                for (final Map.Entry<NetconfOperationServiceFactory, AutoCloseable> reg : regs.entrySet()) {
-                    registrations.remove(reg.getKey(), reg.getValue());
+        return new AbstractRegistration() {
+
+            @Override
+            protected void removeRegistration() {
+                synchronized (AggregatedNetconfOperationServiceFactory.this) {
+                    listeners.remove(listener);
+                    regs.values().forEach(Registration::close);
+                    for (var reg : regs.entrySet()) {
+                        registrations.remove(reg.getKey(), reg.getValue());
+                    }
                 }
             }
         };
@@ -110,35 +98,28 @@ public final class AggregatedNetconfOperationServiceFactory
     }
 
     @Override
-    public synchronized void close() throws Exception {
+    public synchronized void close() {
         factories.clear();
-        for (AutoCloseable reg : registrations.values()) {
-            reg.close();
-        }
+        registrations.values().forEach(Registration::close);
         registrations.clear();
         listeners.clear();
     }
 
     private static final class AggregatedNetconfOperation implements NetconfOperationService {
-
-        private final Set<NetconfOperationService> services;
+        private final ImmutableSet<NetconfOperationService> services;
 
         AggregatedNetconfOperation(final Set<NetconfOperationServiceFactory> factories,
                                    final String netconfSessionIdForReporting) {
-            final Builder<NetconfOperationService> b = ImmutableSet.builder();
-            for (final NetconfOperationServiceFactory factory : factories) {
-                b.add(factory.createService(netconfSessionIdForReporting));
-            }
-            services = b.build();
+            services = factories.stream()
+                .map(factory -> factory.createService(netconfSessionIdForReporting))
+                .collect(ImmutableSet.toImmutableSet());
         }
 
         @Override
         public Set<NetconfOperation> getNetconfOperations() {
-            final Set<NetconfOperation> operations = new HashSet<>();
-            for (final NetconfOperationService service : services) {
-                operations.addAll(service.getNetconfOperations());
-            }
-            return operations;
+            return services.stream()
+                .flatMap(service -> service.getNetconfOperations().stream())
+                .collect(ImmutableSet.toImmutableSet());
         }
 
         @SuppressWarnings("checkstyle:IllegalCatch")
index 87a3cef60398b6be87317df0fc62959960332af7..f533e6ab1ad3d775c1a980a23c024fc83daf91ce 100644 (file)
@@ -73,6 +73,7 @@ import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
 import org.opendaylight.netconf.util.messages.NetconfMessageUtil;
 import org.opendaylight.netconf.util.test.XmlFileLoader;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.CapabilitiesBuilder;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
@@ -283,9 +284,9 @@ public class ConcurrentClientsTest {
         }
 
         @Override
-        public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
-            return () -> {
-            };
+        public Registration registerCapabilityListener(final CapabilityListener listener) {
+            // No-op
+            return () -> { };
         }
 
         @Override
index 44ecfc76e3b7c2674c0ab9d11c4ccaa54649bd9f..da4c9fb60d79ca1c06d3a8d69b1a49ed35e96a31 100644 (file)
@@ -63,7 +63,7 @@ public class DefaultCloseSessionTest {
 
         final ChannelPromise sendFuture = mock(ChannelPromise.class);
         doAnswer(invocation -> {
-            invocation.<GenericFutureListener>getArgument(0).operationComplete(sendFuture);
+            invocation.getArgument(0, GenericFutureListener.class).operationComplete(sendFuture);
             return null;
         }).when(sendFuture).addListener(any(GenericFutureListener.class));
         doReturn(sendFuture).when(channel).newPromise();
index 776fbf21b943a005ac5424a8d4d4a2d6773aee86..5fa86ef605f5e056ab485dc66afb82cfd9a91032 100644 (file)
@@ -25,6 +25,7 @@ import org.opendaylight.netconf.api.capability.BasicCapability;
 import org.opendaylight.netconf.api.capability.Capability;
 import org.opendaylight.netconf.api.monitoring.CapabilityListener;
 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
+import org.opendaylight.yangtools.concepts.Registration;
 
 @RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class AggregatedNetconfOperationServiceFactoryTest {
@@ -43,11 +44,11 @@ public class AggregatedNetconfOperationServiceFactoryTest {
     @Mock
     private NetconfOperationServiceFactory factory2;
     @Mock
-    private AutoCloseable autoCloseable1;
+    private Registration reg1;
     @Mock
-    private AutoCloseable autoCloseable2;
+    private Registration reg2;
     @Mock
-    private AutoCloseable autoCloseable3;
+    private Registration reg3;
 
     private AggregatedNetconfOperationServiceFactory aggregatedFactory;
 
@@ -64,19 +65,19 @@ public class AggregatedNetconfOperationServiceFactoryTest {
         aggregatedFactory.registerCapabilityListener(listener1);
         aggregatedFactory.registerCapabilityListener(listener2);
 
-        doReturn(autoCloseable1).when(factory1).registerCapabilityListener(listener1);
-        doReturn(autoCloseable2).when(factory1).registerCapabilityListener(listener2);
+        doReturn(reg1).when(factory1).registerCapabilityListener(listener1);
+        doReturn(reg2).when(factory1).registerCapabilityListener(listener2);
         doReturn(factory1Caps).when(factory1).getCapabilities();
 
-        doReturn(autoCloseable1).when(factory2).registerCapabilityListener(listener1);
-        doReturn(autoCloseable2).when(factory2).registerCapabilityListener(listener2);
+        doReturn(reg1).when(factory2).registerCapabilityListener(listener1);
+        doReturn(reg2).when(factory2).registerCapabilityListener(listener2);
         doReturn(factory2Caps).when(factory2).getCapabilities();
 
-        doNothing().when(autoCloseable1).close();
-        doNothing().when(autoCloseable2).close();
+        doNothing().when(reg1).close();
+        doNothing().when(reg2).close();
 
-        doReturn(autoCloseable3).when(factory1).registerCapabilityListener(listener3);
-        doReturn(autoCloseable3).when(factory2).registerCapabilityListener(listener3);
+        doReturn(reg3).when(factory1).registerCapabilityListener(listener3);
+        doReturn(reg3).when(factory2).registerCapabilityListener(listener3);
     }
 
     @Test
@@ -92,8 +93,8 @@ public class AggregatedNetconfOperationServiceFactoryTest {
         aggregatedFactory.onRemoveNetconfOperationServiceFactory(factory1);
         aggregatedFactory.onRemoveNetconfOperationServiceFactory(factory2);
 
-        verify(autoCloseable1, times(2)).close();
-        verify(autoCloseable2, times(2)).close();
+        verify(reg1, times(2)).close();
+        verify(reg2, times(2)).close();
     }
 
     @Test
@@ -120,7 +121,7 @@ public class AggregatedNetconfOperationServiceFactoryTest {
         aggregatedFactory.onAddNetconfOperationServiceFactory(factory1);
         aggregatedFactory.onAddNetconfOperationServiceFactory(factory2);
         aggregatedFactory.close();
-        verify(autoCloseable1, times(2)).close();
-        verify(autoCloseable2, times(2)).close();
+        verify(reg1, times(2)).close();
+        verify(reg2, times(2)).close();
     }
 }