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 org.osgi.framework.BundleContext;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 import java.net.InetSocketAddress;
18 import static com.google.common.base.Preconditions.checkNotNull;
20 public final class NetconfConfigUtil {
21 private static final Logger logger = LoggerFactory.getLogger(NetconfConfigUtil.class);
23 private static final String PREFIX_PROP = "netconf.";
25 private NetconfConfigUtil() {}
27 private enum InfixProp {
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";
36 private static final String CONNECTION_TIMEOUT_MILLIS_PROP = "connectionTimeoutMillis";
37 private static final long DEFAULT_TIMEOUT_MILLIS = 5000;
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;
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;
53 public static InetSocketAddress extractTCPNetconfAddress(BundleContext context, String exceptionMessageIfNotFound, boolean forClient) {
55 Optional<InetSocketAddress> inetSocketAddressOptional = extractSomeNetconfAddress(context, InfixProp.tcp, exceptionMessageIfNotFound, forClient);
57 if (inetSocketAddressOptional.isPresent() == false) {
58 throw new IllegalStateException("Netconf tcp address not found." + exceptionMessageIfNotFound);
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);
65 return inetSocketAddress;
68 public static Optional<InetSocketAddress> extractSSHNetconfAddress(BundleContext context, String exceptionMessage) {
69 return extractSomeNetconfAddress(context, InfixProp.ssh, exceptionMessage, false);
72 public static String getPrivateKeyPath(BundleContext context){
73 return getPropertyValue(context,PREFIX_PROP + InfixProp.ssh +PRIVATE_KEY_PATH_PROP);
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+"'");
84 * from which properties are being read.
87 * @return value if address and port are valid.
88 * @throws IllegalStateException
89 * if address or port are invalid, or configuration is missing
91 private static Optional<InetSocketAddress> extractSomeNetconfAddress(BundleContext context,
93 String exceptionMessage,
97 address = context.getProperty(PREFIX_PROP + infixProp + CLIENT_PROP + ADDRESS_SUFFIX_PROP);
99 if (address == null || address.equals("")){
100 address = context.getProperty(PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP);
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);
109 portKey = PREFIX_PROP + infixProp + CLIENT_PROP + PORT_SUFFIX_PROP;
111 if (portKey == null || portKey.equals("")){
112 portKey = PREFIX_PROP + infixProp + PORT_SUFFIX_PROP;
114 String portString = context.getProperty(portKey);
115 checkNotNull(portString, "Netconf port must be specified in properties file with " + portKey);
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);