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";
26 public static InetSocketAddress extractTCPNetconfAddress(BundleContext context, String exceptionMessageIfNotFound) {
28 Optional<InetSocketAddress> inetSocketAddressOptional = extractSomeNetconfAddress(context, InfixProp.tcp, exceptionMessageIfNotFound);
30 if (inetSocketAddressOptional.isPresent() == false) {
31 throw new IllegalStateException("Netconf tcp address not found." + exceptionMessageIfNotFound);
33 return inetSocketAddressOptional.get();
36 public static Optional<InetSocketAddress> extractSSHNetconfAddress(BundleContext context, String exceptionMessage) {
37 return extractSomeNetconfAddress(context, InfixProp.ssh, exceptionMessage);
42 * from which properties are being read.
45 * @return value if address and port are valid.
46 * @throws IllegalStateException
47 * if address or port are invalid, or configuration is missing
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);
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);
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);