2 * Copyright (c) 2013 Cisco 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
9 package org.opendaylight.controller.netconf.util.osgi;
11 import com.google.common.base.Optional;
12 import org.opendaylight.controller.config.stat.ConfigProvider;
13 import org.opendaylight.protocol.util.SSLUtil;
15 import javax.net.ssl.SSLContext;
17 import java.io.FileInputStream;
18 import java.io.InputStream;
19 import java.net.InetSocketAddress;
21 import static com.google.common.base.Preconditions.checkNotNull;
22 import static com.google.common.base.Preconditions.checkState;
24 public class NetconfConfigUtil {
25 private static final String PREFIX_PROP = "netconf.";
27 private enum InfixProp {
31 private static final String PORT_SUFFIX_PROP = ".port";
32 private static final String ADDRESS_SUFFIX_PROP = ".address";
34 private static final String NETCONF_TLS_KEYSTORE_PROP = PREFIX_PROP + InfixProp.tls + ".keystore";
35 private static final String NETCONF_TLS_KEYSTORE_PASSWORD_PROP = NETCONF_TLS_KEYSTORE_PROP + ".password";
37 public static Optional<InetSocketAddress> extractTCPNetconfAddress(ConfigProvider configProvider) {
38 return extractSomeNetconfAddress(configProvider, InfixProp.tcp);
41 public static Optional<TLSConfiguration> extractTLSConfiguration(ConfigProvider configProvider) {
42 Optional<InetSocketAddress> address = extractSomeNetconfAddress(configProvider, InfixProp.tls);
43 if (address.isPresent()) {
44 String keystoreFileName = configProvider.getProperty(NETCONF_TLS_KEYSTORE_PROP);
45 File keystoreFile = new File(keystoreFileName);
46 checkState(keystoreFile.exists() && keystoreFile.isFile() && keystoreFile.canRead(),
47 "Keystore file %s does not exist or is not readable file", keystoreFileName);
48 keystoreFile = keystoreFile.getAbsoluteFile();
49 String keystorePassword = configProvider.getProperty(NETCONF_TLS_KEYSTORE_PASSWORD_PROP);
50 checkNotNull(keystoreFileName, "Property %s must be defined for tls netconf server",
51 NETCONF_TLS_KEYSTORE_PROP);
52 keystorePassword = keystorePassword != null ? keystorePassword : "";
53 return Optional.of(new TLSConfiguration(address.get(), keystoreFile, keystorePassword));
55 return Optional.absent();
59 public static class TLSConfiguration {
60 private final InetSocketAddress address;
61 private final File keystoreFile;
62 private final String keystorePassword;
63 private final SSLContext sslContext;
65 TLSConfiguration(InetSocketAddress address, File keystoreFile, String keystorePassword) {
66 this.address = address;
67 this.keystoreFile = keystoreFile;
68 this.keystorePassword = keystorePassword;
70 try (InputStream keyStoreIS = new FileInputStream(keystoreFile)) {
71 try (InputStream trustStoreIS = new FileInputStream(keystoreFile)) {
72 sslContext = SSLUtil.initializeSecureContext("password", keyStoreIS, trustStoreIS, "SunX509");
75 } catch (Exception e) {
76 throw new RuntimeException("Cannot initialize ssl context for netconf file " + keystoreFile, e);
80 public SSLContext getSslContext() {
84 public InetSocketAddress getAddress() {
88 public File getKeystoreFile() {
92 public String getKeystorePassword() {
93 return keystorePassword;
98 * @param configProvider
99 * from which properties are being read.
102 * @return absent if address is missing, value if address and port are
104 * @throws IllegalStateException
105 * if address or port are invalid
107 private static Optional<InetSocketAddress> extractSomeNetconfAddress(ConfigProvider configProvider,
108 InfixProp infixProp) {
109 String address = configProvider.getProperty(PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP);
110 if (address == null) {
111 return Optional.absent();
113 String portKey = PREFIX_PROP + infixProp + PORT_SUFFIX_PROP;
114 String portString = configProvider.getProperty(portKey);
115 checkNotNull(portString, "Netconf port must be specified in properties file with " + portKey);
117 int port = Integer.valueOf(portString);
118 return Optional.of(new InetSocketAddress(address, port));
119 } catch (RuntimeException e) {
120 throw new IllegalStateException("Cannot create " + infixProp + " netconf address from address:" + address
121 + " and port:" + portString, e);