c67675a91062780df065aedbf9f898abbf7df199
[netconf.git] / netconf / netconf-util / src / main / java / org / opendaylight / netconf / util / osgi / NetconfConfiguration.java
1 /*
2  * Copyright (c) 2016 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 io.netty.channel.local.LocalAddress;
12 import java.net.InetSocketAddress;
13 import java.util.Dictionary;
14 import java.util.concurrent.TimeUnit;
15 import org.osgi.service.cm.ManagedService;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public class NetconfConfiguration implements ManagedService {
20     private static final Logger LOG = LoggerFactory.getLogger(NetconfConfiguration.class);
21
22     /**
23      * Props to access information within the dictionary.
24      */
25
26     private static final String SSH_ADDRESS_PROP = "ssh-address";
27     private static final String SSH_PORT_PROP = "ssh-port";
28     private static final String TCP_ADDRESS_PROP = "tcp-address";
29     private static final String TCP_PORT_PROP = "tcp-port";
30     private static final String SSH_PK_PATH_PROP = "ssh-pk-path";
31
32     /**
33      * Default values used if no dictionary is provided.
34      */
35
36     public static final LocalAddress NETCONF_LOCAL_ADDRESS = new LocalAddress("netconf");
37     public static final long DEFAULT_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(30);
38
39     private static final String LOCAL_HOST = "127.0.0.1";
40     private static final String INADDR_ANY = "0.0.0.0";
41     private static final String DEFAULT_PRIVATE_KEY_PATH = "./configuration/RSA.pk";
42     private static final InetSocketAddress DEFAULT_TCP_SERVER_ADRESS = new InetSocketAddress(LOCAL_HOST, 8383);
43     private static final InetSocketAddress DEFAULT_SSH_SERVER_ADRESS = new InetSocketAddress(INADDR_ANY, 1830);
44
45     private NetconfConfigurationHolder netconfConfiguration;
46
47     NetconfConfiguration() {
48         netconfConfiguration = new NetconfConfigurationHolder(DEFAULT_TCP_SERVER_ADRESS,
49                 DEFAULT_SSH_SERVER_ADRESS, DEFAULT_PRIVATE_KEY_PATH);
50     }
51
52     @Override
53     public void updated(final Dictionary<String, ?> dictionaryConfig) {
54         if (dictionaryConfig == null) {
55             LOG.debug("CSS netconf server configuration cannot be updated as passed dictionary is null");
56             return;
57         }
58         final InetSocketAddress sshServerAddress =
59                 new InetSocketAddress((String) dictionaryConfig.get(SSH_ADDRESS_PROP),
60                         Integer.parseInt((String) dictionaryConfig.get(SSH_PORT_PROP)));
61         final InetSocketAddress tcpServerAddress =
62                 new InetSocketAddress((String) dictionaryConfig.get(TCP_ADDRESS_PROP),
63                 Integer.parseInt((String) dictionaryConfig.get(TCP_PORT_PROP)));
64
65         netconfConfiguration = new NetconfConfigurationHolder(tcpServerAddress,
66                 sshServerAddress,
67                 (String) dictionaryConfig.get(SSH_PK_PATH_PROP));
68
69         LOG.debug("CSS netconf server configuration was updated: {}", dictionaryConfig.toString());
70     }
71
72     public InetSocketAddress getSshServerAddress() {
73         return netconfConfiguration.getSshServerAddress();
74     }
75
76     public InetSocketAddress getTcpServerAddress() {
77         return netconfConfiguration.getTcpServerAddress();
78     }
79
80     public String getPrivateKeyPath() {
81         return netconfConfiguration.getPrivateKeyPath();
82     }
83 }