NetconfSessionPreferences is a record
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / DeviceSourcesResolver.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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 java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.Sets;
13 import java.util.HashSet;
14 import java.util.concurrent.Callable;
15 import org.opendaylight.netconf.sal.connect.api.NetconfDeviceSchemasResolver;
16 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
17 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceRpc;
18 import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
19 import org.opendaylight.netconf.sal.connect.netconf.schema.YangLibrarySchemaYangSourceProvider;
20 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseSchema;
21 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Schema building callable.
27  */
28 final class DeviceSourcesResolver implements Callable<DeviceSources> {
29     private static final Logger LOG = LoggerFactory.getLogger(DeviceSourcesResolver.class);
30
31     private final NetconfSessionPreferences remoteSessionCapabilities;
32     private final NetconfDeviceSchemasResolver stateSchemasResolver;
33     private final NetconfDeviceRpc deviceRpc;
34     private final BaseSchema baseSchema;
35     private final RemoteDeviceId id;
36
37     DeviceSourcesResolver(final RemoteDeviceId id, final BaseSchema baseSchema, final NetconfDeviceRpc deviceRpc,
38             final NetconfSessionPreferences remoteSessionCapabilities,
39             final NetconfDeviceSchemasResolver stateSchemasResolver) {
40         this.id = requireNonNull(id);
41         this.baseSchema = requireNonNull(baseSchema);
42         this.deviceRpc = requireNonNull(deviceRpc);
43         this.remoteSessionCapabilities = requireNonNull(remoteSessionCapabilities);
44         this.stateSchemasResolver = requireNonNull(stateSchemasResolver);
45     }
46
47     @Override
48     public DeviceSources call() {
49         final var availableSchemas = stateSchemasResolver.resolve(deviceRpc, remoteSessionCapabilities, id,
50             baseSchema.getEffectiveModelContext());
51         LOG.debug("{}: Schemas exposed by ietf-netconf-monitoring: {}", id,
52             availableSchemas.getAvailableYangSchemasQNames());
53
54         final var requiredSources = new HashSet<>(remoteSessionCapabilities.moduleBasedCaps().keySet());
55         final var providedSources = availableSchemas.getAvailableYangSchemasQNames();
56         final var requiredSourcesNotProvided = Sets.difference(requiredSources, providedSources);
57         if (!requiredSourcesNotProvided.isEmpty()) {
58             LOG.warn("{}: Netconf device does not provide all yang models reported in hello message capabilities,"
59                     + " required but not provided: {}", id, requiredSourcesNotProvided);
60             LOG.warn("{}: Attempting to build schema context from required sources", id);
61         }
62
63         // Here all the sources reported in netconf monitoring are merged with those reported in hello.
64         // It is necessary to perform this since submodules are not mentioned in hello but still required.
65         // This clashes with the option of a user to specify supported yang models manually in configuration
66         // for netconf-connector and as a result one is not able to fully override yang models of a device.
67         // It is only possible to add additional models.
68         final var providedSourcesNotRequired = Sets.difference(providedSources, requiredSources);
69         if (!providedSourcesNotRequired.isEmpty()) {
70             LOG.warn("{}: Netconf device provides additional yang models not reported in "
71                     + "hello message capabilities: {}", id, providedSourcesNotRequired);
72             LOG.warn("{}: Adding provided but not required sources as required to prevent failures", id);
73             LOG.debug("{}: Netconf device reported in hello: {}", id, requiredSources);
74             requiredSources.addAll(providedSourcesNotRequired);
75         }
76
77         final var sourceProvider = availableSchemas instanceof LibraryModulesSchemas libraryModule
78             ? new YangLibrarySchemaYangSourceProvider(id, libraryModule.getAvailableModels())
79                 : new NetconfRemoteSchemaYangSourceProvider(id, deviceRpc);
80         return new DeviceSources(requiredSources, providedSources, sourceProvider);
81     }
82 }