Fix checkstyle violations in openflow-protocol-impl - part 1
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / SwitchConnectionProviderFactoryImpl.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.openflowjava.protocol.impl.core;
9
10 import com.google.common.base.MoreObjects;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.List;
14 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
15 import org.opendaylight.openflowjava.protocol.api.connection.ThreadConfiguration;
16 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
17 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
18 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProviderFactory;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow._switch.connection.config.rev160506.SwitchConnectionConfig;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow._switch.connection.config.rev160506._switch.connection.config.Threads;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow._switch.connection.config.rev160506._switch.connection.config.Tls;
25
26 /**
27  * Implementation of the SwitchConnectionProviderFactory interface.
28  *
29  * @author Thomas Pantelis
30  */
31 public class SwitchConnectionProviderFactoryImpl implements SwitchConnectionProviderFactory {
32
33     @Override
34     public SwitchConnectionProvider newInstance(SwitchConnectionConfig config) {
35         SwitchConnectionProviderImpl switchConnectionProviderImpl =
36                 new SwitchConnectionProviderImpl(new ConnectionConfigurationImpl(config));
37         return switchConnectionProviderImpl;
38     }
39
40     private static InetAddress extractIpAddressBin(final IpAddress address) throws UnknownHostException {
41         byte[] addressBin = null;
42         if (address != null) {
43             if (address.getIpv4Address() != null) {
44                 addressBin = address2bin(address.getIpv4Address().getValue());
45             } else if (address.getIpv6Address() != null) {
46                 addressBin = address2bin(address.getIpv6Address().getValue());
47             }
48         }
49
50         if (addressBin == null) {
51             return null;
52         } else {
53             return InetAddress.getByAddress(addressBin);
54         }
55     }
56
57     private static byte[] address2bin(final String value) {
58         //TODO: translate ipv4 or ipv6 into byte[]
59         return null;
60     }
61
62     private static class ConnectionConfigurationImpl implements ConnectionConfiguration {
63         private final SwitchConnectionConfig config;
64         private InetAddress address;
65
66         ConnectionConfigurationImpl(SwitchConnectionConfig config) {
67             this.config = config;
68
69             try {
70                 address = extractIpAddressBin(config.getAddress());
71             } catch (UnknownHostException e) {
72                 throw new RuntimeException(e);
73             }
74         }
75
76         @Override
77         public InetAddress getAddress() {
78             return address;
79         }
80
81         @Override
82         public int getPort() {
83             return config.getPort();
84         }
85
86         @Override
87         public Object getTransferProtocol() {
88             return config.getTransportProtocol();
89         }
90
91         @Override
92         public TlsConfiguration getTlsConfiguration() {
93             final Tls tlsConfig = config.getTls();
94             if (tlsConfig == null || !TransportProtocol.TLS.equals(getTransferProtocol())) {
95                 return null;
96             }
97
98             return new TlsConfiguration() {
99                 @Override
100                 public KeystoreType getTlsTruststoreType() {
101                     return MoreObjects.firstNonNull(tlsConfig.getTruststoreType(), null);
102                 }
103
104                 @Override
105                 public String getTlsTruststore() {
106                     return MoreObjects.firstNonNull(tlsConfig.getTruststore(), null);
107                 }
108
109                 @Override
110                 public KeystoreType getTlsKeystoreType() {
111                     return MoreObjects.firstNonNull(tlsConfig.getKeystoreType(), null);
112                 }
113
114                 @Override
115                 public String getTlsKeystore() {
116                     return MoreObjects.firstNonNull(tlsConfig.getKeystore(), null);
117                 }
118
119                 @Override
120                 public org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType
121                         getTlsKeystorePathType() {
122                     return MoreObjects.firstNonNull(tlsConfig.getKeystorePathType(), null);
123                 }
124
125                 @Override
126                 public org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType
127                         getTlsTruststorePathType() {
128                     return MoreObjects.firstNonNull(tlsConfig.getTruststorePathType(), null);
129                 }
130
131                 @Override
132                 public String getKeystorePassword() {
133                     return MoreObjects.firstNonNull(tlsConfig.getKeystorePassword(), null);
134                 }
135
136                 @Override
137                 public String getCertificatePassword() {
138                     return MoreObjects.firstNonNull(tlsConfig.getCertificatePassword(), null);
139                 }
140
141                 @Override
142                 public String getTruststorePassword() {
143                     return MoreObjects.firstNonNull(tlsConfig.getTruststorePassword(), null);
144                 }
145
146                 @Override
147                 public List<String> getCipherSuites() {
148                     return tlsConfig.getCipherSuites();
149                 }
150             };
151         }
152
153         @Override
154         public long getSwitchIdleTimeout() {
155             return config.getSwitchIdleTimeout();
156         }
157
158         @Override
159         public Object getSslContext() {
160             return null;
161         }
162
163         @Override
164         public ThreadConfiguration getThreadConfiguration() {
165             final Threads threads = config.getThreads();
166             if (threads == null) {
167                 return null;
168             }
169
170             return new ThreadConfiguration() {
171                 @Override
172                 public int getWorkerThreadCount() {
173                     return threads.getWorkerThreads();
174                 }
175
176                 @Override
177                 public int getBossThreadCount() {
178                     return threads.getBossThreads();
179                 }
180             };
181         }
182
183         @Override
184         public boolean useBarrier() {
185             return config.isUseBarrier();
186         }
187
188         @Override
189         public boolean isGroupAddModEnabled() {
190             return config.isGroupAddModEnabled();
191         }
192     }
193 }