Adding TestVNF netconf server
[demo.git] / vnfs / TestVNF / netconfserver / src / main / java / com / ericsson / testvnf / server / Server.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package com.ericsson.testvnf.server;
22
23 import java.io.BufferedReader;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.util.Properties;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 import com.ericsson.testvnf.server.builder.ServerBuilder;
35
36 /**
37  * Netconf server using Apache MINA SSHD and netconf4j
38  * The SSHD server extends a netconf subsystem
39  * 
40  * @author Ajith Sreekumar
41  * 
42  */
43 public class Server {
44
45         private static final int DEFAULT_PORT = 2052;
46
47         private static final String DEFAULT_HOST = "0.0.0.0";
48
49         private static final Log log = LogFactory.getLog(Server.class);
50
51         private static String netconfTemplatesDirectory;
52
53         private  static String timeDelayForSendingEvents; // time delay in milliseconds
54         
55         public static String getNetconfTemplatesDirectory() {
56                 return netconfTemplatesDirectory;
57         }
58
59         public static void setNetconfTemplatesDirectory(String netconfTemplatesDirectory) {
60                 Server.netconfTemplatesDirectory = netconfTemplatesDirectory;
61         }
62
63         public static String getTimeDelayForSendingEvents() {
64                 return timeDelayForSendingEvents;
65         }
66
67         public static void setTimeDelayForSendingEvents(String timeDelayForSendingEvents) {
68                 Server.timeDelayForSendingEvents = timeDelayForSendingEvents;
69         }
70
71
72         public static void main(String[] args) throws IOException{
73                 setNetconfTemplatesDirectory(args[0]); // the directory which contains the groovy files to perform actions based on netconf requests, and also contains other netconf templates to be sent back to the client
74                 setTimeDelayForSendingEvents(args[1]); // the delay between sending each event by the VNF
75                 ServerBuilder serverBuilder = new ServerBuilder();
76                 
77                 Properties prop = new Properties();
78                 String host = DEFAULT_HOST;
79                 int port = DEFAULT_PORT;
80                 try {
81                         InputStream input = new FileInputStream(getNetconfTemplatesDirectory()+ "/netconftemplates/server-config.properties");
82                         log.info("Setting host and port from the properties file.");
83                         prop.load(input);
84                         if(null!=prop.getProperty("host"))
85                                 host = prop.getProperty("host");
86                         if(null!=prop.getProperty("port"))
87                                 port = Integer.parseInt(prop.getProperty("port"));
88                 }catch(FileNotFoundException e) {
89                         log.info("server-config.properties file not found. " + e);
90                         log.info("Using default host and port");
91
92                 }
93
94                 serverBuilder.initializeServer(host,port);
95                 serverBuilder.startServer();
96                 
97                 // read lines from input
98                 BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
99
100                 while (true) {
101                         if (buffer.readLine().equalsIgnoreCase("EXIT")) {
102                                 break;
103                         }
104                 }
105
106                 log.info("Exiting");
107                 serverBuilder.stopServer();
108                 System.exit(0);
109         }
110
111
112 }