Add NetconfDevice builder class
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / controller / config / yang / md / sal / connector / netconf / NetconfConnectorModule.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.config.yang.md.sal.connector.netconf;
9
10 import static org.opendaylight.controller.config.api.JmxAttributeValidationException.checkCondition;
11 import static org.opendaylight.controller.config.api.JmxAttributeValidationException.checkNotNull;
12
13 import com.google.common.base.Optional;
14 import com.google.common.base.Strings;
15 import io.netty.util.concurrent.EventExecutor;
16 import java.io.File;
17 import java.math.BigDecimal;
18 import java.net.InetSocketAddress;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.concurrent.ExecutorService;
23 import java.util.concurrent.Executors;
24 import java.util.concurrent.ScheduledExecutorService;
25 import java.util.concurrent.ThreadFactory;
26 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
27 import org.opendaylight.controller.config.threadpool.ScheduledThreadPool;
28 import org.opendaylight.controller.config.threadpool.ThreadPool;
29 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
30 import org.opendaylight.controller.sal.core.api.Broker;
31 import org.opendaylight.netconf.client.NetconfClientDispatcher;
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.LoginPassword;
36 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
37 import org.opendaylight.netconf.sal.connect.netconf.NetconfDevice;
38 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceBuilder;
39 import org.opendaylight.netconf.sal.connect.netconf.NetconfStateSchemas;
40 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
41 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
42 import org.opendaylight.netconf.sal.connect.netconf.listener.UserPreferences;
43 import org.opendaylight.netconf.sal.connect.netconf.sal.KeepaliveSalFacade;
44 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceSalFacade;
45 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
46 import org.opendaylight.protocol.framework.ReconnectStrategy;
47 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
48 import org.opendaylight.protocol.framework.TimedReconnectStrategy;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Host;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
51 import org.opendaylight.yangtools.yang.common.QName;
52 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
53 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
54 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
55 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
56 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
57 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
58 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
59 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
60 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
61 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
62 import org.osgi.framework.BundleContext;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
65
66 /**
67  *
68  */
69 public final class NetconfConnectorModule extends org.opendaylight.controller.config.yang.md.sal.connector.netconf.AbstractNetconfConnectorModule
70 {
71     private static final Logger LOG = LoggerFactory.getLogger(NetconfConnectorModule.class);
72
73     /**
74      * Filesystem based caches are stored relative to the cache directory.
75      */
76     private static final String CACHE_DIRECTORY = "cache";
77
78     /**
79      * The default cache directory relative to <code>CACHE_DIRECTORY</code>
80      */
81     private static final String DEFAULT_CACHE_DIRECTORY = "schema";
82
83     /**
84      * The qualified schema cache directory <code>cache/schema</code>
85      */
86     private static final String QUALIFIED_DEFAULT_CACHE_DIRECTORY = CACHE_DIRECTORY + File.separator+ DEFAULT_CACHE_DIRECTORY;
87
88     /**
89      * The name for the default schema repository
90      */
91     private static final String DEFAULT_SCHEMA_REPOSITORY_NAME = "sal-netconf-connector";
92
93     /**
94      * The default schema repository in the case that one is not specified.
95      */
96     private static final SharedSchemaRepository DEFAULT_SCHEMA_REPOSITORY =
97             new SharedSchemaRepository(DEFAULT_SCHEMA_REPOSITORY_NAME);
98
99     /**
100      * The default <code>FilesystemSchemaSourceCache</code>, which stores cached files in <code>cache/schema</code>.
101      */
102     private static final FilesystemSchemaSourceCache<YangTextSchemaSource> DEFAULT_CACHE =
103             new FilesystemSchemaSourceCache<>(DEFAULT_SCHEMA_REPOSITORY, YangTextSchemaSource.class,
104                     new File(QUALIFIED_DEFAULT_CACHE_DIRECTORY));
105
106     /**
107      * The default factory for creating <code>SchemaContext</code> instances.
108      */
109     private static final SchemaContextFactory DEFAULT_SCHEMA_CONTEXT_FACTORY =
110             DEFAULT_SCHEMA_REPOSITORY.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
111
112     private static final int LOCAL_IO_FALLBACK_COST = PotentialSchemaSource.Costs.LOCAL_IO.getValue() + 1;
113
114     /**
115      * Keeps track of initialized Schema resources.  A Map is maintained in which the key represents the name
116      * of the schema cache directory, and the value is a corresponding <code>SchemaResourcesDTO</code>.  The
117      * <code>SchemaResourcesDTO</code> is essentially a container that allows for the extraction of the
118      * <code>SchemaRegistry</code> and <code>SchemaContextFactory</code> which should be used for a particular
119      * Netconf mount.  Access to <code>schemaResourcesDTOs</code> should be surrounded by appropriate
120      * synchronization locks.
121      */
122     private static volatile Map<String, NetconfDevice.SchemaResourcesDTO> schemaResourcesDTOs = new HashMap<>();
123
124     // Initializes default constant instances for the case when the default schema repository
125     // directory cache/schema is used.
126     static {
127         schemaResourcesDTOs.put(DEFAULT_CACHE_DIRECTORY,
128                 new NetconfDevice.SchemaResourcesDTO(DEFAULT_SCHEMA_REPOSITORY,
129                         DEFAULT_SCHEMA_CONTEXT_FACTORY,
130                         new NetconfStateSchemas.NetconfStateSchemasResolverImpl()));
131         DEFAULT_SCHEMA_REPOSITORY.registerSchemaSourceListener(DEFAULT_CACHE);
132         DEFAULT_SCHEMA_REPOSITORY.registerSchemaSourceListener(
133                 TextToASTTransformer.create(DEFAULT_SCHEMA_REPOSITORY, DEFAULT_SCHEMA_REPOSITORY));
134     }
135
136     private BundleContext bundleContext;
137     private Optional<NetconfSessionPreferences> userCapabilities;
138     private SchemaSourceRegistry schemaRegistry = DEFAULT_SCHEMA_REPOSITORY;
139     private SchemaContextFactory schemaContextFactory = DEFAULT_SCHEMA_CONTEXT_FACTORY;
140
141     private Broker domRegistry;
142     private NetconfClientDispatcher clientDispatcher;
143     private BindingAwareBroker bindingRegistry;
144     private ThreadPool processingExecutor;
145     private ScheduledThreadPool keepaliveExecutor;
146     private EventExecutor eventExecutor;
147
148     /**
149      * The name associated with the Netconf mount point.  This value is passed from <code>NetconfConnectorModuleFactory</code>.
150      */
151     private String instanceName;
152
153     public NetconfConnectorModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
154         super(identifier, dependencyResolver);
155     }
156
157     public NetconfConnectorModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, final NetconfConnectorModule oldModule, final java.lang.AutoCloseable oldInstance) {
158         super(identifier, dependencyResolver, oldModule, oldInstance);
159     }
160
161     @Override
162     protected void customValidation() {
163         checkNotNull(getAddress(), addressJmxAttribute);
164         checkCondition(isHostAddressPresent(getAddress()), "Host address not present in " + getAddress(), addressJmxAttribute);
165         checkNotNull(getPort(), portJmxAttribute);
166
167         checkNotNull(getConnectionTimeoutMillis(), connectionTimeoutMillisJmxAttribute);
168         checkCondition(getConnectionTimeoutMillis() > 0, "must be > 0", connectionTimeoutMillisJmxAttribute);
169
170         checkNotNull(getDefaultRequestTimeoutMillis(), defaultRequestTimeoutMillisJmxAttribute);
171         checkCondition(getDefaultRequestTimeoutMillis() > 0, "must be > 0", defaultRequestTimeoutMillisJmxAttribute);
172
173         checkNotNull(getBetweenAttemptsTimeoutMillis(), betweenAttemptsTimeoutMillisJmxAttribute);
174         checkCondition(getBetweenAttemptsTimeoutMillis() > 0, "must be > 0", betweenAttemptsTimeoutMillisJmxAttribute);
175
176         // Check username + password in case of ssh
177         if(getTcpOnly() == false) {
178             checkNotNull(getUsername(), usernameJmxAttribute);
179             checkNotNull(getPassword(), passwordJmxAttribute);
180         }
181
182         userCapabilities = getUserCapabilities();
183     }
184
185     private boolean isHostAddressPresent(final Host address) {
186         return address.getDomainName() != null ||
187                address.getIpAddress() != null && (address.getIpAddress().getIpv4Address() != null || address.getIpAddress().getIpv6Address() != null);
188     }
189
190     @Deprecated
191     private static ScheduledExecutorService DEFAULT_KEEPALIVE_EXECUTOR;
192
193     @Override
194     public java.lang.AutoCloseable createInstance() {
195         initDependencies();
196         final RemoteDeviceId id = new RemoteDeviceId(getIdentifier(), getSocketAddress());
197
198         final ExecutorService globalProcessingExecutor = processingExecutor.getExecutor();
199
200         RemoteDeviceHandler<NetconfSessionPreferences> salFacade
201                 = new NetconfDeviceSalFacade(id, domRegistry, bindingRegistry);
202
203         final Long keepaliveDelay = getKeepaliveDelay();
204         if (shouldSendKeepalive()) {
205             // Keepalive executor is optional for now and a default instance is supported
206             final ScheduledExecutorService executor = keepaliveExecutor == null ? DEFAULT_KEEPALIVE_EXECUTOR : keepaliveExecutor.getExecutor();
207
208             salFacade = new KeepaliveSalFacade(id, salFacade, executor, keepaliveDelay, getDefaultRequestTimeoutMillis());
209         }
210
211         // Setup information related to the SchemaRegistry, SchemaResourceFactory, etc.
212         NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = null;
213         final String moduleSchemaCacheDirectory = getSchemaCacheDirectory();
214         // Only checks to ensure the String is not empty or null;  further checks related to directory accessibility and file permissions
215         // are handled during the FilesystemScehamSourceCache initialization.
216         if (!Strings.isNullOrEmpty(moduleSchemaCacheDirectory)) {
217             // If a custom schema cache directory is specified, create the backing DTO; otherwise, the SchemaRegistry and
218             // SchemaContextFactory remain the default values.
219             if (!moduleSchemaCacheDirectory.equals(DEFAULT_CACHE_DIRECTORY)) {
220                 // Multiple modules may be created at once;  synchronize to avoid issues with data consistency among threads.
221                 synchronized(schemaResourcesDTOs) {
222                     // Look for the cached DTO to reuse SchemaRegistry and SchemaContextFactory variables if they already exist
223                     final NetconfDevice.SchemaResourcesDTO dto =
224                             schemaResourcesDTOs.get(moduleSchemaCacheDirectory);
225                     if (dto == null) {
226                         schemaResourcesDTO = createSchemaResourcesDTO(moduleSchemaCacheDirectory);
227                         schemaRegistry.registerSchemaSourceListener(
228                                 TextToASTTransformer.create((SchemaRepository) schemaRegistry, schemaRegistry));
229                         schemaResourcesDTOs.put(moduleSchemaCacheDirectory, schemaResourcesDTO);
230                     } else {
231                         setSchemaContextFactory(dto.getSchemaContextFactory());
232                         setSchemaRegistry(dto.getSchemaRegistry());
233                         schemaResourcesDTO = dto;
234                     }
235                     if (userCapabilities.isPresent()) {
236                         for (QName qname : userCapabilities.get().getModuleBasedCaps()) {
237                             final SourceIdentifier sourceIdentifier = new SourceIdentifier(qname.getLocalName(), qname.getFormattedRevision());
238                             dto.getSchemaRegistry().registerSchemaSource(DEFAULT_CACHE, PotentialSchemaSource.create(sourceIdentifier, YangTextSchemaSource.class, LOCAL_IO_FALLBACK_COST));
239                         }
240                     }
241                 }
242                 LOG.info("Netconf connector for device {} will use schema cache directory {} instead of {}",
243                         instanceName, moduleSchemaCacheDirectory, DEFAULT_CACHE_DIRECTORY);
244             }
245         } else {
246             LOG.warn("schema-cache-directory for {} is null or empty;  using the default {}",
247                     instanceName, QUALIFIED_DEFAULT_CACHE_DIRECTORY);
248         }
249
250         if (schemaResourcesDTO == null) {
251             schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(schemaRegistry, schemaContextFactory,
252                     new NetconfStateSchemas.NetconfStateSchemasResolverImpl());
253         }
254
255         final NetconfDevice device = new NetconfDeviceBuilder()
256                 .setReconnectOnSchemasChange(getReconnectOnChangedSchema())
257                 .setSchemaResourcesDTO(schemaResourcesDTO)
258                 .setGlobalProcessingExecutor(globalProcessingExecutor)
259                 .setId(id)
260                 .setSalFacade(salFacade)
261                 .build();
262
263         final NetconfDeviceCommunicator listener = userCapabilities.isPresent() ?
264                 new NetconfDeviceCommunicator(id, device,
265                         new UserPreferences(userCapabilities.get(), getYangModuleCapabilities().getOverride())):
266                 new NetconfDeviceCommunicator(id, device);
267
268         if (shouldSendKeepalive()) {
269             ((KeepaliveSalFacade) salFacade).setListener(listener);
270         }
271
272         final NetconfReconnectingClientConfiguration clientConfig = getClientConfig(listener);
273         listener.initializeRemoteConnection(clientDispatcher, clientConfig);
274
275         return new SalConnectorCloseable(listener, salFacade);
276     }
277
278     /**
279      * Creates the backing Schema classes for a particular directory.
280      *
281      * @param moduleSchemaCacheDirectory The string directory relative to "cache"
282      * @return A DTO containing the Schema classes for the Netconf mount.
283      */
284     private NetconfDevice.SchemaResourcesDTO createSchemaResourcesDTO(final String moduleSchemaCacheDirectory) {
285         final SharedSchemaRepository repository = new SharedSchemaRepository(instanceName);
286         final SchemaContextFactory schemaContextFactory
287                 = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
288         setSchemaRegistry(repository);
289         setSchemaContextFactory(schemaContextFactory);
290         final FilesystemSchemaSourceCache<YangTextSchemaSource> deviceCache =
291                 createDeviceFilesystemCache(moduleSchemaCacheDirectory);
292         repository.registerSchemaSourceListener(deviceCache);
293         return new NetconfDevice.SchemaResourcesDTO(repository, schemaContextFactory,
294                 new NetconfStateSchemas.NetconfStateSchemasResolverImpl());
295     }
296
297     /**
298      * Creates a <code>FilesystemSchemaSourceCache</code> for the custom schema cache directory.
299      *
300      * @param schemaCacheDirectory The custom cache directory relative to "cache"
301      * @return A <code>FilesystemSchemaSourceCache</code> for the custom schema cache directory
302      */
303     private FilesystemSchemaSourceCache<YangTextSchemaSource> createDeviceFilesystemCache(final String schemaCacheDirectory) {
304         final String relativeSchemaCacheDirectory = CACHE_DIRECTORY + File.separator + schemaCacheDirectory;
305         return new FilesystemSchemaSourceCache<>(schemaRegistry, YangTextSchemaSource.class, new File(relativeSchemaCacheDirectory));
306     }
307
308     private void initDependencies() {
309         domRegistry = getDomRegistryDependency();
310         clientDispatcher = getClientDispatcherDependency();
311         bindingRegistry = getBindingRegistryDependency();
312         processingExecutor = getProcessingExecutorDependency();
313         eventExecutor = getEventExecutorDependency();
314
315         if(getKeepaliveExecutor() == null) {
316             LOG.warn("Keepalive executor missing. Using default instance for now, the configuration needs to be updated");
317
318             // Instantiate the default executor, now we know its necessary
319             if(DEFAULT_KEEPALIVE_EXECUTOR == null) {
320                 DEFAULT_KEEPALIVE_EXECUTOR = Executors.newScheduledThreadPool(2, new ThreadFactory() {
321                     @Override
322                     public Thread newThread(final Runnable r) {
323                         final Thread thread = new Thread(r);
324                         thread.setName("netconf-southound-keepalives-" + thread.getId());
325                         thread.setDaemon(true);
326                         return thread;
327                     }
328                 });
329             }
330         } else {
331             keepaliveExecutor = getKeepaliveExecutorDependency();
332         }
333     }
334
335     private boolean shouldSendKeepalive() {
336         return getKeepaliveDelay() > 0;
337     }
338
339     private Optional<NetconfSessionPreferences> getUserCapabilities() {
340         if(getYangModuleCapabilities() == null) {
341             return Optional.absent();
342         }
343
344         final List<String> capabilities = getYangModuleCapabilities().getCapability();
345         if(capabilities == null || capabilities.isEmpty()) {
346             return Optional.absent();
347         }
348
349         final NetconfSessionPreferences parsedOverrideCapabilities = NetconfSessionPreferences.fromStrings(capabilities);
350         JmxAttributeValidationException.checkCondition(
351                 parsedOverrideCapabilities.getNonModuleCaps().isEmpty(),
352                 "Capabilities to override can only contain module based capabilities, non-module capabilities will be retrieved from the device," +
353                         " configured non-module capabilities: " + parsedOverrideCapabilities.getNonModuleCaps(),
354                 yangModuleCapabilitiesJmxAttribute);
355
356         return Optional.of(parsedOverrideCapabilities);
357     }
358
359     public NetconfReconnectingClientConfiguration getClientConfig(final NetconfDeviceCommunicator listener) {
360         final InetSocketAddress socketAddress = getSocketAddress();
361         final long clientConnectionTimeoutMillis = getConnectionTimeoutMillis();
362
363         final ReconnectStrategyFactory sf = new TimedReconnectStrategyFactory(eventExecutor,
364                 getMaxConnectionAttempts(), getBetweenAttemptsTimeoutMillis(), getSleepFactor());
365         final ReconnectStrategy strategy = sf.createReconnectStrategy();
366
367         return NetconfReconnectingClientConfigurationBuilder.create()
368         .withAddress(socketAddress)
369         .withConnectionTimeoutMillis(clientConnectionTimeoutMillis)
370         .withReconnectStrategy(strategy)
371         .withAuthHandler(new LoginPassword(getUsername(), getPassword()))
372         .withProtocol(getTcpOnly() ?
373                 NetconfClientConfiguration.NetconfClientProtocol.TCP :
374                 NetconfClientConfiguration.NetconfClientProtocol.SSH)
375         .withConnectStrategyFactory(sf)
376         .withSessionListener(listener)
377         .build();
378     }
379
380     private static final class SalConnectorCloseable implements AutoCloseable {
381         private final RemoteDeviceHandler<NetconfSessionPreferences> salFacade;
382         private final NetconfDeviceCommunicator listener;
383
384         public SalConnectorCloseable(final NetconfDeviceCommunicator listener,
385                                      final RemoteDeviceHandler<NetconfSessionPreferences> salFacade) {
386             this.listener = listener;
387             this.salFacade = salFacade;
388         }
389
390         @Override
391         public void close() {
392             listener.close();
393             salFacade.close();
394         }
395     }
396
397     private static final class TimedReconnectStrategyFactory implements ReconnectStrategyFactory {
398         private final Long connectionAttempts;
399         private final EventExecutor executor;
400         private final double sleepFactor;
401         private final int minSleep;
402
403         TimedReconnectStrategyFactory(final EventExecutor executor, final Long maxConnectionAttempts, final int minSleep, final BigDecimal sleepFactor) {
404             if (maxConnectionAttempts != null && maxConnectionAttempts > 0) {
405                 connectionAttempts = maxConnectionAttempts;
406             } else {
407                 LOG.trace("Setting {} on {} to infinity", maxConnectionAttemptsJmxAttribute, this);
408                 connectionAttempts = null;
409             }
410
411             this.sleepFactor = sleepFactor.doubleValue();
412             this.executor = executor;
413             this.minSleep = minSleep;
414         }
415
416         @Override
417         public ReconnectStrategy createReconnectStrategy() {
418             final Long maxSleep = null;
419             final Long deadline = null;
420
421             return new TimedReconnectStrategy(executor, minSleep,
422                     minSleep, sleepFactor, maxSleep, connectionAttempts, deadline);
423         }
424     }
425
426     private InetSocketAddress getSocketAddress() {
427         if(getAddress().getDomainName() != null) {
428             return new InetSocketAddress(getAddress().getDomainName().getValue(), getPort().getValue());
429         } else {
430             final IpAddress ipAddress = getAddress().getIpAddress();
431             final String ip = ipAddress.getIpv4Address() != null ? ipAddress.getIpv4Address().getValue() : ipAddress.getIpv6Address().getValue();
432             return new InetSocketAddress(ip, getPort().getValue());
433         }
434     }
435
436     public void setSchemaRegistry(final SchemaSourceRegistry schemaRegistry) {
437         this.schemaRegistry = schemaRegistry;
438     }
439
440     public void setSchemaContextFactory(final SchemaContextFactory schemaContextFactory) {
441         this.schemaContextFactory = schemaContextFactory;
442     }
443
444     public void setInstanceName(final String instanceName) {
445         this.instanceName = instanceName;
446     }
447 }