2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.connect.netconf;
10 import com.google.common.base.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
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.CheckedFuture;
18 import com.google.common.util.concurrent.FutureCallback;
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import com.google.common.util.concurrent.ListeningExecutorService;
22 import com.google.common.util.concurrent.MoreExecutors;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.LinkedList;
26 import java.util.List;
28 import java.util.concurrent.Callable;
29 import java.util.concurrent.ExecutorService;
30 import java.util.stream.Collectors;
31 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
32 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
33 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
34 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
35 import org.opendaylight.netconf.api.NetconfMessage;
36 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
37 import org.opendaylight.netconf.sal.connect.api.NetconfDeviceSchemas;
38 import org.opendaylight.netconf.sal.connect.api.NetconfDeviceSchemasResolver;
39 import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
40 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
41 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
42 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
43 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
44 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
45 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceRpc;
46 import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
47 import org.opendaylight.netconf.sal.connect.netconf.schema.YangLibrarySchemaYangSourceProvider;
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.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapabilityBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.unavailable.capabilities.UnavailableCapability;
55 import org.opendaylight.yangtools.yang.common.QName;
56 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
57 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
58 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
59 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
60 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
61 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
62 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
63 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
64 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
65 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
66 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
67 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
68 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
69 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
70 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
75 * This is a mediator between NetconfDeviceCommunicator and NetconfDeviceSalFacade.
77 public class NetconfDevice
78 implements RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> {
80 private static final Logger LOG = LoggerFactory.getLogger(NetconfDevice.class);
82 public static final Function<QName, SourceIdentifier> QNAME_TO_SOURCE_ID_FUNCTION =
83 input -> RevisionSourceIdentifier.create(input.getLocalName(),
84 Optional.fromNullable(input.getFormattedRevision()));
86 protected final RemoteDeviceId id;
87 private final boolean reconnectOnSchemasChange;
89 protected final SchemaContextFactory schemaContextFactory;
90 private final RemoteDeviceHandler<NetconfSessionPreferences> salFacade;
91 private final ListeningExecutorService processingExecutor;
92 protected final SchemaSourceRegistry schemaRegistry;
93 protected final SchemaRepository schemaRepository;
94 private final NetconfDeviceSchemasResolver stateSchemasResolver;
95 private final NotificationHandler notificationHandler;
96 protected final List<SchemaSourceRegistration<? extends SchemaSourceRepresentation>> sourceRegistrations =
99 // Message transformer is constructed once the schemas are available
100 private MessageTransformer<NetconfMessage> messageTransformer;
103 * Create rpc implementation capable of handling RPC for monitoring and notifications
104 * even before the schemas of remote device are downloaded.
106 static NetconfDeviceRpc getRpcForInitialization(final NetconfDeviceCommunicator listener,
107 final boolean notificationSupport) {
108 final BaseSchema baseSchema = notificationSupport
109 ? BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS
110 : BaseSchema.BASE_NETCONF_CTX;
112 return new NetconfDeviceRpc(baseSchema.getSchemaContext(), listener,
113 new NetconfMessageTransformer(baseSchema.getSchemaContext(), false, baseSchema));
116 public NetconfDevice(final SchemaResourcesDTO schemaResourcesDTO, final RemoteDeviceId id,
117 final RemoteDeviceHandler<NetconfSessionPreferences> salFacade,
118 final ExecutorService globalProcessingExecutor, final boolean reconnectOnSchemasChange) {
120 this.reconnectOnSchemasChange = reconnectOnSchemasChange;
121 this.schemaRegistry = schemaResourcesDTO.getSchemaRegistry();
122 this.schemaRepository = schemaResourcesDTO.getSchemaRepository();
123 this.schemaContextFactory = schemaResourcesDTO.getSchemaContextFactory();
124 this.salFacade = salFacade;
125 this.stateSchemasResolver = schemaResourcesDTO.getStateSchemasResolver();
126 this.processingExecutor = MoreExecutors.listeningDecorator(globalProcessingExecutor);
127 this.notificationHandler = new NotificationHandler(salFacade, id);
131 public void onRemoteSessionUp(final NetconfSessionPreferences remoteSessionCapabilities,
132 final NetconfDeviceCommunicator listener) {
133 // SchemaContext setup has to be performed in a dedicated thread since
134 // we are in a netty thread in this method
135 // Yang models are being downloaded in this method and it would cause a
136 // deadlock if we used the netty thread
137 // http://netty.io/wiki/thread-model.html
138 LOG.debug("{}: Session to remote device established with {}", id, remoteSessionCapabilities);
140 final NetconfDeviceRpc initRpc =
141 getRpcForInitialization(listener, remoteSessionCapabilities.isNotificationsSupported());
142 final DeviceSourcesResolver task =
143 new DeviceSourcesResolver(remoteSessionCapabilities, id, stateSchemasResolver, initRpc);
144 final ListenableFuture<DeviceSources> sourceResolverFuture = processingExecutor.submit(task);
146 if (shouldListenOnSchemaChange(remoteSessionCapabilities)) {
147 registerToBaseNetconfStream(initRpc, listener);
150 final FutureCallback<DeviceSources> resolvedSourceCallback = new FutureCallback<DeviceSources>() {
152 public void onSuccess(final DeviceSources result) {
153 addProvidedSourcesToSchemaRegistry(result);
157 private void setUpSchema(final DeviceSources result) {
158 processingExecutor.submit(new SchemaSetup(result, remoteSessionCapabilities, listener));
162 public void onFailure(final Throwable throwable) {
163 LOG.warn("{}: Unexpected error resolving device sources: {}", id, throwable);
164 handleSalInitializationFailure(throwable, listener);
168 Futures.addCallback(sourceResolverFuture, resolvedSourceCallback);
171 private void registerToBaseNetconfStream(final NetconfDeviceRpc deviceRpc,
172 final NetconfDeviceCommunicator listener) {
173 // TODO check whether the model describing create subscription is present in schema
174 // Perhaps add a default schema context to support create-subscription if the model was not provided
175 // (same as what we do for base netconf operations in transformer)
176 final CheckedFuture<DOMRpcResult, DOMRpcException> rpcResultListenableFuture = deviceRpc.invokeRpc(
177 NetconfMessageTransformUtil.toPath(NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_QNAME),
178 NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_CONTENT);
180 final NotificationHandler.NotificationFilter filter = new NotificationHandler.NotificationFilter() {
182 public Optional<DOMNotification> filterNotification(final DOMNotification notification) {
183 if (isCapabilityChanged(notification)) {
184 LOG.info("{}: Schemas change detected, reconnecting", id);
185 // Only disconnect is enough,
186 // the reconnecting nature of the connector will take care of reconnecting
187 listener.disconnect();
188 return Optional.absent();
190 return Optional.of(notification);
193 private boolean isCapabilityChanged(final DOMNotification notification) {
194 return notification.getBody().getNodeType().equals(NetconfCapabilityChange.QNAME);
198 Futures.addCallback(rpcResultListenableFuture, new FutureCallback<DOMRpcResult>() {
200 public void onSuccess(final DOMRpcResult domRpcResult) {
201 notificationHandler.addNotificationFilter(filter);
205 public void onFailure(final Throwable throwable) {
206 LOG.warn("Unable to subscribe to base notification stream. Schemas will not be reloaded on the fly",
212 private boolean shouldListenOnSchemaChange(final NetconfSessionPreferences remoteSessionCapabilities) {
213 return remoteSessionCapabilities.isNotificationsSupported() && reconnectOnSchemasChange;
216 void handleSalInitializationSuccess(final SchemaContext result,
217 final NetconfSessionPreferences remoteSessionCapabilities,
218 final DOMRpcService deviceRpc) {
219 final BaseSchema baseSchema =
220 remoteSessionCapabilities.isNotificationsSupported()
221 ? BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS : BaseSchema.BASE_NETCONF_CTX;
222 messageTransformer = new NetconfMessageTransformer(result, true, baseSchema);
224 updateTransformer(messageTransformer);
225 // salFacade.onDeviceConnected has to be called before the notification handler is initialized
226 salFacade.onDeviceConnected(result, remoteSessionCapabilities, deviceRpc);
227 notificationHandler.onRemoteSchemaUp(messageTransformer);
229 LOG.info("{}: Netconf connector initialized successfully", id);
232 void handleSalInitializationFailure(final Throwable throwable,
233 final RemoteDeviceCommunicator<NetconfMessage> listener) {
234 LOG.error("{}: Initialization in sal failed, disconnecting from device", id, throwable);
236 onRemoteSessionDown();
237 resetMessageTransformer();
241 * Set the transformer to null as is in initial state.
243 private void resetMessageTransformer() {
244 updateTransformer(null);
247 private void updateTransformer(final MessageTransformer<NetconfMessage> transformer) {
248 messageTransformer = transformer;
251 private void addProvidedSourcesToSchemaRegistry(final DeviceSources deviceSources) {
252 final SchemaSourceProvider<YangTextSchemaSource> yangProvider = deviceSources.getSourceProvider();
253 for (final SourceIdentifier sourceId : deviceSources.getProvidedSources()) {
254 sourceRegistrations.add(schemaRegistry.registerSchemaSource(yangProvider,
255 PotentialSchemaSource.create(
256 sourceId, YangTextSchemaSource.class, PotentialSchemaSource.Costs.REMOTE_IO.getValue())));
261 public void onRemoteSessionDown() {
262 notificationHandler.onRemoteSchemaDown();
264 salFacade.onDeviceDisconnected();
265 for (final SchemaSourceRegistration<? extends SchemaSourceRepresentation> sourceRegistration
266 : sourceRegistrations) {
267 sourceRegistration.close();
269 resetMessageTransformer();
273 public void onRemoteSessionFailed(final Throwable throwable) {
274 salFacade.onDeviceFailed(throwable);
278 public void onNotification(final NetconfMessage notification) {
279 notificationHandler.handleNotification(notification);
283 * Just a transfer object containing schema related dependencies. Injected in constructor.
285 public static class SchemaResourcesDTO {
286 private final SchemaSourceRegistry schemaRegistry;
287 private final SchemaRepository schemaRepository;
288 private final SchemaContextFactory schemaContextFactory;
289 private final NetconfDeviceSchemasResolver stateSchemasResolver;
291 public SchemaResourcesDTO(final SchemaSourceRegistry schemaRegistry,
292 final SchemaRepository schemaRepository,
293 final SchemaContextFactory schemaContextFactory,
294 final NetconfDeviceSchemasResolver deviceSchemasResolver) {
295 this.schemaRegistry = Preconditions.checkNotNull(schemaRegistry);
296 this.schemaRepository = Preconditions.checkNotNull(schemaRepository);
297 this.schemaContextFactory = Preconditions.checkNotNull(schemaContextFactory);
298 this.stateSchemasResolver = Preconditions.checkNotNull(deviceSchemasResolver);
301 public SchemaSourceRegistry getSchemaRegistry() {
302 return schemaRegistry;
305 public SchemaRepository getSchemaRepository() {
306 return schemaRepository;
309 public SchemaContextFactory getSchemaContextFactory() {
310 return schemaContextFactory;
313 public NetconfDeviceSchemasResolver getStateSchemasResolver() {
314 return stateSchemasResolver;
319 * Schema building callable.
321 private static class DeviceSourcesResolver implements Callable<DeviceSources> {
323 private final NetconfDeviceRpc deviceRpc;
324 private final NetconfSessionPreferences remoteSessionCapabilities;
325 private final RemoteDeviceId id;
326 private final NetconfDeviceSchemasResolver stateSchemasResolver;
328 DeviceSourcesResolver(final NetconfDeviceRpc deviceRpc,
329 final NetconfSessionPreferences remoteSessionCapabilities,
330 final RemoteDeviceId id, final NetconfDeviceSchemasResolver stateSchemasResolver) {
331 this.deviceRpc = deviceRpc;
332 this.remoteSessionCapabilities = remoteSessionCapabilities;
334 this.stateSchemasResolver = stateSchemasResolver;
337 DeviceSourcesResolver(final NetconfSessionPreferences remoteSessionCapabilities, final RemoteDeviceId id,
338 final NetconfDeviceSchemasResolver stateSchemasResolver,
339 final NetconfDeviceRpc rpcForMonitoring) {
340 this(rpcForMonitoring, remoteSessionCapabilities, id, stateSchemasResolver);
344 public DeviceSources call() throws Exception {
345 final NetconfDeviceSchemas availableSchemas =
346 stateSchemasResolver.resolve(deviceRpc, remoteSessionCapabilities, id);
347 LOG.debug("{}: Schemas exposed by ietf-netconf-monitoring: {}", id,
348 availableSchemas.getAvailableYangSchemasQNames());
350 final Set<QName> requiredSources = Sets.newHashSet(remoteSessionCapabilities.getModuleBasedCaps());
351 final Set<QName> providedSources = availableSchemas.getAvailableYangSchemasQNames();
353 final Set<QName> requiredSourcesNotProvided = Sets.difference(requiredSources, providedSources);
354 if (!requiredSourcesNotProvided.isEmpty()) {
355 LOG.warn("{}: Netconf device does not provide all yang models reported in hello message capabilities,"
356 + " required but not provided: {}", id, requiredSourcesNotProvided);
357 LOG.warn("{}: Attempting to build schema context from required sources", id);
360 // Here all the sources reported in netconf monitoring are merged with those reported in hello.
361 // It is necessary to perform this since submodules are not mentioned in hello but still required.
362 // This clashes with the option of a user to specify supported yang models manually in configuration
363 // for netconf-connector and as a result one is not able to fully override yang models of a device.
364 // It is only possible to add additional models.
365 final Set<QName> providedSourcesNotRequired = Sets.difference(providedSources, requiredSources);
366 if (!providedSourcesNotRequired.isEmpty()) {
367 LOG.warn("{}: Netconf device provides additional yang models not reported in "
368 + "hello message capabilities: {}", id, providedSourcesNotRequired);
369 LOG.warn("{}: Adding provided but not required sources as required to prevent failures", id);
370 LOG.debug("{}: Netconf device reported in hello: {}", id, requiredSources);
371 requiredSources.addAll(providedSourcesNotRequired);
374 final SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
375 if (availableSchemas instanceof LibraryModulesSchemas) {
376 sourceProvider = new YangLibrarySchemaYangSourceProvider(id,
377 ((LibraryModulesSchemas) availableSchemas).getAvailableModels());
379 sourceProvider = new NetconfRemoteSchemaYangSourceProvider(id, deviceRpc);
382 return new DeviceSources(requiredSources, providedSources, sourceProvider);
387 * Contains RequiredSources - sources from capabilities.
389 private static final class DeviceSources {
390 private final Set<QName> requiredSources;
391 private final Set<QName> providedSources;
392 private final SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
394 DeviceSources(final Set<QName> requiredSources, final Set<QName> providedSources,
395 final SchemaSourceProvider<YangTextSchemaSource> sourceProvider) {
396 this.requiredSources = requiredSources;
397 this.providedSources = providedSources;
398 this.sourceProvider = sourceProvider;
401 public Set<QName> getRequiredSourcesQName() {
402 return requiredSources;
405 public Set<QName> getProvidedSourcesQName() {
406 return providedSources;
409 public Collection<SourceIdentifier> getRequiredSources() {
410 return Collections2.transform(requiredSources, QNAME_TO_SOURCE_ID_FUNCTION);
413 public Collection<SourceIdentifier> getProvidedSources() {
414 return Collections2.transform(providedSources, QNAME_TO_SOURCE_ID_FUNCTION);
417 public SchemaSourceProvider<YangTextSchemaSource> getSourceProvider() {
418 return sourceProvider;
423 * Schema builder that tries to build schema context from provided sources or biggest subset of it.
425 private final class SchemaSetup implements Runnable {
426 private final DeviceSources deviceSources;
427 private final NetconfSessionPreferences remoteSessionCapabilities;
428 private final RemoteDeviceCommunicator<NetconfMessage> listener;
429 private final NetconfDeviceCapabilities capabilities;
431 SchemaSetup(final DeviceSources deviceSources, final NetconfSessionPreferences remoteSessionCapabilities,
432 final RemoteDeviceCommunicator<NetconfMessage> listener) {
433 this.deviceSources = deviceSources;
434 this.remoteSessionCapabilities = remoteSessionCapabilities;
435 this.listener = listener;
436 this.capabilities = remoteSessionCapabilities.getNetconfDeviceCapabilities();
442 final Collection<SourceIdentifier> requiredSources = deviceSources.getRequiredSources();
443 final Collection<SourceIdentifier> missingSources = filterMissingSources(requiredSources);
445 capabilities.addUnresolvedCapabilities(getQNameFromSourceIdentifiers(missingSources),
446 UnavailableCapability.FailureReason.MissingSource);
448 requiredSources.removeAll(missingSources);
449 setUpSchema(requiredSources);
452 private Collection<SourceIdentifier> filterMissingSources(final Collection<SourceIdentifier> requiredSources) {
454 return requiredSources.parallelStream().filter(sourceIdentifier -> {
455 boolean remove = false;
457 schemaRepository.getSchemaSource(sourceIdentifier, ASTSchemaSource.class).checkedGet();
458 } catch (SchemaSourceException e) {
462 }).collect(Collectors.toList());
466 * Build schema context, in case of success or final failure notify device.
468 @SuppressWarnings("checkstyle:IllegalCatch")
469 private void setUpSchema(Collection<SourceIdentifier> requiredSources) {
470 while (!requiredSources.isEmpty()) {
471 LOG.trace("{}: Trying to build schema context from {}", id, requiredSources);
473 final CheckedFuture<SchemaContext, SchemaResolutionException> schemaBuilderFuture =
474 schemaContextFactory.createSchemaContext(requiredSources);
475 final SchemaContext result = schemaBuilderFuture.checkedGet();
476 LOG.debug("{}: Schema context built successfully from {}", id, requiredSources);
477 final Collection<QName> filteredQNames = Sets.difference(deviceSources.getRequiredSourcesQName(),
478 capabilities.getUnresolvedCapabilites().keySet());
479 capabilities.addCapabilities(filteredQNames.stream().map(entry -> new AvailableCapabilityBuilder()
480 .setCapability(entry.toString()).setCapabilityOrigin(
481 remoteSessionCapabilities.getModuleBasedCapsOrigin().get(entry)).build())
482 .collect(Collectors.toList()));
484 capabilities.addNonModuleBasedCapabilities(remoteSessionCapabilities
485 .getNonModuleCaps().stream().map(entry -> new AvailableCapabilityBuilder()
486 .setCapability(entry).setCapabilityOrigin(
487 remoteSessionCapabilities.getNonModuleBasedCapsOrigin().get(entry)).build())
488 .collect(Collectors.toList()));
490 handleSalInitializationSuccess(result, remoteSessionCapabilities, getDeviceSpecificRpc(result));
492 } catch (final Throwable t) {
493 if (t instanceof MissingSchemaSourceException) {
495 handleMissingSchemaSourceException(requiredSources, (MissingSchemaSourceException) t);
496 } else if (t instanceof SchemaResolutionException) {
497 // schemaBuilderFuture.checkedGet() throws only SchemaResolutionException
498 // that might be wrapping a MissingSchemaSourceException so we need to look
499 // at the cause of the exception to make sure we don't misinterpret it.
500 if (t.getCause() instanceof MissingSchemaSourceException) {
501 requiredSources = handleMissingSchemaSourceException(
502 requiredSources, (MissingSchemaSourceException) t.getCause());
506 handleSchemaResolutionException(requiredSources, (SchemaResolutionException) t);
508 // unknown error, fail
509 handleSalInitializationFailure(t, listener);
514 // No more sources, fail
515 final IllegalStateException cause = new IllegalStateException(id + ": No more sources for schema context");
516 handleSalInitializationFailure(cause, listener);
517 salFacade.onDeviceFailed(cause);
520 private Collection<SourceIdentifier> handleMissingSchemaSourceException(
521 final Collection<SourceIdentifier> requiredSources, final MissingSchemaSourceException exception) {
522 // In case source missing, try without it
523 final SourceIdentifier missingSource = exception.getSourceId();
524 LOG.warn("{}: Unable to build schema context, missing source {}, will reattempt without it", id,
526 LOG.debug("{}: Unable to build schema context, missing source {}, will reattempt without it",
528 final Collection<QName> qNameOfMissingSource =
529 getQNameFromSourceIdentifiers(Sets.newHashSet(missingSource));
530 if (!qNameOfMissingSource.isEmpty()) {
531 capabilities.addUnresolvedCapabilities(
532 qNameOfMissingSource, UnavailableCapability.FailureReason.MissingSource);
534 return stripUnavailableSource(requiredSources, missingSource);
537 private Collection<SourceIdentifier> handleSchemaResolutionException(
538 final Collection<SourceIdentifier> requiredSources, final SchemaResolutionException resolutionException) {
539 // In case resolution error, try only with resolved sources
540 // There are two options why schema resolution exception occurred : unsatisfied imports or flawed model
541 // FIXME Do we really have assurance that these two cases cannot happen at once?
542 if (resolutionException.getFailedSource() != null) {
543 // flawed model - exclude it
544 final SourceIdentifier failedSourceId = resolutionException.getFailedSource();
545 LOG.warn("{}: Unable to build schema context, failed to resolve source {}, will reattempt without it",
547 LOG.warn("{}: Unable to build schema context, failed to resolve source {}, will reattempt without it",
548 id, resolutionException);
549 capabilities.addUnresolvedCapabilities(getQNameFromSourceIdentifiers(
550 Collections.singleton(failedSourceId)), UnavailableCapability.FailureReason.UnableToResolve);
551 return stripUnavailableSource(requiredSources, resolutionException.getFailedSource());
553 // unsatisfied imports
554 final Set<SourceIdentifier> unresolvedSources = resolutionException.getUnsatisfiedImports().keySet();
555 capabilities.addUnresolvedCapabilities(
556 getQNameFromSourceIdentifiers(unresolvedSources), UnavailableCapability.FailureReason.UnableToResolve);
557 LOG.warn("{}: Unable to build schema context, unsatisfied imports {}, will reattempt with resolved only",
558 id, resolutionException.getUnsatisfiedImports());
559 LOG.debug("{}: Unable to build schema context, unsatisfied imports {}, will reattempt with resolved only",
560 resolutionException);
561 return resolutionException.getResolvedSources();
564 protected NetconfDeviceRpc getDeviceSpecificRpc(final SchemaContext result) {
565 return new NetconfDeviceRpc(result, listener, new NetconfMessageTransformer(result, true));
568 private Collection<SourceIdentifier> stripUnavailableSource(final Collection<SourceIdentifier> requiredSources,
569 final SourceIdentifier sourceIdToRemove) {
570 final LinkedList<SourceIdentifier> sourceIdentifiers = Lists.newLinkedList(requiredSources);
571 final boolean removed = sourceIdentifiers.remove(sourceIdToRemove);
572 Preconditions.checkState(
573 removed, "{}: Trying to remove {} from {} failed", id, sourceIdToRemove, requiredSources);
574 return sourceIdentifiers;
577 private Collection<QName> getQNameFromSourceIdentifiers(final Collection<SourceIdentifier> identifiers) {
578 final Collection<QName> qNames = Collections2.transform(identifiers, this::getQNameFromSourceIdentifier);
580 if (qNames.isEmpty()) {
581 LOG.debug("{}: Unable to map any source identifiers to a capability reported by device : {}", id,
584 return Collections2.filter(qNames, Predicates.notNull());
587 private QName getQNameFromSourceIdentifier(final SourceIdentifier identifier) {
588 // Required sources are all required and provided merged in DeviceSourcesResolver
589 for (final QName qname : deviceSources.getRequiredSourcesQName()) {
590 if (!qname.getLocalName().equals(identifier.getName())) {
594 final String rev = getNullableRev(identifier);
596 if (qname.getRevision() == null) {
599 } else if (qname.getFormattedRevision().equals(rev)) {
603 LOG.warn("Unable to map identifier to a devices reported capability: {} Available: {}",identifier,
604 deviceSources.getRequiredSourcesQName());
605 // return null since we cannot find the QName,
606 // this capability will be removed from required sources and not reported as unresolved-capability
610 private String getNullableRev(final SourceIdentifier identifier) {
611 final String rev = identifier.getRevision();
612 return rev == null || SourceIdentifier.NOT_PRESENT_FORMATTED_REVISION.equals(rev) ? null : rev;