b0965d42b7aec00601a1e45fdb817110b532257d
[integration.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.pnfsimulator.netconfmonitor;
22
23 import com.tailf.jnc.JNCException;
24 import com.tailf.jnc.NetconfSession;
25 import com.tailf.jnc.SSHConnection;
26 import com.tailf.jnc.SSHSession;
27 import java.io.IOException;
28 import java.util.Map;
29 import java.util.Timer;
30 import org.onap.pnfsimulator.netconfmonitor.netconf.NetconfConfigurationCache;
31 import org.onap.pnfsimulator.netconfmonitor.netconf.NetconfConfigurationReader;
32 import org.onap.pnfsimulator.netconfmonitor.netconf.NetconfConfigurationWriter;
33 import org.onap.pnfsimulator.netconfmonitor.netconf.NetconfConnectionParams;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.context.annotation.Bean;
37 import org.springframework.context.annotation.Configuration;
38
39 @Configuration
40 public class NetconfMonitorServiceConfiguration {
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(NetconfMonitorServiceConfiguration.class);
43     private static final Map<String, String> enviroment = System.getenv();
44
45     private static final String LOG_PATH = "/var/log";
46
47     private static final String NETCONF_ADDRESS = "NETCONF_ADDRESS";
48     private static final String NETCONF_PORT = "NETCONF_PORT";
49     private static final String NETCONF_MODEL = "NETCONF_MODEL";
50     private static final String NETCONF_MAIN_CONTAINER = "NETCONF_MAIN_CONTAINER";
51
52     private static final String DEFAULT_NETCONF_ADDRESS = "localhost";
53     private static final int DEFAULT_NETCONF_PORT = 830;
54     private static final String DEFAULT_NETCONF_MODEL = "pnf-simulator";
55     private static final String DEFAULT_NETCONF_MAIN_CONTAINER = "config";
56
57     private static final String DEFAULT_NETCONF_USER = "netconf";
58     private static final String DEFAULT_NETCONF_PASSWORD = "netconf";
59
60     @Bean
61     public Timer timer() {
62         return new Timer("NetconfMonitorServiceTimer");
63     }
64
65     @Bean
66     public NetconfConfigurationCache configurationCache() {
67         return new NetconfConfigurationCache();
68     }
69
70     @Bean
71     public NetconfConfigurationReader configurationReader() throws IOException, JNCException {
72         NetconfConnectionParams params = resolveConnectionParams();
73         LOGGER.info("Configuration params are : {}", params);
74         NetconfSession session = createNetconfSession(params);
75         return new NetconfConfigurationReader(session, buildModelPath());
76     }
77
78     NetconfSession createNetconfSession(NetconfConnectionParams params) throws IOException, JNCException {
79         SSHConnection sshConnection = new SSHConnection(params.address, params.port);
80         sshConnection.authenticateWithPassword(params.user, params.password);
81         return new NetconfSession( new SSHSession(sshConnection));
82     }
83
84     @Bean
85     public NetconfConfigurationWriter netconfConfigurationWriter() {
86         return new NetconfConfigurationWriter(LOG_PATH);
87     }
88
89     private String buildModelPath() {
90         return String.format("/%s:%s",
91             enviroment.getOrDefault(NETCONF_MODEL, DEFAULT_NETCONF_MODEL),
92             enviroment.getOrDefault(NETCONF_MAIN_CONTAINER, DEFAULT_NETCONF_MAIN_CONTAINER));
93     }
94
95     NetconfConnectionParams resolveConnectionParams() {
96         return new NetconfConnectionParams(
97             enviroment.getOrDefault(NETCONF_ADDRESS, DEFAULT_NETCONF_ADDRESS),
98             resolveNetconfPort(),
99             DEFAULT_NETCONF_USER,
100             DEFAULT_NETCONF_PASSWORD);
101     }
102
103     private int resolveNetconfPort() {
104         try {
105             return Integer.parseInt(enviroment.get(NETCONF_PORT));
106         } catch (NumberFormatException e) {
107             LOGGER.warn("Invalid netconf port: {}. Default netconf port {} is set.", e.getMessage(),
108                 DEFAULT_NETCONF_PORT);
109             return DEFAULT_NETCONF_PORT;
110         }
111     }
112 }