Return ImmutableSet from transformCapabilities() 46/108646/3
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 24 Oct 2023 21:03:23 +0000 (23:03 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 26 Oct 2023 04:00:29 +0000 (06:00 +0200)
Ditch use of Collections2 and modernize to return ImmutableSet, which
conveys the semantics.

JIRA: NETCONF-1106
Change-Id: Ic815d7236299a5da5737299b685fa13592fde2d8
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
plugins/netconf-server-mdsal/src/main/java/org/opendaylight/netconf/server/mdsal/CurrentSchemaContext.java
plugins/netconf-server-mdsal/src/main/java/org/opendaylight/netconf/server/mdsal/MdsalNetconfOperationServiceFactory.java
protocol/netconf-server/src/main/java/org/opendaylight/netconf/server/NetconfServerSessionNegotiatorFactory.java

index 07761374b2bf384cb97245ff034cfde9ad8069b2..7bb366c124a12ed3d06a2f92a3fce4135ae792bb 100644 (file)
@@ -15,7 +15,6 @@ import java.util.Set;
 import java.util.concurrent.atomic.AtomicReference;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
-import org.opendaylight.netconf.server.api.monitoring.Capability;
 import org.opendaylight.netconf.server.api.monitoring.CapabilityListener;
 import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
@@ -27,7 +26,7 @@ import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
 @SuppressWarnings("checkstyle:FinalClass")
 public class CurrentSchemaContext implements EffectiveModelContextListener, AutoCloseable {
     private final AtomicReference<EffectiveModelContext> currentContext = new AtomicReference<>();
-    private final Set<CapabilityListener> listeners1 = Collections.synchronizedSet(new HashSet<>());
+    private final Set<CapabilityListener> listeners = Collections.synchronizedSet(new HashSet<>());
     private final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProvider;
 
     private Registration schemaContextListenerListenerRegistration;
@@ -59,16 +58,16 @@ public class CurrentSchemaContext implements EffectiveModelContextListener, Auto
     public void onModelContextUpdated(final EffectiveModelContext schemaContext) {
         currentContext.set(schemaContext);
         // FIXME is notifying all the listeners from this callback wise ?
-        final Set<Capability> addedCaps = MdsalNetconfOperationServiceFactory.transformCapabilities(
-                currentContext.get(), rootSchemaSourceProvider);
-        for (final CapabilityListener listener : listeners1) {
+        final var addedCaps = MdsalNetconfOperationServiceFactory.transformCapabilities(schemaContext,
+            rootSchemaSourceProvider);
+        for (var listener : listeners) {
             listener.onCapabilitiesChanged(addedCaps, Set.of());
         }
     }
 
     @Override
     public void close() {
-        listeners1.clear();
+        listeners.clear();
         schemaContextListenerListenerRegistration.close();
         currentContext.set(null);
     }
@@ -76,7 +75,7 @@ public class CurrentSchemaContext implements EffectiveModelContextListener, Auto
     public Registration registerCapabilityListener(final CapabilityListener listener) {
         listener.onCapabilitiesChanged(MdsalNetconfOperationServiceFactory.transformCapabilities(currentContext.get(),
                 rootSchemaSourceProvider), Set.of());
-        listeners1.add(listener);
-        return () -> listeners1.remove(listener);
+        listeners.add(listener);
+        return () -> listeners.remove(listener);
     }
 }
index 3649430d242f6652fecbb91fb21d82131606fb13..75bcf3ed8045772bc51639310970628face036fa 100644 (file)
@@ -88,8 +88,7 @@ public final class MdsalNetconfOperationServiceFactory implements NetconfOperati
     }
 
     // FIXME: ImmutableSet
-    static Set<Capability> transformCapabilities(
-            final EffectiveModelContext currentContext,
+    static Set<Capability> transformCapabilities(final EffectiveModelContext currentContext,
             final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProviderDependency) {
         final var capabilities = new HashSet<Capability>();
 
index c93740eb5d998f5466964daf5e3a4535e50e15b8..19c1a191733e3eefeea81fa7b52fd84ab5bcb354 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.netconf.server;
 
-import com.google.common.collect.Collections2;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Sets;
 import io.netty.channel.Channel;
@@ -100,17 +99,18 @@ public class NetconfServerSessionNegotiatorFactory {
     public NetconfServerSessionNegotiator getSessionNegotiator(final Channel channel,
             final Promise<NetconfServerSession> promise) {
         final var sessionId = idProvider.getNextSessionId();
-        final var socketAddress = channel.parent() == null ? null : channel.parent().localAddress();
-        final var service = getOperationServiceForAddress(sessionId, socketAddress);
-        final var listener = new NetconfServerSessionListener(
-            new NetconfOperationRouterImpl(service, monitoringService, sessionId), monitoringService, service);
+        final var service = getOperationServiceForAddress(sessionId,
+            channel.parent() == null ? null : channel.parent().localAddress());
 
         return new NetconfServerSessionNegotiator(createHelloMessage(sessionId, monitoringService), sessionId, promise,
-            channel, timer, listener, connectionTimeoutMillis, maximumIncomingChunkSize);
+            channel, timer,
+            new NetconfServerSessionListener(new NetconfOperationRouterImpl(service, monitoringService, sessionId),
+                monitoringService, service),
+            connectionTimeoutMillis, maximumIncomingChunkSize);
     }
 
     protected NetconfOperationService getOperationServiceForAddress(final SessionIdType sessionId,
-                                                                    final SocketAddress socketAddress) {
+            final SocketAddress socketAddress) {
         return aggregatedOpService.createService(sessionId);
     }
 
@@ -125,7 +125,7 @@ public class NetconfServerSessionNegotiatorFactory {
             sessionId);
     }
 
-    public static Set<String> transformCapabilities(final Capabilities capabilities) {
-        return Sets.newHashSet(Collections2.transform(capabilities.getCapability(), Uri::getValue));
+    public static ImmutableSet<String> transformCapabilities(final Capabilities capabilities) {
+        return capabilities.requireCapability().stream().map(Uri::getValue).collect(ImmutableSet.toImmutableSet());
     }
 }