private key configurable in config.ini
[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 java.net.InetSocketAddress;
13         import org.osgi.framework.BundleContext;
14         import static com.google.common.base.Preconditions.checkNotNull;
15
16 public class NetconfConfigUtil {
17     private static final String PREFIX_PROP = "netconf.";
18
19     private enum InfixProp {
20         tcp, ssh
21     }
22
23     private static final String PORT_SUFFIX_PROP = ".port";
24     private static final String ADDRESS_SUFFIX_PROP = ".address";
25     private static final String CLIENT_PROP = ".client";
26     private static final String PRIVATE_KEY_PATH_PROP = ".pk.path";
27
28     public static InetSocketAddress extractTCPNetconfAddress(BundleContext context, String exceptionMessageIfNotFound, boolean forClient) {
29
30         Optional<InetSocketAddress> inetSocketAddressOptional = extractSomeNetconfAddress(context, InfixProp.tcp, exceptionMessageIfNotFound, forClient);
31
32         if (inetSocketAddressOptional.isPresent() == false) {
33             throw new IllegalStateException("Netconf tcp address not found." + exceptionMessageIfNotFound);
34         }
35         return inetSocketAddressOptional.get();
36     }
37
38     public static Optional<InetSocketAddress> extractSSHNetconfAddress(BundleContext context, String exceptionMessage) {
39         return extractSomeNetconfAddress(context, InfixProp.ssh, exceptionMessage, false);
40     }
41
42     public static String getPrivateKeyPath(BundleContext context){
43         return getPropertyValue(context,PREFIX_PROP + InfixProp.ssh +PRIVATE_KEY_PATH_PROP);
44     }
45     private static String getPropertyValue(BundleContext context, String propertyName){
46         String propertyValue = context.getProperty(propertyName);
47         if (propertyValue == null){
48             throw new IllegalStateException("Cannot find initial property with name '"+propertyName+"'");
49         }
50         return propertyValue;
51     }
52     /**
53      * @param context
54      *            from which properties are being read.
55      * @param infixProp
56      *            either tcp or ssh
57      * @return value if address and port are valid.
58      * @throws IllegalStateException
59      *             if address or port are invalid, or configuration is missing
60      */
61     private static Optional<InetSocketAddress> extractSomeNetconfAddress(BundleContext context,
62                                                                          InfixProp infixProp,
63                                                                          String exceptionMessage,
64                                                                          boolean client) {
65         String address = "";
66         if (client) {
67             address = context.getProperty(PREFIX_PROP + infixProp + CLIENT_PROP + ADDRESS_SUFFIX_PROP);
68         }
69         if (address == null || address.equals("")){
70             address = context.getProperty(PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP);
71         }
72         if (address == null || address.equals("")) {
73             throw new IllegalStateException("Cannot find initial netconf configuration for parameter    "
74                     +PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP
75                     +" in config.ini. "+exceptionMessage);
76         }
77         String portKey = "";
78         if (client) {
79             portKey = PREFIX_PROP + infixProp + CLIENT_PROP + PORT_SUFFIX_PROP;
80         }
81         if (portKey == null || portKey.equals("")){
82             portKey = PREFIX_PROP + infixProp + PORT_SUFFIX_PROP;
83         }
84         String portString = context.getProperty(portKey);
85         checkNotNull(portString, "Netconf port must be specified in properties file with " + portKey);
86         try {
87             int port = Integer.valueOf(portString);
88             return Optional.of(new InetSocketAddress(address, port));
89         } catch (RuntimeException e) {
90             throw new IllegalStateException("Cannot create " + infixProp + " netconf address from address:" + address
91                     + " and port:" + portString, e);
92         }
93     }
94 }