2 * Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowjava.protocol.impl.core;
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;
28 * Implementation of the SwitchConnectionProviderFactory interface.
30 * @author Thomas Pantelis
32 public class SwitchConnectionProviderFactoryImpl implements SwitchConnectionProviderFactory {
35 public SwitchConnectionProvider newInstance(SwitchConnectionConfig config) {
36 SwitchConnectionProviderImpl switchConnectionProviderImpl = new SwitchConnectionProviderImpl();
37 switchConnectionProviderImpl.setConfiguration(new ConnectionConfigurationImpl(config));
38 return switchConnectionProviderImpl;
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());
51 if (addressBin == null) {
54 return InetAddress.getByAddress(addressBin);
58 private static byte[] address2bin(final String value) {
59 //TODO: translate ipv4 or ipv6 into byte[]
63 private static class ConnectionConfigurationImpl implements ConnectionConfiguration {
64 private final SwitchConnectionConfig config;
65 private InetAddress address;
67 private ConnectionConfigurationImpl(SwitchConnectionConfig config) {
71 address = extractIpAddressBin(config.getAddress());
72 } catch(UnknownHostException e) {
73 Throwables.propagate(e);
78 public InetAddress getAddress() {
83 public int getPort() {
84 return config.getPort();
88 public Object getTransferProtocol() {
89 return config.getTransportProtocol();
93 public TlsConfiguration getTlsConfiguration() {
94 final Tls tlsConfig = config.getTls();
95 if(tlsConfig == null || !(TransportProtocol.TLS.equals(getTransferProtocol()))) {
99 return new TlsConfiguration() {
101 public KeystoreType getTlsTruststoreType() {
102 return MoreObjects.firstNonNull(tlsConfig.getTruststoreType(), null);
105 public String getTlsTruststore() {
106 return MoreObjects.firstNonNull(tlsConfig.getTruststore(), null);
109 public KeystoreType getTlsKeystoreType() {
110 return MoreObjects.firstNonNull(tlsConfig.getKeystoreType(), null);
113 public String getTlsKeystore() {
114 return MoreObjects.firstNonNull(tlsConfig.getKeystore(), null);
117 public org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType getTlsKeystorePathType() {
118 return MoreObjects.firstNonNull(tlsConfig.getKeystorePathType(), null);
121 public org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType getTlsTruststorePathType() {
122 return MoreObjects.firstNonNull(tlsConfig.getTruststorePathType(), null);
125 public String getKeystorePassword() {
126 return MoreObjects.firstNonNull(tlsConfig.getKeystorePassword(), null);
129 public String getCertificatePassword() {
130 return MoreObjects.firstNonNull(tlsConfig.getCertificatePassword(), null);
133 public String getTruststorePassword() {
134 return MoreObjects.firstNonNull(tlsConfig.getTruststorePassword(), null);
137 public List<String> getCipherSuites() {
138 return tlsConfig.getCipherSuites();
144 public long getSwitchIdleTimeout() {
145 return config.getSwitchIdleTimeout();
149 public Object getSslContext() {
154 public ThreadConfiguration getThreadConfiguration() {
155 final Threads threads = config.getThreads();
156 if(threads == null) {
160 return new ThreadConfiguration() {
162 public int getWorkerThreadCount() {
163 return threads.getWorkerThreads();
167 public int getBossThreadCount() {
168 return threads.getBossThreads();
174 public boolean useBarrier() {
175 return config.isUseBarrier();