Bug-6346: Allow over-ride of non-module capabilities
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / RemoteDeviceConnectorImpl.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.topology.singleton.impl;
10
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;
20 import java.io.File;
21 import java.math.BigDecimal;
22 import java.net.InetSocketAddress;
23 import java.net.URL;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
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;
77
78 public class RemoteDeviceConnectorImpl implements RemoteDeviceConnector {
79
80     private static final Logger LOG = LoggerFactory.getLogger(RemoteDeviceConnectorImpl.class);
81
82     /**
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.
89      */
90     private static final Map<String, NetconfDevice.SchemaResourcesDTO> schemaResourcesDTOs = new HashMap<>();
91
92     // Initializes default constant instances for the case when the default schema repository
93     // directory cache/schema is used.
94     static {
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));
104     }
105
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;
112
113     public RemoteDeviceConnectorImpl(final NetconfTopologySetup netconfTopologyDeviceSetup,
114                                      final RemoteDeviceId remoteDeviceId) {
115
116         this.netconfTopologyDeviceSetup = Preconditions.checkNotNull(netconfTopologyDeviceSetup);
117         this.remoteDeviceId = remoteDeviceId;
118     }
119
120     @Override
121     public void startRemoteDeviceConnection(final ActorRef deviceContextActorRef) {
122
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());
128
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);
136
137         Futures.addCallback(future, new FutureCallback<NetconfDeviceCapabilities>() {
138             @Override
139             public void onSuccess(final NetconfDeviceCapabilities result) {
140                 LOG.debug("{}: Connector started successfully", remoteDeviceId);
141             }
142
143             @Override
144             public void onFailure(@Nullable final Throwable throwable) {
145                 LOG.error("{}: Connector failed, {}", remoteDeviceId, throwable);
146             }
147         });
148     }
149
150     @Override
151     public void stopRemoteDeviceConnection() {
152         Preconditions.checkNotNull(deviceCommunicatorDTO, remoteDeviceId + ": Device communicator was not created.");
153         try {
154             deviceCommunicatorDTO.close();
155         } catch (final Exception e) {
156             LOG.error("{}: Error at closing device communicator.", remoteDeviceId, e);
157         }
158     }
159
160     @VisibleForTesting
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();
170
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);
179         }
180
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();
187
188             final LibraryModulesSchemas libraryModulesSchemas;
189             if (yangLibURL != null) {
190                 if (yangLibUsername != null && yangLigPassword != null) {
191                     libraryModulesSchemas = LibraryModulesSchemas.create(yangLibURL, yangLibUsername, yangLigPassword);
192                 } else {
193                     libraryModulesSchemas = LibraryModulesSchemas.create(yangLibURL);
194                 }
195
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())));
205                 }
206             }
207         }
208
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);
213         } else {
214             device = new NetconfDeviceBuilder()
215                     .setReconnectOnSchemasChange(reconnectOnChangedSchema)
216                     .setSchemaResourcesDTO(schemaResourcesDTO)
217                     .setGlobalProcessingExecutor(netconfTopologyDeviceSetup.getProcessingExecutor().getExecutor())
218                     .setId(remoteDeviceId)
219                     .setSalFacade(salFacade)
220                     .build();
221         }
222
223         final Optional<NetconfSessionPreferences> userCapabilities = getUserCapabilities(node);
224         final int rpcMessageLimit =
225                 node.getConcurrentRpcLimit() == null
226                         ? NetconfTopologyUtils.DEFAULT_CONCURRENT_RPC_LIMIT : node.getConcurrentRpcLimit();
227
228         if (rpcMessageLimit < 1) {
229             LOG.info("{}: Concurrent rpc limit is smaller than 1, no limit will be enforced.", remoteDeviceId);
230         }
231
232         return new NetconfConnectorDTO(
233                 userCapabilities.isPresent()
234                         ? new NetconfDeviceCommunicator(
235                         remoteDeviceId, device, new UserPreferences(userCapabilities.get(),
236                         node.getYangModuleCapabilities().isOverride(), node.getNonModuleCapabilities().isOverride()),
237                         rpcMessageLimit)
238                         : new NetconfDeviceCommunicator(remoteDeviceId, device, rpcMessageLimit), salFacade);
239     }
240
241     private Optional<NetconfSessionPreferences> getUserCapabilities(final NetconfNode node) {
242         if (node.getYangModuleCapabilities() == null && node.getNonModuleCapabilities() == null) {
243             return Optional.empty();
244         }
245         final List<String> capabilities = new ArrayList<>();
246
247         if (node.getYangModuleCapabilities() != null) {
248             capabilities.addAll(node.getYangModuleCapabilities().getCapability());
249         }
250
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());
256
257         if (node.getNonModuleCapabilities() != null) {
258             capabilities.addAll(node.getNonModuleCapabilities().getCapability());
259         }
260
261         return Optional.of(NetconfSessionPreferences.fromStrings(capabilities, CapabilityOrigin.UserDefined));
262     }
263
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
275                 // threads.
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())
285                         );
286                         schemaResourcesDTOs.put(moduleSchemaCacheDirectory, schemaResourcesDTO);
287                     }
288                 }
289                 LOG.info("{} : netconf connector will use schema cache directory {} instead of {}",
290                         remoteDeviceId, moduleSchemaCacheDirectory, NetconfTopologyUtils.DEFAULT_CACHE_DIRECTORY);
291             }
292         } else {
293             LOG.info("{} : using the default directory {}",
294                     remoteDeviceId, NetconfTopologyUtils.QUALIFIED_DEFAULT_CACHE_DIRECTORY);
295         }
296
297         if (schemaResourcesDTO == null) {
298             schemaResourcesDTO =
299                     new NetconfDevice.SchemaResourcesDTO(schemaRegistry, schemaRepository, schemaContextFactory,
300                             new NetconfStateSchemasResolverImpl());
301         }
302
303         return schemaResourcesDTO;
304     }
305
306     /**
307      * Creates the backing Schema classes for a particular directory.
308      *
309      * @param moduleSchemaCacheDirectory The string directory relative to "cache"
310      * @return A DTO containing the Schema classes for the Netconf mount.
311      */
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;
318
319         final FilesystemSchemaSourceCache<YangTextSchemaSource> deviceCache =
320                 createDeviceFilesystemCache(moduleSchemaCacheDirectory);
321         repository.registerSchemaSourceListener(deviceCache);
322         return new NetconfDevice.SchemaResourcesDTO(repository, repository, schemaContextFactory,
323                 new NetconfStateSchemasResolverImpl());
324     }
325
326     /**
327      * Creates a <code>FilesystemSchemaSourceCache</code> for the custom schema cache directory.
328      *
329      * @param schemaCacheDirectory The custom cache directory relative to "cache"
330      * @return A <code>FilesystemSchemaSourceCache</code> for the custom schema cache directory
331      */
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));
338     }
339
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);
344         } else {
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);
349         }
350     }
351
352     @VisibleForTesting
353     NetconfReconnectingClientConfiguration getClientConfig(final NetconfClientSessionListener listener,
354                                                            final NetconfNode node) {
355
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();
365
366         final InetSocketAddress socketAddress = getSocketAddress(node.getHost(), node.getPort().getValue());
367
368         final ReconnectStrategyFactory sf =
369                 new TimedReconnectStrategyFactory(netconfTopologyDeviceSetup.getEventExecutor(), maxConnectionAttempts,
370                         betweenAttemptsTimeoutMillis, sleepFactor);
371         final ReconnectStrategy strategy = sf.createReconnectStrategy();
372
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());
379         } else {
380             throw new IllegalStateException(remoteDeviceId + ": Only login/password authentication is supported");
381         }
382
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)
393                 .build();
394     }
395
396     @VisibleForTesting
397     Map<String, NetconfDevice.SchemaResourcesDTO> getSchemaResourcesDTOs() {
398         return schemaResourcesDTOs;
399     }
400
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;
406
407         TimedReconnectStrategyFactory(final EventExecutor executor, final Long maxConnectionAttempts,
408                                       final int minSleep, final BigDecimal sleepFactor) {
409             if (maxConnectionAttempts != null && maxConnectionAttempts > 0) {
410                 connectionAttempts = maxConnectionAttempts;
411             } else {
412                 connectionAttempts = null;
413             }
414
415             this.sleepFactor = sleepFactor.doubleValue();
416             this.executor = executor;
417             this.minSleep = minSleep;
418         }
419
420         @Override
421         public ReconnectStrategy createReconnectStrategy() {
422             final Long maxSleep = null;
423             final Long deadline = null;
424
425             return new TimedReconnectStrategy(executor, minSleep,
426                     minSleep, sleepFactor, maxSleep, connectionAttempts, deadline);
427         }
428     }
429 }