Choice and case resolving in JSON output
[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, ssh
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<InetSocketAddress> extractSSHNetconfAddress(BundleContext context) {
43         return extractSomeNetconfAddress(context, InfixProp.ssh);
44     }
45
46
47     public static Optional<TLSConfiguration> extractTLSConfiguration(BundleContext context) {
48         Optional<InetSocketAddress> address = extractSomeNetconfAddress(context, InfixProp.tls);
49         if (address.isPresent()) {
50             String keystoreFileName = context.getProperty(NETCONF_TLS_KEYSTORE_PROP);
51             File keystoreFile = new File(keystoreFileName);
52             checkState(keystoreFile.exists() && keystoreFile.isFile() && keystoreFile.canRead(),
53                     "Keystore file %s does not exist or is not readable file", keystoreFileName);
54             keystoreFile = keystoreFile.getAbsoluteFile();
55             String keystorePassword = context.getProperty(NETCONF_TLS_KEYSTORE_PASSWORD_PROP);
56             checkNotNull(keystoreFileName, "Property %s must be defined for tls netconf server",
57                     NETCONF_TLS_KEYSTORE_PROP);
58             keystorePassword = keystorePassword != null ? keystorePassword : "";
59             return Optional.of(new TLSConfiguration(address.get(), keystoreFile, keystorePassword));
60         } else {
61             return Optional.absent();
62         }
63     }
64
65     public static class TLSConfiguration {
66         private final InetSocketAddress address;
67         private final File keystoreFile;
68         private final String keystorePassword;
69         private final SSLContext sslContext;
70
71         TLSConfiguration(InetSocketAddress address, File keystoreFile, String keystorePassword) {
72             this.address = address;
73             this.keystoreFile = keystoreFile;
74             this.keystorePassword = keystorePassword;
75             try {
76                 try (InputStream keyStoreIS = new FileInputStream(keystoreFile)) {
77                     try (InputStream trustStoreIS = new FileInputStream(keystoreFile)) {
78                         sslContext = SSLUtil.initializeSecureContext("password", keyStoreIS, trustStoreIS, KeyManagerFactory.getDefaultAlgorithm());
79                     }
80                 }
81             } catch (Exception e) {
82                 throw new RuntimeException("Cannot initialize ssl context for netconf file " + keystoreFile, e);
83             }
84         }
85
86         public SSLContext getSslContext() {
87             return sslContext;
88         }
89
90         public InetSocketAddress getAddress() {
91             return address;
92         }
93
94         public File getKeystoreFile() {
95             return keystoreFile;
96         }
97
98         public String getKeystorePassword() {
99             return keystorePassword;
100         }
101     }
102
103     /**
104      * @param context
105      *            from which properties are being read.
106      * @param infixProp
107      *            either tcp or tls
108      * @return absent if address is missing, value if address and port are
109      *         valid.
110      * @throws IllegalStateException
111      *             if address or port are invalid
112      */
113     private static Optional<InetSocketAddress> extractSomeNetconfAddress(BundleContext context,
114             InfixProp infixProp) {
115         String address = context.getProperty(PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP);
116         if (address == null) {
117             return Optional.absent();
118         }
119         String portKey = PREFIX_PROP + infixProp + PORT_SUFFIX_PROP;
120         String portString = context.getProperty(portKey);
121         checkNotNull(portString, "Netconf port must be specified in properties file with " + portKey);
122         try {
123             int port = Integer.valueOf(portString);
124             return Optional.of(new InetSocketAddress(address, port));
125         } catch (RuntimeException e) {
126             throw new IllegalStateException("Cannot create " + infixProp + " netconf address from address:" + address
127                     + " and port:" + portString, e);
128         }
129     }
130 }