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 io.netty.channel.EventLoopGroup;
11 import io.netty.util.concurrent.GlobalEventExecutor;
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 javax.net.ssl.SSLContext;
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;
35 import static com.google.common.base.Preconditions.*;
37 import com.google.common.base.Optional;
38 import com.google.common.net.InetAddresses;
43 public final class NetconfConnectorModule extends org.opendaylight.controller.config.yang.md.sal.connector.netconf.AbstractNetconfConnectorModule
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 public void validate(){
61 checkState(getAddress() != null,"Address must be set.");
62 //checkState(getAddress().getIpv4Address() != null || getAddress().getIpv6Address() != null,"Address must be set.");
63 checkState(getPort() != null,"Port must be set.");
64 checkState(getDomRegistry() != null,"Dom Registry must be provided.");
69 public java.lang.AutoCloseable createInstance() {
71 getDomRegistryDependency();
72 NetconfDevice device = new NetconfDevice(getIdentifier().getInstanceName());
73 String addressValue = getAddress();
76 int attemptMsTimeout = 60*1000;
77 int connectionAttempts = 5;
79 * Uncomment after Switch to IP Address
80 if(getAddress().getIpv4Address() != null) {
81 addressValue = getAddress().getIpv4Address().getValue();
83 addressValue = getAddress().getIpv6Address().getValue();
86 ReconnectStrategy strategy = new TimedReconnectStrategy(GlobalEventExecutor.INSTANCE, attemptMsTimeout, 1000, 1.0, null,
87 Long.valueOf(connectionAttempts), null);
89 device.setReconnectStrategy(strategy);
91 InetAddress addr = InetAddresses.forString(addressValue);
92 InetSocketAddress socketAddress = new InetSocketAddress(addr , getPort().intValue());
95 device.setProcessingExecutor(getGlobalProcessingExecutor());
97 device.setSocketAddress(socketAddress);
98 device.setEventExecutor(getEventExecutorDependency());
99 device.setDispatcher(createDispatcher());
100 device.setSchemaSourceProvider(getGlobalNetconfSchemaProvider(bundleContext));
102 getDomRegistryDependency().registerProvider(device, bundleContext);
107 private ExecutorService getGlobalProcessingExecutor() {
108 if(GLOBAL_PROCESSING_EXECUTOR == null) {
110 GLOBAL_PROCESSING_EXECUTOR = Executors.newCachedThreadPool();
113 return GLOBAL_PROCESSING_EXECUTOR;
116 private synchronized AbstractCachingSchemaSourceProvider<String, InputStream> getGlobalNetconfSchemaProvider(BundleContext bundleContext) {
117 if(GLOBAL_NETCONF_SOURCE_PROVIDER == null) {
118 String storageFile = "cache/schema";
119 // File directory = bundleContext.getDataFile(storageFile);
120 File directory = new File("cache/schema");
121 SchemaSourceProvider<String> defaultProvider = SchemaSourceProviders.noopProvider();
122 GLOBAL_NETCONF_SOURCE_PROVIDER = FilesystemSchemaCachingProvider.createFromStringSourceProvider(defaultProvider, directory);
124 return GLOBAL_NETCONF_SOURCE_PROVIDER;
127 private NetconfClientDispatcher createDispatcher() {
128 EventLoopGroup bossGroup = getBossThreadGroupDependency();
129 EventLoopGroup workerGroup = getWorkerThreadGroupDependency();
131 return new NetconfClientDispatcher( bossGroup, workerGroup);
133 AuthenticationHandler authHandler = new LoginPassword(getUsername(),getPassword());
134 return new NetconfSshClientDispatcher(authHandler , bossGroup, workerGroup);
138 public void setBundleContext(BundleContext bundleContext) {
139 this.bundleContext = bundleContext;