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.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
25 import org.opendaylight.yangtools.yang.model.repo.api.YangIRSchemaSource;
26 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
27 import org.opendaylight.yangtools.yang.model.repo.fs.FilesystemSchemaSourceCache;
28 import org.opendaylight.yangtools.yang.model.repo.spi.SoftSchemaSourceCache;
29 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
30 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
31 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
32 import org.osgi.service.component.annotations.Activate;
33 import org.osgi.service.component.annotations.Component;
34 import org.osgi.service.component.annotations.Reference;
35 import org.osgi.service.component.annotations.RequireServiceComponentRuntime;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Simple single-node implementation of the {@link SchemaResourceManager} contract. Operates on the specified base
41 * root directory, where a number of independent subdirectories are created, each for a global default and anything
42 * encountered based on configuration.
46 @Component(immediate = true)
47 @RequireServiceComponentRuntime
48 public final class DefaultSchemaResourceManager implements SchemaResourceManager {
49 private static final Logger LOG = LoggerFactory.getLogger(DefaultSchemaResourceManager.class);
52 private final Map<String, SchemaResourcesDTO> resources = new HashMap<>();
53 private final @NonNull SchemaResourcesDTO defaultResources;
54 private final YangParserFactory parserFactory;
55 private final String defaultSubdirectory;
56 private final String rootDirectory;
60 public DefaultSchemaResourceManager(@Reference final YangParserFactory parserFactory) {
61 this(parserFactory, "cache", "schema");
64 public DefaultSchemaResourceManager(final YangParserFactory parserFactory, final String rootDirectory,
65 final String defaultSubdirectory) {
66 this.parserFactory = requireNonNull(parserFactory);
67 this.rootDirectory = requireNonNull(rootDirectory);
68 this.defaultSubdirectory = requireNonNull(defaultSubdirectory);
69 defaultResources = createResources(defaultSubdirectory);
70 LOG.info("Schema Resource Manager instantiated on {}/{}", rootDirectory, defaultSubdirectory);
74 public SchemaResourcesDTO getSchemaResources(final String subdir, final Object nodeId) {
75 if (defaultSubdirectory.equals(subdir)) {
76 // Fast path for default devices
77 return defaultResources;
79 if (Strings.isNullOrEmpty(subdir)) {
80 // FIXME: we probably want to change semantics here:
81 // - update model to not allow empty name
82 // - silently default to the default
83 LOG.warn("schema-cache-directory for {} is null or empty; using the default {}", nodeId,
85 return defaultResources;
88 LOG.info("Netconf connector for device {} will use schema cache directory {} instead of {}", nodeId, subdir,
90 return getSchemaResources(subdir);
93 private synchronized @NonNull SchemaResourcesDTO getSchemaResources(final String subdir) {
94 // Fast path for unusual devices
95 final SchemaResourcesDTO existing = resources.get(subdir);
96 if (existing != null) {
100 final SchemaResourcesDTO created = createResources(subdir);
101 resources.put(subdir, created);
105 private @NonNull SchemaResourcesDTO createResources(final String subdir) {
106 // Setup the baseline empty registry
107 final SharedSchemaRepository repository = new SharedSchemaRepository(subdir, parserFactory);
109 // Teach the registry how to transform YANG text to IRSchemaSource internally
110 repository.registerSchemaSourceListener(TextToIRTransformer.create(repository, repository));
112 // Attach a soft cache of IRSchemaSource instances. This is important during convergence when we are fishing
113 // for a consistent set of modules, as it skips the need to re-parse the text sources multiple times. It also
114 // helps establishing different sets of contexts, as they can share this pre-made cache.
115 repository.registerSchemaSourceListener(
116 new SoftSchemaSourceCache<>(repository, YangIRSchemaSource.class));
118 // Attach the filesystem cache, providing persistence capability, so that restarts do not require us to
119 // re-populate the cache. This also acts as a side-load capability, as anything pre-populated into that
120 // directory will not be fetched from the device.
121 repository.registerSchemaSourceListener(new FilesystemSchemaSourceCache<>(repository,
122 YangTextSchemaSource.class, new File(rootDirectory + File.separator + subdir)));
124 return new SchemaResourcesDTO(repository, repository,
125 repository.createEffectiveModelContextFactory(SchemaContextFactoryConfiguration.getDefault()),
126 new NetconfStateSchemasResolverImpl());