Move netconf-dom-api
[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 package org.opendaylight.netconf.util;
9
10 import java.lang.annotation.Annotation;
11 import java.net.InetSocketAddress;
12 import org.osgi.service.component.annotations.Activate;
13 import org.osgi.service.component.annotations.Component;
14 import org.osgi.service.component.annotations.Deactivate;
15 import org.osgi.service.metatype.annotations.AttributeDefinition;
16 import org.osgi.service.metatype.annotations.Designate;
17 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 @Component(immediate = true, configurationPid = "netconf")
22 @Designate(ocd = NetconfConfigurationImpl.Configuration.class)
23 public class NetconfConfigurationImpl implements NetconfConfiguration {
24     @ObjectClassDefinition
25     public @interface Configuration {
26         @AttributeDefinition(name = "tcp-address")
27         String tcpAddress() default "127.0.0.1";
28         @AttributeDefinition(name = "tcp-port", min = "0", max = "65535")
29         int tcpPort() default 8383;
30         @AttributeDefinition(name = "ssh-address")
31         String sshAddress() default "0.0.0.0";
32         @AttributeDefinition(name = "ssh-port", min = "0", max = "65535")
33         int sshPort() default 1830;
34         @AttributeDefinition(name = "sshpk-path")
35         String sshPrivateKeyPath() default "./configuration/RSA.pk";
36     }
37
38     private static final Logger LOG = LoggerFactory.getLogger(NetconfConfigurationImpl.class);
39
40     private NetconfConfigurationHolder netconfConfiguration;
41
42     public NetconfConfigurationImpl() {
43         // for DI
44     }
45
46     public NetconfConfigurationImpl(final String tcpServerAddress, final int tcpServerPort,
47                                     final String sshServerAddress, final int sshServerPort,
48                                     final String privateKeyPath) {
49         activate(new Configuration() {
50             @Override
51             public Class<? extends Annotation> annotationType() {
52                 return Configuration.class;
53             }
54
55             @Override
56             public String tcpAddress() {
57                 return tcpServerAddress;
58             }
59
60             @Override
61             public int tcpPort() {
62                 return tcpServerPort;
63             }
64
65             @Override
66             public String sshAddress() {
67                 return sshServerAddress;
68             }
69
70             @Override
71             public int sshPort() {
72                 return sshServerPort;
73             }
74
75             @Override
76             public String sshPrivateKeyPath() {
77                 return privateKeyPath;
78             }
79         });
80     }
81
82     @Activate
83     void activate(final Configuration config) {
84         final InetSocketAddress sshServerAddress = new InetSocketAddress(config.sshAddress(), config.sshPort());
85         final InetSocketAddress tcpServerAddress = new InetSocketAddress(config.tcpAddress(), config.tcpPort());
86
87         netconfConfiguration = new NetconfConfigurationHolder(tcpServerAddress, sshServerAddress,
88             config.sshPrivateKeyPath());
89         LOG.debug("CSS netconf server configuration was updated");
90     }
91
92     @Deactivate
93     void deactivate(final Configuration config) {
94         netconfConfiguration = null;
95     }
96
97     @Override
98     public InetSocketAddress getSshServerAddress() {
99         return netconfConfiguration.getSshServerAddress();
100     }
101
102     @Override
103     public InetSocketAddress getTcpServerAddress() {
104         return netconfConfiguration.getTcpServerAddress();
105     }
106
107     @Override
108     public String getPrivateKeyPath() {
109         return netconfConfiguration.getPrivateKeyPath();
110     }
111 }