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;
11 import akka.actor.ActorRef;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.base.Preconditions;
14 import com.google.common.base.Strings;
15 import com.google.common.collect.Lists;
16 import com.google.common.util.concurrent.FutureCallback;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import io.netty.util.concurrent.EventExecutor;
21 import java.math.BigDecimal;
22 import java.net.InetSocketAddress;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.Optional;
28 import javax.annotation.Nullable;
29 import org.opendaylight.netconf.api.NetconfMessage;
30 import org.opendaylight.netconf.client.NetconfClientSessionListener;
31 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
32 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
33 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfigurationBuilder;
34 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
35 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
36 import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
37 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
38 import org.opendaylight.netconf.sal.connect.netconf.LibraryModulesSchemas;
39 import org.opendaylight.netconf.sal.connect.netconf.NetconfDevice;
40 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceBuilder;
41 import org.opendaylight.netconf.sal.connect.netconf.NetconfStateSchemasResolverImpl;
42 import org.opendaylight.netconf.sal.connect.netconf.SchemalessNetconfDevice;
43 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
44 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
45 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
46 import org.opendaylight.netconf.sal.connect.netconf.listener.UserPreferences;
47 import org.opendaylight.netconf.sal.connect.netconf.sal.KeepaliveSalFacade;
48 import org.opendaylight.netconf.sal.connect.netconf.schema.YangLibrarySchemaYangSourceProvider;
49 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
50 import org.opendaylight.netconf.topology.singleton.api.RemoteDeviceConnector;
51 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfConnectorDTO;
52 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
53 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
54 import org.opendaylight.protocol.framework.ReconnectStrategy;
55 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
56 import org.opendaylight.protocol.framework.TimedReconnectStrategy;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.Credentials;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
62 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
63 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
64 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
65 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
66 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
67 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
68 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
69 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
70 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
71 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
72 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
76 public class RemoteDeviceConnectorImpl implements RemoteDeviceConnector {
78 private static final Logger LOG = LoggerFactory.getLogger(RemoteDeviceConnectorImpl.class);
81 * Keeps track of initialized Schema resources. A Map is maintained in which the key represents the name
82 * of the schema cache directory, and the value is a corresponding <code>SchemaResourcesDTO</code>. The
83 * <code>SchemaResourcesDTO</code> is essentially a container that allows for the extraction of the
84 * <code>SchemaRegistry</code> and <code>SchemaContextFactory</code> which should be used for a particular
85 * Netconf mount. Access to <code>schemaResourcesDTOs</code> should be surrounded by appropriate
86 * synchronization locks.
88 private static final Map<String, NetconfDevice.SchemaResourcesDTO> schemaResourcesDTOs = new HashMap<>();
90 private SchemaSourceRegistry schemaRegistry = NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY;
91 private SchemaRepository schemaRepository = NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY;
93 private final NetconfTopologySetup netconfTopologyDeviceSetup;
94 private final RemoteDeviceId remoteDeviceId;
96 private SchemaContextFactory schemaContextFactory = NetconfTopologyUtils.DEFAULT_SCHEMA_CONTEXT_FACTORY;
97 private NetconfConnectorDTO deviceCommunicatorDTO;
99 // Initializes default constant instances for the case when the default schema repository
100 // directory cache/schema is used.
102 schemaResourcesDTOs.put(NetconfTopologyUtils.DEFAULT_CACHE_DIRECTORY,
103 new NetconfDevice.SchemaResourcesDTO(NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY,
104 NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY,
105 NetconfTopologyUtils.DEFAULT_SCHEMA_CONTEXT_FACTORY,
106 new NetconfStateSchemasResolverImpl()));
107 NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY.registerSchemaSourceListener(NetconfTopologyUtils.DEFAULT_CACHE);
108 NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY.registerSchemaSourceListener(
109 TextToASTTransformer.create(NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY,
110 NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY));
113 public RemoteDeviceConnectorImpl(final NetconfTopologySetup netconfTopologyDeviceSetup,
114 final RemoteDeviceId remoteDeviceId) {
116 this.netconfTopologyDeviceSetup = Preconditions.checkNotNull(netconfTopologyDeviceSetup);
117 this.remoteDeviceId = remoteDeviceId;
121 public void startRemoteDeviceConnection(final ActorRef deviceContextActorRef) {
123 final NetconfNode netconfNode = netconfTopologyDeviceSetup.getNode().getAugmentation(NetconfNode.class);
124 final NodeId nodeId = netconfTopologyDeviceSetup.getNode().getNodeId();
125 Preconditions.checkNotNull(netconfNode.getHost());
126 Preconditions.checkNotNull(netconfNode.getPort());
127 Preconditions.checkNotNull(netconfNode.isTcpOnly());
129 this.deviceCommunicatorDTO = createDeviceCommunicator(nodeId, netconfNode, deviceContextActorRef);
130 final NetconfDeviceCommunicator deviceCommunicator = deviceCommunicatorDTO.getCommunicator();
131 final NetconfClientSessionListener netconfClientSessionListener = deviceCommunicatorDTO.getSessionListener();
132 final NetconfReconnectingClientConfiguration clientConfig =
133 getClientConfig(netconfClientSessionListener, netconfNode);
134 final ListenableFuture<NetconfDeviceCapabilities> future = deviceCommunicator
135 .initializeRemoteConnection(netconfTopologyDeviceSetup.getNetconfClientDispatcher(), clientConfig);
137 Futures.addCallback(future, new FutureCallback<NetconfDeviceCapabilities>() {
139 public void onSuccess(NetconfDeviceCapabilities result) {
140 LOG.debug("{}: Connector started successfully", remoteDeviceId);
144 public void onFailure(@Nullable Throwable throwable) {
145 LOG.error("{}: Connector failed, {}", remoteDeviceId, throwable);
151 public void stopRemoteDeviceConnection() {
152 Preconditions.checkNotNull(deviceCommunicatorDTO, remoteDeviceId + ": Device communicator was not created.");
154 deviceCommunicatorDTO.close();
155 } catch (Exception e) {
156 LOG.error("{}: Error at closing device communicator.", remoteDeviceId, e);
161 NetconfConnectorDTO createDeviceCommunicator(final NodeId nodeId, final NetconfNode node,
162 final ActorRef deviceContextActorRef) {
163 //setup default values since default value is not supported in mdsal
164 final Long defaultRequestTimeoutMillis = node.getDefaultRequestTimeoutMillis() == null
165 ? NetconfTopologyUtils.DEFAULT_REQUEST_TIMEOUT_MILLIS : node.getDefaultRequestTimeoutMillis();
166 final Long keepaliveDelay = node.getKeepaliveDelay() == null
167 ? NetconfTopologyUtils.DEFAULT_KEEPALIVE_DELAY : node.getKeepaliveDelay();
168 final Boolean reconnectOnChangedSchema = node.isReconnectOnChangedSchema() == null
169 ? NetconfTopologyUtils.DEFAULT_RECONNECT_ON_CHANGED_SCHEMA : node.isReconnectOnChangedSchema();
171 RemoteDeviceHandler<NetconfSessionPreferences> salFacade = new MasterSalFacade(remoteDeviceId,
172 netconfTopologyDeviceSetup.getDomBroker(), netconfTopologyDeviceSetup.getBindingAwareBroker(),
173 netconfTopologyDeviceSetup.getActorSystem(), deviceContextActorRef);
174 if (keepaliveDelay > 0) {
175 LOG.info("{}: Adding keepalive facade.", remoteDeviceId);
176 salFacade = new KeepaliveSalFacade(remoteDeviceId, salFacade,
177 netconfTopologyDeviceSetup.getKeepaliveExecutor().getExecutor(), keepaliveDelay,
178 defaultRequestTimeoutMillis);
181 // pre register yang library sources as fallback schemas to schema registry
182 List<SchemaSourceRegistration<YangTextSchemaSource>> registeredYangLibSources = Lists.newArrayList();
183 if (node.getYangLibrary() != null) {
184 final String yangLibURL = node.getYangLibrary().getYangLibraryUrl().getValue();
185 final String yangLibUsername = node.getYangLibrary().getUsername();
186 final String yangLigPassword = node.getYangLibrary().getPassword();
188 LibraryModulesSchemas libraryModulesSchemas;
189 if (yangLibURL != null) {
190 if (yangLibUsername != null && yangLigPassword != null) {
191 libraryModulesSchemas = LibraryModulesSchemas.create(yangLibURL, yangLibUsername, yangLigPassword);
193 libraryModulesSchemas = LibraryModulesSchemas.create(yangLibURL);
196 for (Map.Entry<SourceIdentifier, URL> sourceIdentifierURLEntry :
197 libraryModulesSchemas.getAvailableModels().entrySet()) {
198 registeredYangLibSources
199 .add(schemaRegistry.registerSchemaSource(
200 new YangLibrarySchemaYangSourceProvider(remoteDeviceId,
201 libraryModulesSchemas.getAvailableModels()),
202 PotentialSchemaSource
203 .create(sourceIdentifierURLEntry.getKey(), YangTextSchemaSource.class,
204 PotentialSchemaSource.Costs.REMOTE_IO.getValue())));
209 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = setupSchemaCacheDTO(nodeId, node);
210 final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> device;
211 if (node.isSchemaless()) {
212 device = new SchemalessNetconfDevice(remoteDeviceId, salFacade);
214 device = new NetconfDeviceBuilder()
215 .setReconnectOnSchemasChange(reconnectOnChangedSchema)
216 .setSchemaResourcesDTO(schemaResourcesDTO)
217 .setGlobalProcessingExecutor(netconfTopologyDeviceSetup.getProcessingExecutor().getExecutor())
218 .setId(remoteDeviceId)
219 .setSalFacade(salFacade)
223 final Optional<NetconfSessionPreferences> userCapabilities = getUserCapabilities(node);
224 final int rpcMessageLimit =
225 node.getConcurrentRpcLimit() == null
226 ? NetconfTopologyUtils.DEFAULT_CONCURRENT_RPC_LIMIT : node.getConcurrentRpcLimit();
228 if (rpcMessageLimit < 1) {
229 LOG.info("{}: Concurrent rpc limit is smaller than 1, no limit will be enforced.", remoteDeviceId);
232 return new NetconfConnectorDTO(
233 userCapabilities.isPresent()
234 ? new NetconfDeviceCommunicator(
235 remoteDeviceId, device, new UserPreferences(userCapabilities.get(),
236 node.getYangModuleCapabilities().isOverride()), rpcMessageLimit) :
237 new NetconfDeviceCommunicator(remoteDeviceId, device, rpcMessageLimit), salFacade);
240 private Optional<NetconfSessionPreferences> getUserCapabilities(final NetconfNode node) {
241 if (node.getYangModuleCapabilities() == null) {
242 return Optional.empty();
245 final List<String> capabilities = node.getYangModuleCapabilities().getCapability();
246 if (capabilities == null || capabilities.isEmpty()) {
247 return Optional.empty();
250 final NetconfSessionPreferences parsedOverrideCapabilities =
251 NetconfSessionPreferences.fromStrings(capabilities);
252 Preconditions.checkState(parsedOverrideCapabilities.getNonModuleCaps().isEmpty(), remoteDeviceId +
253 ": Capabilities to override can only contain module based capabilities, non-module capabilities "
254 + "will be retrieved from the device, configured non-module capabilities: "
255 + parsedOverrideCapabilities.getNonModuleCaps());
257 return Optional.of(parsedOverrideCapabilities);
260 private NetconfDevice.SchemaResourcesDTO setupSchemaCacheDTO(final NodeId nodeId, final NetconfNode node) {
261 // Setup information related to the SchemaRegistry, SchemaResourceFactory, etc.
262 NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = null;
263 final String moduleSchemaCacheDirectory = node.getSchemaCacheDirectory();
264 // Only checks to ensure the String is not empty or null; further checks related to directory accessibility
265 // and file permissions are handled during the FilesystemSchemaSourceCache initialization.
266 if (!Strings.isNullOrEmpty(moduleSchemaCacheDirectory)) {
267 // If a custom schema cache directory is specified, create the backing DTO; otherwise, the SchemaRegistry
268 // and SchemaContextFactory remain the default values.
269 if (!moduleSchemaCacheDirectory.equals(NetconfTopologyUtils.DEFAULT_CACHE_DIRECTORY)) {
270 // Multiple modules may be created at once; synchronize to avoid issues with data consistency among
272 synchronized (schemaResourcesDTOs) {
273 // Look for the cached DTO to reuse SchemaRegistry and SchemaContextFactory variables if
274 // they already exist
275 schemaResourcesDTO = schemaResourcesDTOs.get(moduleSchemaCacheDirectory);
276 if (schemaResourcesDTO == null) {
277 schemaResourcesDTO = createSchemaResourcesDTO(moduleSchemaCacheDirectory);
278 schemaResourcesDTO.getSchemaRegistry().registerSchemaSourceListener(
279 TextToASTTransformer.create((SchemaRepository) schemaResourcesDTO.getSchemaRegistry(),
280 schemaResourcesDTO.getSchemaRegistry())
282 schemaResourcesDTOs.put(moduleSchemaCacheDirectory, schemaResourcesDTO);
285 LOG.info("{} : netconf connector will use schema cache directory {} instead of {}",
286 remoteDeviceId, moduleSchemaCacheDirectory, NetconfTopologyUtils.DEFAULT_CACHE_DIRECTORY);
289 LOG.info("{} : using the default directory {}",
290 remoteDeviceId, NetconfTopologyUtils.QUALIFIED_DEFAULT_CACHE_DIRECTORY);
293 if (schemaResourcesDTO == null) {
295 new NetconfDevice.SchemaResourcesDTO(schemaRegistry, schemaRepository, schemaContextFactory,
296 new NetconfStateSchemasResolverImpl());
299 return schemaResourcesDTO;
303 * Creates the backing Schema classes for a particular directory.
305 * @param moduleSchemaCacheDirectory The string directory relative to "cache"
306 * @return A DTO containing the Schema classes for the Netconf mount.
308 private NetconfDevice.SchemaResourcesDTO createSchemaResourcesDTO(final String moduleSchemaCacheDirectory) {
309 final SharedSchemaRepository repository = new SharedSchemaRepository(moduleSchemaCacheDirectory);
310 final SchemaContextFactory schemaContextFactory
311 = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
312 this.schemaRegistry = repository;
313 this.schemaContextFactory = schemaContextFactory;
315 final FilesystemSchemaSourceCache<YangTextSchemaSource> deviceCache =
316 createDeviceFilesystemCache(moduleSchemaCacheDirectory);
317 repository.registerSchemaSourceListener(deviceCache);
318 return new NetconfDevice.SchemaResourcesDTO(repository, repository, schemaContextFactory,
319 new NetconfStateSchemasResolverImpl());
323 * Creates a <code>FilesystemSchemaSourceCache</code> for the custom schema cache directory.
325 * @param schemaCacheDirectory The custom cache directory relative to "cache"
326 * @return A <code>FilesystemSchemaSourceCache</code> for the custom schema cache directory
328 private FilesystemSchemaSourceCache<YangTextSchemaSource> createDeviceFilesystemCache(
329 final String schemaCacheDirectory) {
330 final String relativeSchemaCacheDirectory =
331 NetconfTopologyUtils.CACHE_DIRECTORY + File.separator + schemaCacheDirectory;
332 return new FilesystemSchemaSourceCache<>(schemaRegistry, YangTextSchemaSource.class,
333 new File(relativeSchemaCacheDirectory));
336 //TODO: duplicate code
337 private InetSocketAddress getSocketAddress(final Host host, int port) {
338 if (host.getDomainName() != null) {
339 return new InetSocketAddress(host.getDomainName().getValue(), port);
341 final IpAddress ipAddress = host.getIpAddress();
342 final String ip = ipAddress.getIpv4Address() != null ? ipAddress.getIpv4Address().getValue() :
343 ipAddress.getIpv6Address().getValue();
344 return new InetSocketAddress(ip, port);
348 private static final class TimedReconnectStrategyFactory implements ReconnectStrategyFactory {
349 private final Long connectionAttempts;
350 private final EventExecutor executor;
351 private final double sleepFactor;
352 private final int minSleep;
354 TimedReconnectStrategyFactory(final EventExecutor executor, final Long maxConnectionAttempts,
355 final int minSleep, final BigDecimal sleepFactor) {
356 if (maxConnectionAttempts != null && maxConnectionAttempts > 0) {
357 connectionAttempts = maxConnectionAttempts;
359 connectionAttempts = null;
362 this.sleepFactor = sleepFactor.doubleValue();
363 this.executor = executor;
364 this.minSleep = minSleep;
368 public ReconnectStrategy createReconnectStrategy() {
369 final Long maxSleep = null;
370 final Long deadline = null;
372 return new TimedReconnectStrategy(executor, minSleep,
373 minSleep, sleepFactor, maxSleep, connectionAttempts, deadline);
378 NetconfReconnectingClientConfiguration getClientConfig(final NetconfClientSessionListener listener,
379 final NetconfNode node) {
381 //setup default values since default value is not supported in mdsal
382 final long clientConnectionTimeoutMillis = node.getConnectionTimeoutMillis() == null
383 ? NetconfTopologyUtils.DEFAULT_CONNECTION_TIMEOUT_MILLIS : node.getConnectionTimeoutMillis();
384 final long maxConnectionAttempts = node.getMaxConnectionAttempts() == null
385 ? NetconfTopologyUtils.DEFAULT_MAX_CONNECTION_ATTEMPTS : node.getMaxConnectionAttempts();
386 final int betweenAttemptsTimeoutMillis = node.getBetweenAttemptsTimeoutMillis() == null
387 ? NetconfTopologyUtils.DEFAULT_BETWEEN_ATTEMPTS_TIMEOUT_MILLIS : node.getBetweenAttemptsTimeoutMillis();
388 final BigDecimal sleepFactor = node.getSleepFactor() == null
389 ? NetconfTopologyUtils.DEFAULT_SLEEP_FACTOR : node.getSleepFactor();
391 final InetSocketAddress socketAddress = getSocketAddress(node.getHost(), node.getPort().getValue());
393 final ReconnectStrategyFactory sf =
394 new TimedReconnectStrategyFactory(netconfTopologyDeviceSetup.getEventExecutor(), maxConnectionAttempts,
395 betweenAttemptsTimeoutMillis, sleepFactor);
396 final ReconnectStrategy strategy = sf.createReconnectStrategy();
398 final AuthenticationHandler authHandler;
399 final Credentials credentials = node.getCredentials();
400 if (credentials instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPassword) {
401 authHandler = new LoginPassword(
402 ((org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPassword) credentials).getUsername(),
403 ((org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPassword) credentials).getPassword());
405 throw new IllegalStateException(remoteDeviceId + ": Only login/password authentication is supported");
408 return NetconfReconnectingClientConfigurationBuilder.create()
409 .withAddress(socketAddress)
410 .withConnectionTimeoutMillis(clientConnectionTimeoutMillis)
411 .withReconnectStrategy(strategy)
412 .withAuthHandler(authHandler)
413 .withProtocol(node.isTcpOnly()
414 ? NetconfClientConfiguration.NetconfClientProtocol.TCP
415 : NetconfClientConfiguration.NetconfClientProtocol.SSH)
416 .withConnectStrategyFactory(sf)
417 .withSessionListener(listener)
422 Map<String, NetconfDevice.SchemaResourcesDTO> getSchemaResourcesDTOs() {
423 return schemaResourcesDTOs;