Adding TestVNF netconf server
[demo.git] / vnfs / TestVNF / netconfserver / src / main / java / com / ericsson / testvnf / server / helper / HelperUtils.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.helper;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.OutputStream;
26 import java.nio.file.Files;
27 import java.nio.file.Paths;
28 import java.util.Map;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import com.ericsson.testvnf.server.models.NetconfMessage;
34
35 import groovy.lang.Binding;
36 import groovy.util.GroovyScriptEngine;
37
38 /*
39  * The utils class
40  */
41 public class HelperUtils{
42         
43         private static final Log log = LogFactory.getLog(HelperUtils.class);
44         
45         private HelperUtils() {
46                 super();
47         }
48
49         // executes the groovy file specified in the path
50         public static String executeGroovy(String groovyFilePath, NetconfMessage data, Map<String, Boolean> connectionResetMap) {
51
52                 Object result = "";
53                 try {
54                         log.info("groovy path------" + groovyFilePath);
55                         File file = new File(groovyFilePath);
56                         GroovyScriptEngine engine = new GroovyScriptEngine(file.getParent());
57                         Binding binding = new Binding();
58                         binding.setVariable("RpcData", data);
59                         binding.setVariable("connectionResetMap", connectionResetMap);
60                         log.info("binding " + binding + "  " + file.getParent() + " " + file.getName());
61                         result = engine.run(file.getName(), binding);
62                 } catch (Exception e) {
63                         log.error("Exception while trying to execute groovy file", e);
64                 }
65                 return result.toString();
66         }
67         
68         // send bytes to output stream
69         public static void sendAsBytesToOutput(byte[] buffer, OutputStream out){
70                 try {
71                         log.info("Sending message as bytes..\n");
72                         int len = buffer.length;
73                         out.write(buffer, 0, len);
74                         String tail = "]]>]]>";
75                         out.write(tail.getBytes(), 0, tail.getBytes().length);
76                         out.flush();
77                 }catch(Exception e) {
78                         log.info("Error while sending response message as bytes: "+e);
79                 }
80         }
81         
82         // the method is used to read the contents of the file specified
83         public static String readFile(String filename) {
84                 String fileAsString= "";
85                 try {
86                         fileAsString = new String(Files.readAllBytes(Paths.get(filename)));
87                 } catch (IOException e) {
88                         log.error("Error reading file: "+ filename);
89                 }
90                 return fileAsString;
91         }
92 }