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