Add netconf support to pnfsimulator.
[integration.git] / test / mocks / pnfsimulator / src / main / java / org / onap / pnfsimulator / netconfmonitor / NetconfMonitorServiceConfiguration.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.integration
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 org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
27 import org.onap.pnfsimulator.netconfmonitor.netconf.*;
28 import org.springframework.context.annotation.Bean;
29 import org.springframework.context.annotation.Configuration;
30
31 import java.io.IOException;
32 import java.util.Map;
33 import java.util.Timer;
34
35 @Configuration
36 public class NetconfMonitorServiceConfiguration {
37     private static final Logger LOGGER = LogManager.getLogger(NetconfMonitorServiceConfiguration.class);
38     private static final Map<String, String> enviroment = System.getenv();
39
40     private static final String LOG_PATH = "/var/log";
41
42     private static final String NETCONF_ADDRESS = "NETCONF_ADDRESS";
43     private static final String NETCONF_PORT = "NETCONF_PORT";
44     private static final String NETCONF_MODEL = "NETCONF_MODEL";
45     private static final String NETCONF_MAIN_CONTAINER = "NETCONF_MAIN_CONTAINER";
46
47     private static final String DEFAULT_NETCONF_ADDRESS = "netopeer";
48     private static final int DEFAULT_NETCONF_PORT = 830;
49     private static final String DEFAULT_NETCONF_MODEL = "pnf-simulator";
50     private static final String DEFAULT_NETCONF_MAIN_CONTAINER = "config";
51
52     private static final String DEFAULT_NETCONF_USER = "netconf";
53     private static final String DEFAULT_NETCONF_PASSWORD = "netconf";
54
55     @Bean
56     public Timer timer() {
57         return new Timer("NetconfMonitorServiceTimer");
58     }
59
60     @Bean
61     public NetconfConfigurationCache configurationCache() {
62         return new NetconfConfigurationCache();
63     }
64
65     @Bean
66     public NetconfConfigurationReader configurationReader() throws IOException, JNCException {
67         NetconfConnectionParams params = createConnectionParams();
68         LOGGER.info("Configuration params are : {}", params);
69         NetconfSession session = NetconfSessionFactory.create(params);
70         return new NetconfConfigurationReader(session, buildModelPath());
71     }
72
73     @Bean
74     public NetconfConfigurationWriter netconfConfigurationWriter() {
75         return new NetconfConfigurationWriter(LOG_PATH);
76     }
77
78     private String buildModelPath() {
79         return String.format("/%s:%s",
80                 enviroment.getOrDefault(NETCONF_MODEL, DEFAULT_NETCONF_MODEL),
81                 enviroment.getOrDefault(NETCONF_MAIN_CONTAINER, DEFAULT_NETCONF_MAIN_CONTAINER));
82     }
83
84     private NetconfConnectionParams createConnectionParams() {
85         return new NetconfConnectionParams(
86                 enviroment.getOrDefault(NETCONF_ADDRESS, DEFAULT_NETCONF_ADDRESS),
87                 resolveNetconfPort(),
88                 DEFAULT_NETCONF_USER,
89                 DEFAULT_NETCONF_PASSWORD);
90     }
91
92     private int resolveNetconfPort() {
93         try {
94             return Integer.parseInt(enviroment.get(NETCONF_PORT));
95         } catch (NumberFormatException e) {
96             return DEFAULT_NETCONF_PORT;
97         }
98     }
99 }