- private NetconfDevice.SchemaResourcesDTO setupSchemaCacheDTO(final NodeId nodeId, final NetconfNode node) {
- // Setup information related to the SchemaRegistry, SchemaResourceFactory, etc.
- NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = null;
- final String moduleSchemaCacheDirectory = node.getSchemaCacheDirectory();
- // Only checks to ensure the String is not empty or null; further checks related to directory accessibility
- // and file permissions are handled during the FilesystemSchemaSourceCache initialization.
- if (!Strings.isNullOrEmpty(moduleSchemaCacheDirectory)) {
- // If a custom schema cache directory is specified, create the backing DTO; otherwise, the SchemaRegistry
- // and SchemaContextFactory remain the default values.
- if (!moduleSchemaCacheDirectory.equals(NetconfTopologyUtils.DEFAULT_CACHE_DIRECTORY)) {
- // Multiple modules may be created at once; synchronize to avoid issues with data consistency among
- // threads.
- synchronized (schemaResourcesDTOs) {
- // Look for the cached DTO to reuse SchemaRegistry and SchemaContextFactory variables if
- // they already exist
- schemaResourcesDTO = schemaResourcesDTOs.get(moduleSchemaCacheDirectory);
- if (schemaResourcesDTO == null) {
- schemaResourcesDTO = createSchemaResourcesDTO(moduleSchemaCacheDirectory);
- schemaResourcesDTO.getSchemaRegistry().registerSchemaSourceListener(
- TextToASTTransformer.create((SchemaRepository) schemaResourcesDTO.getSchemaRegistry(),
- schemaResourcesDTO.getSchemaRegistry())
- );
- schemaResourcesDTOs.put(moduleSchemaCacheDirectory, schemaResourcesDTO);
- }
- }
- LOG.info("{} : netconf connector will use schema cache directory {} instead of {}",
- remoteDeviceId, moduleSchemaCacheDirectory, NetconfTopologyUtils.DEFAULT_CACHE_DIRECTORY);
- }
- } else {
- LOG.info("{} : using the default directory {}",
- remoteDeviceId, NetconfTopologyUtils.QUALIFIED_DEFAULT_CACHE_DIRECTORY);
- }
-
- if (schemaResourcesDTO == null) {
- schemaResourcesDTO =
- new NetconfDevice.SchemaResourcesDTO(schemaRegistry, schemaRepository, schemaContextFactory,
- new NetconfStateSchemasResolverImpl());
- }
-
- return schemaResourcesDTO;
- }
-
- /**
- * Creates the backing Schema classes for a particular directory.
- *
- * @param moduleSchemaCacheDirectory The string directory relative to "cache"
- * @return A DTO containing the Schema classes for the Netconf mount.
- */
- private NetconfDevice.SchemaResourcesDTO createSchemaResourcesDTO(final String moduleSchemaCacheDirectory) {
- final SharedSchemaRepository repository = new SharedSchemaRepository(moduleSchemaCacheDirectory);
- final SchemaContextFactory schemaContextFactory
- = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
- this.schemaRegistry = repository;
- this.schemaContextFactory = schemaContextFactory;
-
- final FilesystemSchemaSourceCache<YangTextSchemaSource> deviceCache =
- createDeviceFilesystemCache(moduleSchemaCacheDirectory);
- repository.registerSchemaSourceListener(deviceCache);
- return new NetconfDevice.SchemaResourcesDTO(repository, repository, schemaContextFactory,
- new NetconfStateSchemasResolverImpl());
- }
-
- /**
- * 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 =
- NetconfTopologyUtils.CACHE_DIRECTORY + File.separator + schemaCacheDirectory;
- return new FilesystemSchemaSourceCache<>(schemaRegistry, YangTextSchemaSource.class,
- new File(relativeSchemaCacheDirectory));
- }
-
- //TODO: duplicate code
- private InetSocketAddress getSocketAddress(final Host host, final int port) {
- if (host.getDomainName() != null) {
- return new InetSocketAddress(host.getDomainName().getValue(), port);
- } else {
- final IpAddress ipAddress = host.getIpAddress();
- final String ip = ipAddress.getIpv4Address() != null ? ipAddress.getIpv4Address().getValue() :
- ipAddress.getIpv6Address().getValue();
- return new InetSocketAddress(ip, port);
- }
- }
-