35c341e9387082fafbf5bf69b984a33d88a7cdb3
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / impl / DefaultSchemaResourceManager.java
1 /*
2  * Copyright (c) 2020 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.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Strings;
14 import java.io.File;
15 import java.util.HashMap;
16 import java.util.Map;
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.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.ir.IRSchemaSource;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Reference;
36 import org.osgi.service.component.annotations.RequireServiceComponentRuntime;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Simple single-node implementation of the {@link SchemaResourceManager} contract. Operates on the specified base
42  * root directory, where a number of independent subdirectories are created, each for a global default and anything
43  * encountered based on configuration.
44  */
45 @Beta
46 @Singleton
47 @Component(immediate = true)
48 @RequireServiceComponentRuntime
49 public final class DefaultSchemaResourceManager implements SchemaResourceManager {
50     private static final Logger LOG = LoggerFactory.getLogger(DefaultSchemaResourceManager.class);
51
52     @GuardedBy("this")
53     private final Map<String, SchemaResourcesDTO> resources = new HashMap<>();
54     private final @NonNull SchemaResourcesDTO defaultResources;
55     private final YangParserFactory parserFactory;
56     private final String defaultSubdirectory;
57     private final String rootDirectory;
58
59     @Activate
60     @Inject
61     public DefaultSchemaResourceManager(@Reference final YangParserFactory parserFactory) {
62         this(parserFactory, "cache", "schema");
63     }
64
65     public DefaultSchemaResourceManager(final YangParserFactory parserFactory, final String rootDirectory,
66             final String defaultSubdirectory) {
67         this.parserFactory = requireNonNull(parserFactory);
68         this.rootDirectory = requireNonNull(rootDirectory);
69         this.defaultSubdirectory = requireNonNull(defaultSubdirectory);
70         defaultResources = createResources(defaultSubdirectory);
71         LOG.info("Schema Resource Manager instantiated on {}/{}", rootDirectory, defaultSubdirectory);
72     }
73
74     @Override
75     public SchemaResourcesDTO getSchemaResources(final NetconfNode node, final Object nodeId) {
76         final String subdir = node.getSchemaCacheDirectory();
77         if (defaultSubdirectory.equals(subdir)) {
78             // Fast path for default devices
79             return defaultResources;
80         }
81         if (Strings.isNullOrEmpty(subdir)) {
82             // FIXME: we probably want to change semantics here:
83             //        - update model to not allow empty name
84             //        - silently default to the default
85             LOG.warn("schema-cache-directory for {} is null or empty;  using the default {}", nodeId,
86                 defaultSubdirectory);
87             return defaultResources;
88         }
89
90         LOG.info("Netconf connector for device {} will use schema cache directory {} instead of {}", nodeId, subdir,
91             defaultSubdirectory);
92         return getSchemaResources(subdir);
93     }
94
95     private synchronized @NonNull SchemaResourcesDTO getSchemaResources(final String subdir) {
96         // Fast path for unusual devices
97         final SchemaResourcesDTO existing = resources.get(subdir);
98         if (existing != null) {
99             return existing;
100         }
101
102         final SchemaResourcesDTO created = createResources(subdir);
103         resources.put(subdir, created);
104         return created;
105     }
106
107     private @NonNull SchemaResourcesDTO createResources(final String subdir) {
108         // Setup the baseline empty registry
109         final SharedSchemaRepository repository = new SharedSchemaRepository(subdir, parserFactory);
110
111         // Teach the registry how to transform YANG text to IRSchemaSource internally
112         repository.registerSchemaSourceListener(TextToIRTransformer.create(repository, repository));
113
114         // Attach a soft cache of IRSchemaSource instances. This is important during convergence when we are fishing
115         // for a consistent set of modules, as it skips the need to re-parse the text sources multiple times. It also
116         // helps establishing different sets of contexts, as they can share this pre-made cache.
117         repository.registerSchemaSourceListener(
118             new SoftSchemaSourceCache<>(repository, IRSchemaSource.class));
119
120         // Attach the filesystem cache, providing persistence capability, so that restarts do not require us to
121         // re-populate the cache. This also acts as a side-load capability, as anything pre-populated into that
122         // directory will not be fetched from the device.
123         repository.registerSchemaSourceListener(new FilesystemSchemaSourceCache<>(repository,
124                 YangTextSchemaSource.class, new File(rootDirectory + File.separator + subdir)));
125
126         return new SchemaResourcesDTO(repository, repository,
127             repository.createEffectiveModelContextFactory(SchemaContextFactoryConfiguration.getDefault()),
128             new NetconfStateSchemasResolverImpl());
129     }
130 }