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