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;
12 import io.netty.channel.EventLoopGroup;
13 import io.netty.util.concurrent.GlobalEventExecutor;
16 import java.io.InputStream;
17 import java.net.InetAddress;
18 import java.net.InetSocketAddress;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
22 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
23 import org.opendaylight.controller.netconf.client.NetconfSshClientDispatcher;
24 import org.opendaylight.controller.netconf.util.handler.ssh.authentication.AuthenticationHandler;
25 import org.opendaylight.controller.netconf.util.handler.ssh.authentication.LoginPassword;
26 import org.opendaylight.controller.sal.connect.netconf.NetconfDevice;
27 import org.opendaylight.protocol.framework.ReconnectStrategy;
28 import org.opendaylight.protocol.framework.TimedReconnectStrategy;
29 import org.opendaylight.yangtools.yang.model.util.repo.AbstractCachingSchemaSourceProvider;
30 import org.opendaylight.yangtools.yang.model.util.repo.FilesystemSchemaCachingProvider;
31 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProvider;
32 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProviders;
33 import org.osgi.framework.BundleContext;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import com.google.common.net.InetAddresses;
42 public final class NetconfConnectorModule extends org.opendaylight.controller.config.yang.md.sal.connector.netconf.AbstractNetconfConnectorModule
44 private static final Logger logger = LoggerFactory.getLogger(NetconfConnectorModule.class);
46 private static ExecutorService GLOBAL_PROCESSING_EXECUTOR = null;
47 private static AbstractCachingSchemaSourceProvider<String, InputStream> GLOBAL_NETCONF_SOURCE_PROVIDER = null;
48 private BundleContext bundleContext;
50 public NetconfConnectorModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
51 super(identifier, dependencyResolver);
54 public NetconfConnectorModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, NetconfConnectorModule oldModule, java.lang.AutoCloseable oldInstance) {
55 super(identifier, dependencyResolver, oldModule, oldInstance);
59 protected void customValidation() {
60 checkNotNull(getAddress(), addressJmxAttribute);
61 //checkState(getAddress().getIpv4Address() != null || getAddress().getIpv6Address() != null,"Address must be set.");
62 checkNotNull(getPort(), portJmxAttribute);
63 checkNotNull(getDomRegistry(), portJmxAttribute);
64 checkNotNull(getDomRegistry(), domRegistryJmxAttribute);
66 checkNotNull(getConnectionTimeoutMillis(), connectionTimeoutMillisJmxAttribute);
67 checkCondition(getConnectionTimeoutMillis() > 0, "must be > 0", connectionTimeoutMillisJmxAttribute);
69 checkNotNull(getBetweenAttemptsTimeoutMillis(), betweenAttemptsTimeoutMillisJmxAttribute);
70 checkCondition(getBetweenAttemptsTimeoutMillis() > 0, "must be > 0", betweenAttemptsTimeoutMillisJmxAttribute);
75 public java.lang.AutoCloseable createInstance() {
77 getDomRegistryDependency();
78 NetconfDevice device = new NetconfDevice(getIdentifier().getInstanceName());
79 String addressValue = getAddress();
81 Long connectionAttempts;
82 if (getMaxConnectionAttempts() != null && getMaxConnectionAttempts() > 0) {
83 connectionAttempts = getMaxConnectionAttempts();
85 logger.trace("Setting {} on {} to infinity", maxConnectionAttemptsJmxAttribute, this);
86 connectionAttempts = null;
88 long clientConnectionTimeoutMillis = getConnectionTimeoutMillis();
90 * Uncomment after Switch to IP Address
91 if(getAddress().getIpv4Address() != null) {
92 addressValue = getAddress().getIpv4Address().getValue();
94 addressValue = getAddress().getIpv6Address().getValue();
97 double sleepFactor = 1.0;
100 Long deadline = null;
101 ReconnectStrategy strategy = new TimedReconnectStrategy(GlobalEventExecutor.INSTANCE, getBetweenAttemptsTimeoutMillis(),
102 minSleep, sleepFactor, maxSleep, connectionAttempts, deadline);
104 device.setReconnectStrategy(strategy);
106 InetAddress addr = InetAddresses.forString(addressValue);
107 InetSocketAddress socketAddress = new InetSocketAddress(addr , getPort().intValue());
110 device.setProcessingExecutor(getGlobalProcessingExecutor());
112 device.setSocketAddress(socketAddress);
113 device.setEventExecutor(getEventExecutorDependency());
114 device.setDispatcher(createDispatcher(clientConnectionTimeoutMillis));
115 device.setSchemaSourceProvider(getGlobalNetconfSchemaProvider(bundleContext));
117 getDomRegistryDependency().registerProvider(device, bundleContext);
122 private ExecutorService getGlobalProcessingExecutor() {
123 if(GLOBAL_PROCESSING_EXECUTOR == null) {
125 GLOBAL_PROCESSING_EXECUTOR = Executors.newCachedThreadPool();
128 return GLOBAL_PROCESSING_EXECUTOR;
131 private synchronized AbstractCachingSchemaSourceProvider<String, InputStream> getGlobalNetconfSchemaProvider(BundleContext bundleContext) {
132 if(GLOBAL_NETCONF_SOURCE_PROVIDER == null) {
133 String storageFile = "cache/schema";
134 // File directory = bundleContext.getDataFile(storageFile);
135 File directory = new File(storageFile);
136 SchemaSourceProvider<String> defaultProvider = SchemaSourceProviders.noopProvider();
137 GLOBAL_NETCONF_SOURCE_PROVIDER = FilesystemSchemaCachingProvider.createFromStringSourceProvider(defaultProvider, directory);
139 return GLOBAL_NETCONF_SOURCE_PROVIDER;
142 private NetconfClientDispatcher createDispatcher(long clientConnectionTimeoutMillis) {
143 EventLoopGroup bossGroup = getBossThreadGroupDependency();
144 EventLoopGroup workerGroup = getWorkerThreadGroupDependency();
146 return new NetconfClientDispatcher( bossGroup, workerGroup, clientConnectionTimeoutMillis);
148 AuthenticationHandler authHandler = new LoginPassword(getUsername(),getPassword());
149 return new NetconfSshClientDispatcher(authHandler , bossGroup, workerGroup, clientConnectionTimeoutMillis);
153 public void setBundleContext(BundleContext bundleContext) {
154 this.bundleContext = bundleContext;