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.parser.api.YangParserFactory;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
27 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
28 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
29 import org.opendaylight.yangtools.yang.model.repo.util.InMemorySchemaSourceCache;
30 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
31 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Simple single-node implementation of the {@link SchemaResourceManager} contract. Operates on the specified base
38 * root directory, where a number of independent subdirectories are created, each for a global default and anything
39 * encountered based on configuration.
43 public final class DefaultSchemaResourceManager implements SchemaResourceManager {
44 private static final Logger LOG = LoggerFactory.getLogger(DefaultSchemaResourceManager.class);
47 private final Map<String, SchemaResourcesDTO> resources = new HashMap<>();
48 private final @NonNull SchemaResourcesDTO defaultResources;
49 private final YangParserFactory parserFactory;
50 private final String defaultSubdirectory;
51 private final String rootDirectory;
54 public DefaultSchemaResourceManager(final YangParserFactory parserFactory) {
55 this(parserFactory, "cache", "schema");
58 public DefaultSchemaResourceManager(final YangParserFactory parserFactory, final String rootDirectory,
59 final String defaultSubdirectory) {
60 this.parserFactory = requireNonNull(parserFactory);
61 this.rootDirectory = requireNonNull(rootDirectory);
62 this.defaultSubdirectory = requireNonNull(defaultSubdirectory);
63 this.defaultResources = createResources(defaultSubdirectory);
67 public SchemaResourcesDTO getSchemaResources(final NetconfNode node, final Object nodeId) {
68 final String subdir = node.getSchemaCacheDirectory();
69 if (defaultSubdirectory.equals(subdir)) {
70 // Fast path for default devices
71 return defaultResources;
73 if (Strings.isNullOrEmpty(subdir)) {
74 // FIXME: we probably want to change semantics here:
75 // - update model to not allow empty name
76 // - silently default to the default
77 LOG.warn("schema-cache-directory for {} is null or empty; using the default {}", nodeId,
79 return defaultResources;
82 LOG.info("Netconf connector for device {} will use schema cache directory {} instead of {}", nodeId, subdir,
84 return getSchemaResources(subdir);
87 private synchronized @NonNull SchemaResourcesDTO getSchemaResources(final String subdir) {
88 // Fast path for unusual devices
89 final SchemaResourcesDTO existing = resources.get(subdir);
90 if (existing != null) {
94 final SchemaResourcesDTO created = createResources(subdir);
95 resources.put(subdir, created);
99 private @NonNull SchemaResourcesDTO createResources(final String subdir) {
100 // Setup the baseline empty registry
101 final SharedSchemaRepository repository = new SharedSchemaRepository(subdir, parserFactory);
103 // Teach the registry how to transform YANG text to IRSchemaSource internally
104 repository.registerSchemaSourceListener(TextToIRTransformer.create(repository, repository));
106 // Attach a soft cache of IRSchemaSource instances. This is important during convergence when we are fishing
107 // for a consistent set of modules, as it skips the need to re-parse the text sources multiple times. It also
108 // helps establishing different sets of contexts, as they can share this pre-made cache.
109 repository.registerSchemaSourceListener(
110 // FIXME: add knobs to control cache lifetime explicitly
111 InMemorySchemaSourceCache.createSoftCache(repository, IRSchemaSource.class));
113 // Attach the filesystem cache, providing persistence capability, so that restarts do not require us to
114 // re-populate the cache. This also acts as a side-load capability, as anything pre-populated into that
115 // directory will not be fetched from the device.
116 repository.registerSchemaSourceListener(new FilesystemSchemaSourceCache<>(repository,
117 YangTextSchemaSource.class, new File(rootDirectory + File.separator + subdir)));
119 return new SchemaResourcesDTO(repository, repository,
120 repository.createEffectiveModelContextFactory(SchemaContextFactoryConfiguration.getDefault()),
121 new NetconfStateSchemasResolverImpl());