2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.util.osgi;
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;
16 public class NetconfConfigUtil {
17 private static final String PREFIX_PROP = "netconf.";
19 private enum InfixProp {
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";
28 public static InetSocketAddress extractTCPNetconfAddress(BundleContext context, String exceptionMessageIfNotFound, boolean forClient) {
30 Optional<InetSocketAddress> inetSocketAddressOptional = extractSomeNetconfAddress(context, InfixProp.tcp, exceptionMessageIfNotFound, forClient);
32 if (inetSocketAddressOptional.isPresent() == false) {
33 throw new IllegalStateException("Netconf tcp address not found." + exceptionMessageIfNotFound);
35 return inetSocketAddressOptional.get();
38 public static Optional<InetSocketAddress> extractSSHNetconfAddress(BundleContext context, String exceptionMessage) {
39 return extractSomeNetconfAddress(context, InfixProp.ssh, exceptionMessage, false);
42 public static String getPrivateKeyPath(BundleContext context){
43 return getPropertyValue(context,PREFIX_PROP + InfixProp.ssh +PRIVATE_KEY_PATH_PROP);
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+"'");
54 * from which properties are being read.
57 * @return value if address and port are valid.
58 * @throws IllegalStateException
59 * if address or port are invalid, or configuration is missing
61 private static Optional<InetSocketAddress> extractSomeNetconfAddress(BundleContext context,
63 String exceptionMessage,
67 address = context.getProperty(PREFIX_PROP + infixProp + CLIENT_PROP + ADDRESS_SUFFIX_PROP);
69 if (address == null || address.equals("")){
70 address = context.getProperty(PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP);
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);
79 portKey = PREFIX_PROP + infixProp + CLIENT_PROP + PORT_SUFFIX_PROP;
81 if (portKey == null || portKey.equals("")){
82 portKey = PREFIX_PROP + infixProp + PORT_SUFFIX_PROP;
84 String portString = context.getProperty(portKey);
85 checkNotNull(portString, "Netconf port must be specified in properties file with " + portKey);
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);