Merge "Add blueprint wiring for netconf-console"
[netconf.git] / netconf / netconf-util / src / main / java / org / opendaylight / 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.netconf.util.osgi;
10
11 import com.google.common.base.Optional;
12 import io.netty.channel.local.LocalAddress;
13 import java.net.InetSocketAddress;
14 import java.util.Collection;
15 import java.util.concurrent.TimeUnit;
16 import org.osgi.framework.BundleContext;
17 import org.osgi.framework.InvalidSyntaxException;
18 import org.osgi.framework.ServiceReference;
19 import org.osgi.service.cm.ManagedService;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public final class NetconfConfigUtil {
24     private static final Logger LOG = LoggerFactory.getLogger(NetconfConfigUtil.class);
25
26     private static final String PREFIX_PROP = "netconf.";
27
28     private NetconfConfigUtil() {
29     }
30
31     public enum InfixProp {
32         tcp, ssh
33     }
34
35     private static final String PORT_SUFFIX_PROP = ".port";
36     private static final String ADDRESS_SUFFIX_PROP = ".address";
37     private static final String PRIVATE_KEY_PATH_PROP = ".pk.path";
38
39     private static final String CONNECTION_TIMEOUT_MILLIS_PROP = "connectionTimeoutMillis";
40     private static final String LOCAL_HOST = "127.0.0.1";
41     private static final String INADDR_ANY = "0.0.0.0";
42     public static final long DEFAULT_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(30);
43     private static final LocalAddress NETCONF_LOCAL_ADDRESS = new LocalAddress("netconf");
44     public static final String DEFAULT_PRIVATE_KEY_PATH = "./configuration/RSA.pk";
45     public static final InetSocketAddress DEFAULT_TCP_SERVER_ADRESS = new InetSocketAddress(LOCAL_HOST, 8383);
46     public static final InetSocketAddress DEFAULT_SSH_SERVER_ADRESS = new InetSocketAddress(INADDR_ANY, 1830);
47
48     public static LocalAddress getNetconfLocalAddress() {
49         return NETCONF_LOCAL_ADDRESS;
50     }
51
52     public static long extractTimeoutMillis(final BundleContext bundleContext) {
53         final String key = PREFIX_PROP + CONNECTION_TIMEOUT_MILLIS_PROP;
54         final String timeoutString = bundleContext.getProperty(key);
55         if (timeoutString == null || timeoutString.length() == 0) {
56             return DEFAULT_TIMEOUT_MILLIS;
57         }
58         try {
59             return Long.parseLong(timeoutString);
60         } catch (final NumberFormatException e) {
61             LOG.warn("Cannot parse {} property: {}, using defaults", key, timeoutString, e);
62             return DEFAULT_TIMEOUT_MILLIS;
63         }
64     }
65
66     /**
67      * @param context from which properties are being read.
68      * @return value of private key path if value is present, Optional.absent otherwise
69      */
70     public static Optional<String> getPrivateKeyPath(final BundleContext context) {
71         return getProperty(context, getPrivateKeyKey());
72     }
73
74     public static String getPrivateKeyKey() {
75         return PREFIX_PROP + InfixProp.ssh + PRIVATE_KEY_PATH_PROP;
76     }
77
78     public static String getNetconfServerAddressKey(final InfixProp infixProp) {
79         return PREFIX_PROP + infixProp + ADDRESS_SUFFIX_PROP;
80     }
81
82     /**
83      * @param context   from which properties are being read.
84      * @param infixProp either tcp or ssh
85      * @return value if address and port are present and valid, Optional.absent otherwise.
86      */
87     public static Optional<InetSocketAddress> extractNetconfServerAddress(final BundleContext context,
88                                                                            final InfixProp infixProp) {
89
90         final Optional<String> address = getProperty(context, getNetconfServerAddressKey(infixProp));
91         final Optional<String> port = getProperty(context, PREFIX_PROP + infixProp + PORT_SUFFIX_PROP);
92
93         if (address.isPresent() && port.isPresent()) {
94             try {
95                 return Optional.of(parseAddress(address, port));
96             } catch (final IllegalArgumentException | SecurityException e) {
97                 LOG.warn("Unable to parse {} netconf address from {}:{}, fallback to default",
98                         infixProp, address, port, e);
99             }
100         }
101         return Optional.absent();
102     }
103
104     private static InetSocketAddress parseAddress(final Optional<String> address, final Optional<String> port) {
105         final int portNumber = Integer.valueOf(port.get());
106         return new InetSocketAddress(address.get(), portNumber);
107     }
108
109     private static Optional<String> getProperty(final BundleContext context, final String propKey) {
110         String value = context.getProperty(propKey);
111         if (value != null && value.isEmpty()) {
112             value = null;
113         }
114         return Optional.fromNullable(value);
115     }
116
117     public static java.util.Optional<NetconfConfiguration> getNetconfConfigurationService(BundleContext bundleContext) {
118         final Collection<ServiceReference<ManagedService>> serviceReferences;
119         try {
120             serviceReferences = bundleContext.getServiceReferences(ManagedService.class, null);
121             for (final ServiceReference<ManagedService> serviceReference : serviceReferences) {
122                 ManagedService service = bundleContext.getService(serviceReference);
123                 if (service instanceof NetconfConfiguration){
124                     return java.util.Optional.of((NetconfConfiguration) service);
125                 }
126             }
127         } catch (InvalidSyntaxException e) {
128             LOG.error("Unable to retrieve references for ManagedService: {}", e);
129         }
130         LOG.error("Unable to retrieve NetconfConfiguration service. Not found. Bundle netconf-util probably failed.");
131         return java.util.Optional.empty();
132     }
133 }