42c7b58871681786f58aece834783342ffad4ab9
[netconf.git] / netconf / netconf-util / src / main / java / org / opendaylight / netconf / util / NetconfConfigurationImpl.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;
10
11 import java.net.InetSocketAddress;
12 import java.util.Dictionary;
13 import java.util.Hashtable;
14 import org.osgi.service.cm.ManagedService;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 public class NetconfConfigurationImpl implements NetconfConfiguration, ManagedService {
19
20     private static final Logger LOG = LoggerFactory.getLogger(NetconfConfigurationImpl.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     private NetconfConfigurationHolder netconfConfiguration;
33
34     public NetconfConfigurationImpl(final String tcpServerAddress, final String tcpServerPort,
35                                     final String sshServerAddress, final String sshServerPort,
36                                     final String privateKeyPath) throws NumberFormatException {
37
38         // isolate configuration to "updated(...)" instead of repeating logic here
39         final Dictionary<String, String> dictionaryConfig = new Hashtable<>();
40         dictionaryConfig.put(TCP_ADDRESS_PROP, tcpServerAddress);
41         dictionaryConfig.put(TCP_PORT_PROP, tcpServerPort);
42         dictionaryConfig.put(SSH_ADDRESS_PROP, sshServerAddress);
43         dictionaryConfig.put(SSH_PORT_PROP, sshServerPort);
44         dictionaryConfig.put(SSH_PK_PATH_PROP, privateKeyPath);
45
46         updated(dictionaryConfig);
47     }
48
49     @Override
50     public void updated(final Dictionary<String, ?> dictionaryConfig) {
51         if (dictionaryConfig == null) {
52             LOG.debug("CSS NETCONF server configuration cannot be updated as passed dictionary is null");
53             return;
54         }
55         final InetSocketAddress sshServerAddress =
56                 new InetSocketAddress((String) dictionaryConfig.get(SSH_ADDRESS_PROP),
57                         Integer.parseInt((String) dictionaryConfig.get(SSH_PORT_PROP)));
58         final InetSocketAddress tcpServerAddress =
59                 new InetSocketAddress((String) dictionaryConfig.get(TCP_ADDRESS_PROP),
60                 Integer.parseInt((String) dictionaryConfig.get(TCP_PORT_PROP)));
61
62         netconfConfiguration = new NetconfConfigurationHolder(tcpServerAddress,
63                 sshServerAddress,
64                 (String) dictionaryConfig.get(SSH_PK_PATH_PROP));
65
66         LOG.debug("CSS netconf server configuration was updated: {}", dictionaryConfig.toString());
67     }
68
69     @Override
70     public InetSocketAddress getSshServerAddress() {
71         return netconfConfiguration.getSshServerAddress();
72     }
73
74     @Override
75     public InetSocketAddress getTcpServerAddress() {
76         return netconfConfiguration.getTcpServerAddress();
77     }
78
79     @Override
80     public String getPrivateKeyPath() {
81         return netconfConfiguration.getPrivateKeyPath();
82     }
83 }