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 com.google.common.net.InetAddresses;
11 import io.netty.channel.EventLoopGroup;
12 import io.netty.util.concurrent.GlobalEventExecutor;
13 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
14 import org.opendaylight.controller.netconf.client.NetconfSshClientDispatcher;
15 import org.opendaylight.controller.netconf.util.handler.ssh.authentication.AuthenticationHandler;
16 import org.opendaylight.controller.netconf.util.handler.ssh.authentication.LoginPassword;
17 import org.opendaylight.controller.sal.connect.netconf.NetconfDevice;
18 import org.opendaylight.protocol.framework.ReconnectStrategy;
19 import org.opendaylight.protocol.framework.TimedReconnectStrategy;
20 import org.opendaylight.yangtools.yang.model.util.repo.AbstractCachingSchemaSourceProvider;
21 import org.opendaylight.yangtools.yang.model.util.repo.FilesystemSchemaCachingProvider;
22 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProvider;
23 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProviders;
24 import org.osgi.framework.BundleContext;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 import java.io.InputStream;
30 import java.net.InetAddress;
31 import java.net.InetSocketAddress;
32 import java.util.concurrent.ExecutorService;
33 import java.util.concurrent.Executors;
35 import static org.opendaylight.controller.config.api.JmxAttributeValidationException.checkCondition;
36 import static org.opendaylight.controller.config.api.JmxAttributeValidationException.checkNotNull;
41 public final class NetconfConnectorModule extends org.opendaylight.controller.config.yang.md.sal.connector.netconf.AbstractNetconfConnectorModule
43 private static final Logger logger = LoggerFactory.getLogger(NetconfConnectorModule.class);
45 private static ExecutorService GLOBAL_PROCESSING_EXECUTOR = null;
46 private static AbstractCachingSchemaSourceProvider<String, InputStream> GLOBAL_NETCONF_SOURCE_PROVIDER = null;
47 private BundleContext bundleContext;
49 public NetconfConnectorModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
50 super(identifier, dependencyResolver);
53 public NetconfConnectorModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, NetconfConnectorModule oldModule, java.lang.AutoCloseable oldInstance) {
54 super(identifier, dependencyResolver, oldModule, oldInstance);
58 protected void customValidation() {
59 checkNotNull(getAddress(), addressJmxAttribute);
60 //checkState(getAddress().getIpv4Address() != null || getAddress().getIpv6Address() != null,"Address must be set.");
61 checkNotNull(getPort(), portJmxAttribute);
62 checkNotNull(getDomRegistry(), portJmxAttribute);
63 checkNotNull(getDomRegistry(), domRegistryJmxAttribute);
65 checkNotNull(getConnectionTimeoutMillis(), connectionTimeoutMillisJmxAttribute);
66 checkCondition(getConnectionTimeoutMillis() > 0, "must be > 0", connectionTimeoutMillisJmxAttribute);
68 checkNotNull(getBetweenAttemptsTimeoutMillis(), betweenAttemptsTimeoutMillisJmxAttribute);
69 checkCondition(getBetweenAttemptsTimeoutMillis() > 0, "must be > 0", betweenAttemptsTimeoutMillisJmxAttribute);
74 public java.lang.AutoCloseable createInstance() {
76 getDomRegistryDependency();
77 NetconfDevice device = new NetconfDevice(getIdentifier().getInstanceName());
78 String addressValue = getAddress();
80 Long connectionAttempts;
81 if (getMaxConnectionAttempts() != null && getMaxConnectionAttempts() > 0) {
82 connectionAttempts = getMaxConnectionAttempts();
84 logger.trace("Setting {} on {} to infinity", maxConnectionAttemptsJmxAttribute, this);
85 connectionAttempts = null;
87 long clientConnectionTimeoutMillis = getConnectionTimeoutMillis();
89 * Uncomment after Switch to IP Address
90 if(getAddress().getIpv4Address() != null) {
91 addressValue = getAddress().getIpv4Address().getValue();
93 addressValue = getAddress().getIpv6Address().getValue();
96 double sleepFactor = 1.0;
100 ReconnectStrategy strategy = new TimedReconnectStrategy(GlobalEventExecutor.INSTANCE, getBetweenAttemptsTimeoutMillis(),
101 minSleep, sleepFactor, maxSleep, connectionAttempts, deadline);
103 device.setReconnectStrategy(strategy);
105 InetAddress addr = InetAddresses.forString(addressValue);
106 InetSocketAddress socketAddress = new InetSocketAddress(addr , getPort().intValue());
109 device.setProcessingExecutor(getGlobalProcessingExecutor());
111 device.setSocketAddress(socketAddress);
112 device.setEventExecutor(getEventExecutorDependency());
113 device.setDispatcher(createDispatcher(clientConnectionTimeoutMillis));
114 device.setSchemaSourceProvider(getGlobalNetconfSchemaProvider(bundleContext));
116 getDomRegistryDependency().registerProvider(device, bundleContext);
121 private ExecutorService getGlobalProcessingExecutor() {
122 if(GLOBAL_PROCESSING_EXECUTOR == null) {
124 GLOBAL_PROCESSING_EXECUTOR = Executors.newCachedThreadPool();
127 return GLOBAL_PROCESSING_EXECUTOR;
130 private synchronized AbstractCachingSchemaSourceProvider<String, InputStream> getGlobalNetconfSchemaProvider(BundleContext bundleContext) {
131 if(GLOBAL_NETCONF_SOURCE_PROVIDER == null) {
132 String storageFile = "cache/schema";
133 // File directory = bundleContext.getDataFile(storageFile);
134 File directory = new File("cache/schema");
135 SchemaSourceProvider<String> defaultProvider = SchemaSourceProviders.noopProvider();
136 GLOBAL_NETCONF_SOURCE_PROVIDER = FilesystemSchemaCachingProvider.createFromStringSourceProvider(defaultProvider, directory);
138 return GLOBAL_NETCONF_SOURCE_PROVIDER;
141 private NetconfClientDispatcher createDispatcher(long clientConnectionTimeoutMillis) {
142 EventLoopGroup bossGroup = getBossThreadGroupDependency();
143 EventLoopGroup workerGroup = getWorkerThreadGroupDependency();
145 return new NetconfClientDispatcher( bossGroup, workerGroup, clientConnectionTimeoutMillis);
147 AuthenticationHandler authHandler = new LoginPassword(getUsername(),getPassword());
148 return new NetconfSshClientDispatcher(authHandler , bossGroup, workerGroup, clientConnectionTimeoutMillis);
152 public void setBundleContext(BundleContext bundleContext) {
153 this.bundleContext = bundleContext;