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";
27 public static InetSocketAddress extractTCPNetconfAddress(BundleContext context, String exceptionMessageIfNotFound, boolean forClient) {
29 Optional<InetSocketAddress> inetSocketAddressOptional = extractSomeNetconfAddress(context, InfixProp.tcp, exceptionMessageIfNotFound, forClient);
31 if (inetSocketAddressOptional.isPresent() == false) {
32 throw new IllegalStateException("Netconf tcp address not found." + exceptionMessageIfNotFound);
34 return inetSocketAddressOptional.get();
37 public static Optional<InetSocketAddress> extractSSHNetconfAddress(BundleContext context, String exceptionMessage) {
38 return extractSomeNetconfAddress(context, InfixProp.ssh, exceptionMessage, false);
43 * from which properties are being read.
46 * @return value if address and port are valid.
47 * @throws IllegalStateException
48 * if address or port are invalid, or configuration is missing
50 private static Optional<InetSocketAddress> extractSomeNetconfAddress(BundleContext context,
52 String exceptionMessage,
56 address = context.getProperty(PREFIX_PROP + infixProp + CLIENT_PROP + ADDRESS_SUFFIX_PROP);
58 if (address == null || address.equals("")){
59 address = context.getProperty(PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP);
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);
68 portKey = PREFIX_PROP + infixProp + CLIENT_PROP + PORT_SUFFIX_PROP;
70 if (portKey == null || portKey.equals("")){
71 portKey = PREFIX_PROP + infixProp + PORT_SUFFIX_PROP;
73 String portString = context.getProperty(portKey);
74 checkNotNull(portString, "Netconf port must be specified in properties file with " + portKey);
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);