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;
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;
{
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);
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());
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;
}
public void setSchemaContextFactory(final SchemaContextFactory schemaContextFactory) {
this.schemaContextFactory = schemaContextFactory;
}
+
+ public void setInstanceName(final String instanceName) {
+ this.instanceName = instanceName;
+ }
}
public class NetconfConnectorModuleFactory extends
org.opendaylight.controller.config.yang.md.sal.connector.netconf.AbstractNetconfConnectorModuleFactory {
- // TODO this should be injected
// Netconf devices have separated schema registry + factory from controller
private final SharedSchemaRepository repository = new SharedSchemaRepository(NAME);
private final SchemaContextFactory schemaContextFactory
final DynamicMBeanWithInstance old, final BundleContext bundleContext) throws Exception {
final NetconfConnectorModule module = (NetconfConnectorModule) super.createModule(instanceName, dependencyResolver,
old, bundleContext);
-
+ module.setInstanceName(instanceName);
module.setBundleContext(bundleContext);
module.setSchemaRegistry(repository);
module.setSchemaContextFactory(schemaContextFactory);
public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final BundleContext bundleContext) {
final NetconfConnectorModule module = (NetconfConnectorModule) super.createModule(instanceName, dependencyResolver,
bundleContext);
+ module.setInstanceName(instanceName);
module.setBundleContext(bundleContext);
module.setSchemaRegistry(repository);
module.setSchemaContextFactory(schemaContextFactory);