-
- }
-
- public static NetconfDevice.SchemaResourcesDTO setupSchemaCacheDTO(final Node node) {
- final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
- final String moduleSchemaCacheDirectory = netconfNode.getSchemaCacheDirectory();
- final RemoteDeviceId deviceId = createRemoteDeviceId(node.getNodeId(), netconfNode);
-
- // Setup information related to the SchemaRegistry, SchemaResourceFactory, etc.
- NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = null;
- // 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(DEFAULT_CACHE_DIRECTORY)) {
- // Multiple modules may be created at once; synchronize to avoid issues with data consistency among
- // threads.
- synchronized (SCHEMA_RESOURCES_DTO_MAP) {
- // Look for the cached DTO to reuse SchemaRegistry and SchemaContextFactory variables if
- // they already exist
- schemaResourcesDTO = SCHEMA_RESOURCES_DTO_MAP.get(moduleSchemaCacheDirectory);
- if (schemaResourcesDTO == null) {
- schemaResourcesDTO = createSchemaResourcesDTO(moduleSchemaCacheDirectory);
- schemaResourcesDTO.getSchemaRegistry().registerSchemaSourceListener(
- TextToASTTransformer.create((SchemaRepository) schemaResourcesDTO.getSchemaRegistry(),
- schemaResourcesDTO.getSchemaRegistry())
- );
- SCHEMA_RESOURCES_DTO_MAP.put(moduleSchemaCacheDirectory, schemaResourcesDTO);
- }
- }
- LOG.info("{} : netconf connector will use schema cache directory {} instead of {}",
- deviceId, moduleSchemaCacheDirectory, DEFAULT_CACHE_DIRECTORY);
- }
- }
-
- if (schemaResourcesDTO == null) {
- synchronized (SCHEMA_RESOURCES_DTO_MAP) {
- schemaResourcesDTO = SCHEMA_RESOURCES_DTO_MAP.get(DEFAULT_CACHE_DIRECTORY);
- }
- LOG.info("{} : using the default directory {}",
- deviceId, QUALIFIED_DEFAULT_CACHE_DIRECTORY);
- }
-
- 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 static NetconfDevice.SchemaResourcesDTO createSchemaResourcesDTO(final String moduleSchemaCacheDirectory) {
- final SharedSchemaRepository repository = new SharedSchemaRepository(moduleSchemaCacheDirectory);
- final SchemaContextFactory schemaContextFactory
- = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
-
- final FilesystemSchemaSourceCache<YangTextSchemaSource> deviceCache =
- createDeviceFilesystemCache(moduleSchemaCacheDirectory, repository);
- repository.registerSchemaSourceListener(deviceCache);
- repository.registerSchemaSourceListener(InMemorySchemaSourceCache.createSoftCache(repository,
- ASTSchemaSource.class));
- 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 static FilesystemSchemaSourceCache<YangTextSchemaSource> createDeviceFilesystemCache(
- final String schemaCacheDirectory, final SchemaSourceRegistry schemaRegistry) {
- final String relativeSchemaCacheDirectory =
- NetconfTopologyUtils.CACHE_DIRECTORY + File.separator + schemaCacheDirectory;
- return new FilesystemSchemaSourceCache<>(schemaRegistry, YangTextSchemaSource.class,
- new File(relativeSchemaCacheDirectory));
- }
-
-
- public static RemoteDeviceId createRemoteDeviceId(final NodeId nodeId, final NetconfNode node) {
- final IpAddress ipAddress = node.getHost().getIpAddress();
- final InetSocketAddress address = new InetSocketAddress(ipAddress.getIpv4Address() != null
- ? ipAddress.getIpv4Address().getValue() : ipAddress.getIpv6Address().getValue(),
- node.getPort().getValue());
- return new RemoteDeviceId(nodeId.getValue(), address);