Merge changes Iae4ee2f2,I148c22c4,I1dab84ae
[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 org.osgi.framework.BundleContext;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import java.net.InetSocketAddress;
17
18 import static com.google.common.base.Preconditions.checkNotNull;
19
20 public class NetconfConfigUtil {
21     private static final Logger logger = LoggerFactory.getLogger(NetconfConfigUtil.class);
22
23     private static final String PREFIX_PROP = "netconf.";
24
25
26
27     private 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 CLIENT_PROP = ".client";
34     private static final String PRIVATE_KEY_PATH_PROP = ".pk.path";
35
36     private static final String CONNECTION_TIMEOUT_MILLIS_PROP = "connectionTimeoutMillis";
37     private static final long DEFAULT_TIMEOUT_MILLIS = 5000;
38
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;
44         }
45         try {
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;
50         }
51     }
52
53     public static InetSocketAddress extractTCPNetconfAddress(BundleContext context, String exceptionMessageIfNotFound, boolean forClient) {
54
55         Optional<InetSocketAddress> inetSocketAddressOptional = extractSomeNetconfAddress(context, InfixProp.tcp, exceptionMessageIfNotFound, forClient);
56
57         if (inetSocketAddressOptional.isPresent() == false) {
58             throw new IllegalStateException("Netconf tcp address not found." + exceptionMessageIfNotFound);
59         }
60         return inetSocketAddressOptional.get();
61     }
62
63     public static Optional<InetSocketAddress> extractSSHNetconfAddress(BundleContext context, String exceptionMessage) {
64         return extractSomeNetconfAddress(context, InfixProp.ssh, exceptionMessage, false);
65     }
66
67     public static String getPrivateKeyPath(BundleContext context){
68         return getPropertyValue(context,PREFIX_PROP + InfixProp.ssh +PRIVATE_KEY_PATH_PROP);
69     }
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+"'");
74         }
75         return propertyValue;
76     }
77     /**
78      * @param context
79      *            from which properties are being read.
80      * @param infixProp
81      *            either tcp or ssh
82      * @return value if address and port are valid.
83      * @throws IllegalStateException
84      *             if address or port are invalid, or configuration is missing
85      */
86     private static Optional<InetSocketAddress> extractSomeNetconfAddress(BundleContext context,
87                                                                          InfixProp infixProp,
88                                                                          String exceptionMessage,
89                                                                          boolean client) {
90         String address = "";
91         if (client) {
92             address = context.getProperty(PREFIX_PROP + infixProp + CLIENT_PROP + ADDRESS_SUFFIX_PROP);
93         }
94         if (address == null || address.equals("")){
95             address = context.getProperty(PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP);
96         }
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);
101         }
102         String portKey = "";
103         if (client) {
104             portKey = PREFIX_PROP + infixProp + CLIENT_PROP + PORT_SUFFIX_PROP;
105         }
106         if (portKey == null || portKey.equals("")){
107             portKey = PREFIX_PROP + infixProp + PORT_SUFFIX_PROP;
108         }
109         String portString = context.getProperty(portKey);
110         checkNotNull(portString, "Netconf port must be specified in properties file with " + portKey);
111         try {
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);
117         }
118     }
119 }