Separate out DeviceSources(Resolver)
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / NetconfDevice.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.sal.connect.netconf;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.Predicates;
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.Lists;
16 import com.google.common.collect.Sets;
17 import com.google.common.util.concurrent.FutureCallback;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import com.google.common.util.concurrent.ListeningExecutorService;
21 import com.google.common.util.concurrent.MoreExecutors;
22 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
23 import io.netty.util.concurrent.EventExecutor;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.Optional;
30 import java.util.Set;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.TimeUnit;
33 import java.util.stream.Collectors;
34 import org.checkerframework.checker.lock.qual.GuardedBy;
35 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
36 import org.opendaylight.mdsal.dom.api.DOMRpcService;
37 import org.opendaylight.netconf.api.NetconfMessage;
38 import org.opendaylight.netconf.sal.connect.api.DeviceActionFactory;
39 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
40 import org.opendaylight.netconf.sal.connect.api.NetconfDeviceSchemasResolver;
41 import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
42 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
43 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
44 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
45 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
46 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
47 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceRpc;
48 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseSchema;
49 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
50 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
51 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.NetconfNodeAugmentedOptional;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapabilityBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.unavailable.capabilities.UnavailableCapability;
57 import org.opendaylight.yangtools.rcf8528.data.util.EmptyMountPointContext;
58 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
59 import org.opendaylight.yangtools.yang.common.QName;
60 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
61 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
62 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
63 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
64 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
65 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
66 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
67 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
68 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
69 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
70 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
73
74 /**
75  *  This is a mediator between NetconfDeviceCommunicator and NetconfDeviceSalFacade.
76  */
77 public class NetconfDevice
78         implements RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> {
79
80     @SuppressFBWarnings(value = "SLF4J_LOGGER_SHOULD_BE_PRIVATE",
81             justification = "Needed for common logging of related classes")
82     static final Logger LOG = LoggerFactory.getLogger(NetconfDevice.class);
83
84     protected final RemoteDeviceId id;
85     protected final SchemaContextFactory schemaContextFactory;
86     protected final SchemaSourceRegistry schemaRegistry;
87     protected final SchemaRepository schemaRepository;
88
89     protected final List<SchemaSourceRegistration<?>> sourceRegistrations = new ArrayList<>();
90
91     private final RemoteDeviceHandler<NetconfSessionPreferences> salFacade;
92     private final ListeningExecutorService processingExecutor;
93     private final DeviceActionFactory deviceActionFactory;
94     private final NetconfDeviceSchemasResolver stateSchemasResolver;
95     private final NotificationHandler notificationHandler;
96     private final boolean reconnectOnSchemasChange;
97     private final NetconfNode node;
98     private final EventExecutor eventExecutor;
99     private final NetconfNodeAugmentedOptional nodeOptional;
100
101     @GuardedBy("this")
102     private boolean connected = false;
103
104     // Message transformer is constructed once the schemas are available
105     private MessageTransformer<NetconfMessage> messageTransformer;
106
107     public NetconfDevice(final SchemaResourcesDTO schemaResourcesDTO, final RemoteDeviceId id,
108                          final RemoteDeviceHandler<NetconfSessionPreferences> salFacade,
109                          final ListeningExecutorService globalProcessingExecutor,
110                          final boolean reconnectOnSchemasChange) {
111         this(schemaResourcesDTO, id, salFacade, globalProcessingExecutor, reconnectOnSchemasChange, null, null, null,
112                 null);
113     }
114
115     public NetconfDevice(final SchemaResourcesDTO schemaResourcesDTO, final RemoteDeviceId id,
116             final RemoteDeviceHandler<NetconfSessionPreferences> salFacade,
117             final ListeningExecutorService globalProcessingExecutor, final boolean reconnectOnSchemasChange,
118             final DeviceActionFactory deviceActionFactory, final NetconfNode node, final EventExecutor eventExecutor,
119             final NetconfNodeAugmentedOptional nodeOptional) {
120         this.id = id;
121         this.reconnectOnSchemasChange = reconnectOnSchemasChange;
122         this.deviceActionFactory = deviceActionFactory;
123         this.node = node;
124         this.eventExecutor = eventExecutor;
125         this.nodeOptional = nodeOptional;
126         this.schemaRegistry = schemaResourcesDTO.getSchemaRegistry();
127         this.schemaRepository = schemaResourcesDTO.getSchemaRepository();
128         this.schemaContextFactory = schemaResourcesDTO.getSchemaContextFactory();
129         this.salFacade = salFacade;
130         this.stateSchemasResolver = schemaResourcesDTO.getStateSchemasResolver();
131         this.processingExecutor = requireNonNull(globalProcessingExecutor);
132         this.notificationHandler = new NotificationHandler(salFacade, id);
133     }
134
135     @Override
136     public void onRemoteSessionUp(final NetconfSessionPreferences remoteSessionCapabilities,
137                                   final NetconfDeviceCommunicator listener) {
138         // SchemaContext setup has to be performed in a dedicated thread since
139         // we are in a netty thread in this method
140         // Yang models are being downloaded in this method and it would cause a
141         // deadlock if we used the netty thread
142         // http://netty.io/wiki/thread-model.html
143         setConnected(true);
144         LOG.debug("{}: Session to remote device established with {}", id, remoteSessionCapabilities);
145
146         final BaseSchema baseSchema = resolveBaseSchema(remoteSessionCapabilities.isNotificationsSupported());
147         final NetconfDeviceRpc initRpc = new NetconfDeviceRpc(baseSchema.getSchemaContext(), listener,
148             new NetconfMessageTransformer(baseSchema.getMountPointContext(), false, baseSchema));
149         final ListenableFuture<DeviceSources> sourceResolverFuture = processingExecutor.submit(
150             new DeviceSourcesResolver(id, baseSchema, initRpc, remoteSessionCapabilities, stateSchemasResolver));
151
152         if (shouldListenOnSchemaChange(remoteSessionCapabilities)) {
153             registerToBaseNetconfStream(initRpc, listener);
154         }
155
156         final FutureCallback<DeviceSources> resolvedSourceCallback = new FutureCallback<DeviceSources>() {
157             @Override
158             public void onSuccess(final DeviceSources result) {
159                 addProvidedSourcesToSchemaRegistry(result);
160                 setUpSchema(result);
161             }
162
163             private void setUpSchema(final DeviceSources result) {
164                 processingExecutor.submit(new SchemaSetup(result, remoteSessionCapabilities, listener));
165             }
166
167             @Override
168             public void onFailure(final Throwable throwable) {
169                 LOG.warn("{}: Unexpected error resolving device sources", id, throwable);
170                 handleSalInitializationFailure(throwable, listener);
171             }
172         };
173
174         Futures.addCallback(sourceResolverFuture, resolvedSourceCallback, MoreExecutors.directExecutor());
175     }
176
177     private void registerToBaseNetconfStream(final NetconfDeviceRpc deviceRpc,
178                                              final NetconfDeviceCommunicator listener) {
179         // TODO check whether the model describing create subscription is present in schema
180         // Perhaps add a default schema context to support create-subscription if the model was not provided
181         // (same as what we do for base netconf operations in transformer)
182         final ListenableFuture<DOMRpcResult> rpcResultListenableFuture = deviceRpc.invokeRpc(
183                 NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_PATH,
184                 NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_CONTENT);
185
186         Futures.addCallback(rpcResultListenableFuture, new FutureCallback<DOMRpcResult>() {
187             @Override
188             public void onSuccess(final DOMRpcResult domRpcResult) {
189                 notificationHandler.addNotificationFilter(notification -> {
190                     if (NetconfCapabilityChange.QNAME.equals(notification.getBody().getNodeType())) {
191                         LOG.info("{}: Schemas change detected, reconnecting", id);
192                         // Only disconnect is enough,
193                         // the reconnecting nature of the connector will take care of reconnecting
194                         listener.disconnect();
195                         return Optional.empty();
196                     }
197                     return Optional.of(notification);
198                 });
199             }
200
201             @Override
202             public void onFailure(final Throwable throwable) {
203                 LOG.warn("Unable to subscribe to base notification stream. Schemas will not be reloaded on the fly",
204                         throwable);
205             }
206         }, MoreExecutors.directExecutor());
207     }
208
209     private boolean shouldListenOnSchemaChange(final NetconfSessionPreferences remoteSessionCapabilities) {
210         return remoteSessionCapabilities.isNotificationsSupported() && reconnectOnSchemasChange;
211     }
212
213     private synchronized void handleSalInitializationSuccess(final MountPointContext result,
214                                         final NetconfSessionPreferences remoteSessionCapabilities,
215                                         final DOMRpcService deviceRpc,
216                                         final RemoteDeviceCommunicator<NetconfMessage> listener) {
217         //NetconfDevice.SchemaSetup can complete after NetconfDeviceCommunicator was closed. In that case do nothing,
218         //since salFacade.onDeviceDisconnected was already called.
219         if (connected) {
220             final BaseSchema baseSchema =
221                 remoteSessionCapabilities.isNotificationsSupported()
222                         ? BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS : BaseSchema.BASE_NETCONF_CTX;
223             this.messageTransformer = new NetconfMessageTransformer(result, true, baseSchema);
224
225             // salFacade.onDeviceConnected has to be called before the notification handler is initialized
226             this.salFacade.onDeviceConnected(result, remoteSessionCapabilities, deviceRpc,
227                     this.deviceActionFactory == null ? null : this.deviceActionFactory.createDeviceAction(
228                             this.messageTransformer, listener, result.getSchemaContext()));
229             this.notificationHandler.onRemoteSchemaUp(this.messageTransformer);
230
231             LOG.info("{}: Netconf connector initialized successfully", id);
232         } else {
233             LOG.warn("{}: Device communicator was closed before schema setup finished.", id);
234         }
235     }
236
237     private void handleSalInitializationFailure(final Throwable throwable,
238                                                 final RemoteDeviceCommunicator<NetconfMessage> listener) {
239         LOG.error("{}: Initialization in sal failed, disconnecting from device", id, throwable);
240         listener.close();
241         onRemoteSessionDown();
242         resetMessageTransformer();
243     }
244
245     /**
246      * Set the transformer to null as is in initial state.
247      */
248     private void resetMessageTransformer() {
249         updateTransformer(null);
250     }
251
252     private synchronized void updateTransformer(final MessageTransformer<NetconfMessage> transformer) {
253         messageTransformer = transformer;
254     }
255
256     private synchronized void setConnected(final boolean connected) {
257         this.connected = connected;
258     }
259
260     private void addProvidedSourcesToSchemaRegistry(final DeviceSources deviceSources) {
261         final SchemaSourceProvider<YangTextSchemaSource> yangProvider = deviceSources.getSourceProvider();
262         for (final SourceIdentifier sourceId : deviceSources.getProvidedSources()) {
263             sourceRegistrations.add(schemaRegistry.registerSchemaSource(yangProvider,
264                     PotentialSchemaSource.create(
265                             sourceId, YangTextSchemaSource.class, PotentialSchemaSource.Costs.REMOTE_IO.getValue())));
266         }
267     }
268
269     @Override
270     public void onRemoteSessionDown() {
271         setConnected(false);
272         notificationHandler.onRemoteSchemaDown();
273
274         salFacade.onDeviceDisconnected();
275         sourceRegistrations.forEach(SchemaSourceRegistration::close);
276         sourceRegistrations.clear();
277         resetMessageTransformer();
278     }
279
280     @Override
281     public void onRemoteSessionFailed(final Throwable throwable) {
282         setConnected(false);
283         salFacade.onDeviceFailed(throwable);
284     }
285
286     @Override
287     public void onNotification(final NetconfMessage notification) {
288         notificationHandler.handleNotification(notification);
289     }
290
291     private static BaseSchema resolveBaseSchema(final boolean notificationSupport) {
292         return notificationSupport ? BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS : BaseSchema.BASE_NETCONF_CTX;
293     }
294
295     /**
296      * Just a transfer object containing schema related dependencies. Injected in constructor.
297      */
298     public static class SchemaResourcesDTO {
299         private final SchemaSourceRegistry schemaRegistry;
300         private final SchemaRepository schemaRepository;
301         private final SchemaContextFactory schemaContextFactory;
302         private final NetconfDeviceSchemasResolver stateSchemasResolver;
303
304         public SchemaResourcesDTO(final SchemaSourceRegistry schemaRegistry,
305                                   final SchemaRepository schemaRepository,
306                                   final SchemaContextFactory schemaContextFactory,
307                                   final NetconfDeviceSchemasResolver deviceSchemasResolver) {
308             this.schemaRegistry = requireNonNull(schemaRegistry);
309             this.schemaRepository = requireNonNull(schemaRepository);
310             this.schemaContextFactory = requireNonNull(schemaContextFactory);
311             this.stateSchemasResolver = requireNonNull(deviceSchemasResolver);
312         }
313
314         public SchemaSourceRegistry getSchemaRegistry() {
315             return schemaRegistry;
316         }
317
318         public SchemaRepository getSchemaRepository() {
319             return schemaRepository;
320         }
321
322         public SchemaContextFactory getSchemaContextFactory() {
323             return schemaContextFactory;
324         }
325
326         public NetconfDeviceSchemasResolver getStateSchemasResolver() {
327             return stateSchemasResolver;
328         }
329     }
330
331     /**
332      * Schema builder that tries to build schema context from provided sources or biggest subset of it.
333      */
334     private final class SchemaSetup implements Runnable {
335         private final DeviceSources deviceSources;
336         private final NetconfSessionPreferences remoteSessionCapabilities;
337         private final RemoteDeviceCommunicator<NetconfMessage> listener;
338         private final NetconfDeviceCapabilities capabilities;
339
340         SchemaSetup(final DeviceSources deviceSources, final NetconfSessionPreferences remoteSessionCapabilities,
341                            final RemoteDeviceCommunicator<NetconfMessage> listener) {
342             this.deviceSources = deviceSources;
343             this.remoteSessionCapabilities = remoteSessionCapabilities;
344             this.listener = listener;
345             this.capabilities = remoteSessionCapabilities.getNetconfDeviceCapabilities();
346         }
347
348         @Override
349         public void run() {
350
351             final Collection<SourceIdentifier> requiredSources = deviceSources.getRequiredSources();
352             final Collection<SourceIdentifier> missingSources = filterMissingSources(requiredSources);
353
354             capabilities.addUnresolvedCapabilities(getQNameFromSourceIdentifiers(missingSources),
355                     UnavailableCapability.FailureReason.MissingSource);
356
357             requiredSources.removeAll(missingSources);
358             setUpSchema(requiredSources);
359         }
360
361         private Collection<SourceIdentifier> filterMissingSources(final Collection<SourceIdentifier> requiredSources) {
362             return requiredSources.parallelStream().filter(sourceIdentifier -> {
363                 try {
364                     schemaRepository.getSchemaSource(sourceIdentifier, YangTextSchemaSource.class).get();
365                     return false;
366                 } catch (InterruptedException | ExecutionException e) {
367                     return true;
368                 }
369             }).collect(Collectors.toList());
370         }
371
372         /**
373          * Build schema context, in case of success or final failure notify device.
374          *
375          * @param requiredSources required sources
376          */
377         @SuppressWarnings("checkstyle:IllegalCatch")
378         private void setUpSchema(Collection<SourceIdentifier> requiredSources) {
379             while (!requiredSources.isEmpty()) {
380                 LOG.trace("{}: Trying to build schema context from {}", id, requiredSources);
381                 try {
382                     final ListenableFuture<SchemaContext> schemaBuilderFuture = schemaContextFactory
383                             .createSchemaContext(requiredSources);
384                     final SchemaContext result = schemaBuilderFuture.get();
385                     LOG.debug("{}: Schema context built successfully from {}", id, requiredSources);
386                     final Collection<QName> filteredQNames = Sets.difference(deviceSources.getRequiredSourcesQName(),
387                             capabilities.getUnresolvedCapabilites().keySet());
388                     capabilities.addCapabilities(filteredQNames.stream().map(entry -> new AvailableCapabilityBuilder()
389                             .setCapability(entry.toString()).setCapabilityOrigin(
390                                     remoteSessionCapabilities.getModuleBasedCapsOrigin().get(entry)).build())
391                             .collect(Collectors.toList()));
392
393                     capabilities.addNonModuleBasedCapabilities(remoteSessionCapabilities
394                             .getNonModuleCaps().stream().map(entry -> new AvailableCapabilityBuilder()
395                                     .setCapability(entry).setCapabilityOrigin(
396                                             remoteSessionCapabilities.getNonModuleBasedCapsOrigin().get(entry)).build())
397                             .collect(Collectors.toList()));
398
399                     final MountPointContext mountContext = new EmptyMountPointContext(result);
400                     handleSalInitializationSuccess(mountContext, remoteSessionCapabilities,
401                         getDeviceSpecificRpc(mountContext), listener);
402                     return;
403                 } catch (final ExecutionException e) {
404                     // schemaBuilderFuture.checkedGet() throws only SchemaResolutionException
405                     // that might be wrapping a MissingSchemaSourceException so we need to look
406                     // at the cause of the exception to make sure we don't misinterpret it.
407                     final Throwable cause = e.getCause();
408
409                     if (cause instanceof MissingSchemaSourceException) {
410                         requiredSources = handleMissingSchemaSourceException(
411                                 requiredSources, (MissingSchemaSourceException) cause);
412                         continue;
413                     }
414                     if (cause instanceof SchemaResolutionException) {
415                         requiredSources = handleSchemaResolutionException(requiredSources,
416                             (SchemaResolutionException) cause);
417                     } else {
418                         handleSalInitializationFailure(e, listener);
419                         return;
420                     }
421                 } catch (final Exception e) {
422                     // unknown error, fail
423                     handleSalInitializationFailure(e, listener);
424                     return;
425                 }
426             }
427             // No more sources, fail or try to reconnect
428             if (nodeOptional != null && nodeOptional.getIgnoreMissingSchemaSources().isAllowed()) {
429                 eventExecutor.schedule(() -> {
430                     LOG.warn("Reconnection is allowed! This can lead to unexpected errors at runtime.");
431                     LOG.warn("{} : No more sources for schema context.", id);
432                     LOG.info("{} : Try to remount device.", id);
433                     onRemoteSessionDown();
434                     salFacade.onDeviceReconnected(remoteSessionCapabilities, node);
435                 }, nodeOptional.getIgnoreMissingSchemaSources().getReconnectTime(), TimeUnit.MILLISECONDS);
436             } else {
437                 final IllegalStateException cause =
438                         new IllegalStateException(id + ": No more sources for schema context");
439                 handleSalInitializationFailure(cause, listener);
440                 salFacade.onDeviceFailed(cause);
441             }
442         }
443
444         private Collection<SourceIdentifier> handleMissingSchemaSourceException(
445                 final Collection<SourceIdentifier> requiredSources, final MissingSchemaSourceException exception) {
446             // In case source missing, try without it
447             final SourceIdentifier missingSource = exception.getSourceId();
448             LOG.warn("{}: Unable to build schema context, missing source {}, will reattempt without it",
449                 id, missingSource);
450             LOG.debug("{}: Unable to build schema context, missing source {}, will reattempt without it",
451                 id, missingSource, exception);
452             final Collection<QName> qNameOfMissingSource =
453                 getQNameFromSourceIdentifiers(Sets.newHashSet(missingSource));
454             if (!qNameOfMissingSource.isEmpty()) {
455                 capabilities.addUnresolvedCapabilities(
456                         qNameOfMissingSource, UnavailableCapability.FailureReason.MissingSource);
457             }
458             return stripUnavailableSource(requiredSources, missingSource);
459         }
460
461         private Collection<SourceIdentifier> handleSchemaResolutionException(
462             final Collection<SourceIdentifier> requiredSources, final SchemaResolutionException resolutionException) {
463             // In case resolution error, try only with resolved sources
464             // There are two options why schema resolution exception occurred : unsatisfied imports or flawed model
465             // FIXME Do we really have assurance that these two cases cannot happen at once?
466             if (resolutionException.getFailedSource() != null) {
467                 // flawed model - exclude it
468                 final SourceIdentifier failedSourceId = resolutionException.getFailedSource();
469                 LOG.warn("{}: Unable to build schema context, failed to resolve source {}, will reattempt without it",
470                     id, failedSourceId);
471                 LOG.warn("{}: Unable to build schema context, failed to resolve source {}, will reattempt without it",
472                     id, failedSourceId, resolutionException);
473                 capabilities.addUnresolvedCapabilities(
474                         getQNameFromSourceIdentifiers(Collections.singleton(failedSourceId)),
475                         UnavailableCapability.FailureReason.UnableToResolve);
476                 return stripUnavailableSource(requiredSources, resolutionException.getFailedSource());
477             }
478             // unsatisfied imports
479             final Set<SourceIdentifier> unresolvedSources = resolutionException.getUnsatisfiedImports().keySet();
480             capabilities.addUnresolvedCapabilities(getQNameFromSourceIdentifiers(unresolvedSources),
481                 UnavailableCapability.FailureReason.UnableToResolve);
482             LOG.warn("{}: Unable to build schema context, unsatisfied imports {}, will reattempt with resolved only",
483                 id, resolutionException.getUnsatisfiedImports());
484             LOG.debug("{}: Unable to build schema context, unsatisfied imports {}, will reattempt with resolved only",
485                 id, resolutionException.getUnsatisfiedImports(), resolutionException);
486             return resolutionException.getResolvedSources();
487         }
488
489         protected NetconfDeviceRpc getDeviceSpecificRpc(final MountPointContext result) {
490             return new NetconfDeviceRpc(result.getSchemaContext(), listener,
491                 new NetconfMessageTransformer(result, true));
492         }
493
494         private Collection<SourceIdentifier> stripUnavailableSource(final Collection<SourceIdentifier> requiredSources,
495                                                                     final SourceIdentifier sourceIdToRemove) {
496             final LinkedList<SourceIdentifier> sourceIdentifiers = Lists.newLinkedList(requiredSources);
497             checkState(sourceIdentifiers.remove(sourceIdToRemove),
498                     "%s: Trying to remove %s from %s failed", id, sourceIdToRemove, requiredSources);
499             return sourceIdentifiers;
500         }
501
502         private Collection<QName> getQNameFromSourceIdentifiers(final Collection<SourceIdentifier> identifiers) {
503             final Collection<QName> qNames = Collections2.transform(identifiers, this::getQNameFromSourceIdentifier);
504
505             if (qNames.isEmpty()) {
506                 LOG.debug("{}: Unable to map any source identifiers to a capability reported by device : {}", id,
507                         identifiers);
508             }
509             return Collections2.filter(qNames, Predicates.notNull());
510         }
511
512         private QName getQNameFromSourceIdentifier(final SourceIdentifier identifier) {
513             // Required sources are all required and provided merged in DeviceSourcesResolver
514             for (final QName qname : deviceSources.getRequiredSourcesQName()) {
515                 if (!qname.getLocalName().equals(identifier.getName())) {
516                     continue;
517                 }
518
519                 if (identifier.getRevision().equals(qname.getRevision())) {
520                     return qname;
521                 }
522             }
523             LOG.warn("Unable to map identifier to a devices reported capability: {} Available: {}",identifier,
524                     deviceSources.getRequiredSourcesQName());
525             // return null since we cannot find the QName,
526             // this capability will be removed from required sources and not reported as unresolved-capability
527             return null;
528         }
529     }
530 }