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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.pnfsimulator.netconfmonitor;
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;
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;
40 public class NetconfMonitorServiceConfiguration {
42 private static final Logger LOGGER = LoggerFactory.getLogger(NetconfMonitorServiceConfiguration.class);
43 private static final Map<String, String> enviroment = System.getenv();
45 private static final String LOG_PATH = "/var/log";
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";
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";
57 private static final String DEFAULT_NETCONF_USER = "netconf";
58 private static final String DEFAULT_NETCONF_PASSWORD = "netconf";
61 public Timer timer() {
62 return new Timer("NetconfMonitorServiceTimer");
66 public NetconfConfigurationCache configurationCache() {
67 return new NetconfConfigurationCache();
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());
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));
85 public NetconfConfigurationWriter netconfConfigurationWriter() {
86 return new NetconfConfigurationWriter(LOG_PATH);
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));
95 NetconfConnectionParams resolveConnectionParams() {
96 return new NetconfConnectionParams(
97 enviroment.getOrDefault(NETCONF_ADDRESS, DEFAULT_NETCONF_ADDRESS),
100 DEFAULT_NETCONF_PASSWORD);
103 private int resolveNetconfPort() {
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;