Adjust to yangtools-2.0.0/odlparent-3.0.0 changes
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / NetconfDevice.java
index 11efe5a25b1aedf45353003476623d43ce794cb6..2dc16331726e2857897b372bcd55745e3ad36daa 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.netconf.sal.connect.netconf;
 
-import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Predicates;
@@ -20,12 +19,14 @@ import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.ListeningExecutorService;
 import com.google.common.util.concurrent.MoreExecutors;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
 import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.stream.Collectors;
 import javax.annotation.concurrent.GuardedBy;
@@ -60,7 +61,6 @@ import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
 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.SchemaSourceException;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
@@ -68,7 +68,7 @@ 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.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
+import org.opendaylight.yangtools.yang.parser.rfc7950.repo.ASTSchemaSource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -80,10 +80,6 @@ public class NetconfDevice
 
     private static final Logger LOG = LoggerFactory.getLogger(NetconfDevice.class);
 
-    public static final Function<QName, SourceIdentifier> QNAME_TO_SOURCE_ID_FUNCTION =
-        input -> RevisionSourceIdentifier.create(input.getLocalName(),
-                    Optional.fromNullable(input.getFormattedRevision()));
-
     protected final RemoteDeviceId id;
     private final boolean reconnectOnSchemasChange;
 
@@ -95,7 +91,7 @@ public class NetconfDevice
     private final NetconfDeviceSchemasResolver stateSchemasResolver;
     private final NotificationHandler notificationHandler;
     protected final List<SchemaSourceRegistration<? extends SchemaSourceRepresentation>> sourceRegistrations =
-            Lists.newArrayList();
+            new ArrayList<>();
     @GuardedBy("this")
     private boolean connected = false;
 
@@ -169,7 +165,7 @@ public class NetconfDevice
             }
         };
 
-        Futures.addCallback(sourceResolverFuture, resolvedSourceCallback);
+        Futures.addCallback(sourceResolverFuture, resolvedSourceCallback, MoreExecutors.directExecutor());
     }
 
     private void registerToBaseNetconfStream(final NetconfDeviceRpc deviceRpc,
@@ -210,7 +206,7 @@ public class NetconfDevice
                 LOG.warn("Unable to subscribe to base notification stream. Schemas will not be reloaded on the fly",
                         throwable);
             }
-        });
+        }, MoreExecutors.directExecutor());
     }
 
     private boolean shouldListenOnSchemaChange(final NetconfSessionPreferences remoteSessionCapabilities) {
@@ -423,16 +419,20 @@ public class NetconfDevice
         }
 
         public Collection<SourceIdentifier> getRequiredSources() {
-            return Collections2.transform(requiredSources, QNAME_TO_SOURCE_ID_FUNCTION);
+            return Collections2.transform(requiredSources, DeviceSources::toSourceId);
         }
 
         public Collection<SourceIdentifier> getProvidedSources() {
-            return Collections2.transform(providedSources, QNAME_TO_SOURCE_ID_FUNCTION);
+            return Collections2.transform(providedSources, DeviceSources::toSourceId);
         }
 
         public SchemaSourceProvider<YangTextSchemaSource> getSourceProvider() {
             return sourceProvider;
         }
