2 * ============LICENSE_START=======================================================
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 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;
31 import java.io.IOException;
33 import java.util.Timer;
36 public class NetconfMonitorServiceConfiguration {
37 private static final Logger LOGGER = LogManager.getLogger(NetconfMonitorServiceConfiguration.class);
38 private static final Map<String, String> enviroment = System.getenv();
40 private static final String LOG_PATH = "/var/log";
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";
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";
52 private static final String DEFAULT_NETCONF_USER = "netconf";
53 private static final String DEFAULT_NETCONF_PASSWORD = "netconf";
56 public Timer timer() {
57 return new Timer("NetconfMonitorServiceTimer");
61 public NetconfConfigurationCache configurationCache() {
62 return new NetconfConfigurationCache();
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());
74 public NetconfConfigurationWriter netconfConfigurationWriter() {
75 return new NetconfConfigurationWriter(LOG_PATH);
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));
84 private NetconfConnectionParams createConnectionParams() {
85 return new NetconfConnectionParams(
86 enviroment.getOrDefault(NETCONF_ADDRESS, DEFAULT_NETCONF_ADDRESS),
89 DEFAULT_NETCONF_PASSWORD);
92 private int resolveNetconfPort() {
94 return Integer.parseInt(enviroment.get(NETCONF_PORT));
95 } catch (NumberFormatException e) {
96 return DEFAULT_NETCONF_PORT;