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 class NetconfConfigUtil {
21 private static final Logger logger = LoggerFactory.getLogger(NetconfConfigUtil.class);
23 private static final String PREFIX_PROP = "netconf.";
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 return inetSocketAddressOptional.get();
63 public static Optional<InetSocketAddress> extractSSHNetconfAddress(BundleContext context, String exceptionMessage) {
64 return extractSomeNetconfAddress(context, InfixProp.ssh, exceptionMessage, false);
67 public static String getPrivateKeyPath(BundleContext context){
68 return getPropertyValue(context,PREFIX_PROP + InfixProp.ssh +PRIVATE_KEY_PATH_PROP);
70 private static String getPropertyValue(BundleContext context, String propertyName){
71 String propertyValue = context.getProperty(propertyName);
72 if (propertyValue == null){
73 throw new IllegalStateException("Cannot find initial property with name '"+propertyName+"'");
79 * from which properties are being read.
82 * @return value if address and port are valid.
83 * @throws IllegalStateException
84 * if address or port are invalid, or configuration is missing
86 private static Optional<InetSocketAddress> extractSomeNetconfAddress(BundleContext context,
88 String exceptionMessage,
92 address = context.getProperty(PREFIX_PROP + infixProp + CLIENT_PROP + ADDRESS_SUFFIX_PROP);
94 if (address == null || address.equals("")){
95 address = context.getProperty(PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP);
97 if (address == null || address.equals("")) {
98 throw new IllegalStateException("Cannot find initial netconf configuration for parameter "
99 +PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP
100 +" in config.ini. "+exceptionMessage);
104 portKey = PREFIX_PROP + infixProp + CLIENT_PROP + PORT_SUFFIX_PROP;
106 if (portKey == null || portKey.equals("")){
107 portKey = PREFIX_PROP + infixProp + PORT_SUFFIX_PROP;
109 String portString = context.getProperty(portKey);
110 checkNotNull(portString, "Netconf port must be specified in properties file with " + portKey);
112 int port = Integer.valueOf(portString);
113 return Optional.of(new InetSocketAddress(address, port));
114 } catch (RuntimeException e) {
115 throw new IllegalStateException("Cannot create " + infixProp + " netconf address from address:" + address
116 + " and port:" + portString, e);