0d59f0edfbcfacfa3ece8ace9b2e7d27be2a48cc
[netconf.git] / plugins / netconf-client-mdsal / src / main / java / org / opendaylight / netconf / client / mdsal / 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.client.mdsal.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.client.mdsal.NetconfDevice.SchemaResourcesDTO;
22 import org.opendaylight.netconf.client.mdsal.NetconfStateSchemasResolverImpl;
23 import org.opendaylight.netconf.client.mdsal.api.SchemaResourceManager;
24 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
25 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
26 import org.opendaylight.yangtools.yang.model.repo.fs.FilesystemSchemaSourceCache;
27 import org.opendaylight.yangtools.yang.model.repo.spi.SoftSchemaSourceCache;
28 import org.opendaylight.yangtools.yang.model.spi.source.YangIRSource;
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;
38
39 /**
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.
43  */
44 @Beta
45 @Singleton
46 @Component(immediate = true)
47 @RequireServiceComponentRuntime
48 public final class DefaultSchemaResourceManager implements SchemaResourceManager {
49     private static final Logger LOG = LoggerFactory.getLogger(DefaultSchemaResourceManager.class);
50
51     @GuardedBy("this")
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;
57
58     @Activate
59     @Inject
60     public DefaultSchemaResourceManager(@Reference final YangParserFactory parserFactory) {
61         this(parserFactory, "cache", "schema");
62     }
63
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);
71     }
72
73     @Override
74     public SchemaResourcesDTO getSchemaResources(final String subdir, final Object nodeId) {
75         if (defaultSubdirectory.equals(subdir)) {
76             // Fast path for default devices
77             return defaultResources;
78         }
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,
84                 defaultSubdirectory);
85             return defaultResources;
86         }
87
88         LOG.info("Netconf connector for device {} will use schema cache directory {} instead of {}", nodeId, subdir,
89             defaultSubdirectory);
90         return getSchemaResources(subdir);
91     }
92
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) {
97             return existing;
98         }
99
100         final SchemaResourcesDTO created = createResources(subdir);
101         resources.put(subdir, created);
102         return created;
103     }
104
105     private @NonNull SchemaResourcesDTO createResources(final String subdir) {
106         // Setup the baseline empty registry
107         final SharedSchemaRepository repository = new SharedSchemaRepository(subdir, parserFactory);
108
109         // Teach the registry how to transform YANG text to IRSchemaSource internally
110         repository.registerSchemaSourceListener(TextToIRTransformer.create(repository, repository));
111
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, YangIRSource.class));
117
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                 YangTextSource.class, new File(rootDirectory + File.separator + subdir)));
123
124         return new SchemaResourcesDTO(repository, repository,
125             repository.createEffectiveModelContextFactory(SchemaContextFactoryConfiguration.getDefault()),
126             new NetconfStateSchemasResolverImpl());
127     }
128 }