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