Merge "Log the address and port when binding fails"
[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 javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.apache.aries.blueprint.annotation.service.Reference;
17 import org.apache.aries.blueprint.annotation.service.Service;
18 import org.opendaylight.infrautils.diagstatus.DiagStatusService;
19 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
20 import org.opendaylight.openflowjava.protocol.api.connection.ThreadConfiguration;
21 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
22 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
23 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProviderFactory;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow._switch.connection.config.rev160506.SwitchConnectionConfig;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow._switch.connection.config.rev160506._switch.connection.config.Threads;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow._switch.connection.config.rev160506._switch.connection.config.Tls;
30
31 /**
32  * Implementation of the SwitchConnectionProviderFactory interface.
33  */
34 @Singleton
35 @Service(classes = SwitchConnectionProviderFactory.class)
36 public class SwitchConnectionProviderFactoryImpl implements SwitchConnectionProviderFactory {
37
38     private final DiagStatusService diagStatusService;
39
40     @Inject
41     public SwitchConnectionProviderFactoryImpl(@Reference DiagStatusService diagStatusService) {
42         this.diagStatusService = diagStatusService;
43     }
44
45     @Override
46     public SwitchConnectionProvider newInstance(SwitchConnectionConfig config) {
47         return new SwitchConnectionProviderImpl(new ConnectionConfigurationImpl(config), diagStatusService);
48     }
49
50     private static InetAddress getInetAddress(final IpAddress address) throws UnknownHostException {
51         if (address != null) {
52             if (address.getIpv4Address() != null) {
53                 return InetAddress.getByName(address.getIpv4Address().getValue());
54             } else if (address.getIpv6Address() != null) {
55                 return InetAddress.getByName(address.getIpv6Address().getValue());
56             }
57         }
58         return null;
59     }
60
61     private static class ConnectionConfigurationImpl implements ConnectionConfiguration {
62         private final SwitchConnectionConfig config;
63         private InetAddress address;
64
65         ConnectionConfigurationImpl(SwitchConnectionConfig config) {
66             this.config = config;
67
68             try {
69                 address = getInetAddress(config.getAddress());
70             } catch (UnknownHostException e) {
71                 throw new RuntimeException(e);
72             }
73         }
74
75         @Override
76         public InetAddress getAddress() {
77             return address;
78         }
79
80         @Override
81         public int getPort() {
82             return config.getPort();
83         }
84
85         @Override
86         public Object getTransferProtocol() {
87             return config.getTransportProtocol();
88         }
89
90         @Override
91         public int getChannelOutboundQueueSize() {
92             return config.getChannelOutboundQueueSize();
93         }
94
95         @Override
96         public TlsConfiguration getTlsConfiguration() {
97             final Tls tlsConfig = config.getTls();
98             if (tlsConfig == null || !TransportProtocol.TLS.equals(getTransferProtocol())) {
99                 return null;
100             }
101
102             return new TlsConfiguration() {
103                 @Override
104                 public KeystoreType getTlsTruststoreType() {
105                     return MoreObjects.firstNonNull(tlsConfig.getTruststoreType(), null);
106                 }
107
108                 @Override
109                 public String getTlsTruststore() {
110                     return MoreObjects.firstNonNull(tlsConfig.getTruststore(), null);
111                 }
112
113                 @Override
114                 public KeystoreType getTlsKeystoreType() {
115                     return MoreObjects.firstNonNull(tlsConfig.getKeystoreType(), null);
116                 }
117
118                 @Override
119                 public String getTlsKeystore() {
120                     return MoreObjects.firstNonNull(tlsConfig.getKeystore(), null);
121                 }
122
123                 @Override
124                 public org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType
125                         getTlsKeystorePathType() {
126                     return MoreObjects.firstNonNull(tlsConfig.getKeystorePathType(), null);
127                 }
128
129                 @Override
130                 public org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType
131                         getTlsTruststorePathType() {
132                     return MoreObjects.firstNonNull(tlsConfig.getTruststorePathType(), null);
133                 }
134
135                 @Override
136                 public String getKeystorePassword() {
137                     return MoreObjects.firstNonNull(tlsConfig.getKeystorePassword(), null);
138                 }
139
140                 @Override
141                 public String getCertificatePassword() {
142                     return MoreObjects.firstNonNull(tlsConfig.getCertificatePassword(), null);
143                 }
144
145                 @Override
146                 public String getTruststorePassword() {
147                     return MoreObjects.firstNonNull(tlsConfig.getTruststorePassword(), null);
148                 }
149
150                 @Override
151                 public List<String> getCipherSuites() {
152                     return tlsConfig.getCipherSuites();
153                 }
154             };
155         }
156
157         @Override
158         public long getSwitchIdleTimeout() {
159             return config.getSwitchIdleTimeout();
160         }
161
162         @Override
163         public Object getSslContext() {
164             return null;
165         }
166
167         @Override
168         public ThreadConfiguration getThreadConfiguration() {
169             final Threads threads = config.getThreads();
170             if (threads == null) {
171                 return null;
172             }
173
174             return new ThreadConfiguration() {
175                 @Override
176                 public int getWorkerThreadCount() {
177                     return threads.getWorkerThreads();
178                 }
179
180                 @Override
181                 public int getBossThreadCount() {
182                     return threads.getBossThreads();
183                 }
184             };
185         }
186
187         @Override
188         public boolean useBarrier() {
189             return config.isUseBarrier();
190         }
191
192         @Override
193         public boolean isGroupAddModEnabled() {
194             return config.isGroupAddModEnabled();
195         }
196     }
197 }