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