d64941083860214cbba9a50d8179bfc0bf130936
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / yang / gen / v1 / urn / opendaylight / params / xml / ns / yang / openflow / _switch / connection / provider / impl / rev140328 / SwitchConnectionProviderModule.java
1 /**
2 * Generated file
3
4 * Generated from: yang module name: openflow-switch-connection-provider-impl  yang module local name: openflow-switch-connection-provider-impl
5 * Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator
6 * Generated at: Fri Mar 28 17:50:58 PDT 2014
7 *
8 * Do not modify this file unless it is present under src/main directory
9 */
10 package org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow._switch.connection.provider.impl.rev140328;
11
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14
15 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
16 import org.opendaylight.openflowjava.protocol.api.connection.StatisticsConfiguration;
17 import org.opendaylight.openflowjava.protocol.api.connection.ThreadConfiguration;
18 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
19 import org.opendaylight.openflowjava.protocol.impl.core.SwitchConnectionProviderImpl;
20 import org.opendaylight.openflowjava.statistics.StatisticsCounters;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.google.common.base.Objects;
28
29 /**
30 *
31 */
32 public final class SwitchConnectionProviderModule extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow._switch.connection.provider.impl.rev140328.AbstractSwitchConnectionProviderModule
33  {
34     
35     private static Logger LOG = LoggerFactory
36             .getLogger(SwitchConnectionProviderModule.class);
37
38     /**
39      * @param identifier
40      * @param dependencyResolver
41      */
42     public SwitchConnectionProviderModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
43         super(identifier, dependencyResolver);
44     }
45
46     /**
47      * @param identifier
48      * @param dependencyResolver
49      * @param oldModule
50      * @param oldInstance
51      */
52     public SwitchConnectionProviderModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
53             SwitchConnectionProviderModule oldModule, java.lang.AutoCloseable oldInstance) {
54         super(identifier, dependencyResolver, oldModule, oldInstance);
55     }
56
57     @Override
58     protected void customValidation(){
59         // Add custom validation for module attributes here.
60     }
61
62     @Override
63     public java.lang.AutoCloseable createInstance() {
64         LOG.info("SwitchConnectionProvider started.");
65         SwitchConnectionProviderImpl switchConnectionProviderImpl = new SwitchConnectionProviderImpl();
66         StatisticsCounters sc = StatisticsCounters.getInstance();
67         try {
68             ConnectionConfiguration connConfiguration = createConnectionConfiguration();
69             switchConnectionProviderImpl.setConfiguration(connConfiguration);
70             startStatistics(sc, connConfiguration.getStatisticsConfiguration());
71         } catch (UnknownHostException e) {
72             throw new IllegalArgumentException(e.getMessage(), e);
73         }
74         return switchConnectionProviderImpl;
75     }
76
77     
78     /**
79      * @return instance configuration object
80      * @throws UnknownHostException 
81      */
82     private ConnectionConfiguration createConnectionConfiguration() throws UnknownHostException {
83         final InetAddress address = extractIpAddressBin(getAddress());
84         final Integer port = getPort();
85         final long switchIdleTimeout = getSwitchIdleTimeout();
86         final Tls tlsConfig = getTls();
87         final Threads threads = getThreads();
88         final TransportProtocol transportProtocol = getTransportProtocol();
89         final Statistics statistics = getStatistics();
90         
91         return new ConnectionConfiguration() {
92             @Override
93             public InetAddress getAddress() {
94                 return address;
95             }
96             @Override
97             public int getPort() {
98                 return port;
99             }
100             @Override
101             public Object getTransferProtocol() {
102                 return transportProtocol;
103             }
104             @Override
105             public TlsConfiguration getTlsConfiguration() {
106                 if (tlsConfig == null || !(TransportProtocol.TLS.equals(transportProtocol))) {
107                     return null;
108                 }
109                 return new TlsConfiguration() {
110                     @Override
111                     public KeystoreType getTlsTruststoreType() {
112                         return Objects.firstNonNull(tlsConfig.getTruststoreType(), null);
113                     }
114                     @Override
115                     public String getTlsTruststore() {
116                         return Objects.firstNonNull(tlsConfig.getTruststore(), null);
117                     }
118                     @Override
119                     public KeystoreType getTlsKeystoreType() {
120                         return Objects.firstNonNull(tlsConfig.getKeystoreType(), null);
121                     }
122                     @Override
123                     public String getTlsKeystore() {
124                         return Objects.firstNonNull(tlsConfig.getKeystore(), null);
125                     }
126                     @Override
127                     public org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType getTlsKeystorePathType() {
128                         return Objects.firstNonNull(tlsConfig.getKeystorePathType(), null);
129                     }
130                     @Override
131                     public org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType getTlsTruststorePathType() {
132                         return Objects.firstNonNull(tlsConfig.getTruststorePathType(), null);
133                     }
134                     @Override
135                     public String getKeystorePassword() {
136                         return Objects.firstNonNull(tlsConfig.getKeystorePassword(), null);
137                     }
138                     @Override
139                     public String getCertificatePassword() {
140                         return Objects.firstNonNull(tlsConfig.getCertificatePassword(), null);
141                     }
142                     @Override
143                     public String getTruststorePassword() {
144                         return Objects.firstNonNull(tlsConfig.getTruststorePassword(), null);
145                     }
146                 };
147             }
148             @Override
149             public long getSwitchIdleTimeout() {
150                 return switchIdleTimeout;
151             }
152             @Override
153             public Object getSslContext() {
154                 // TODO Auto-generated method stub
155                 return null;
156             }
157             @Override
158             public ThreadConfiguration getThreadConfiguration() {
159                 if (threads == null) {
160                     return null;
161                 }
162                 return new ThreadConfiguration() {
163                     
164                     @Override
165                     public int getWorkerThreadCount() {
166                         return threads.getWorkerThreads();
167                     }
168                     
169                     @Override
170                     public int getBossThreadCount() {
171                         return threads.getBossThreads();
172                     }
173                 };
174             }
175             @Override
176             public StatisticsConfiguration getStatisticsConfiguration(){
177                return new StatisticsConfiguration() {
178                 @Override
179                 public Boolean getStatisticsCollect() {
180                     if(statistics == null){
181                         return false;
182                     }
183                     if (statistics.getStatisticsCollect() == null){
184                         return false;
185                     }
186                     return statistics.getStatisticsCollect();
187                 }
188                 @Override
189                 public Integer getLogReportDelay() {
190                     if(statistics == null){
191                         return -1;
192                     }
193                     if(statistics.getLogReportDelay() == null){
194                         return -1;
195                     };
196                     return statistics.getLogReportDelay();
197                 }
198             };
199             }
200         };
201     }
202
203     /**
204      * @param address
205      * @return
206      * @throws UnknownHostException 
207      */
208     private static InetAddress extractIpAddressBin(IpAddress address) throws UnknownHostException {
209         byte[] addressBin = null;
210         if (address != null) {
211             if (address.getIpv4Address() != null) {
212                 addressBin = address2bin(address.getIpv4Address().getValue());
213             } else if (address.getIpv6Address() != null) {
214                 addressBin = address2bin(address.getIpv6Address().getValue());
215             }
216         }
217         
218         if (addressBin == null) {
219             return null;
220         } else {
221             return InetAddress.getByAddress(addressBin);
222         }
223     }
224
225     /**
226      * Configure and start Statistics Counters by configuration parameters. 
227      *      No operations is performed if:
228      *      - statisticsConfig is null
229      *      - sc is null
230      *      - sc is not null and if counting is running
231      * @param sc - statistic counter to configure and start
232      * @param statisticsConfig - configuration parameters
233      */
234     private static void startStatistics(StatisticsCounters sc, StatisticsConfiguration statisticsConfig){
235         if(statisticsConfig == null){
236             return;
237         }
238         if(sc == null || sc.isRunCounting()){
239             return;
240         }
241         Boolean toCollectStats = statisticsConfig.getStatisticsCollect();
242         Integer logDelay = statisticsConfig.getLogReportDelay();
243         if(toCollectStats != null && toCollectStats.booleanValue()){
244             int logPeriod = -1;
245             if(logDelay != null){
246                 logPeriod = logDelay.intValue();
247             }
248             if(logPeriod >0){
249                 sc.startCounting(true, logPeriod);
250             } else {
251                 sc.startCounting(false, 0);
252             }
253         }
254     }
255     
256     /**
257      * @param value
258      * @return
259      */
260     private static byte[] address2bin(String value) {
261         //TODO: translate ipv4 or ipv6 into byte[]
262         return null;
263     }
264 }