+
+        private static SourceIdentifier toSourceId(final QName input) {
+            return RevisionSourceIdentifier.create(input.getLocalName(), input.getRevision());
+        }
     }
 
     /**
@@ -466,15 +466,13 @@ public class NetconfDevice
         }
 
         private Collection<SourceIdentifier> filterMissingSources(final Collection<SourceIdentifier> requiredSources) {
-
             return requiredSources.parallelStream().filter(sourceIdentifier -> {
-                boolean remove = false;
                 try {
-                    schemaRepository.getSchemaSource(sourceIdentifier, ASTSchemaSource.class).checkedGet();
-                } catch (SchemaSourceException e) {
-                    remove = true;
+                    schemaRepository.getSchemaSource(sourceIdentifier, ASTSchemaSource.class).get();
+                    return false;
+                } catch (InterruptedException | ExecutionException e) {
+                    return true;
                 }
-                return remove;
             }).collect(Collectors.toList());
         }
 
@@ -486,9 +484,9 @@ public class NetconfDevice
             while (!requiredSources.isEmpty()) {
                 LOG.trace("{}: Trying to build schema context from {}", id, requiredSources);
                 try {
-                    final CheckedFuture<SchemaContext, SchemaResolutionException> schemaBuilderFuture =
-                            schemaContextFactory.createSchemaContext(requiredSources);
-                    final SchemaContext result = schemaBuilderFuture.checkedGet();
+                    final ListenableFuture<SchemaContext> schemaBuilderFuture = schemaContextFactory
+                            .createSchemaContext(requiredSources);
+                    final SchemaContext result = schemaBuilderFuture.get();
                     LOG.debug("{}: Schema context built successfully from {}", id, requiredSources);
                     final Collection<QName> filteredQNames = Sets.difference(deviceSources.getRequiredSourcesQName(),
                             capabilities.getUnresolvedCapabilites().keySet());
@@ -499,32 +497,34 @@ public class NetconfDevice
 
                     capabilities.addNonModuleBasedCapabilities(remoteSessionCapabilities
                             .getNonModuleCaps().stream().map(entry -> new AvailableCapabilityBuilder()
-                            .setCapability(entry).setCapabilityOrigin(
+                                    .setCapability(entry).setCapabilityOrigin(
                                             remoteSessionCapabilities.getNonModuleBasedCapsOrigin().get(entry)).build())
                             .collect(Collectors.toList()));
 
                     handleSalInitializationSuccess(result, remoteSessionCapabilities, getDeviceSpecificRpc(result));
                     return;
-                } catch (final Throwable t) {
-                    if (t instanceof MissingSchemaSourceException) {
-                        requiredSources =
-                                handleMissingSchemaSourceException(requiredSources, (MissingSchemaSourceException) t);
-                    } else if (t instanceof SchemaResolutionException) {
-                        // schemaBuilderFuture.checkedGet() throws only SchemaResolutionException
-                        // that might be wrapping a MissingSchemaSourceException so we need to look
-                        // at the cause of the exception to make sure we don't misinterpret it.
-                        if (t.getCause() instanceof MissingSchemaSourceException) {
-                            requiredSources = handleMissingSchemaSourceException(
-                                    requiredSources, (MissingSchemaSourceException) t.getCause());
-                            continue;
-                        }
-                        requiredSources =
-                                handleSchemaResolutionException(requiredSources, (SchemaResolutionException) t);
+                } catch (final ExecutionException e) {
+                    // schemaBuilderFuture.checkedGet() throws only SchemaResolutionException
+                    // that might be wrapping a MissingSchemaSourceException so we need to look
+                    // at the cause of the exception to make sure we don't misinterpret it.
+                    final Throwable cause = e.getCause();
+
+                    if (cause instanceof MissingSchemaSourceException) {
+                        requiredSources = handleMissingSchemaSourceException(
+                                requiredSources, (MissingSchemaSourceException) e.getCause());
+                        continue;
+                    }
+                    if (cause instanceof SchemaResolutionException) {
+                        requiredSources = handleSchemaResolutionException(requiredSources,
+                            (SchemaResolutionException) cause);
                     } else {
-                        // unknown error, fail
-                        handleSalInitializationFailure(t, listener);
+                        handleSalInitializationFailure(e, listener);
                         return;
                     }
+                } catch (final Exception e) {
+                    // unknown error, fail
+                    handleSalInitializationFailure(e, listener);
+                    return;
                 }
             }
             // No more sources, fail
@@ -607,12 +607,7 @@ public class NetconfDevice
                     continue;
                 }
 
-                final String rev = getNullableRev(identifier);
-                if (rev == null) {
-                    if (qname.getRevision() == null) {
-                        return qname;
-                    }
-                } else if (qname.getFormattedRevision().equals(rev)) {
+                if (identifier.getRevision().equals(qname.getRevision())) {
                     return qname;
                 }
             }
@@ -622,10 +617,5 @@ public class NetconfDevice
             // this capability will be removed from required sources and not reported as unresolved-capability
             return null;
         }
-
-        private String getNullableRev(final SourceIdentifier identifier) {
-            final String rev = identifier.getRevision();
-            return rev == null || SourceIdentifier.NOT_PRESENT_FORMATTED_REVISION.equals(rev) ? null : rev;
-        }
     }
 }