76068399c17f470b249c0ba35027f89d723b5d81
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / osgi / NetconfConfigUtil.java
1 /*
2  * Copyright (c) 2013 Cisco 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
9 package org.opendaylight.controller.netconf.util.osgi;
10
11 import com.google.common.base.Optional;
12 import org.opendaylight.protocol.util.SSLUtil;
13 import org.osgi.framework.BundleContext;
14
15 import javax.net.ssl.KeyManagerFactory;
16 import javax.net.ssl.SSLContext;
17 import java.io.File;
18 import java.io.FileInputStream;
19 import java.io.InputStream;
20 import java.net.InetSocketAddress;
21
22 import static com.google.common.base.Preconditions.checkNotNull;
23 import static com.google.common.base.Preconditions.checkState;
24
25 public class NetconfConfigUtil {
26     private static final String PREFIX_PROP = "netconf.";
27
28     private enum InfixProp {
29         tcp, tls
30     }
31
32     private static final String PORT_SUFFIX_PROP = ".port";
33     private static final String ADDRESS_SUFFIX_PROP = ".address";
34
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";
37
38     public static Optional<InetSocketAddress> extractTCPNetconfAddress(BundleContext context) {
39         return extractSomeNetconfAddress(context, InfixProp.tcp);
40     }
41
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));
55         } else {
56             return Optional.absent();
57         }
58     }
59
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;
65
66         TLSConfiguration(InetSocketAddress address, File keystoreFile, String keystorePassword) {
67             this.address = address;
68             this.keystoreFile = keystoreFile;
69             this.keystorePassword = keystorePassword;
70             try {
71                 try (InputStream keyStoreIS = new FileInputStream(keystoreFile)) {
72                     try (InputStream trustStoreIS = new FileInputStream(keystoreFile)) {
73                         sslContext = SSLUtil.initializeSecureContext("password", keyStoreIS, trustStoreIS, KeyManagerFactory.getDefaultAlgorithm());
74                     }
75                 }
76             } catch (Exception e) {
77                 throw new RuntimeException("Cannot initialize ssl context for netconf file " + keystoreFile, e);
78             }
79         }
80
81         public SSLContext getSslContext() {
82             return sslContext;
83         }
84
85         public InetSocketAddress getAddress() {
86             return address;
87         }
88
89         public File getKeystoreFile() {
90             return keystoreFile;
91         }
92
93         public String getKeystorePassword() {
94             return keystorePassword;
95         }
96     }
97
98     /**
99      * @param context
100      *            from which properties are being read.
101      * @param infixProp
102      *            either tcp or tls
103      * @return absent if address is missing, value if address and port are
104      *         valid.
105      * @throws IllegalStateException
106      *             if address or port are invalid
107      */
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();
113         }
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);
117         try {
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);
123         }
124     }
125 }