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.protocol.util.SSLUtil;
13 import org.osgi.framework.BundleContext;
15 import javax.net.ssl.KeyManagerFactory;
16 import javax.net.ssl.SSLContext;
18 import java.io.FileInputStream;
19 import java.io.InputStream;
20 import java.net.InetSocketAddress;
22 import static com.google.common.base.Preconditions.checkNotNull;
23 import static com.google.common.base.Preconditions.checkState;
25 public class NetconfConfigUtil {
26 private static final String PREFIX_PROP = "netconf.";
28 private enum InfixProp {
32 private static final String PORT_SUFFIX_PROP = ".port";
33 private static final String ADDRESS_SUFFIX_PROP = ".address";
35 private static final String NETCONF_TLS_KEYSTORE_PROP = PREFIX_PROP + InfixProp.tls + ".keystore";
36 private static final String NETCONF_TLS_KEYSTORE_PASSWORD_PROP = NETCONF_TLS_KEYSTORE_PROP + ".password";
38 public static Optional<InetSocketAddress> extractTCPNetconfAddress(BundleContext context) {
39 return extractSomeNetconfAddress(context, InfixProp.tcp);
42 public static Optional<TLSConfiguration> extractTLSConfiguration(BundleContext context) {
43 Optional<InetSocketAddress> address = extractSomeNetconfAddress(context, InfixProp.tls);
44 if (address.isPresent()) {
45 String keystoreFileName = context.getProperty(NETCONF_TLS_KEYSTORE_PROP);
46 File keystoreFile = new File(keystoreFileName);
47 checkState(keystoreFile.exists() && keystoreFile.isFile() && keystoreFile.canRead(),
48 "Keystore file %s does not exist or is not readable file", keystoreFileName);
49 keystoreFile = keystoreFile.getAbsoluteFile();
50 String keystorePassword = context.getProperty(NETCONF_TLS_KEYSTORE_PASSWORD_PROP);
51 checkNotNull(keystoreFileName, "Property %s must be defined for tls netconf server",
52 NETCONF_TLS_KEYSTORE_PROP);
53 keystorePassword = keystorePassword != null ? keystorePassword : "";
54 return Optional.of(new TLSConfiguration(address.get(), keystoreFile, keystorePassword));
56 return Optional.absent();
60 public static class TLSConfiguration {
61 private final InetSocketAddress address;
62 private final File keystoreFile;
63 private final String keystorePassword;
64 private final SSLContext sslContext;
66 TLSConfiguration(InetSocketAddress address, File keystoreFile, String keystorePassword) {
67 this.address = address;
68 this.keystoreFile = keystoreFile;
69 this.keystorePassword = keystorePassword;
71 try (InputStream keyStoreIS = new FileInputStream(keystoreFile)) {
72 try (InputStream trustStoreIS = new FileInputStream(keystoreFile)) {
73 sslContext = SSLUtil.initializeSecureContext("password", keyStoreIS, trustStoreIS, KeyManagerFactory.getDefaultAlgorithm());
76 } catch (Exception e) {
77 throw new RuntimeException("Cannot initialize ssl context for netconf file " + keystoreFile, e);
81 public SSLContext getSslContext() {
85 public InetSocketAddress getAddress() {
89 public File getKeystoreFile() {
93 public String getKeystorePassword() {
94 return keystorePassword;
100 * from which properties are being read.
103 * @return absent if address is missing, value if address and port are
105 * @throws IllegalStateException
106 * if address or port are invalid
108 private static Optional<InetSocketAddress> extractSomeNetconfAddress(BundleContext context,
109 InfixProp infixProp) {
110 String address = context.getProperty(PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP);
111 if (address == null) {
112 return Optional.absent();
114 String portKey = PREFIX_PROP + infixProp + PORT_SUFFIX_PROP;
115 String portString = context.getProperty(portKey);
116 checkNotNull(portString, "Netconf port must be specified in properties file with " + portKey);
118 int port = Integer.valueOf(portString);
119 return Optional.of(new InetSocketAddress(address, port));
120 } catch (RuntimeException e) {
121 throw new IllegalStateException("Cannot create " + infixProp + " netconf address from address:" + address
122 + " and port:" + portString, e);