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 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;
27 * Implementation of the SwitchConnectionProviderFactory interface.
29 * @author Thomas Pantelis
31 public class SwitchConnectionProviderFactoryImpl implements SwitchConnectionProviderFactory {
34 public SwitchConnectionProvider newInstance(SwitchConnectionConfig config) {
35 SwitchConnectionProviderImpl switchConnectionProviderImpl =
36 new SwitchConnectionProviderImpl(new ConnectionConfigurationImpl(config));
37 return switchConnectionProviderImpl;
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());
50 if (addressBin == null) {
53 return InetAddress.getByAddress(addressBin);
57 private static byte[] address2bin(final String value) {
58 //TODO: translate ipv4 or ipv6 into byte[]
62 private static class ConnectionConfigurationImpl implements ConnectionConfiguration {
63 private final SwitchConnectionConfig config;
64 private InetAddress address;
66 ConnectionConfigurationImpl(SwitchConnectionConfig config) {
70 address = extractIpAddressBin(config.getAddress());
71 } catch (UnknownHostException e) {
72 throw new RuntimeException(e);
77 public InetAddress getAddress() {
82 public int getPort() {
83 return config.getPort();
87 public Object getTransferProtocol() {
88 return config.getTransportProtocol();
92 public TlsConfiguration getTlsConfiguration() {
93 final Tls tlsConfig = config.getTls();
94 if (tlsConfig == null || !TransportProtocol.TLS.equals(getTransferProtocol())) {
98 return new TlsConfiguration() {
100 public KeystoreType getTlsTruststoreType() {
101 return MoreObjects.firstNonNull(tlsConfig.getTruststoreType(), null);
105 public String getTlsTruststore() {
106 return MoreObjects.firstNonNull(tlsConfig.getTruststore(), null);
110 public KeystoreType getTlsKeystoreType() {
111 return MoreObjects.firstNonNull(tlsConfig.getKeystoreType(), null);
115 public String getTlsKeystore() {
116 return MoreObjects.firstNonNull(tlsConfig.getKeystore(), null);
120 public org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType
121 getTlsKeystorePathType() {
122 return MoreObjects.firstNonNull(tlsConfig.getKeystorePathType(), null);
126 public org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType
127 getTlsTruststorePathType() {
128 return MoreObjects.firstNonNull(tlsConfig.getTruststorePathType(), null);
132 public String getKeystorePassword() {
133 return MoreObjects.firstNonNull(tlsConfig.getKeystorePassword(), null);
137 public String getCertificatePassword() {
138 return MoreObjects.firstNonNull(tlsConfig.getCertificatePassword(), null);
142 public String getTruststorePassword() {
143 return MoreObjects.firstNonNull(tlsConfig.getTruststorePassword(), null);
147 public List<String> getCipherSuites() {
148 return tlsConfig.getCipherSuites();
154 public long getSwitchIdleTimeout() {
155 return config.getSwitchIdleTimeout();
159 public Object getSslContext() {
164 public ThreadConfiguration getThreadConfiguration() {
165 final Threads threads = config.getThreads();
166 if (threads == null) {
170 return new ThreadConfiguration() {
172 public int getWorkerThreadCount() {
173 return threads.getWorkerThreads();
177 public int getBossThreadCount() {
178 return threads.getBossThreads();
184 public boolean useBarrier() {
185 return config.isUseBarrier();
189 public boolean isGroupAddModEnabled() {
190 return config.isGroupAddModEnabled();