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 akka.util.Timeout;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.base.Preconditions;
15 import com.google.common.base.Strings;
16 import com.google.common.collect.Lists;
17 import com.google.common.util.concurrent.FutureCallback;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import io.netty.util.concurrent.EventExecutor;
22 import java.math.BigDecimal;
23 import java.net.InetSocketAddress;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.Optional;
30 import javax.annotation.Nullable;
31 import org.opendaylight.netconf.api.NetconfMessage;
32 import org.opendaylight.netconf.client.NetconfClientSessionListener;
33 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
34 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
35 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfigurationBuilder;
36 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
37 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
38 import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
39 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
40 import org.opendaylight.netconf.sal.connect.netconf.LibraryModulesSchemas;
41 import org.opendaylight.netconf.sal.connect.netconf.NetconfDevice;
42 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceBuilder;
43 import org.opendaylight.netconf.sal.connect.netconf.NetconfStateSchemasResolverImpl;
44 import org.opendaylight.netconf.sal.connect.netconf.SchemalessNetconfDevice;
45 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
46 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
47 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
48 import org.opendaylight.netconf.sal.connect.netconf.listener.UserPreferences;
49 import org.opendaylight.netconf.sal.connect.netconf.sal.KeepaliveSalFacade;
50 import org.opendaylight.netconf.sal.connect.netconf.schema.YangLibrarySchemaYangSourceProvider;
51 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
52 import org.opendaylight.netconf.topology.singleton.api.RemoteDeviceConnector;
53 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfConnectorDTO;
54 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
55 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
56 import org.opendaylight.protocol.framework.ReconnectStrategy;
57 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
58 import org.opendaylight.protocol.framework.TimedReconnectStrategy;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability.CapabilityOrigin;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.Credentials;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
65 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
66 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
67 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
68 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
69 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
70 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
71 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
72 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
73 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
74 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
75 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
76 import org.slf4j.Logger;
77 import org.slf4j.LoggerFactory;
79 public class RemoteDeviceConnectorImpl implements RemoteDeviceConnector {
81 private static final Logger LOG = LoggerFactory.getLogger(RemoteDeviceConnectorImpl.class);
84 * Keeps track of initialized Schema resources. A Map is maintained in which the key represents the name
85 * of the schema cache directory, and the value is a corresponding <code>SchemaResourcesDTO</code>. The
86 * <code>SchemaResourcesDTO</code> is essentially a container that allows for the extraction of the
87 * <code>SchemaRegistry</code> and <code>SchemaContextFactory</code> which should be used for a particular
88 * Netconf mount. Access to <code>schemaResourcesDTOs</code> should be surrounded by appropriate
89 * synchronization locks.
91 private static final Map<String, NetconfDevice.SchemaResourcesDTO> schemaResourcesDTOs = new HashMap<>();
92 private final Timeout actorResponseWaitTime;
94 // Initializes default constant instances for the case when the default schema repository
95 // directory cache/schema is used.
97 schemaResourcesDTOs.put(NetconfTopologyUtils.DEFAULT_CACHE_DIRECTORY,
98 new NetconfDevice.SchemaResourcesDTO(NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY,
99 NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY,
100 NetconfTopologyUtils.DEFAULT_SCHEMA_CONTEXT_FACTORY,
101 new NetconfStateSchemasResolverImpl()));
102 NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY.registerSchemaSourceListener(NetconfTopologyUtils.DEFAULT_CACHE);
103 NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY.registerSchemaSourceListener(
104 TextToASTTransformer.create(NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY,
105 NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY));
108 private final NetconfTopologySetup netconfTopologyDeviceSetup;
109 private final RemoteDeviceId remoteDeviceId;
110 private SchemaSourceRegistry schemaRegistry = NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY;
111 private final SchemaRepository schemaRepository = NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY;
112 private SchemaContextFactory schemaContextFactory = NetconfTopologyUtils.DEFAULT_SCHEMA_CONTEXT_FACTORY;
113 private NetconfConnectorDTO deviceCommunicatorDTO;
115 public RemoteDeviceConnectorImpl(final NetconfTopologySetup netconfTopologyDeviceSetup,
116 final RemoteDeviceId remoteDeviceId, final Timeout actorResponseWaitTime) {
118 this.netconfTopologyDeviceSetup = Preconditions.checkNotNull(netconfTopologyDeviceSetup);
119 this.remoteDeviceId = remoteDeviceId;
120 this.actorResponseWaitTime = actorResponseWaitTime;
124 public void startRemoteDeviceConnection(final ActorRef deviceContextActorRef) {
126 final NetconfNode netconfNode = netconfTopologyDeviceSetup.getNode().getAugmentation(NetconfNode.class);
127 final NodeId nodeId = netconfTopologyDeviceSetup.getNode().getNodeId();
128 Preconditions.checkNotNull(netconfNode.getHost());
129 Preconditions.checkNotNull(netconfNode.getPort());
130 Preconditions.checkNotNull(netconfNode.isTcpOnly());
132 this.deviceCommunicatorDTO = createDeviceCommunicator(nodeId, netconfNode, deviceContextActorRef);
133 final NetconfDeviceCommunicator deviceCommunicator = deviceCommunicatorDTO.getCommunicator();
134 final NetconfClientSessionListener netconfClientSessionListener = deviceCommunicatorDTO.getSessionListener();
135 final NetconfReconnectingClientConfiguration clientConfig =
136 getClientConfig(netconfClientSessionListener, netconfNode);
137 final ListenableFuture<NetconfDeviceCapabilities> future = deviceCommunicator
138 .initializeRemoteConnection(netconfTopologyDeviceSetup.getNetconfClientDispatcher(), clientConfig);
140 Futures.addCallback(future, new FutureCallback<NetconfDeviceCapabilities>() {
142 public void onSuccess(final NetconfDeviceCapabilities result) {
143 LOG.debug("{}: Connector started successfully", remoteDeviceId);
147 public void onFailure(@Nullable final Throwable throwable) {
148 LOG.error("{}: Connector failed, {}", remoteDeviceId, throwable);
154 public void stopRemoteDeviceConnection() {
155 Preconditions.checkNotNull(deviceCommunicatorDTO, remoteDeviceId + ": Device communicator was not created.");
157 deviceCommunicatorDTO.close();
158 } catch (final Exception e) {
159 LOG.error("{}: Error at closing device communicator.", remoteDeviceId, e);
164 NetconfConnectorDTO createDeviceCommunicator(final NodeId nodeId, final NetconfNode node,
165 final ActorRef deviceContextActorRef) {
166 //setup default values since default value is not supported in mdsal
167 final Long defaultRequestTimeoutMillis = node.getDefaultRequestTimeoutMillis() == null
168 ? NetconfTopologyUtils.DEFAULT_REQUEST_TIMEOUT_MILLIS : node.getDefaultRequestTimeoutMillis();
169 final Long keepaliveDelay = node.getKeepaliveDelay() == null
170 ? NetconfTopologyUtils.DEFAULT_KEEPALIVE_DELAY : node.getKeepaliveDelay();
171 final Boolean reconnectOnChangedSchema = node.isReconnectOnChangedSchema() == null
172 ? NetconfTopologyUtils.DEFAULT_RECONNECT_ON_CHANGED_SCHEMA : node.isReconnectOnChangedSchema();
174 RemoteDeviceHandler<NetconfSessionPreferences> salFacade = new MasterSalFacade(remoteDeviceId,
175 netconfTopologyDeviceSetup.getDomBroker(), netconfTopologyDeviceSetup.getBindingAwareBroker(),
176 netconfTopologyDeviceSetup.getActorSystem(), deviceContextActorRef, actorResponseWaitTime);
177 if (keepaliveDelay > 0) {
178 LOG.info("{}: Adding keepalive facade.", remoteDeviceId);
179 salFacade = new KeepaliveSalFacade(remoteDeviceId, salFacade,
180 netconfTopologyDeviceSetup.getKeepaliveExecutor().getExecutor(), keepaliveDelay,
181 defaultRequestTimeoutMillis);
184 // pre register yang library sources as fallback schemas to schema registry
185 final List<SchemaSourceRegistration<YangTextSchemaSource>> registeredYangLibSources = Lists.newArrayList();
186 if (node.getYangLibrary() != null) {
187 final String yangLibURL = node.getYangLibrary().getYangLibraryUrl().getValue();
188 final String yangLibUsername = node.getYangLibrary().getUsername();
189 final String yangLigPassword = node.getYangLibrary().getPassword();
191 final LibraryModulesSchemas libraryModulesSchemas;
192 if (yangLibURL != null) {
193 if (yangLibUsername != null && yangLigPassword != null) {
194 libraryModulesSchemas = LibraryModulesSchemas.create(yangLibURL, yangLibUsername, yangLigPassword);
196 libraryModulesSchemas = LibraryModulesSchemas.create(yangLibURL);
199 for (final Map.Entry<SourceIdentifier, URL> sourceIdentifierURLEntry :
200 libraryModulesSchemas.getAvailableModels().entrySet()) {
201 registeredYangLibSources
202 .add(schemaRegistry.registerSchemaSource(
203 new YangLibrarySchemaYangSourceProvider(remoteDeviceId,
204 libraryModulesSchemas.getAvailableModels()),
205 PotentialSchemaSource
206 .create(sourceIdentifierURLEntry.getKey(), YangTextSchemaSource.class,
207 PotentialSchemaSource.Costs.REMOTE_IO.getValue())));
212 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = setupSchemaCacheDTO(nodeId, node);
213 final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> device;
214 if (node.isSchemaless()) {
215 device = new SchemalessNetconfDevice(remoteDeviceId, salFacade);
217 device = new NetconfDeviceBuilder()
218 .setReconnectOnSchemasChange(reconnectOnChangedSchema)
219 .setSchemaResourcesDTO(schemaResourcesDTO)
220 .setGlobalProcessingExecutor(netconfTopologyDeviceSetup.getProcessingExecutor().getExecutor())
221 .setId(remoteDeviceId)
222 .setSalFacade(salFacade)
226 final Optional<NetconfSessionPreferences> userCapabilities = getUserCapabilities(node);
227 final int rpcMessageLimit =
228 node.getConcurrentRpcLimit() == null
229 ? NetconfTopologyUtils.DEFAULT_CONCURRENT_RPC_LIMIT : node.getConcurrentRpcLimit();
231 if (rpcMessageLimit < 1) {
232 LOG.info("{}: Concurrent rpc limit is smaller than 1, no limit will be enforced.", remoteDeviceId);
235 return new NetconfConnectorDTO(
236 userCapabilities.isPresent()
237 ? new NetconfDeviceCommunicator(
238 remoteDeviceId, device, new UserPreferences(userCapabilities.get(),
239 node.getYangModuleCapabilities().isOverride(), node.getNonModuleCapabilities().isOverride()),
241 : new NetconfDeviceCommunicator(remoteDeviceId, device, rpcMessageLimit), salFacade);
244 private Optional<NetconfSessionPreferences> getUserCapabilities(final NetconfNode node) {
245 if (node.getYangModuleCapabilities() == null && node.getNonModuleCapabilities() == null) {
246 return Optional.empty();
248 final List<String> capabilities = new ArrayList<>();
250 if (node.getYangModuleCapabilities() != null) {
251 capabilities.addAll(node.getYangModuleCapabilities().getCapability());
254 //non-module capabilities should not exist in yang module capabilities
255 final NetconfSessionPreferences netconfSessionPreferences = NetconfSessionPreferences.fromStrings(capabilities);
256 Preconditions.checkState(netconfSessionPreferences.getNonModuleCaps().isEmpty(), "List yang-module-capabilities/capability " +
257 "should contain only module based capabilities. Non-module capabilities used: " +
258 netconfSessionPreferences.getNonModuleCaps());
260 if (node.getNonModuleCapabilities() != null) {
261 capabilities.addAll(node.getNonModuleCapabilities().getCapability());
264 return Optional.of(NetconfSessionPreferences.fromStrings(capabilities, CapabilityOrigin.UserDefined));
267 private NetconfDevice.SchemaResourcesDTO setupSchemaCacheDTO(final NodeId nodeId, final NetconfNode node) {
268 // Setup information related to the SchemaRegistry, SchemaResourceFactory, etc.
269 NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = null;
270 final String moduleSchemaCacheDirectory = node.getSchemaCacheDirectory();
271 // Only checks to ensure the String is not empty or null; further checks related to directory accessibility
272 // and file permissions are handled during the FilesystemSchemaSourceCache initialization.
273 if (!Strings.isNullOrEmpty(moduleSchemaCacheDirectory)) {
274 // If a custom schema cache directory is specified, create the backing DTO; otherwise, the SchemaRegistry
275 // and SchemaContextFactory remain the default values.
276 if (!moduleSchemaCacheDirectory.equals(NetconfTopologyUtils.DEFAULT_CACHE_DIRECTORY)) {
277 // Multiple modules may be created at once; synchronize to avoid issues with data consistency among
279 synchronized (schemaResourcesDTOs) {
280 // Look for the cached DTO to reuse SchemaRegistry and SchemaContextFactory variables if
281 // they already exist
282 schemaResourcesDTO = schemaResourcesDTOs.get(moduleSchemaCacheDirectory);
283 if (schemaResourcesDTO == null) {
284 schemaResourcesDTO = createSchemaResourcesDTO(moduleSchemaCacheDirectory);
285 schemaResourcesDTO.getSchemaRegistry().registerSchemaSourceListener(
286 TextToASTTransformer.create((SchemaRepository) schemaResourcesDTO.getSchemaRegistry(),
287 schemaResourcesDTO.getSchemaRegistry())
289 schemaResourcesDTOs.put(moduleSchemaCacheDirectory, schemaResourcesDTO);
292 LOG.info("{} : netconf connector will use schema cache directory {} instead of {}",
293 remoteDeviceId, moduleSchemaCacheDirectory, NetconfTopologyUtils.DEFAULT_CACHE_DIRECTORY);
296 LOG.info("{} : using the default directory {}",
297 remoteDeviceId, NetconfTopologyUtils.QUALIFIED_DEFAULT_CACHE_DIRECTORY);
300 if (schemaResourcesDTO == null) {
302 new NetconfDevice.SchemaResourcesDTO(schemaRegistry, schemaRepository, schemaContextFactory,
303 new NetconfStateSchemasResolverImpl());
306 return schemaResourcesDTO;
310 * Creates the backing Schema classes for a particular directory.
312 * @param moduleSchemaCacheDirectory The string directory relative to "cache"
313 * @return A DTO containing the Schema classes for the Netconf mount.
315 private NetconfDevice.SchemaResourcesDTO createSchemaResourcesDTO(final String moduleSchemaCacheDirectory) {
316 final SharedSchemaRepository repository = new SharedSchemaRepository(moduleSchemaCacheDirectory);
317 final SchemaContextFactory schemaContextFactory
318 = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
319 this.schemaRegistry = repository;
320 this.schemaContextFactory = schemaContextFactory;
322 final FilesystemSchemaSourceCache<YangTextSchemaSource> deviceCache =
323 createDeviceFilesystemCache(moduleSchemaCacheDirectory);
324 repository.registerSchemaSourceListener(deviceCache);
325 return new NetconfDevice.SchemaResourcesDTO(repository, repository, schemaContextFactory,
326 new NetconfStateSchemasResolverImpl());
330 * Creates a <code>FilesystemSchemaSourceCache</code> for the custom schema cache directory.
332 * @param schemaCacheDirectory The custom cache directory relative to "cache"
333 * @return A <code>FilesystemSchemaSourceCache</code> for the custom schema cache directory
335 private FilesystemSchemaSourceCache<YangTextSchemaSource> createDeviceFilesystemCache(
336 final String schemaCacheDirectory) {
337 final String relativeSchemaCacheDirectory =
338 NetconfTopologyUtils.CACHE_DIRECTORY + File.separator + schemaCacheDirectory;
339 return new FilesystemSchemaSourceCache<>(schemaRegistry, YangTextSchemaSource.class,
340 new File(relativeSchemaCacheDirectory));
343 //TODO: duplicate code
344 private InetSocketAddress getSocketAddress(final Host host, final int port) {
345 if (host.getDomainName() != null) {
346 return new InetSocketAddress(host.getDomainName().getValue(), port);
348 final IpAddress ipAddress = host.getIpAddress();
349 final String ip = ipAddress.getIpv4Address() != null ? ipAddress.getIpv4Address().getValue() :
350 ipAddress.getIpv6Address().getValue();
351 return new InetSocketAddress(ip, port);
356 NetconfReconnectingClientConfiguration getClientConfig(final NetconfClientSessionListener listener,
357 final NetconfNode node) {
359 //setup default values since default value is not supported in mdsal
360 final long clientConnectionTimeoutMillis = node.getConnectionTimeoutMillis() == null
361 ? NetconfTopologyUtils.DEFAULT_CONNECTION_TIMEOUT_MILLIS : node.getConnectionTimeoutMillis();
362 final long maxConnectionAttempts = node.getMaxConnectionAttempts() == null
363 ? NetconfTopologyUtils.DEFAULT_MAX_CONNECTION_ATTEMPTS : node.getMaxConnectionAttempts();
364 final int betweenAttemptsTimeoutMillis = node.getBetweenAttemptsTimeoutMillis() == null
365 ? NetconfTopologyUtils.DEFAULT_BETWEEN_ATTEMPTS_TIMEOUT_MILLIS : node.getBetweenAttemptsTimeoutMillis();
366 final BigDecimal sleepFactor = node.getSleepFactor() == null
367 ? NetconfTopologyUtils.DEFAULT_SLEEP_FACTOR : node.getSleepFactor();
369 final InetSocketAddress socketAddress = getSocketAddress(node.getHost(), node.getPort().getValue());
371 final ReconnectStrategyFactory sf =
372 new TimedReconnectStrategyFactory(netconfTopologyDeviceSetup.getEventExecutor(), maxConnectionAttempts,
373 betweenAttemptsTimeoutMillis, sleepFactor);
374 final ReconnectStrategy strategy = sf.createReconnectStrategy();
376 final AuthenticationHandler authHandler;
377 final Credentials credentials = node.getCredentials();
378 if (credentials instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPassword) {
379 authHandler = new LoginPassword(
380 ((org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPassword) credentials).getUsername(),
381 ((org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPassword) credentials).getPassword());
383 throw new IllegalStateException(remoteDeviceId + ": Only login/password authentication is supported");
386 return NetconfReconnectingClientConfigurationBuilder.create()
387 .withAddress(socketAddress)
388 .withConnectionTimeoutMillis(clientConnectionTimeoutMillis)
389 .withReconnectStrategy(strategy)
390 .withAuthHandler(authHandler)
391 .withProtocol(node.isTcpOnly()
392 ? NetconfClientConfiguration.NetconfClientProtocol.TCP
393 : NetconfClientConfiguration.NetconfClientProtocol.SSH)
394 .withConnectStrategyFactory(sf)
395 .withSessionListener(listener)
400 Map<String, NetconfDevice.SchemaResourcesDTO> getSchemaResourcesDTOs() {
401 return schemaResourcesDTOs;
404 private static final class TimedReconnectStrategyFactory implements ReconnectStrategyFactory {
405 private final Long connectionAttempts;
406 private final EventExecutor executor;
407 private final double sleepFactor;
408 private final int minSleep;
410 TimedReconnectStrategyFactory(final EventExecutor executor, final Long maxConnectionAttempts,
411 final int minSleep, final BigDecimal sleepFactor) {
412 if (maxConnectionAttempts != null && maxConnectionAttempts > 0) {
413 connectionAttempts = maxConnectionAttempts;
415 connectionAttempts = null;
418 this.sleepFactor = sleepFactor.doubleValue();
419 this.executor = executor;
420 this.minSleep = minSleep;
424 public ReconnectStrategy createReconnectStrategy() {
425 final Long maxSleep = null;
426 final Long deadline = null;
428 return new TimedReconnectStrategy(executor, minSleep,
429 minSleep, sleepFactor, maxSleep, connectionAttempts, deadline);