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.netconf.util.osgi;
11 import com.google.common.base.Optional;
12 import io.netty.channel.local.LocalAddress;
13 import java.net.InetSocketAddress;
14 import java.util.concurrent.TimeUnit;
15 import org.osgi.framework.BundleContext;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 public final class NetconfConfigUtil {
20 private static final Logger LOG = LoggerFactory.getLogger(NetconfConfigUtil.class);
22 private static final String PREFIX_PROP = "netconf.";
24 private NetconfConfigUtil() {
27 public 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 PRIVATE_KEY_PATH_PROP = ".pk.path";
35 private static final String CONNECTION_TIMEOUT_MILLIS_PROP = "connectionTimeoutMillis";
36 private static final String LOCAL_HOST = "127.0.0.1";
37 private static final String INADDR_ANY = "0.0.0.0";
38 public static final long DEFAULT_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(30);
39 private static final LocalAddress NETCONF_LOCAL_ADDRESS = new LocalAddress("netconf");
40 public static final String DEFAULT_PRIVATE_KEY_PATH = "./configuration/RSA.pk";
41 public static final InetSocketAddress DEFAULT_TCP_SERVER_ADRESS = new InetSocketAddress(LOCAL_HOST, 8383);
42 public static final InetSocketAddress DEFAULT_SSH_SERVER_ADRESS = new InetSocketAddress(INADDR_ANY, 1830);
44 public static LocalAddress getNetconfLocalAddress() {
45 return NETCONF_LOCAL_ADDRESS;
48 public static long extractTimeoutMillis(final BundleContext bundleContext) {
49 final String key = PREFIX_PROP + CONNECTION_TIMEOUT_MILLIS_PROP;
50 final String timeoutString = bundleContext.getProperty(key);
51 if (timeoutString == null || timeoutString.length() == 0) {
52 return DEFAULT_TIMEOUT_MILLIS;
55 return Long.parseLong(timeoutString);
56 } catch (final NumberFormatException e) {
57 LOG.warn("Cannot parse {} property: {}, using defaults", key, timeoutString, e);
58 return DEFAULT_TIMEOUT_MILLIS;
63 * @param context from which properties are being read.
64 * @return value of private key path if value is present, Optional.absent otherwise
66 public static Optional<String> getPrivateKeyPath(final BundleContext context) {
67 return getProperty(context, getPrivateKeyKey());
70 public static String getPrivateKeyKey() {
71 return PREFIX_PROP + InfixProp.ssh + PRIVATE_KEY_PATH_PROP;
74 public static String getNetconfServerAddressKey(final InfixProp infixProp) {
75 return PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP;
79 * @param context from which properties are being read.
80 * @param infixProp either tcp or ssh
81 * @return value if address and port are present and valid, Optional.absent otherwise.
83 public static Optional<InetSocketAddress> extractNetconfServerAddress(final BundleContext context,
84 final InfixProp infixProp) {
86 final Optional<String> address = getProperty(context, getNetconfServerAddressKey(infixProp));
87 final Optional<String> port = getProperty(context, PREFIX_PROP + infixProp + PORT_SUFFIX_PROP);
89 if (address.isPresent() && port.isPresent()) {
91 return Optional.of(parseAddress(address, port));
92 } catch (final IllegalArgumentException | SecurityException e) {
93 LOG.warn("Unable to parse {} netconf address from {}:{}, fallback to default",
94 infixProp, address, port, e);
97 return Optional.absent();
100 private static InetSocketAddress parseAddress(final Optional<String> address, final Optional<String> port) {
101 final int portNumber = Integer.valueOf(port.get());
102 return new InetSocketAddress(address.get(), portNumber);
105 private static Optional<String> getProperty(final BundleContext context, final String propKey) {
106 String value = context.getProperty(propKey);
107 if (value != null && value.isEmpty()) {
110 return Optional.fromNullable(value);