2 * Copyright (c) 2024 PANTHEON.tech, s.r.o. 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.client.mdsal.impl;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.collect.Sets;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import java.util.HashSet;
18 import java.util.concurrent.Executor;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.netconf.client.mdsal.LibraryModulesSchemas;
21 import org.opendaylight.netconf.client.mdsal.LibrarySchemaSourceProvider;
22 import org.opendaylight.netconf.client.mdsal.NetconfStateSchemasResolverImpl;
23 import org.opendaylight.netconf.client.mdsal.api.BaseNetconfSchema;
24 import org.opendaylight.netconf.client.mdsal.api.DeviceNetconfSchema;
25 import org.opendaylight.netconf.client.mdsal.api.DeviceNetconfSchemaProvider;
26 import org.opendaylight.netconf.client.mdsal.api.NetconfDeviceSchemasResolver;
27 import org.opendaylight.netconf.client.mdsal.api.NetconfRpcService;
28 import org.opendaylight.netconf.client.mdsal.api.NetconfSessionPreferences;
29 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
30 import org.opendaylight.yangtools.concepts.Registration;
31 import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory;
32 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
33 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
34 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
35 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 public final class DefaultDeviceNetconfSchemaProvider implements DeviceNetconfSchemaProvider {
41 private static final Logger LOG = LoggerFactory.getLogger(DefaultDeviceNetconfSchemaProvider.class);
43 // FIXME: resolver seems to be a useless indirection
44 private final NetconfDeviceSchemasResolver resolver;
45 private final @NonNull EffectiveModelContextFactory contextFactory;
46 private final @NonNull SchemaSourceRegistry registry;
47 private final @NonNull SchemaRepository repository;
48 // FIXME: private final Executor processingExecutor;
50 DefaultDeviceNetconfSchemaProvider(final SharedSchemaRepository repository) {
51 this(repository, repository,
52 repository.createEffectiveModelContextFactory(SchemaContextFactoryConfiguration.getDefault()),
53 new NetconfStateSchemasResolverImpl());
57 public DefaultDeviceNetconfSchemaProvider(final SchemaSourceRegistry registry, final SchemaRepository repository,
58 final EffectiveModelContextFactory contextFactory, final NetconfDeviceSchemasResolver resolver) {
59 this.registry = requireNonNull(registry);
60 this.repository = requireNonNull(repository);
61 this.contextFactory = requireNonNull(contextFactory);
62 this.resolver = requireNonNull(resolver);
66 public ListenableFuture<DeviceNetconfSchema> deviceNetconfSchemaFor(final RemoteDeviceId deviceId,
67 final NetconfSessionPreferences sessionPreferences, final NetconfRpcService deviceRpc,
68 final BaseNetconfSchema baseSchema, final Executor processingExecutor) {
71 final var futureSchemas = resolver.resolve(deviceRpc, sessionPreferences, deviceId, baseSchema.modelContext());
74 final var sourceResolverFuture = Futures.transform(futureSchemas, availableSchemas -> {
75 final var providedSources = availableSchemas.getAvailableYangSchemasQNames();
76 LOG.debug("{}: Schemas exposed by ietf-netconf-monitoring: {}", deviceId, providedSources);
78 final var requiredSources = new HashSet<>(sessionPreferences.moduleBasedCaps().keySet());
79 final var requiredSourcesNotProvided = Sets.difference(requiredSources, providedSources);
80 if (!requiredSourcesNotProvided.isEmpty()) {
81 LOG.warn("{}: Netconf device does not provide all yang models reported in hello message capabilities,"
82 + " required but not provided: {}", deviceId, requiredSourcesNotProvided);
83 LOG.warn("{}: Attempting to build schema context from required sources", deviceId);
86 // Here all the sources reported in netconf monitoring are merged with those reported in hello.
87 // It is necessary to perform this since submodules are not mentioned in hello but still required.
88 // This clashes with the option of a user to specify supported yang models manually in configuration
89 // for netconf-connector and as a result one is not able to fully override yang models of a device.
90 // It is only possible to add additional models.
91 final var providedSourcesNotRequired = Sets.difference(providedSources, requiredSources);
92 if (!providedSourcesNotRequired.isEmpty()) {
93 LOG.warn("{}: Netconf device provides additional yang models not reported in "
94 + "hello message capabilities: {}", deviceId, providedSourcesNotRequired);
95 LOG.warn("{}: Adding provided but not required sources as required to prevent failures", deviceId);
96 LOG.debug("{}: Netconf device reported in hello: {}", deviceId, requiredSources);
97 requiredSources.addAll(providedSourcesNotRequired);
100 // FIXME: this instanceof check is quite bad
101 final var sourceProvider = availableSchemas instanceof LibraryModulesSchemas libraryModule
102 ? new LibrarySchemaSourceProvider(libraryModule.getAvailableModels())
103 : new MonitoringSchemaSourceProvider(deviceId, deviceRpc);
104 return new DeviceSources(requiredSources, providedSources, sourceProvider);
105 }, MoreExecutors.directExecutor());
107 // Set up the EffectiveModelContext for the device
108 return Futures.transformAsync(sourceResolverFuture, deviceSources -> {
109 LOG.debug("{}: Resolved device sources to {}", deviceId, deviceSources);
111 // Register all sources with repository and start resolution
112 final var registrations = deviceSources.register(registry);
113 final var future = new SchemaSetup(repository, contextFactory, deviceId, deviceSources, sessionPreferences)
116 // Unregister sources once resolution is complete
117 future.addListener(() -> registrations.forEach(Registration::close), processingExecutor);
120 }, processingExecutor);
125 public SchemaRepository repository() {
131 public SchemaSourceRegistry registry() {
137 public EffectiveModelContextFactory contextFactory() {
138 return contextFactory;