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