2 * Copyright (c) 2020 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.sal.connect.impl;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Strings;
15 import java.util.HashMap;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.checkerframework.checker.lock.qual.GuardedBy;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.netconf.sal.connect.api.SchemaResourceManager;
22 import org.opendaylight.netconf.sal.connect.netconf.NetconfDevice.SchemaResourcesDTO;
23 import org.opendaylight.netconf.sal.connect.netconf.NetconfStateSchemasResolverImpl;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
25 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
26 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
27 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
28 import org.opendaylight.yangtools.yang.model.repo.util.InMemorySchemaSourceCache;
29 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
30 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.ASTSchemaSource;
31 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToASTTransformer;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Simple single-node implementation of the {@link SchemaResourceManager} contract. Operates on the specified base
37 * root directory, where a number of independent subdirectories are created, each for a global default and anything
38 * encountered based on configuration.
42 public final class DefaultSchemaResourceManager implements SchemaResourceManager {
43 private static final Logger LOG = LoggerFactory.getLogger(DefaultSchemaResourceManager.class);
46 private final Map<String, SchemaResourcesDTO> resources = new HashMap<>();
47 private final @NonNull SchemaResourcesDTO defaultResources;
48 private final String defaultSubdirectory;
49 private final String rootDirectory;
52 public DefaultSchemaResourceManager() {
53 this("cache", "schema");
56 public DefaultSchemaResourceManager(final String rootDirectory, final String defaultSubdirectory) {
57 this.rootDirectory = requireNonNull(rootDirectory);
58 this.defaultSubdirectory = requireNonNull(defaultSubdirectory);
59 this.defaultResources = createResources(defaultSubdirectory);
63 public SchemaResourcesDTO getSchemaResources(final NetconfNode node, final Object nodeId) {
64 final String subdir = node.getSchemaCacheDirectory();
65 if (defaultSubdirectory.equals(subdir)) {
66 // Fast path for default devices
67 return defaultResources;
69 if (Strings.isNullOrEmpty(subdir)) {
70 // FIXME: we probably want to change semantics here:
71 // - update model to not allow empty name
72 // - silently default to the default
73 LOG.warn("schema-cache-directory for {} is null or empty; using the default {}", nodeId,
75 return defaultResources;
78 LOG.info("Netconf connector for device {} will use schema cache directory {} instead of {}", nodeId, subdir,
80 return getSchemaResources(subdir);
83 private synchronized @NonNull SchemaResourcesDTO getSchemaResources(final String subdir) {
84 // Fast path for unusual devices
85 final SchemaResourcesDTO existing = resources.get(subdir);
86 if (existing != null) {
90 final SchemaResourcesDTO created = createResources(subdir);
91 resources.put(subdir, created);
95 private @NonNull SchemaResourcesDTO createResources(final String subdir) {
96 // Setup the baseline empty registry
97 // FIXME: add YangParserFactory argument
98 final SharedSchemaRepository repository = new SharedSchemaRepository(subdir);
100 // Teach the registry how to transform YANG text to ASTSchemaSource internally
101 repository.registerSchemaSourceListener(TextToASTTransformer.create(repository, repository));
103 // Attach a soft cache of ASTSchemaSource instances. This is important during convergence when we are fishing
104 // for a consistent set of modules, as it skips the need to re-parse the text sources multiple times. It also
105 // helps establishing different sets of contexts, as they can share this pre-made cache.
106 repository.registerSchemaSourceListener(
107 // FIXME: add knobs to control cache lifetime explicitly
108 InMemorySchemaSourceCache.createSoftCache(repository, ASTSchemaSource.class));
110 // Attach the filesystem cache, providing persistence capability, so that restarts do not require us to
111 // re-populate the cache. This also acts as a side-load capability, as anything pre-populated into that
112 // directory will not be fetched from the device.
113 repository.registerSchemaSourceListener(new FilesystemSchemaSourceCache<>(repository,
114 YangTextSchemaSource.class, new File(rootDirectory + File.separator + subdir)));
116 return new SchemaResourcesDTO(repository, repository,
117 repository.createEffectiveModelContextFactory(SchemaContextFactoryConfiguration.getDefault()),
118 new NetconfStateSchemasResolverImpl());