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