Bulk-add copyright headers to java files
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / config / yang / md / sal / connector / netconf / NetconfConnectorModule.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.yang.md.sal.connector.netconf;
9
10 import io.netty.channel.EventLoopGroup;
11 import io.netty.util.concurrent.GlobalEventExecutor;
12
13 import java.io.File;
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;
19
20 import javax.net.ssl.SSLContext;
21
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
35 import static com.google.common.base.Preconditions.*;
36
37 import com.google.common.base.Optional;
38 import com.google.common.net.InetAddresses;
39
40 /**
41 *
42 */
43 public final class NetconfConnectorModule extends org.opendaylight.controller.config.yang.md.sal.connector.netconf.AbstractNetconfConnectorModule
44 {
45
46     private static ExecutorService GLOBAL_PROCESSING_EXECUTOR = null;
47     private static AbstractCachingSchemaSourceProvider<String, InputStream> GLOBAL_NETCONF_SOURCE_PROVIDER = null;
48     private BundleContext bundleContext;
49
50     public NetconfConnectorModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
51         super(identifier, dependencyResolver);
52     }
53
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);
56     }
57
58     @Override
59     public void validate(){
60         super.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.");
65     }
66
67
68     @Override
69     public java.lang.AutoCloseable createInstance() {
70         
71         getDomRegistryDependency();
72         NetconfDevice device = new NetconfDevice(getIdentifier().getInstanceName());
73         String addressValue = getAddress();
74         
75         
76         int attemptMsTimeout = 60*1000;
77         int connectionAttempts = 5;
78         /*
79          * Uncomment after Switch to IP Address
80         if(getAddress().getIpv4Address() != null) {
81             addressValue = getAddress().getIpv4Address().getValue();
82         } else {
83             addressValue = getAddress().getIpv6Address().getValue();
84         }
85         */
86         ReconnectStrategy strategy = new TimedReconnectStrategy(GlobalEventExecutor.INSTANCE, attemptMsTimeout, 1000, 1.0, null,
87                 Long.valueOf(connectionAttempts), null);
88         
89         device.setReconnectStrategy(strategy);
90         
91         InetAddress addr = InetAddresses.forString(addressValue);
92         InetSocketAddress socketAddress = new InetSocketAddress(addr , getPort().intValue());
93
94         
95         device.setProcessingExecutor(getGlobalProcessingExecutor());
96         
97         device.setSocketAddress(socketAddress);
98         device.setEventExecutor(getEventExecutorDependency());
99         device.setDispatcher(createDispatcher());
100         device.setSchemaSourceProvider(getGlobalNetconfSchemaProvider(bundleContext));
101         
102         getDomRegistryDependency().registerProvider(device, bundleContext);
103         device.start();
104         return device;
105     }
106
107     private ExecutorService getGlobalProcessingExecutor() {
108         if(GLOBAL_PROCESSING_EXECUTOR == null) {
109             
110             GLOBAL_PROCESSING_EXECUTOR = Executors.newCachedThreadPool();
111             
112         }
113         return GLOBAL_PROCESSING_EXECUTOR;
114     }
115
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);
123         }
124         return GLOBAL_NETCONF_SOURCE_PROVIDER;
125     }
126
127     private NetconfClientDispatcher createDispatcher() {
128         EventLoopGroup bossGroup = getBossThreadGroupDependency();
129         EventLoopGroup workerGroup = getWorkerThreadGroupDependency();
130         if(getTcpOnly()) {
131             return new NetconfClientDispatcher( bossGroup, workerGroup);
132         } else {
133             AuthenticationHandler authHandler = new LoginPassword(getUsername(),getPassword());
134             return new NetconfSshClientDispatcher(authHandler , bossGroup, workerGroup);
135         }
136     }
137
138     public void setBundleContext(BundleContext bundleContext) {
139         this.bundleContext = bundleContext;
140     }
141 }