Merge "minor test fix"
[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 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;
27
28 import java.io.File;
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;
34
35 import static org.opendaylight.controller.config.api.JmxAttributeValidationException.checkCondition;
36 import static org.opendaylight.controller.config.api.JmxAttributeValidationException.checkNotNull;
37
38 /**
39 *
40 */
41 public final class NetconfConnectorModule extends org.opendaylight.controller.config.yang.md.sal.connector.netconf.AbstractNetconfConnectorModule
42 {
43     private static final Logger logger = LoggerFactory.getLogger(NetconfConnectorModule.class);
44
45     private static ExecutorService GLOBAL_PROCESSING_EXECUTOR = null;
46     private static AbstractCachingSchemaSourceProvider<String, InputStream> GLOBAL_NETCONF_SOURCE_PROVIDER = null;
47     private BundleContext bundleContext;
48
49     public NetconfConnectorModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
50         super(identifier, dependencyResolver);
51     }
52
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);
55     }
56
57     @Override
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);
64
65         checkNotNull(getConnectionTimeoutMillis(), connectionTimeoutMillisJmxAttribute);
66         checkCondition(getConnectionTimeoutMillis() > 0, "must be > 0", connectionTimeoutMillisJmxAttribute);
67
68         checkNotNull(getBetweenAttemptsTimeoutMillis(), betweenAttemptsTimeoutMillisJmxAttribute);
69         checkCondition(getBetweenAttemptsTimeoutMillis() > 0, "must be > 0", betweenAttemptsTimeoutMillisJmxAttribute);
70
71     }
72
73     @Override
74     public java.lang.AutoCloseable createInstance() {
75         
76         getDomRegistryDependency();
77         NetconfDevice device = new NetconfDevice(getIdentifier().getInstanceName());
78         String addressValue = getAddress();
79
80         Long connectionAttempts;
81         if (getMaxConnectionAttempts() != null && getMaxConnectionAttempts() > 0) {
82             connectionAttempts = getMaxConnectionAttempts();
83         } else {
84             logger.trace("Setting {} on {} to infinity", maxConnectionAttemptsJmxAttribute, this);
85             connectionAttempts = null;
86         }
87         long clientConnectionTimeoutMillis = getConnectionTimeoutMillis();
88         /*
89          * Uncomment after Switch to IP Address
90         if(getAddress().getIpv4Address() != null) {
91             addressValue = getAddress().getIpv4Address().getValue();
92         } else {
93             addressValue = getAddress().getIpv6Address().getValue();
94         }
95         */
96         double sleepFactor = 1.0;
97         int minSleep = 1000;
98         Long maxSleep = null;
99         Long deadline = null;
100         ReconnectStrategy strategy = new TimedReconnectStrategy(GlobalEventExecutor.INSTANCE, getBetweenAttemptsTimeoutMillis(),
101                 minSleep, sleepFactor, maxSleep, connectionAttempts, deadline);
102         
103         device.setReconnectStrategy(strategy);
104         
105         InetAddress addr = InetAddresses.forString(addressValue);
106         InetSocketAddress socketAddress = new InetSocketAddress(addr , getPort().intValue());
107
108         
109         device.setProcessingExecutor(getGlobalProcessingExecutor());
110         
111         device.setSocketAddress(socketAddress);
112         device.setEventExecutor(getEventExecutorDependency());
113         device.setDispatcher(createDispatcher(clientConnectionTimeoutMillis));
114         device.setSchemaSourceProvider(getGlobalNetconfSchemaProvider(bundleContext));
115         
116         getDomRegistryDependency().registerProvider(device, bundleContext);
117         device.start();
118         return device;
119     }
120
121     private ExecutorService getGlobalProcessingExecutor() {
122         if(GLOBAL_PROCESSING_EXECUTOR == null) {
123             
124             GLOBAL_PROCESSING_EXECUTOR = Executors.newCachedThreadPool();
125             
126         }
127         return GLOBAL_PROCESSING_EXECUTOR;
128     }
129
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);
137         }
138         return GLOBAL_NETCONF_SOURCE_PROVIDER;
139     }
140
141     private NetconfClientDispatcher createDispatcher(long clientConnectionTimeoutMillis) {
142         EventLoopGroup bossGroup = getBossThreadGroupDependency();
143         EventLoopGroup workerGroup = getWorkerThreadGroupDependency();
144         if(getTcpOnly()) {
145             return new NetconfClientDispatcher( bossGroup, workerGroup, clientConnectionTimeoutMillis);
146         } else {
147             AuthenticationHandler authHandler = new LoginPassword(getUsername(),getPassword());
148             return new NetconfSshClientDispatcher(authHandler , bossGroup, workerGroup, clientConnectionTimeoutMillis);
149         }
150     }
151
152     public void setBundleContext(BundleContext bundleContext) {
153         this.bundleContext = bundleContext;
154     }
155 }