Introducing the Modification classses
[controller.git] / opendaylight / md-sal / 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 java.io.File;
14 import java.io.InputStream;
15 import java.net.InetAddress;
16 import java.net.InetSocketAddress;
17 import java.util.concurrent.ExecutorService;
18 import java.util.concurrent.Executors;
19
20 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
21 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
22 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
23 import org.opendaylight.controller.netconf.client.conf.NetconfReconnectingClientConfiguration;
24 import org.opendaylight.controller.netconf.client.conf.NetconfReconnectingClientConfigurationBuilder;
25 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
26 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
27 import org.opendaylight.controller.sal.connect.api.RemoteDeviceHandler;
28 import org.opendaylight.controller.sal.connect.netconf.NetconfDevice;
29 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfDeviceCommunicator;
30 import org.opendaylight.controller.sal.connect.netconf.sal.NetconfDeviceSalFacade;
31 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
32 import org.opendaylight.controller.sal.core.api.Broker;
33 import org.opendaylight.protocol.framework.ReconnectStrategy;
34 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
35 import org.opendaylight.protocol.framework.TimedReconnectStrategy;
36 import org.opendaylight.yangtools.yang.model.util.repo.AbstractCachingSchemaSourceProvider;
37 import org.opendaylight.yangtools.yang.model.util.repo.FilesystemSchemaCachingProvider;
38 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProvider;
39 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProviders;
40 import org.osgi.framework.BundleContext;
41 import org.osgi.framework.ServiceReference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 import com.google.common.base.Preconditions;
46 import com.google.common.net.InetAddresses;
47 import io.netty.util.HashedWheelTimer;
48
49 /**
50  *
51  */
52 public final class NetconfConnectorModule extends org.opendaylight.controller.config.yang.md.sal.connector.netconf.AbstractNetconfConnectorModule
53 {
54     private static final Logger logger = LoggerFactory.getLogger(NetconfConnectorModule.class);
55
56     private static AbstractCachingSchemaSourceProvider<String, InputStream> GLOBAL_NETCONF_SOURCE_PROVIDER = null;
57     private BundleContext bundleContext;
58
59     public NetconfConnectorModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
60         super(identifier, dependencyResolver);
61     }
62
63     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) {
64         super(identifier, dependencyResolver, oldModule, oldInstance);
65     }
66
67     @Override
68     protected void customValidation() {
69         checkNotNull(getAddress(), addressJmxAttribute);
70         checkNotNull(getPort(), portJmxAttribute);
71         checkNotNull(getDomRegistry(), portJmxAttribute);
72         checkNotNull(getDomRegistry(), domRegistryJmxAttribute);
73
74         checkNotNull(getConnectionTimeoutMillis(), connectionTimeoutMillisJmxAttribute);
75         checkCondition(getConnectionTimeoutMillis() > 0, "must be > 0", connectionTimeoutMillisJmxAttribute);
76
77         checkNotNull(getBetweenAttemptsTimeoutMillis(), betweenAttemptsTimeoutMillisJmxAttribute);
78         checkCondition(getBetweenAttemptsTimeoutMillis() > 0, "must be > 0", betweenAttemptsTimeoutMillisJmxAttribute);
79
80         // FIXME BUG-944 remove backwards compatibility
81         if(getClientDispatcher() == null) {
82             checkCondition(getBossThreadGroup() != null, "Client dispatcher was not set, thread groups have to be set instead", bossThreadGroupJmxAttribute);
83             checkCondition(getWorkerThreadGroup() != null, "Client dispatcher was not set, thread groups have to be set instead", workerThreadGroupJmxAttribute);
84         }
85
86         // Check username + password in case of ssh
87         if(getTcpOnly() == false) {
88             checkNotNull(getUsername(), usernameJmxAttribute);
89             checkNotNull(getPassword(), passwordJmxAttribute);
90         }
91
92         // FIXME BUG 944 remove this warning
93         if(getBindingRegistry() == null) {
94             logger.warn("Configuration property: \"binding-registry\" not set for sal-netconf-connector (" + getIdentifier() + "). " +
95                     "Netconf-connector now requires a dependency on \"binding-broker-osgi-registry\". " +
96                     "The dependency is optional for now to preserve backwards compatibility, but will be mandatory in the future. " +
97                     "Please set the property as in \"01-netconf-connector\" initial config file. " +
98                     "The service will be retrieved from OSGi service registry now.");
99         }
100
101         // FIXME BUG 944 remove this warning
102         if(getProcessingExecutor() == null) {
103             logger.warn("Configuration property: \"processing-executor\" not set for sal-netconf-connector (" + getIdentifier() + "). " +
104                     "Netconf-connector now requires a dependency on \"threadpool\". " +
105                     "The dependency is optional for now to preserve backwards compatibility, but will be mandatory in the future. " +
106                     "Please set the property as in \"01-netconf-connector\" initial config file. " +
107                     "New instance will be created for the executor.");
108         }
109     }
110
111     @Override
112     public java.lang.AutoCloseable createInstance() {
113         final RemoteDeviceId id = new RemoteDeviceId(getIdentifier());
114
115         final ExecutorService globalProcessingExecutor = getGlobalProcessingExecutor();
116
117         final Broker domBroker = getDomRegistryDependency();
118         final BindingAwareBroker bindingBroker = getBindingRegistryBackwards();
119
120         final RemoteDeviceHandler salFacade = new NetconfDeviceSalFacade(id, domBroker, bindingBroker, bundleContext, globalProcessingExecutor);
121         final NetconfDevice device =
122                 NetconfDevice.createNetconfDevice(id, getGlobalNetconfSchemaProvider(), globalProcessingExecutor, salFacade);
123         final NetconfDeviceCommunicator listener = new NetconfDeviceCommunicator(id, device);
124         final NetconfReconnectingClientConfiguration clientConfig = getClientConfig(listener);
125
126         // FIXME BUG-944 remove backwards compatibility
127         final NetconfClientDispatcher dispatcher = getClientDispatcher() == null ? createDispatcher() : getClientDispatcherDependency();
128         listener.initializeRemoteConnection(dispatcher, clientConfig);
129
130         return new AutoCloseable() {
131             @Override
132             public void close() throws Exception {
133                 listener.close();
134                 salFacade.close();
135             }
136         };
137     }
138
139     private BindingAwareBroker getBindingRegistryBackwards() {
140         if(getBindingRegistry() != null) {
141             return getBindingRegistryDependency();
142         } else {
143             // FIXME BUG 944 remove backwards compatibility
144             final ServiceReference<BindingAwareBroker> serviceReference = bundleContext.getServiceReference(BindingAwareBroker.class);
145             Preconditions
146                     .checkNotNull(
147                             serviceReference,
148                             "Unable to retrieve %s from OSGi service registry, use binding-registry config property to inject %s with config subsystem",
149                             BindingAwareBroker.class, BindingAwareBroker.class);
150             return bundleContext.getService(serviceReference);
151         }
152     }
153
154     private ExecutorService getGlobalProcessingExecutor() {
155         if(getProcessingExecutor() != null) {
156             return getProcessingExecutorDependency().getExecutor();
157         } else {
158             // FIXME BUG 944 remove backwards compatibility
159             return Executors.newCachedThreadPool();
160         }
161     }
162
163     private synchronized AbstractCachingSchemaSourceProvider<String, InputStream> getGlobalNetconfSchemaProvider() {
164         if(GLOBAL_NETCONF_SOURCE_PROVIDER == null) {
165             final String storageFile = "cache/schema";
166             //            File directory = bundleContext.getDataFile(storageFile);
167             final File directory = new File(storageFile);
168             final SchemaSourceProvider<String> defaultProvider = SchemaSourceProviders.noopProvider();
169             GLOBAL_NETCONF_SOURCE_PROVIDER = FilesystemSchemaCachingProvider.createFromStringSourceProvider(defaultProvider, directory);
170         }
171         return GLOBAL_NETCONF_SOURCE_PROVIDER;
172     }
173
174     // FIXME BUG-944 remove backwards compatibility
175     /**
176      * @deprecated Use getClientDispatcherDependency method instead to retrieve injected dispatcher.
177      * This one creates new instance of NetconfClientDispatcher and will be removed in near future.
178      */
179     @Deprecated
180     private NetconfClientDispatcher createDispatcher() {
181         return new NetconfClientDispatcherImpl(getBossThreadGroupDependency(), getWorkerThreadGroupDependency(), new HashedWheelTimer());
182     }
183
184     public void setBundleContext(final BundleContext bundleContext) {
185         this.bundleContext = bundleContext;
186     }
187
188     public NetconfReconnectingClientConfiguration getClientConfig(final NetconfDeviceCommunicator listener) {
189         final InetSocketAddress socketAddress = getSocketAddress();
190         final ReconnectStrategy strategy = getReconnectStrategy();
191         final long clientConnectionTimeoutMillis = getConnectionTimeoutMillis();
192
193         return NetconfReconnectingClientConfigurationBuilder.create()
194         .withAddress(socketAddress)
195         .withConnectionTimeoutMillis(clientConnectionTimeoutMillis)
196         .withReconnectStrategy(strategy)
197         .withSessionListener(listener)
198         .withAuthHandler(new LoginPassword(getUsername(),getPassword()))
199         .withProtocol(getTcpOnly() ?
200                 NetconfClientConfiguration.NetconfClientProtocol.TCP :
201                 NetconfClientConfiguration.NetconfClientProtocol.SSH)
202         .withConnectStrategyFactory(new ReconnectStrategyFactory() {
203             @Override
204             public ReconnectStrategy createReconnectStrategy() {
205                 return getReconnectStrategy();
206             }
207         })
208         .build();
209     }
210
211     private ReconnectStrategy getReconnectStrategy() {
212         final Long connectionAttempts;
213         if (getMaxConnectionAttempts() != null && getMaxConnectionAttempts() > 0) {
214             connectionAttempts = getMaxConnectionAttempts();
215         } else {
216             logger.trace("Setting {} on {} to infinity", maxConnectionAttemptsJmxAttribute, this);
217             connectionAttempts = null;
218         }
219         final double sleepFactor = getSleepFactor().doubleValue();
220         final int minSleep = getBetweenAttemptsTimeoutMillis();
221         final Long maxSleep = null;
222         final Long deadline = null;
223
224         return new TimedReconnectStrategy(getEventExecutorDependency(), getBetweenAttemptsTimeoutMillis(),
225                 minSleep, sleepFactor, maxSleep, connectionAttempts, deadline);
226     }
227
228     private InetSocketAddress getSocketAddress() {
229         /*
230          * Uncomment after Switch to IP Address
231         if(getAddress().getIpv4Address() != null) {
232             addressValue = getAddress().getIpv4Address().getValue();
233         } else {
234             addressValue = getAddress().getIpv6Address().getValue();
235         }
236          */
237         final InetAddress inetAddress = InetAddresses.forString(getAddress());
238         return new InetSocketAddress(inetAddress, getPort().intValue());
239     }
240 }