60f4cf556f10d613092454df11295e57558157fb
[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.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;
34
35 /**
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.
39  */
40 @Beta
41 @Singleton
42 public final class DefaultSchemaResourceManager implements SchemaResourceManager {
43     private static final Logger LOG = LoggerFactory.getLogger(DefaultSchemaResourceManager.class);
44
45     @GuardedBy("this")
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;
50
51     @Inject
52     public DefaultSchemaResourceManager() {
53         this("cache", "schema");
54     }
55
56     public DefaultSchemaResourceManager(final String rootDirectory, final String defaultSubdirectory) {
57         this.rootDirectory = requireNonNull(rootDirectory);
58         this.defaultSubdirectory = requireNonNull(defaultSubdirectory);
59         this.defaultResources = createResources(defaultSubdirectory);
60     }
61
62     @Override
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;
68         }
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,
74                 defaultSubdirectory);
75             return defaultResources;
76         }
77
78         LOG.info("Netconf connector for device {} will use schema cache directory {} instead of {}", nodeId, subdir,
79             defaultSubdirectory);
80         return getSchemaResources(subdir);
81     }
82
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) {
87             return existing;
88         }
89
90         final SchemaResourcesDTO created = createResources(subdir);
91         resources.put(subdir, created);
92         return created;
93     }
94
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);
99
100         // Teach the registry how to transform YANG text to ASTSchemaSource internally
101         repository.registerSchemaSourceListener(TextToASTTransformer.create(repository, repository));
102
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));
109
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)));
115
116         return new SchemaResourcesDTO(repository, repository,
117             repository.createEffectiveModelContextFactory(SchemaContextFactoryConfiguration.getDefault()),
118             new NetconfStateSchemasResolverImpl());
119     }
120 }