2 * Copyright (c) 2014 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
8 package org.opendaylight.controller.config.yang.md.sal.connector.netconf;
10 import static org.opendaylight.controller.config.api.JmxAttributeValidationException.checkCondition;
11 import static org.opendaylight.controller.config.api.JmxAttributeValidationException.checkNotNull;
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;
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;
45 import com.google.common.base.Preconditions;
46 import com.google.common.net.InetAddresses;
47 import io.netty.util.HashedWheelTimer;
52 public final class NetconfConnectorModule extends org.opendaylight.controller.config.yang.md.sal.connector.netconf.AbstractNetconfConnectorModule
54 private static final Logger logger = LoggerFactory.getLogger(NetconfConnectorModule.class);
56 private static AbstractCachingSchemaSourceProvider<String, InputStream> GLOBAL_NETCONF_SOURCE_PROVIDER = null;
57 private BundleContext bundleContext;
59 public NetconfConnectorModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
60 super(identifier, dependencyResolver);
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);
68 protected void customValidation() {
69 checkNotNull(getAddress(), addressJmxAttribute);
70 checkNotNull(getPort(), portJmxAttribute);
71 checkNotNull(getDomRegistry(), portJmxAttribute);
72 checkNotNull(getDomRegistry(), domRegistryJmxAttribute);
74 checkNotNull(getConnectionTimeoutMillis(), connectionTimeoutMillisJmxAttribute);
75 checkCondition(getConnectionTimeoutMillis() > 0, "must be > 0", connectionTimeoutMillisJmxAttribute);
77 checkNotNull(getBetweenAttemptsTimeoutMillis(), betweenAttemptsTimeoutMillisJmxAttribute);
78 checkCondition(getBetweenAttemptsTimeoutMillis() > 0, "must be > 0", betweenAttemptsTimeoutMillisJmxAttribute);
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);
86 // Check username + password in case of ssh
87 if(getTcpOnly() == false) {
88 checkNotNull(getUsername(), usernameJmxAttribute);
89 checkNotNull(getPassword(), passwordJmxAttribute);
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.");
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.");
112 public java.lang.AutoCloseable createInstance() {
113 final RemoteDeviceId id = new RemoteDeviceId(getIdentifier());
115 final ExecutorService globalProcessingExecutor = getGlobalProcessingExecutor();
117 final Broker domBroker = getDomRegistryDependency();
118 final BindingAwareBroker bindingBroker = getBindingRegistryBackwards();
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);
126 // FIXME BUG-944 remove backwards compatibility
127 final NetconfClientDispatcher dispatcher = getClientDispatcher() == null ? createDispatcher() : getClientDispatcherDependency();
128 listener.initializeRemoteConnection(dispatcher, clientConfig);
130 return new AutoCloseable() {
132 public void close() throws Exception {
139 private BindingAwareBroker getBindingRegistryBackwards() {
140 if(getBindingRegistry() != null) {
141 return getBindingRegistryDependency();
143 // FIXME BUG 944 remove backwards compatibility
144 final ServiceReference<BindingAwareBroker> serviceReference = bundleContext.getServiceReference(BindingAwareBroker.class);
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);
154 private ExecutorService getGlobalProcessingExecutor() {
155 if(getProcessingExecutor() != null) {
156 return getProcessingExecutorDependency().getExecutor();
158 // FIXME BUG 944 remove backwards compatibility
159 return Executors.newCachedThreadPool();
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);
171 return GLOBAL_NETCONF_SOURCE_PROVIDER;
174 // FIXME BUG-944 remove backwards compatibility
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.
180 private NetconfClientDispatcher createDispatcher() {
181 return new NetconfClientDispatcherImpl(getBossThreadGroupDependency(), getWorkerThreadGroupDependency(), new HashedWheelTimer());
184 public void setBundleContext(final BundleContext bundleContext) {
185 this.bundleContext = bundleContext;
188 public NetconfReconnectingClientConfiguration getClientConfig(final NetconfDeviceCommunicator listener) {
189 final InetSocketAddress socketAddress = getSocketAddress();
190 final ReconnectStrategy strategy = getReconnectStrategy();
191 final long clientConnectionTimeoutMillis = getConnectionTimeoutMillis();
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() {
204 public ReconnectStrategy createReconnectStrategy() {
205 return getReconnectStrategy();
211 private ReconnectStrategy getReconnectStrategy() {
212 final Long connectionAttempts;
213 if (getMaxConnectionAttempts() != null && getMaxConnectionAttempts() > 0) {
214 connectionAttempts = getMaxConnectionAttempts();
216 logger.trace("Setting {} on {} to infinity", maxConnectionAttemptsJmxAttribute, this);
217 connectionAttempts = null;
219 final double sleepFactor = getSleepFactor().doubleValue();
220 final int minSleep = getBetweenAttemptsTimeoutMillis();
221 final Long maxSleep = null;
222 final Long deadline = null;
224 return new TimedReconnectStrategy(getEventExecutorDependency(), getBetweenAttemptsTimeoutMillis(),
225 minSleep, sleepFactor, maxSleep, connectionAttempts, deadline);
228 private InetSocketAddress getSocketAddress() {
230 * Uncomment after Switch to IP Address
231 if(getAddress().getIpv4Address() != null) {
232 addressValue = getAddress().getIpv4Address().getValue();
234 addressValue = getAddress().getIpv6Address().getValue();
237 final InetAddress inetAddress = InetAddresses.forString(getAddress());
238 return new InetSocketAddress(inetAddress, getPort().intValue());