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 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;
19 public final class NetconfConfigUtil {
20 private static final Logger logger = LoggerFactory.getLogger(NetconfConfigUtil.class);
22 private static final String PREFIX_PROP = "netconf.";
24 private NetconfConfigUtil() {}
26 private enum InfixProp {
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";
37 private static final String CONNECTION_TIMEOUT_MILLIS_PROP = "connectionTimeoutMillis";
38 private static final long DEFAULT_TIMEOUT_MILLIS = 5000;
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;
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;
54 public static InetSocketAddress extractTCPNetconfAddress(BundleContext context, String exceptionMessageIfNotFound, boolean forClient) {
56 Optional<InetSocketAddress> inetSocketAddressOptional = extractSomeNetconfAddress(context, InfixProp.tcp, exceptionMessageIfNotFound, forClient);
58 if (!inetSocketAddressOptional.isPresent()) {
59 throw new IllegalStateException("Netconf tcp address not found." + exceptionMessageIfNotFound);
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);
66 return inetSocketAddress;
69 public static Optional<InetSocketAddress> extractSSHNetconfAddress(BundleContext context, String exceptionMessage) {
70 return extractSomeNetconfAddress(context, InfixProp.ssh, exceptionMessage, false);
73 public static String getPrivateKeyPath(BundleContext context){
74 return getPropertyValue(context,PREFIX_PROP + InfixProp.ssh +PRIVATE_KEY_PATH_PROP);
76 public static Optional<String> getSSHDefaultUser(BundleContext context){
77 return getOptionalPropertyValue(context,PREFIX_PROP + InfixProp.ssh +SSH_DEFAULT_USER);
79 public static Optional<String> getSSHDefaultPassword(BundleContext context){
80 return getOptionalPropertyValue(context,PREFIX_PROP + InfixProp.ssh +SSH_DEFAULT_PASSWORD);
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+"'");
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();
95 return Optional.fromNullable(propertyValue);
99 * from which properties are being read.
102 * @return value if address and port are valid.
103 * @throws IllegalStateException
104 * if address or port are invalid, or configuration is missing
106 private static Optional<InetSocketAddress> extractSomeNetconfAddress(BundleContext context,
108 String exceptionMessage,
112 address = context.getProperty(PREFIX_PROP + infixProp + CLIENT_PROP + ADDRESS_SUFFIX_PROP);
114 if (address == null || address.equals("")){
115 address = context.getProperty(PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP);
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);
124 portKey = PREFIX_PROP + infixProp + CLIENT_PROP + PORT_SUFFIX_PROP;
126 if (portKey == null || portKey.equals("")){
127 portKey = PREFIX_PROP + infixProp + PORT_SUFFIX_PROP;
129 String portString = context.getProperty(portKey);
130 checkNotNull(portString, "Netconf port must be specified in properties file with " + portKey);
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);