Merge "Bug 1036 - Allow using container in case stmt to preserve uniqueness."
[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 com.google.common.base.Strings;
13 import java.net.InetSocketAddress;
14 import org.osgi.framework.BundleContext;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17 import static com.google.common.base.Preconditions.checkNotNull;
18
19 public final class NetconfConfigUtil {
20     private static final Logger logger = LoggerFactory.getLogger(NetconfConfigUtil.class);
21
22     private static final String PREFIX_PROP = "netconf.";
23
24     private NetconfConfigUtil() {}
25
26     private enum InfixProp {
27         tcp, ssh
28     }
29
30     private static final String PORT_SUFFIX_PROP = ".port";
31     private static final String ADDRESS_SUFFIX_PROP = ".address";
32     private static final String CLIENT_PROP = ".client";
33     private static final String PRIVATE_KEY_PATH_PROP = ".pk.path";
34     private static final String SSH_DEFAULT_USER = ".default.user";
35     private static final String SSH_DEFAULT_PASSWORD = ".default.password";
36
37     private static final String CONNECTION_TIMEOUT_MILLIS_PROP = "connectionTimeoutMillis";
38     private static final long DEFAULT_TIMEOUT_MILLIS = 5000;
39
40     public static long extractTimeoutMillis(BundleContext bundleContext) {
41         String key = PREFIX_PROP + CONNECTION_TIMEOUT_MILLIS_PROP;
42         String timeoutString = bundleContext.getProperty(key);
43         if (timeoutString == null || timeoutString.length() == 0) {
44             return DEFAULT_TIMEOUT_MILLIS;
45         }
46         try {
47             return Long.parseLong(timeoutString);
48         }catch(NumberFormatException e) {
49             logger.warn("Cannot parse {} property: {}, using defaults", key, timeoutString, e);
50             return DEFAULT_TIMEOUT_MILLIS;
51         }
52     }
53
54     public static InetSocketAddress extractTCPNetconfAddress(BundleContext context, String exceptionMessageIfNotFound, boolean forClient) {
55
56         Optional<InetSocketAddress> inetSocketAddressOptional = extractSomeNetconfAddress(context, InfixProp.tcp, exceptionMessageIfNotFound, forClient);
57
58         if (!inetSocketAddressOptional.isPresent()) {
59             throw new IllegalStateException("Netconf tcp address not found." + exceptionMessageIfNotFound);
60         }
61         InetSocketAddress inetSocketAddress = inetSocketAddressOptional.get();
62         if (inetSocketAddress.getAddress().isAnyLocalAddress()) {
63             logger.warn("Unprotected netconf TCP address is configured to ANY local address. This is a security risk. " +
64                     "Consider changing {} to 127.0.0.1", PREFIX_PROP + InfixProp.tcp + ADDRESS_SUFFIX_PROP);
65         }
66         return inetSocketAddress;
67     }
68
69     public static Optional<InetSocketAddress> extractSSHNetconfAddress(BundleContext context, String exceptionMessage) {
70         return extractSomeNetconfAddress(context, InfixProp.ssh, exceptionMessage, false);
71     }
72
73     public static String getPrivateKeyPath(BundleContext context){
74         return getPropertyValue(context,PREFIX_PROP + InfixProp.ssh +PRIVATE_KEY_PATH_PROP);
75     }
76     public static Optional<String> getSSHDefaultUser(BundleContext context){
77         return getOptionalPropertyValue(context,PREFIX_PROP + InfixProp.ssh +SSH_DEFAULT_USER);
78     }
79     public static Optional<String> getSSHDefaultPassword(BundleContext context){
80         return getOptionalPropertyValue(context,PREFIX_PROP + InfixProp.ssh +SSH_DEFAULT_PASSWORD);
81     }
82
83     private static String getPropertyValue(BundleContext context, String propertyName){
84         String propertyValue = context.getProperty(propertyName);
85         if (propertyValue == null){
86             throw new IllegalStateException("Cannot find initial property with name '"+propertyName+"'");
87         }
88         return propertyValue;
89     }
90     private static Optional<String> getOptionalPropertyValue(BundleContext context, String propertyName){
91         String propertyValue = context.getProperty(propertyName);
92         if (Strings.isNullOrEmpty(propertyValue)){
93             return Optional.absent();
94         }
95         return Optional.fromNullable(propertyValue);
96     }
97     /**
98      * @param context
99      *            from which properties are being read.
100      * @param infixProp
101      *            either tcp or ssh
102      * @return value if address and port are valid.
103      * @throws IllegalStateException
104      *             if address or port are invalid, or configuration is missing
105      */
106     private static Optional<InetSocketAddress> extractSomeNetconfAddress(BundleContext context,
107                                                                          InfixProp infixProp,
108                                                                          String exceptionMessage,
109                                                                          boolean client) {
110         String address = "";
111         if (client) {
112             address = context.getProperty(PREFIX_PROP + infixProp + CLIENT_PROP + ADDRESS_SUFFIX_PROP);
113         }
114         if (address == null || address.equals("")){
115             address = context.getProperty(PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP);
116         }
117         if (address == null || address.equals("")) {
118             throw new IllegalStateException("Cannot find initial netconf configuration for parameter    "
119                     +PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP
120                     +" in config.ini. "+exceptionMessage);
121         }
122         String portKey = "";
123         if (client) {
124             portKey = PREFIX_PROP + infixProp + CLIENT_PROP + PORT_SUFFIX_PROP;
125         }
126         if (portKey == null || portKey.equals("")){
127             portKey = PREFIX_PROP + infixProp + PORT_SUFFIX_PROP;
128         }
129         String portString = context.getProperty(portKey);
130         checkNotNull(portString, "Netconf port must be specified in properties file with " + portKey);
131         try {
132             int port = Integer.valueOf(portString);
133             return Optional.of(new InetSocketAddress(address, port));
134         } catch (RuntimeException e) {
135             throw new IllegalStateException("Cannot create " + infixProp + " netconf address from address:" + address
136                     + " and port:" + portString, e);
137         }
138     }
139 }