Bug 4577 Allow specification of a distinct schema cache directory per netconf device
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / config / yang / md / sal / connector / netconf / NetconfConnectorModule.java
index 12e4855fbde9d8de73c049c2a10f2124c76f33bb..20221d788c345df7e695626b6297571c46ac6029 100644 (file)
@@ -11,7 +11,9 @@ import static org.opendaylight.controller.config.api.JmxAttributeValidationExcep
 import static org.opendaylight.controller.config.api.JmxAttributeValidationException.checkNotNull;
 
 import com.google.common.base.Optional;
+import com.google.common.base.Strings;
 import io.netty.util.concurrent.EventExecutor;
+import java.io.File;
 import java.math.BigDecimal;
 import java.net.InetSocketAddress;
 import java.util.List;
@@ -41,7 +43,13 @@ import org.opendaylight.protocol.framework.TimedReconnectStrategy;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Host;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
+import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
+import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
+import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
+import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
+import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
 import org.osgi.framework.BundleContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -53,10 +61,21 @@ public final class NetconfConnectorModule extends org.opendaylight.controller.co
 {
     private static final Logger logger = LoggerFactory.getLogger(NetconfConnectorModule.class);
 
+    /**
+     * Filesystem based caches are stored relative to the cache directory.
+     */
+    private static final String CACHE_DIRECTORY = "cache";
+
+    /**
+     * The default cache directory relative to <code>CACHE_DIRECTORY</code>
+     */
+    private static final String DEFAULT_CACHE_DIRECTORY = "schema";
+
     private BundleContext bundleContext;
     private Optional<NetconfSessionPreferences> userCapabilities;
     private SchemaSourceRegistry schemaRegistry;
     private SchemaContextFactory schemaContextFactory;
+    private String instanceName;
 
     public NetconfConnectorModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
         super(identifier, dependencyResolver);
@@ -141,8 +160,31 @@ public final class NetconfConnectorModule extends org.opendaylight.controller.co
             salFacade = new KeepaliveSalFacade(id, salFacade, executor, keepaliveDelay);
         }
 
+        final String moduleSchemaCacheDirectory = getSchemaCacheDirectory();
+        // If a custom schema cache directory is specified, then create the backing Repository,
+        // SchemaContextFactory and FilesystemSchemaSourceCache.  If the default is specified,
+        // the the defaults are used from NetconfConnectorModuleFactory.
+        if (!Strings.isNullOrEmpty(moduleSchemaCacheDirectory) &&
+                !moduleSchemaCacheDirectory.equals(DEFAULT_CACHE_DIRECTORY)) {
+
+            final SharedSchemaRepository repository = new SharedSchemaRepository(instanceName);
+            final SchemaContextFactory schemaContextFactory
+                    = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
+            setSchemaRegistry(repository);
+            setSchemaContextFactory(schemaContextFactory);
+
+            final FilesystemSchemaSourceCache<YangTextSchemaSource> deviceCache =
+                    createDeviceFilesystemCache(moduleSchemaCacheDirectory);
+            repository.registerSchemaSourceListener(deviceCache);
+            logger.info("Netconf connector for device {} will use schema cache directory {} instead of {}",
+                    instanceName, moduleSchemaCacheDirectory, DEFAULT_CACHE_DIRECTORY);
+        }
+
+        schemaRegistry.registerSchemaSourceListener(
+                TextToASTTransformer.create((SchemaRepository) schemaRegistry, schemaRegistry));
+
         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO =
-                new NetconfDevice.SchemaResourcesDTO(schemaRegistry, schemaContextFactory, new NetconfStateSchemas.NetconfStateSchemasResolverImpl());
+                new NetconfDevice.SchemaResourcesDTO(this.schemaRegistry, this.schemaContextFactory, new NetconfStateSchemas.NetconfStateSchemasResolverImpl());
 
         final NetconfDevice device =
                 new NetconfDevice(schemaResourcesDTO, id, salFacade, globalProcessingExecutor, getReconnectOnChangedSchema());
@@ -162,6 +204,17 @@ public final class NetconfConnectorModule extends org.opendaylight.controller.co
         return new SalConnectorCloseable(listener, salFacade);
     }
 
+    /**
+     * Creates a <code>FilesystemSchemaSourceCache</code> for the custom schema cache directory.
+     *
+     * @param schemaCacheDirectory The custom cache directory relative to "cache"
+     * @return A <code>FilesystemSchemaSourceCache</code> for the custom schema cache directory
+     */
+    private FilesystemSchemaSourceCache<YangTextSchemaSource> createDeviceFilesystemCache(final String schemaCacheDirectory) {
+        final String relativeSchemaCacheDirectory = CACHE_DIRECTORY + File.separator + schemaCacheDirectory;
+        return new FilesystemSchemaSourceCache<>(schemaRegistry, YangTextSchemaSource.class, new File(relativeSchemaCacheDirectory));
+    }
+
     private boolean shouldSendKeepalive() {
         return getKeepaliveDelay() > 0;
     }
@@ -274,4 +327,8 @@ public final class NetconfConnectorModule extends org.opendaylight.controller.co
     public void setSchemaContextFactory(final SchemaContextFactory schemaContextFactory) {
         this.schemaContextFactory = schemaContextFactory;
     }
+
+    public void setInstanceName(final String instanceName) {
+        this.instanceName = instanceName;
+    }
 }