Bug 6023 - Adress for config subsystem netconf endpoint is not configurable
[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 java.net.InetSocketAddress;
12 import java.util.Dictionary;
13 import org.osgi.service.cm.ManagedService;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 public class NetconfConfiguration implements ManagedService {
18     private static final Logger LOG = LoggerFactory.getLogger(NetconfConfiguration.class);
19
20     private static final NetconfConfiguration instance = new NetconfConfiguration();
21     private NetconfConfigurationHolder netconfConfiguration;
22
23     public static final String KEY_SSH_ADDRESS = "ssh-address";
24     public static final String KEY_SSH_PORT = "ssh-port";
25     public static final String KEY_TCP_ADDRESS = "tcp-address";
26     public static final String KEY_TCP_PORT = "tcp-port";
27     public static final String KEY_SSH_PK_PATH = "ssh-pk-path";
28
29     public static NetconfConfiguration getInstance() {
30         return instance;
31     }
32
33     private NetconfConfiguration() {
34         netconfConfiguration = new NetconfConfigurationHolder(NetconfConfigUtil.DEFAULT_TCP_SERVER_ADRESS,
35                 NetconfConfigUtil.DEFAULT_SSH_SERVER_ADRESS, NetconfConfigUtil.DEFAULT_PRIVATE_KEY_PATH);
36     }
37
38     @Override
39     public void updated(final Dictionary<String, ?> dictionaryConfig) {
40         if (dictionaryConfig == null) {
41             LOG.warn("Netconf configuration cannot be updated.");
42             return;
43         }
44         final InetSocketAddress sshServerAddress = new InetSocketAddress((String) dictionaryConfig.get(KEY_SSH_ADDRESS),
45                 Integer.parseInt((String) dictionaryConfig.get(KEY_SSH_PORT)));
46         final InetSocketAddress tcpServerAddress = new InetSocketAddress((String) dictionaryConfig.get(KEY_TCP_ADDRESS),
47                 Integer.parseInt((String) dictionaryConfig.get(KEY_TCP_PORT)));
48
49         netconfConfiguration = new NetconfConfigurationHolder(tcpServerAddress, sshServerAddress,
50                 (String) dictionaryConfig.get(KEY_SSH_PK_PATH));
51
52         LOG.info("Netconf configuration was updated: {}", dictionaryConfig.toString());
53     }
54
55     public InetSocketAddress getSshServerAddress(){
56         return netconfConfiguration.getSshServerAddress();
57     }
58
59     public InetSocketAddress getTcpServerAddress(){
60         return netconfConfiguration.getTcpServerAddress();
61     }
62
63     public String getPrivateKeyPath() {
64         return netconfConfiguration.getPrivateKeyPath();
65     }
66 }