2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.topology.singleton.impl.utils;
11 import com.google.common.base.Strings;
12 import com.google.common.util.concurrent.Uninterruptibles;
14 import java.math.BigDecimal;
15 import java.net.InetSocketAddress;
16 import java.util.HashMap;
18 import java.util.concurrent.TimeUnit;
19 import org.opendaylight.netconf.api.DocumentedException;
20 import org.opendaylight.netconf.sal.connect.netconf.NetconfDevice;
21 import org.opendaylight.netconf.sal.connect.netconf.NetconfStateSchemasResolverImpl;
22 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
32 import org.opendaylight.yangtools.yang.binding.Identifier;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
36 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
37 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
38 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
39 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
40 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
41 import org.opendaylight.yangtools.yang.model.repo.util.InMemorySchemaSourceCache;
42 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
43 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.ASTSchemaSource;
44 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToASTTransformer;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public final class NetconfTopologyUtils {
49 private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyUtils.class);
51 private static final String DEFAULT_SCHEMA_REPOSITORY_NAME = "sal-netconf-connector";
53 public static final long DEFAULT_REQUEST_TIMEOUT_MILLIS = 60000L;
54 public static final int DEFAULT_KEEPALIVE_DELAY = 0;
55 public static final boolean DEFAULT_RECONNECT_ON_CHANGED_SCHEMA = false;
56 public static final boolean DEFAULT_IS_TCP_ONLY = false;
57 public static final int DEFAULT_CONCURRENT_RPC_LIMIT = 0;
58 public static final int DEFAULT_MAX_CONNECTION_ATTEMPTS = 0;
59 public static final int DEFAULT_BETWEEN_ATTEMPTS_TIMEOUT_MILLIS = 2000;
60 public static final long DEFAULT_CONNECTION_TIMEOUT_MILLIS = 20000L;
61 public static final BigDecimal DEFAULT_SLEEP_FACTOR = new BigDecimal(1.5);
64 // The default cache directory relative to <code>CACHE_DIRECTORY</code>
66 public static final String DEFAULT_CACHE_DIRECTORY = "schema";
68 // Filesystem based caches are stored relative to the cache directory.
69 public static final String CACHE_DIRECTORY = "cache";
71 // The qualified schema cache directory <code>cache/schema</code>
72 public static final String QUALIFIED_DEFAULT_CACHE_DIRECTORY =
73 CACHE_DIRECTORY + File.separator + DEFAULT_CACHE_DIRECTORY;
75 // The default schema repository in the case that one is not specified.
76 public static final SharedSchemaRepository DEFAULT_SCHEMA_REPOSITORY =
77 new SharedSchemaRepository(DEFAULT_SCHEMA_REPOSITORY_NAME);
79 public static final InMemorySchemaSourceCache<ASTSchemaSource> DEFAULT_AST_CACHE =
80 InMemorySchemaSourceCache.createSoftCache(DEFAULT_SCHEMA_REPOSITORY, ASTSchemaSource.class);
82 // The default factory for creating <code>SchemaContext</code> instances.
83 public static final SchemaContextFactory DEFAULT_SCHEMA_CONTEXT_FACTORY =
84 DEFAULT_SCHEMA_REPOSITORY.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
87 * Keeps track of initialized Schema resources. A Map is maintained in which the key represents the name
88 * of the schema cache directory, and the value is a corresponding <code>SchemaResourcesDTO</code>. The
89 * <code>SchemaResourcesDTO</code> is essentially a container that allows for the extraction of the
90 * <code>SchemaRegistry</code> and <code>SchemaContextFactory</code> which should be used for a particular
91 * Netconf mount. Access to <code>SCHEMA_RESOURCES_DTO_MAP</code> should be surrounded by appropriate
92 * synchronization locks.
94 private static final Map<String, NetconfDevice.SchemaResourcesDTO> SCHEMA_RESOURCES_DTO_MAP = new HashMap<>();
96 // Initializes default constant instances for the case when the default schema repository
97 // directory cache/schema is used.
99 SCHEMA_RESOURCES_DTO_MAP.put(DEFAULT_CACHE_DIRECTORY,
100 new NetconfDevice.SchemaResourcesDTO(DEFAULT_SCHEMA_REPOSITORY, DEFAULT_SCHEMA_REPOSITORY,
101 DEFAULT_SCHEMA_CONTEXT_FACTORY, new NetconfStateSchemasResolverImpl()));
102 DEFAULT_SCHEMA_REPOSITORY.registerSchemaSourceListener(DEFAULT_AST_CACHE);
103 DEFAULT_SCHEMA_REPOSITORY.registerSchemaSourceListener(
104 TextToASTTransformer.create(DEFAULT_SCHEMA_REPOSITORY, DEFAULT_SCHEMA_REPOSITORY));
107 * Create the default <code>FilesystemSchemaSourceCache</code>, which stores cached files
108 * in <code>cache/schema</code>. Try up to 3 times - we've seen intermittent failures on jenkins where
109 * FilesystemSchemaSourceCache throws an IAE due to mkdirs failure. The theory is that there's a race
110 * creating the dir and it already exists when mkdirs is called (mkdirs returns false in this case). In this
111 * scenario, a retry should succeed.
116 FilesystemSchemaSourceCache<YangTextSchemaSource> defaultCache =
117 new FilesystemSchemaSourceCache<>(DEFAULT_SCHEMA_REPOSITORY, YangTextSchemaSource.class,
118 new File(QUALIFIED_DEFAULT_CACHE_DIRECTORY));
119 DEFAULT_SCHEMA_REPOSITORY.registerSchemaSourceListener(defaultCache);
121 } catch (IllegalArgumentException e) {
123 LOG.error("Error creating default schema cache", e);
126 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
131 private NetconfTopologyUtils() {
135 public static NetconfDevice.SchemaResourcesDTO setupSchemaCacheDTO(final Node node) {
136 final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
137 final String moduleSchemaCacheDirectory = netconfNode.getSchemaCacheDirectory();
138 final RemoteDeviceId deviceId = createRemoteDeviceId(node.getNodeId(), netconfNode);
140 // Setup information related to the SchemaRegistry, SchemaResourceFactory, etc.
141 NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = null;
142 // Only checks to ensure the String is not empty or null; further checks related to directory accessibility
143 // and file permissions are handled during the FilesystemSchemaSourceCache initialization.
144 if (!Strings.isNullOrEmpty(moduleSchemaCacheDirectory)) {
145 // If a custom schema cache directory is specified, create the backing DTO; otherwise, the SchemaRegistry
146 // and SchemaContextFactory remain the default values.
147 if (!moduleSchemaCacheDirectory.equals(DEFAULT_CACHE_DIRECTORY)) {
148 // Multiple modules may be created at once; synchronize to avoid issues with data consistency among
150 synchronized (SCHEMA_RESOURCES_DTO_MAP) {
151 // Look for the cached DTO to reuse SchemaRegistry and SchemaContextFactory variables if
152 // they already exist
153 schemaResourcesDTO = SCHEMA_RESOURCES_DTO_MAP.get(moduleSchemaCacheDirectory);
154 if (schemaResourcesDTO == null) {
155 schemaResourcesDTO = createSchemaResourcesDTO(moduleSchemaCacheDirectory);
156 schemaResourcesDTO.getSchemaRegistry().registerSchemaSourceListener(
157 TextToASTTransformer.create((SchemaRepository) schemaResourcesDTO.getSchemaRegistry(),
158 schemaResourcesDTO.getSchemaRegistry())
160 SCHEMA_RESOURCES_DTO_MAP.put(moduleSchemaCacheDirectory, schemaResourcesDTO);
163 LOG.info("{} : netconf connector will use schema cache directory {} instead of {}",
164 deviceId, moduleSchemaCacheDirectory, DEFAULT_CACHE_DIRECTORY);
168 if (schemaResourcesDTO == null) {
169 synchronized (SCHEMA_RESOURCES_DTO_MAP) {
170 schemaResourcesDTO = SCHEMA_RESOURCES_DTO_MAP.get(DEFAULT_CACHE_DIRECTORY);
172 LOG.info("{} : using the default directory {}",
173 deviceId, QUALIFIED_DEFAULT_CACHE_DIRECTORY);
176 return schemaResourcesDTO;
180 * Creates the backing Schema classes for a particular directory.
182 * @param moduleSchemaCacheDirectory The string directory relative to "cache"
183 * @return A DTO containing the Schema classes for the Netconf mount.
185 private static NetconfDevice.SchemaResourcesDTO createSchemaResourcesDTO(final String moduleSchemaCacheDirectory) {
186 final SharedSchemaRepository repository = new SharedSchemaRepository(moduleSchemaCacheDirectory);
187 final SchemaContextFactory schemaContextFactory
188 = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
190 final FilesystemSchemaSourceCache<YangTextSchemaSource> deviceCache =
191 createDeviceFilesystemCache(moduleSchemaCacheDirectory, repository);
192 repository.registerSchemaSourceListener(deviceCache);
193 repository.registerSchemaSourceListener(InMemorySchemaSourceCache.createSoftCache(repository,
194 ASTSchemaSource.class));
195 return new NetconfDevice.SchemaResourcesDTO(repository, repository, schemaContextFactory,
196 new NetconfStateSchemasResolverImpl());
200 * Creates a <code>FilesystemSchemaSourceCache</code> for the custom schema cache directory.
202 * @param schemaCacheDirectory The custom cache directory relative to "cache"
203 * @return A <code>FilesystemSchemaSourceCache</code> for the custom schema cache directory
205 private static FilesystemSchemaSourceCache<YangTextSchemaSource> createDeviceFilesystemCache(
206 final String schemaCacheDirectory, final SchemaSourceRegistry schemaRegistry) {
207 final String relativeSchemaCacheDirectory =
208 NetconfTopologyUtils.CACHE_DIRECTORY + File.separator + schemaCacheDirectory;
209 return new FilesystemSchemaSourceCache<>(schemaRegistry, YangTextSchemaSource.class,
210 new File(relativeSchemaCacheDirectory));
214 public static RemoteDeviceId createRemoteDeviceId(final NodeId nodeId, final NetconfNode node) {
215 final IpAddress ipAddress = node.getHost().getIpAddress();
216 final InetSocketAddress address = new InetSocketAddress(ipAddress.getIpv4Address() != null
217 ? ipAddress.getIpv4Address().getValue() : ipAddress.getIpv6Address().getValue(),
218 node.getPort().getValue());
219 return new RemoteDeviceId(nodeId.getValue(), address);
222 public static String createActorPath(final String masterMember, final String name) {
223 return masterMember + "/user/" + name;
226 public static String createMasterActorName(final String name, final String masterAddress) {
227 return masterAddress.replaceAll("//", "") + "_" + name;
230 public static NodeId getNodeId(final InstanceIdentifier.PathArgument pathArgument) {
231 if (pathArgument instanceof InstanceIdentifier.IdentifiableItem) {
232 final Identifier<?> key = ((InstanceIdentifier.IdentifiableItem<?, ?>) pathArgument).getKey();
233 if (key instanceof NodeKey) {
234 return ((NodeKey) key).getNodeId();
237 throw new IllegalStateException("Unable to create NodeId from: " + pathArgument);
240 public static KeyedInstanceIdentifier<Topology, TopologyKey> createTopologyListPath(final String topologyId) {
241 final InstanceIdentifier<NetworkTopology> networkTopology = InstanceIdentifier.create(NetworkTopology.class);
242 return networkTopology.child(Topology.class, new TopologyKey(new TopologyId(topologyId)));
245 public static InstanceIdentifier<Node> createTopologyNodeListPath(final NodeKey key, final String topologyId) {
246 return createTopologyListPath(topologyId)
247 .child(Node.class, new NodeKey(new NodeId(key.getNodeId().getValue())));
250 public static InstanceIdentifier<Node> createTopologyNodePath(final String topologyId) {
251 return createTopologyListPath(topologyId).child(Node.class);
254 public static DocumentedException createMasterIsDownException(final RemoteDeviceId id, final Exception cause) {
255 return new DocumentedException(id + ":Master is down. Please try again.", cause,
256 DocumentedException.ErrorType.APPLICATION, DocumentedException.ErrorTag.OPERATION_FAILED,
257 DocumentedException.ErrorSeverity.WARNING);