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;
13 import com.google.common.base.Optional;
14 import io.netty.util.concurrent.EventExecutor;
15 import java.math.BigDecimal;
16 import java.net.InetSocketAddress;
17 import java.util.List;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.ThreadFactory;
22 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
23 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
24 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
25 import org.opendaylight.controller.netconf.client.conf.NetconfReconnectingClientConfiguration;
26 import org.opendaylight.controller.netconf.client.conf.NetconfReconnectingClientConfigurationBuilder;
27 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
28 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
29 import org.opendaylight.controller.sal.connect.api.RemoteDeviceHandler;
30 import org.opendaylight.controller.sal.connect.netconf.NetconfDevice;
31 import org.opendaylight.controller.sal.connect.netconf.NetconfStateSchemas;
32 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfDeviceCommunicator;
33 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionPreferences;
34 import org.opendaylight.controller.sal.connect.netconf.sal.KeepaliveSalFacade;
35 import org.opendaylight.controller.sal.connect.netconf.sal.NetconfDeviceSalFacade;
36 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
37 import org.opendaylight.controller.sal.core.api.Broker;
38 import org.opendaylight.protocol.framework.ReconnectStrategy;
39 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
40 import org.opendaylight.protocol.framework.TimedReconnectStrategy;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Host;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
43 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
44 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
45 import org.osgi.framework.BundleContext;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
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 BundleContext bundleContext;
57 private Optional<NetconfSessionPreferences> userCapabilities;
58 private SchemaSourceRegistry schemaRegistry;
59 private SchemaContextFactory schemaContextFactory;
61 public NetconfConnectorModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
62 super(identifier, dependencyResolver);
65 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) {
66 super(identifier, dependencyResolver, oldModule, oldInstance);
70 protected void customValidation() {
71 checkNotNull(getAddress(), addressJmxAttribute);
72 checkCondition(isHostAddressPresent(getAddress()), "Host address not present in " + getAddress(), addressJmxAttribute);
73 checkNotNull(getPort(), portJmxAttribute);
74 checkNotNull(getDomRegistry(), portJmxAttribute);
75 checkNotNull(getDomRegistry(), domRegistryJmxAttribute);
77 checkNotNull(getConnectionTimeoutMillis(), connectionTimeoutMillisJmxAttribute);
78 checkCondition(getConnectionTimeoutMillis() > 0, "must be > 0", connectionTimeoutMillisJmxAttribute);
80 checkNotNull(getConnectionTimeoutMillis(), defaultRequestTimeoutMillisJmxAttribute);
81 checkCondition(getConnectionTimeoutMillis() > 0, "must be > 0", defaultRequestTimeoutMillisJmxAttribute);
83 checkNotNull(getBetweenAttemptsTimeoutMillis(), betweenAttemptsTimeoutMillisJmxAttribute);
84 checkCondition(getBetweenAttemptsTimeoutMillis() > 0, "must be > 0", betweenAttemptsTimeoutMillisJmxAttribute);
86 checkNotNull(getClientDispatcher(), clientDispatcherJmxAttribute);
87 checkNotNull(getBindingRegistry(), bindingRegistryJmxAttribute);
88 checkNotNull(getProcessingExecutor(), processingExecutorJmxAttribute);
90 // Check username + password in case of ssh
91 if(getTcpOnly() == false) {
92 checkNotNull(getUsername(), usernameJmxAttribute);
93 checkNotNull(getPassword(), passwordJmxAttribute);
96 userCapabilities = getUserCapabilities();
98 if(getKeepaliveExecutor() == null) {
99 logger.warn("Keepalive executor missing. Using default instance for now, the configuration needs to be updated");
101 // Instantiate the default executor, now we know its necessary
102 if(DEFAULT_KEEPALIVE_EXECUTOR == null) {
103 DEFAULT_KEEPALIVE_EXECUTOR = Executors.newScheduledThreadPool(2, new ThreadFactory() {
105 public Thread newThread(final Runnable r) {
106 final Thread thread = new Thread(r);
107 thread.setName("netconf-southound-keepalives-" + thread.getId());
108 thread.setDaemon(true);
116 private boolean isHostAddressPresent(final Host address) {
117 return address.getDomainName() != null ||
118 address.getIpAddress() != null && (address.getIpAddress().getIpv4Address() != null || address.getIpAddress().getIpv6Address() != null);
122 private static ScheduledExecutorService DEFAULT_KEEPALIVE_EXECUTOR;
125 public java.lang.AutoCloseable createInstance() {
126 final RemoteDeviceId id = new RemoteDeviceId(getIdentifier(), getSocketAddress());
128 final ExecutorService globalProcessingExecutor = getProcessingExecutorDependency().getExecutor();
130 final Broker domBroker = getDomRegistryDependency();
131 final BindingAwareBroker bindingBroker = getBindingRegistryDependency();
133 RemoteDeviceHandler<NetconfSessionPreferences> salFacade
134 = new NetconfDeviceSalFacade(id, domBroker, bindingBroker, bundleContext, getDefaultRequestTimeoutMillis());
136 final Long keepaliveDelay = getKeepaliveDelay();
137 if(shouldSendKeepalive()) {
138 // Keepalive executor is optional for now and a default instance is supported
139 final ScheduledExecutorService executor = getKeepaliveExecutor() == null ?
140 DEFAULT_KEEPALIVE_EXECUTOR : getKeepaliveExecutorDependency().getExecutor();
141 salFacade = new KeepaliveSalFacade(id, salFacade, executor, keepaliveDelay);
144 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO =
145 new NetconfDevice.SchemaResourcesDTO(schemaRegistry, schemaContextFactory, new NetconfStateSchemas.NetconfStateSchemasResolverImpl());
147 final NetconfDevice device =
148 new NetconfDevice(schemaResourcesDTO, id, salFacade, globalProcessingExecutor, getReconnectOnChangedSchema());
150 final NetconfDeviceCommunicator listener = userCapabilities.isPresent() ?
151 new NetconfDeviceCommunicator(id, device, userCapabilities.get()) : new NetconfDeviceCommunicator(id, device);
153 if(shouldSendKeepalive()) {
154 ((KeepaliveSalFacade) salFacade).setListener(listener);
157 final NetconfReconnectingClientConfiguration clientConfig = getClientConfig(listener);
158 final NetconfClientDispatcher dispatcher = getClientDispatcherDependency();
160 listener.initializeRemoteConnection(dispatcher, clientConfig);
162 return new SalConnectorCloseable(listener, salFacade);
165 private boolean shouldSendKeepalive() {
166 return getKeepaliveDelay() > 0;
169 private Optional<NetconfSessionPreferences> getUserCapabilities() {
170 if(getYangModuleCapabilities() == null) {
171 return Optional.absent();
174 final List<String> capabilities = getYangModuleCapabilities().getCapability();
175 if(capabilities == null || capabilities.isEmpty()) {
176 return Optional.absent();
179 final NetconfSessionPreferences parsedOverrideCapabilities = NetconfSessionPreferences.fromStrings(capabilities);
180 JmxAttributeValidationException.checkCondition(
181 parsedOverrideCapabilities.getNonModuleCaps().isEmpty(),
182 "Capabilities to override can only contain module based capabilities, non-module capabilities will be retrieved from the device," +
183 " configured non-module capabilities: " + parsedOverrideCapabilities.getNonModuleCaps(),
184 yangModuleCapabilitiesJmxAttribute);
186 return Optional.of(parsedOverrideCapabilities);
189 public void setBundleContext(final BundleContext bundleContext) {
190 this.bundleContext = bundleContext;
193 public NetconfReconnectingClientConfiguration getClientConfig(final NetconfDeviceCommunicator listener) {
194 final InetSocketAddress socketAddress = getSocketAddress();
195 final long clientConnectionTimeoutMillis = getConnectionTimeoutMillis();
197 final ReconnectStrategyFactory sf = new TimedReconnectStrategyFactory(
198 getEventExecutorDependency(), getMaxConnectionAttempts(), getBetweenAttemptsTimeoutMillis(), getSleepFactor());
199 final ReconnectStrategy strategy = sf.createReconnectStrategy();
201 return NetconfReconnectingClientConfigurationBuilder.create()
202 .withAddress(socketAddress)
203 .withConnectionTimeoutMillis(clientConnectionTimeoutMillis)
204 .withReconnectStrategy(strategy)
205 .withAuthHandler(new LoginPassword(getUsername(), getPassword()))
206 .withProtocol(getTcpOnly() ?
207 NetconfClientConfiguration.NetconfClientProtocol.TCP :
208 NetconfClientConfiguration.NetconfClientProtocol.SSH)
209 .withConnectStrategyFactory(sf)
210 .withSessionListener(listener)
214 private static final class SalConnectorCloseable implements AutoCloseable {
215 private final RemoteDeviceHandler<NetconfSessionPreferences> salFacade;
216 private final NetconfDeviceCommunicator listener;
218 public SalConnectorCloseable(final NetconfDeviceCommunicator listener,
219 final RemoteDeviceHandler<NetconfSessionPreferences> salFacade) {
220 this.listener = listener;
221 this.salFacade = salFacade;
225 public void close() {
231 private static final class TimedReconnectStrategyFactory implements ReconnectStrategyFactory {
232 private final Long connectionAttempts;
233 private final EventExecutor executor;
234 private final double sleepFactor;
235 private final int minSleep;
237 TimedReconnectStrategyFactory(final EventExecutor executor, final Long maxConnectionAttempts, final int minSleep, final BigDecimal sleepFactor) {
238 if (maxConnectionAttempts != null && maxConnectionAttempts > 0) {
239 connectionAttempts = maxConnectionAttempts;
241 logger.trace("Setting {} on {} to infinity", maxConnectionAttemptsJmxAttribute, this);
242 connectionAttempts = null;
245 this.sleepFactor = sleepFactor.doubleValue();
246 this.executor = executor;
247 this.minSleep = minSleep;
251 public ReconnectStrategy createReconnectStrategy() {
252 final Long maxSleep = null;
253 final Long deadline = null;
255 return new TimedReconnectStrategy(executor, minSleep,
256 minSleep, sleepFactor, maxSleep, connectionAttempts, deadline);
260 private InetSocketAddress getSocketAddress() {
261 if(getAddress().getDomainName() != null) {
262 return new InetSocketAddress(getAddress().getDomainName().getValue(), getPort().getValue());
264 final IpAddress ipAddress = getAddress().getIpAddress();
265 final String ip = ipAddress.getIpv4Address() != null ? ipAddress.getIpv4Address().getValue() : ipAddress.getIpv6Address().getValue();
266 return new InetSocketAddress(ip, getPort().getValue());
270 public void setSchemaRegistry(final SchemaSourceRegistry schemaRegistry) {
271 this.schemaRegistry = schemaRegistry;
274 public void setSchemaContextFactory(final SchemaContextFactory schemaContextFactory) {
275 this.schemaContextFactory = schemaContextFactory;