Move DeviceSources registration 17/103617/13
authorRobert Varga <[email protected]>
Tue, 6 Dec 2022 17:56:09 +0000 (18:56 +0100)
committerRobert Varga <[email protected]>
Thu, 8 Dec 2022 01:21:24 +0000 (02:21 +0100)
Minimize DeviceSources collection exposure by defining better APIs.
This improves isolation and potential clashes.

JIRA: NETCONF-920
Change-Id: I20132725b538f46680f2546d4c91c9960e3b367a
Signed-off-by: Robert Varga <[email protected]>
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/DeviceSources.java
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/NetconfDevice.java

index 1c771cf346e3da7e12ecb535cc43808dc303ec3f..7281332227fb7525c5461495a009ca3f2a71b420 100644 (file)
@@ -9,14 +9,17 @@ package org.opendaylight.netconf.sal.connect.netconf;
 
 import static java.util.Objects.requireNonNull;
 
-import com.google.common.collect.Collections2;
-import java.util.Collection;
+import java.util.List;
 import java.util.Set;
+import java.util.stream.Collectors;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
+import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
 
 /**
  * Contains RequiredSources - sources from capabilities.
@@ -41,16 +44,17 @@ final class DeviceSources {
         return providedSources;
     }
 
-    Collection<SourceIdentifier> getRequiredSources() {
-        return Collections2.transform(requiredSources, DeviceSources::toSourceId);
+    List<SourceIdentifier> getRequiredSources() {
+        return requiredSources.stream().map(DeviceSources::toSourceId).collect(Collectors.toList());
     }
 
-    Collection<SourceIdentifier> getProvidedSources() {
-        return Collections2.transform(providedSources, DeviceSources::toSourceId);
-    }
-
-    SchemaSourceProvider<YangTextSchemaSource> getSourceProvider() {
-        return sourceProvider;
+    List<Registration> register(final SchemaSourceRegistry schemaRegistry) {
+        return providedSources.stream()
+            .map(DeviceSources::toSourceId)
+            .map(sourceId -> schemaRegistry.registerSchemaSource(sourceProvider,
+                PotentialSchemaSource.create(sourceId, YangTextSchemaSource.class,
+                    PotentialSchemaSource.Costs.REMOTE_IO.getValue())))
+            .collect(Collectors.toUnmodifiableList());
     }
 
     private static SourceIdentifier toSourceId(final QName input) {
index 2a68e0948e12b073757b40d7dd02b91ba4bbab7d..c166a21fa518dcfcd67d177c8a02896900d3de87 100644 (file)
@@ -25,7 +25,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Objects;
 import java.util.Optional;
@@ -58,6 +57,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev19
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapabilityBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.unavailable.capabilities.UnavailableCapability;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
 import org.opendaylight.yangtools.rfc8528.data.util.EmptyMountPointContext;
 import org.opendaylight.yangtools.rfc8528.model.api.SchemaMountConstants;
@@ -75,9 +75,6 @@ import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
-import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
-import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
-import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -98,7 +95,7 @@ public class NetconfDevice implements RemoteDevice<NetconfDeviceCommunicator> {
     protected final SchemaSourceRegistry schemaRegistry;
     protected final SchemaRepository schemaRepository;
 
-    protected final List<SchemaSourceRegistration<?>> sourceRegistrations = new ArrayList<>();
+    protected final List<Registration> sourceRegistrations = new ArrayList<>();
 
     private final RemoteDeviceHandler salFacade;
     private final ListeningExecutorService processingExecutor;
@@ -288,12 +285,8 @@ public class NetconfDevice implements RemoteDevice<NetconfDeviceCommunicator> {
     private ListenableFuture<EffectiveModelContext> assembleSchemaContext(final DeviceSources deviceSources,
             final NetconfSessionPreferences remoteSessionCapabilities) {
         LOG.debug("{}: Resolved device sources to {}", id, deviceSources);
-        final SchemaSourceProvider<YangTextSchemaSource> yangProvider = deviceSources.getSourceProvider();
-        for (final SourceIdentifier sourceId : deviceSources.getProvidedSources()) {
-            sourceRegistrations.add(schemaRegistry.registerSchemaSource(yangProvider,
-                PotentialSchemaSource.create(sourceId, YangTextSchemaSource.class,
-                    PotentialSchemaSource.Costs.REMOTE_IO.getValue())));
-        }
+
+        sourceRegistrations.addAll(deviceSources.register(schemaRegistry));
 
         return new SchemaSetup(deviceSources, remoteSessionCapabilities).startResolution();
     }
@@ -340,7 +333,7 @@ public class NetconfDevice implements RemoteDevice<NetconfDeviceCommunicator> {
         notificationHandler.onRemoteSchemaDown();
 
         salFacade.onDeviceDisconnected();
-        sourceRegistrations.forEach(SchemaSourceRegistration::close);
+        sourceRegistrations.forEach(Registration::close);
         sourceRegistrations.clear();
         resetMessageTransformer();
     }
@@ -509,7 +502,7 @@ public class NetconfDevice implements RemoteDevice<NetconfDeviceCommunicator> {
             }
         }
 
-        private Collection<SourceIdentifier> filterMissingSources(final Collection<SourceIdentifier> origSources) {
+        private List<SourceIdentifier> filterMissingSources(final Collection<SourceIdentifier> origSources) {
             return origSources.parallelStream().filter(sourceIdentifier -> {
                 try {
                     schemaRepository.getSchemaSource(sourceIdentifier, YangTextSchemaSource.class).get();
@@ -520,7 +513,7 @@ public class NetconfDevice implements RemoteDevice<NetconfDeviceCommunicator> {
             }).collect(Collectors.toList());
         }
 
-        private Collection<SourceIdentifier> handleMissingSchemaSourceException(
+        private List<SourceIdentifier> handleMissingSchemaSourceException(
                 final MissingSchemaSourceException exception) {
             // In case source missing, try without it
             final SourceIdentifier missingSource = exception.getSourceId();
@@ -565,11 +558,11 @@ public class NetconfDevice implements RemoteDevice<NetconfDeviceCommunicator> {
             return resolutionException.getResolvedSources();
         }
 
-        private Collection<SourceIdentifier> stripUnavailableSource(final SourceIdentifier sourceIdToRemove) {
-            final LinkedList<SourceIdentifier> sourceIdentifiers = new LinkedList<>(requiredSources);
-            checkState(sourceIdentifiers.remove(sourceIdToRemove),
-                    "%s: Trying to remove %s from %s failed", id, sourceIdToRemove, requiredSources);
-            return sourceIdentifiers;
+        private List<SourceIdentifier> stripUnavailableSource(final SourceIdentifier sourceIdToRemove) {
+            final var tmp = new ArrayList<>(requiredSources);
+            checkState(tmp.remove(sourceIdToRemove), "%s: Trying to remove %s from %s failed", id, sourceIdToRemove,
+                requiredSources);
+            return tmp;
         }
 
         private Collection<QName> getQNameFromSourceIdentifiers(final Collection<SourceIdentifier> identifiers) {