Fix of build breakage caused by changes in Yangtools
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / NetconfDevice.java
index 1b8a820f4f04abd31673343b6a224179c0c37817..c5e36cfc44182490b078e0a6d171a0f10fb63511 100644 (file)
@@ -48,6 +48,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev15
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
+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.SchemaResolutionException;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
@@ -69,7 +70,8 @@ public class NetconfDevice implements RemoteDevice<NetconfSessionPreferences, Ne
     public static final Function<QName, SourceIdentifier> QNAME_TO_SOURCE_ID_FUNCTION = new Function<QName, SourceIdentifier>() {
         @Override
         public SourceIdentifier apply(final QName input) {
-            return new SourceIdentifier(input.getLocalName(), Optional.fromNullable(input.getFormattedRevision()));
+            return RevisionSourceIdentifier
+                    .create(input.getLocalName(), Optional.fromNullable(input.getFormattedRevision()));
         }
     };
 
@@ -87,11 +89,6 @@ public class NetconfDevice implements RemoteDevice<NetconfSessionPreferences, Ne
     // Message transformer is constructed once the schemas are available
     private MessageTransformer<NetconfMessage> messageTransformer;
 
-    public NetconfDevice(final SchemaResourcesDTO schemaResourcesDTO, final RemoteDeviceId id, final RemoteDeviceHandler<NetconfSessionPreferences> salFacade,
-                         final ExecutorService globalProcessingExecutor) {
-        this(schemaResourcesDTO, id, salFacade, globalProcessingExecutor, false);
-    }
-
     /**
      * Create rpc implementation capable of handling RPC for monitoring and notifications even before the schemas of remote device are downloaded
      */
@@ -103,9 +100,7 @@ public class NetconfDevice implements RemoteDevice<NetconfSessionPreferences, Ne
         return new NetconfDeviceRpc(baseSchema.getSchemaContext(), listener, new NetconfMessageTransformer(baseSchema.getSchemaContext(), false, baseSchema));
     }
 
-
-    // FIXME reduce parameters
-    public NetconfDevice(final SchemaResourcesDTO schemaResourcesDTO, final RemoteDeviceId id, final RemoteDeviceHandler<NetconfSessionPreferences> salFacade,
+    protected NetconfDevice(final SchemaResourcesDTO schemaResourcesDTO, final RemoteDeviceId id, final RemoteDeviceHandler<NetconfSessionPreferences> salFacade,
                          final ExecutorService globalProcessingExecutor, final boolean reconnectOnSchemasChange) {
         this.id = id;
         this.reconnectOnSchemasChange = reconnectOnSchemasChange;
@@ -407,23 +402,16 @@ public class NetconfDevice implements RemoteDevice<NetconfSessionPreferences, Ne
                     return;
                 } catch (Throwable t) {
                     if (t instanceof MissingSchemaSourceException){
-                        // In case source missing, try without it
-                        final SourceIdentifier missingSource = ((MissingSchemaSourceException) t).getSourceId();
-                        LOG.warn("{}: Unable to build schema context, missing source {}, will reattempt without it", id, missingSource);
-                        LOG.debug("{}: Unable to build schema context, missing source {}, will reattempt without it", t);
-                        final Collection<QName> qNameOfMissingSource = getQNameFromSourceIdentifiers(Sets.newHashSet(missingSource));
-                        if (!qNameOfMissingSource.isEmpty()) {
-                            capabilities.addUnresolvedCapabilities(qNameOfMissingSource, UnavailableCapability.FailureReason.MissingSource);
-                        }
-                        requiredSources = stripMissingSource(requiredSources, missingSource);
+                        requiredSources = handleMissingSchemaSourceException(requiredSources, (MissingSchemaSourceException) t);
                     } else if (t instanceof SchemaResolutionException) {
-                        // In case resolution error, try only with resolved sources
-                        SchemaResolutionException resolutionException = (SchemaResolutionException) t;
-                        final Set<SourceIdentifier> unresolvedSources = resolutionException.getUnsatisfiedImports().keySet();
-                        capabilities.addUnresolvedCapabilities(getQNameFromSourceIdentifiers(unresolvedSources), UnavailableCapability.FailureReason.UnableToResolve);
-                        LOG.warn("{}: Unable to build schema context, unsatisfied imports {}, will reattempt with resolved only", id, resolutionException.getUnsatisfiedImports());
-                        LOG.debug("{}: Unable to build schema context, unsatisfied imports {}, will reattempt with resolved only", resolutionException);
-                        requiredSources = resolutionException.getResolvedSources();
+                        // 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);
                     } else {
                         // unknown error, fail
                         handleSalInitializationFailure(t, listener);
@@ -437,6 +425,26 @@ public class NetconfDevice implements RemoteDevice<NetconfSessionPreferences, Ne
             salFacade.onDeviceFailed(cause);
         }
 
+        private Collection<SourceIdentifier> handleMissingSchemaSourceException(Collection<SourceIdentifier> requiredSources, final MissingSchemaSourceException t) {
+            // In case source missing, try without it
+            final SourceIdentifier missingSource = t.getSourceId();
+            LOG.warn("{}: Unable to build schema context, missing source {}, will reattempt without it", id, missingSource);
+            LOG.debug("{}: Unable to build schema context, missing source {}, will reattempt without it", t);
+            final Collection<QName> qNameOfMissingSource = getQNameFromSourceIdentifiers(Sets.newHashSet(missingSource));
+            if (!qNameOfMissingSource.isEmpty()) {
+                capabilities.addUnresolvedCapabilities(qNameOfMissingSource, UnavailableCapability.FailureReason.MissingSource);
+            }
+            return stripMissingSource(requiredSources, missingSource);
+        }
+
+        private Collection<SourceIdentifier> handleSchemaResolutionException(Collection<SourceIdentifier> requiredSources, final SchemaResolutionException resolutionException) {
+            // In case resolution error, try only with resolved sources
+            final Set<SourceIdentifier> unresolvedSources = resolutionException.getUnsatisfiedImports().keySet();
+            capabilities.addUnresolvedCapabilities(getQNameFromSourceIdentifiers(unresolvedSources), UnavailableCapability.FailureReason.UnableToResolve);
+            LOG.warn("{}: Unable to build schema context, unsatisfied imports {}, will reattempt with resolved only", id, resolutionException.getUnsatisfiedImports());
+            LOG.debug("{}: Unable to build schema context, unsatisfied imports {}, will reattempt with resolved only", resolutionException);
+            return resolutionException.getResolvedSources();
+        }
 
         protected NetconfDeviceRpc getDeviceSpecificRpc(final SchemaContext result) {
             return new NetconfDeviceRpc(result, listener, new NetconfMessageTransformer(result, true));