9a6ff2e054e7e351ee441e00c8bf10fa028036e7
[netconf.git] / netconf / netconf-util / src / main / java / org / opendaylight / 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.netconf.util.osgi;
10
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;
18
19 public final class NetconfConfigUtil {
20     private static final Logger LOG = LoggerFactory.getLogger(NetconfConfigUtil.class);
21
22     private static final String PREFIX_PROP = "netconf.";
23
24     private NetconfConfigUtil() {
25     }
26
27     public 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 PRIVATE_KEY_PATH_PROP = ".pk.path";
34
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);
43
44     public static LocalAddress getNetconfLocalAddress() {
45         return NETCONF_LOCAL_ADDRESS;
46     }
47
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;
53         }
54         try {
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;
59         }
60     }
61
62     /**
63      * @param context from which properties are being read.
64      * @return value of private key path if value is present, Optional.absent otherwise
65      */
66     public static Optional<String> getPrivateKeyPath(final BundleContext context) {
67         return getProperty(context, getPrivateKeyKey());
68     }
69
70     public static String getPrivateKeyKey() {
71         return PREFIX_PROP + InfixProp.ssh + PRIVATE_KEY_PATH_PROP;
72     }
73
74     public static String getNetconfServerAddressKey(final InfixProp infixProp) {
75         return PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP;
76     }
77
78     /**
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.
82      */
83     public static Optional<InetSocketAddress> extractNetconfServerAddress(final BundleContext context,
84                                                                            final InfixProp infixProp) {
85
86         final Optional<String> address = getProperty(context, getNetconfServerAddressKey(infixProp));
87         final Optional<String> port = getProperty(context, PREFIX_PROP + infixProp + PORT_SUFFIX_PROP);
88
89         if (address.isPresent() && port.isPresent()) {
90             try {
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);
95             }
96         }
97         return Optional.absent();
98     }
99
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);
103     }
104
105     private static Optional<String> getProperty(final BundleContext context, final String propKey) {
106         String value = context.getProperty(propKey);
107         if (value != null && value.isEmpty()) {
108             value = null;
109         }
110         return Optional.fromNullable(value);
111     }
112 }