Merge "Fixed sal-netconf-connector readConfiguration"
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / config / yang / md / sal / connector / netconf / NetconfConnectorModule.java
1 /**
2 * Generated file
3
4 * Generated from: yang module name: opendaylight-sal-netconf-connector  yang module local name: sal-netconf-connector
5 * Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator
6 * Generated at: Mon Nov 18 09:44:16 CET 2013
7 *
8 * Do not modify this file unless it is present under src/main directory
9 */
10 package org.opendaylight.controller.config.yang.md.sal.connector.netconf;
11
12 import io.netty.channel.EventLoopGroup;
13 import io.netty.util.concurrent.GlobalEventExecutor;
14
15 import java.net.InetAddress;
16 import java.net.InetSocketAddress;
17
18 import javax.net.ssl.SSLContext;
19
20 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
21 import org.opendaylight.controller.netconf.client.NetconfSshClientDispatcher;
22 import org.opendaylight.controller.netconf.util.handler.ssh.authentication.AuthenticationHandler;
23 import org.opendaylight.controller.netconf.util.handler.ssh.authentication.LoginPassword;
24 import org.opendaylight.controller.sal.connect.netconf.NetconfDevice;
25 import org.opendaylight.protocol.framework.ReconnectStrategy;
26 import org.opendaylight.protocol.framework.TimedReconnectStrategy;
27 import org.osgi.framework.BundleContext;
28
29 import static com.google.common.base.Preconditions.*;
30
31 import com.google.common.base.Optional;
32 import com.google.common.net.InetAddresses;
33
34 /**
35 *
36 */
37 public final class NetconfConnectorModule extends org.opendaylight.controller.config.yang.md.sal.connector.netconf.AbstractNetconfConnectorModule
38 {
39
40     private BundleContext bundleContext;
41
42     public NetconfConnectorModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
43         super(identifier, dependencyResolver);
44     }
45
46     public NetconfConnectorModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, NetconfConnectorModule oldModule, java.lang.AutoCloseable oldInstance) {
47         super(identifier, dependencyResolver, oldModule, oldInstance);
48     }
49
50     @Override
51     public void validate(){
52         super.validate();
53         checkState(getAddress() != null,"Address must be set.");
54         //checkState(getAddress().getIpv4Address() != null || getAddress().getIpv6Address() != null,"Address must be set.");
55         checkState(getPort() != null,"Port must be set.");
56         checkState(getDomRegistry() != null,"Dom Registry must be provided.");
57     }
58
59
60     @Override
61     public java.lang.AutoCloseable createInstance() {
62         
63         getDomRegistryDependency();
64         NetconfDevice device = new NetconfDevice(getIdentifier().getInstanceName());
65         String addressValue = getAddress();
66         
67         
68         int attemptMsTimeout = 60*1000;
69         int connectionAttempts = 5;
70         /*
71          * Uncomment after Switch to IP Address
72         if(getAddress().getIpv4Address() != null) {
73             addressValue = getAddress().getIpv4Address().getValue();
74         } else {
75             addressValue = getAddress().getIpv6Address().getValue();
76         }
77         
78         */
79         ReconnectStrategy strategy = new TimedReconnectStrategy(GlobalEventExecutor.INSTANCE, attemptMsTimeout, 1000, 1.0, null,
80                 Long.valueOf(connectionAttempts), null);
81         
82         
83         device.setStrategy(strategy);
84         
85         InetAddress addr = InetAddresses.forString(addressValue);
86         InetSocketAddress socketAddress = new InetSocketAddress(addr , getPort().intValue());
87         device.setSocketAddress(socketAddress);
88         
89         EventLoopGroup bossGroup = getBossThreadGroupDependency();
90         EventLoopGroup workerGroup = getWorkerThreadGroupDependency();
91         NetconfClientDispatcher dispatcher = null;
92         if(getTcpOnly()) {
93             dispatcher = new NetconfClientDispatcher( bossGroup, workerGroup);
94         } else {
95             AuthenticationHandler authHandler = new LoginPassword(getUsername(),getPassword());
96             dispatcher = new NetconfSshClientDispatcher(authHandler , bossGroup, workerGroup);
97         }
98         getDomRegistryDependency().registerProvider(device, bundleContext);
99         
100         device.start(dispatcher);
101         return device;
102     }
103
104     public void setBundleContext(BundleContext bundleContext) {
105         this.bundleContext = bundleContext;
106     }
107 }