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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package com.ericsson.testvnf.server.helper;
24 import java.io.IOException;
25 import java.io.OutputStream;
26 import java.nio.file.Files;
27 import java.nio.file.Paths;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
33 import com.ericsson.testvnf.server.models.NetconfMessage;
35 import groovy.lang.Binding;
36 import groovy.util.GroovyScriptEngine;
41 public class HelperUtils{
43 private static final Log log = LogFactory.getLog(HelperUtils.class);
45 private HelperUtils() {
49 // executes the groovy file specified in the path
50 public static String executeGroovy(String groovyFilePath, NetconfMessage data, Map<String, Boolean> connectionResetMap) {
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);
65 return result.toString();
68 // send bytes to output stream
69 public static void sendAsBytesToOutput(byte[] buffer, OutputStream out){
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);
78 log.info("Error while sending response message as bytes: "+e);
82 // the method is used to read the contents of the file specified
83 public static String readFile(String filename) {
84 String fileAsString= "";
86 fileAsString = new String(Files.readAllBytes(Paths.get(filename)));
87 } catch (IOException e) {
88 log.error("Error reading file: "+ filename);