client IP/port definition in configuration separation
[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
27     public static InetSocketAddress extractTCPNetconfAddress(BundleContext context, String exceptionMessageIfNotFound, boolean forClient) {
28
29         Optional<InetSocketAddress> inetSocketAddressOptional = extractSomeNetconfAddress(context, InfixProp.tcp, exceptionMessageIfNotFound, forClient);
30
31         if (inetSocketAddressOptional.isPresent() == false) {
32             throw new IllegalStateException("Netconf tcp address not found." + exceptionMessageIfNotFound);
33         }
34         return inetSocketAddressOptional.get();
35     }
36
37     public static Optional<InetSocketAddress> extractSSHNetconfAddress(BundleContext context, String exceptionMessage) {
38         return extractSomeNetconfAddress(context, InfixProp.ssh, exceptionMessage, false);
39     }
40
41     /**
42      * @param context
43      *            from which properties are being read.
44      * @param infixProp
45      *            either tcp or ssh
46      * @return value if address and port are valid.
47      * @throws IllegalStateException
48      *             if address or port are invalid, or configuration is missing
49      */
50     private static Optional<InetSocketAddress> extractSomeNetconfAddress(BundleContext context,
51                                                                          InfixProp infixProp,
52                                                                          String exceptionMessage,
53                                                                          boolean client) {
54         String address = "";
55         if (client) {
56             address = context.getProperty(PREFIX_PROP + infixProp + CLIENT_PROP + ADDRESS_SUFFIX_PROP);
57         }
58         if (address == null || address.equals("")){
59             address = context.getProperty(PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP);
60         }
61         if (address == null || address.equals("")) {
62             throw new IllegalStateException("Cannot find initial netconf configuration for parameter    "
63                     +PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP
64                     +" in config.ini. "+exceptionMessage);
65         }
66         String portKey = "";
67         if (client) {
68             portKey = PREFIX_PROP + infixProp + CLIENT_PROP + PORT_SUFFIX_PROP;
69         }
70         if (portKey == null || portKey.equals("")){
71             portKey = PREFIX_PROP + infixProp + PORT_SUFFIX_PROP;
72         }
73         String portString = context.getProperty(portKey);
74         checkNotNull(portString, "Netconf port must be specified in properties file with " + portKey);
75         try {
76             int port = Integer.valueOf(portString);
77             return Optional.of(new InetSocketAddress(address, port));
78         } catch (RuntimeException e) {
79             throw new IllegalStateException("Cannot create " + infixProp + " netconf address from address:" + address
80                     + " and port:" + portString, e);
81         }
82     }
83 }