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.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.Optional;
29 import javax.annotation.Nullable;
30 import org.opendaylight.netconf.api.NetconfMessage;
31 import org.opendaylight.netconf.client.NetconfClientSessionListener;
32 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
33 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
34 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfigurationBuilder;
35 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
36 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
37 import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
38 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
39 import org.opendaylight.netconf.sal.connect.netconf.LibraryModulesSchemas;
40 import org.opendaylight.netconf.sal.connect.netconf.NetconfDevice;
41 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceBuilder;
42 import org.opendaylight.netconf.sal.connect.netconf.NetconfStateSchemasResolverImpl;
43 import org.opendaylight.netconf.sal.connect.netconf.SchemalessNetconfDevice;
44 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
45 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
46 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
47 import org.opendaylight.netconf.sal.connect.netconf.listener.UserPreferences;
48 import org.opendaylight.netconf.sal.connect.netconf.sal.KeepaliveSalFacade;
49 import org.opendaylight.netconf.sal.connect.netconf.schema.YangLibrarySchemaYangSourceProvider;
50 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
51 import org.opendaylight.netconf.topology.singleton.api.RemoteDeviceConnector;
52 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfConnectorDTO;
53 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
54 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
55 import org.opendaylight.protocol.framework.ReconnectStrategy;
56 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
57 import org.opendaylight.protocol.framework.TimedReconnectStrategy;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability.CapabilityOrigin;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.Credentials;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
64 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
65 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
66 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
67 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
68 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
69 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
70 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
71 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
72 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
73 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
74 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
75 import org.slf4j.Logger;
76 import org.slf4j.LoggerFactory;
78 public class RemoteDeviceConnectorImpl implements RemoteDeviceConnector {
80 private static final Logger LOG = LoggerFactory.getLogger(RemoteDeviceConnectorImpl.class);
83 * Keeps track of initialized Schema resources. A Map is maintained in which the key represents the name
84 * of the schema cache directory, and the value is a corresponding <code>SchemaResourcesDTO</code>. The
85 * <code>SchemaResourcesDTO</code> is essentially a container that allows for the extraction of the
86 * <code>SchemaRegistry</code> and <code>SchemaContextFactory</code> which should be used for a particular
87 * Netconf mount. Access to <code>schemaResourcesDTOs</code> should be surrounded by appropriate
88 * synchronization locks.
90 private static final Map<String, NetconfDevice.SchemaResourcesDTO> schemaResourcesDTOs = new HashMap<>();
92 // Initializes default constant instances for the case when the default schema repository
93 // directory cache/schema is used.
95 schemaResourcesDTOs.put(NetconfTopologyUtils.DEFAULT_CACHE_DIRECTORY,
96 new NetconfDevice.SchemaResourcesDTO(NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY,
97 NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY,
98 NetconfTopologyUtils.DEFAULT_SCHEMA_CONTEXT_FACTORY,
99 new NetconfStateSchemasResolverImpl()));
100 NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY.registerSchemaSourceListener(NetconfTopologyUtils.DEFAULT_CACHE);
101 NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY.registerSchemaSourceListener(
102 TextToASTTransformer.create(NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY,
103 NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY));
106 private final NetconfTopologySetup netconfTopologyDeviceSetup;
107 private final RemoteDeviceId remoteDeviceId;
108 private SchemaSourceRegistry schemaRegistry = NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY;
109 private final SchemaRepository schemaRepository = NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY;
110 private SchemaContextFactory schemaContextFactory = NetconfTopologyUtils.DEFAULT_SCHEMA_CONTEXT_FACTORY;
111 private NetconfConnectorDTO deviceCommunicatorDTO;
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(final NetconfDeviceCapabilities result) {
140 LOG.debug("{}: Connector started successfully", remoteDeviceId);
144 public void onFailure(@Nullable final 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 (final 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 final 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 final 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 (final 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(), node.getNonModuleCapabilities().isOverride()),
238 : new NetconfDeviceCommunicator(remoteDeviceId, device, rpcMessageLimit), salFacade);
241 private Optional<NetconfSessionPreferences> getUserCapabilities(final NetconfNode node) {
242 if (node.getYangModuleCapabilities() == null && node.getNonModuleCapabilities() == null) {
243 return Optional.empty();
245 final List<String> capabilities = new ArrayList<>();
247 if (node.getYangModuleCapabilities() != null) {
248 capabilities.addAll(node.getYangModuleCapabilities().getCapability());
251 //non-module capabilities should not exist in yang module capabilities
252 final NetconfSessionPreferences netconfSessionPreferences = NetconfSessionPreferences.fromStrings(capabilities);
253 Preconditions.checkState(netconfSessionPreferences.getNonModuleCaps().isEmpty(), "List yang-module-capabilities/capability " +
254 "should contain only module based capabilities. Non-module capabilities used: " +
255 netconfSessionPreferences.getNonModuleCaps());
257 if (node.getNonModuleCapabilities() != null) {
258 capabilities.addAll(node.getNonModuleCapabilities().getCapability());
261 return Optional.of(NetconfSessionPreferences.fromStrings(capabilities, CapabilityOrigin.UserDefined));
264 private NetconfDevice.SchemaResourcesDTO setupSchemaCacheDTO(final NodeId nodeId, final NetconfNode node) {
265 // Setup information related to the SchemaRegistry, SchemaResourceFactory, etc.
266 NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = null;
267 final String moduleSchemaCacheDirectory = node.getSchemaCacheDirectory();
268 // Only checks to ensure the String is not empty or null; further checks related to directory accessibility
269 // and file permissions are handled during the FilesystemSchemaSourceCache initialization.
270 if (!Strings.isNullOrEmpty(moduleSchemaCacheDirectory)) {
271 // If a custom schema cache directory is specified, create the backing DTO; otherwise, the SchemaRegistry
272 // and SchemaContextFactory remain the default values.
273 if (!moduleSchemaCacheDirectory.equals(NetconfTopologyUtils.DEFAULT_CACHE_DIRECTORY)) {
274 // Multiple modules may be created at once; synchronize to avoid issues with data consistency among
276 synchronized (schemaResourcesDTOs) {
277 // Look for the cached DTO to reuse SchemaRegistry and SchemaContextFactory variables if
278 // they already exist
279 schemaResourcesDTO = schemaResourcesDTOs.get(moduleSchemaCacheDirectory);
280 if (schemaResourcesDTO == null) {
281 schemaResourcesDTO = createSchemaResourcesDTO(moduleSchemaCacheDirectory);
282 schemaResourcesDTO.getSchemaRegistry().registerSchemaSourceListener(
283 TextToASTTransformer.create((SchemaRepository) schemaResourcesDTO.getSchemaRegistry(),
284 schemaResourcesDTO.getSchemaRegistry())
286 schemaResourcesDTOs.put(moduleSchemaCacheDirectory, schemaResourcesDTO);
289 LOG.info("{} : netconf connector will use schema cache directory {} instead of {}",
290 remoteDeviceId, moduleSchemaCacheDirectory, NetconfTopologyUtils.DEFAULT_CACHE_DIRECTORY);
293 LOG.info("{} : using the default directory {}",
294 remoteDeviceId, NetconfTopologyUtils.QUALIFIED_DEFAULT_CACHE_DIRECTORY);
297 if (schemaResourcesDTO == null) {
299 new NetconfDevice.SchemaResourcesDTO(schemaRegistry, schemaRepository, schemaContextFactory,
300 new NetconfStateSchemasResolverImpl());
303 return schemaResourcesDTO;
307 * Creates the backing Schema classes for a particular directory.
309 * @param moduleSchemaCacheDirectory The string directory relative to "cache"
310 * @return A DTO containing the Schema classes for the Netconf mount.
312 private NetconfDevice.SchemaResourcesDTO createSchemaResourcesDTO(final String moduleSchemaCacheDirectory) {
313 final SharedSchemaRepository repository = new SharedSchemaRepository(moduleSchemaCacheDirectory);
314 final SchemaContextFactory schemaContextFactory
315 = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
316 this.schemaRegistry = repository;
317 this.schemaContextFactory = schemaContextFactory;
319 final FilesystemSchemaSourceCache<YangTextSchemaSource> deviceCache =
320 createDeviceFilesystemCache(moduleSchemaCacheDirectory);
321 repository.registerSchemaSourceListener(deviceCache);
322 return new NetconfDevice.SchemaResourcesDTO(repository, repository, schemaContextFactory,
323 new NetconfStateSchemasResolverImpl());
327 * Creates a <code>FilesystemSchemaSourceCache</code> for the custom schema cache directory.
329 * @param schemaCacheDirectory The custom cache directory relative to "cache"
330 * @return A <code>FilesystemSchemaSourceCache</code> for the custom schema cache directory
332 private FilesystemSchemaSourceCache<YangTextSchemaSource> createDeviceFilesystemCache(
333 final String schemaCacheDirectory) {
334 final String relativeSchemaCacheDirectory =
335 NetconfTopologyUtils.CACHE_DIRECTORY + File.separator + schemaCacheDirectory;
336 return new FilesystemSchemaSourceCache<>(schemaRegistry, YangTextSchemaSource.class,
337 new File(relativeSchemaCacheDirectory));
340 //TODO: duplicate code
341 private InetSocketAddress getSocketAddress(final Host host, final int port) {
342 if (host.getDomainName() != null) {
343 return new InetSocketAddress(host.getDomainName().getValue(), port);
345 final IpAddress ipAddress = host.getIpAddress();
346 final String ip = ipAddress.getIpv4Address() != null ? ipAddress.getIpv4Address().getValue() :
347 ipAddress.getIpv6Address().getValue();
348 return new InetSocketAddress(ip, port);
353 NetconfReconnectingClientConfiguration getClientConfig(final NetconfClientSessionListener listener,
354 final NetconfNode node) {
356 //setup default values since default value is not supported in mdsal
357 final long clientConnectionTimeoutMillis = node.getConnectionTimeoutMillis() == null
358 ? NetconfTopologyUtils.DEFAULT_CONNECTION_TIMEOUT_MILLIS : node.getConnectionTimeoutMillis();
359 final long maxConnectionAttempts = node.getMaxConnectionAttempts() == null
360 ? NetconfTopologyUtils.DEFAULT_MAX_CONNECTION_ATTEMPTS : node.getMaxConnectionAttempts();
361 final int betweenAttemptsTimeoutMillis = node.getBetweenAttemptsTimeoutMillis() == null
362 ? NetconfTopologyUtils.DEFAULT_BETWEEN_ATTEMPTS_TIMEOUT_MILLIS : node.getBetweenAttemptsTimeoutMillis();
363 final BigDecimal sleepFactor = node.getSleepFactor() == null
364 ? NetconfTopologyUtils.DEFAULT_SLEEP_FACTOR : node.getSleepFactor();
366 final InetSocketAddress socketAddress = getSocketAddress(node.getHost(), node.getPort().getValue());
368 final ReconnectStrategyFactory sf =
369 new TimedReconnectStrategyFactory(netconfTopologyDeviceSetup.getEventExecutor(), maxConnectionAttempts,
370 betweenAttemptsTimeoutMillis, sleepFactor);
371 final ReconnectStrategy strategy = sf.createReconnectStrategy();
373 final AuthenticationHandler authHandler;
374 final Credentials credentials = node.getCredentials();
375 if (credentials instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPassword) {
376 authHandler = new LoginPassword(
377 ((org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPassword) credentials).getUsername(),
378 ((org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPassword) credentials).getPassword());
380 throw new IllegalStateException(remoteDeviceId + ": Only login/password authentication is supported");
383 return NetconfReconnectingClientConfigurationBuilder.create()
384 .withAddress(socketAddress)
385 .withConnectionTimeoutMillis(clientConnectionTimeoutMillis)
386 .withReconnectStrategy(strategy)
387 .withAuthHandler(authHandler)
388 .withProtocol(node.isTcpOnly()
389 ? NetconfClientConfiguration.NetconfClientProtocol.TCP
390 : NetconfClientConfiguration.NetconfClientProtocol.SSH)
391 .withConnectStrategyFactory(sf)
392 .withSessionListener(listener)
397 Map<String, NetconfDevice.SchemaResourcesDTO> getSchemaResourcesDTOs() {
398 return schemaResourcesDTOs;
401 private static final class TimedReconnectStrategyFactory implements ReconnectStrategyFactory {
402 private final Long connectionAttempts;
403 private final EventExecutor executor;
404 private final double sleepFactor;
405 private final int minSleep;
407 TimedReconnectStrategyFactory(final EventExecutor executor, final Long maxConnectionAttempts,
408 final int minSleep, final BigDecimal sleepFactor) {
409 if (maxConnectionAttempts != null && maxConnectionAttempts > 0) {
410 connectionAttempts = maxConnectionAttempts;
412 connectionAttempts = null;
415 this.sleepFactor = sleepFactor.doubleValue();
416 this.executor = executor;
417 this.minSleep = minSleep;
421 public ReconnectStrategy createReconnectStrategy() {
422 final Long maxSleep = null;
423 final Long deadline = null;
425 return new TimedReconnectStrategy(executor, minSleep,
426 minSleep, sleepFactor, maxSleep, connectionAttempts, deadline);