Remove netconf from commons/opendaylight pom
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / 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.controller.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     public static final long DEFAULT_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(30);
37     private static final LocalAddress netconfLocalAddress = new LocalAddress("netconf");
38
39     public static LocalAddress getNetconfLocalAddress() {
40         return netconfLocalAddress;
41     }
42
43     public static long extractTimeoutMillis(final BundleContext bundleContext) {
44         final String key = PREFIX_PROP + CONNECTION_TIMEOUT_MILLIS_PROP;
45         final String timeoutString = bundleContext.getProperty(key);
46         if (timeoutString == null || timeoutString.length() == 0) {
47             return DEFAULT_TIMEOUT_MILLIS;
48         }
49         try {
50             return Long.parseLong(timeoutString);
51         } catch (final NumberFormatException e) {
52             LOG.warn("Cannot parse {} property: {}, using defaults", key, timeoutString, e);
53             return DEFAULT_TIMEOUT_MILLIS;
54         }
55     }
56
57     public static String getPrivateKeyPath(final BundleContext context) {
58         return getPropertyValue(context, getPrivateKeyKey());
59     }
60
61     public static String getPrivateKeyKey() {
62         return PREFIX_PROP + InfixProp.ssh + PRIVATE_KEY_PATH_PROP;
63     }
64
65     private static String getPropertyValue(final BundleContext context, final String propertyName) {
66         final String propertyValue = context.getProperty(propertyName);
67         if (propertyValue == null) {
68             throw new IllegalStateException("Cannot find initial property with name '" + propertyName + "'");
69         }
70         return propertyValue;
71     }
72
73     public static String getNetconfServerAddressKey(InfixProp infixProp) {
74         return PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP;
75     }
76
77     /**
78      * @param context   from which properties are being read.
79      * @param infixProp either tcp or ssh
80      * @return value if address and port are present and valid, Optional.absent otherwise.
81      * @throws IllegalStateException if address or port are invalid, or configuration is missing
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 RuntimeException 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 }