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;
23 import java.io.OutputStream;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
29 import com.ericsson.testvnf.server.Server;
30 import com.ericsson.testvnf.server.models.NetconfMessage;
31 import com.ericsson.testvnf.server.models.RpcData;
34 * The helper class which performs actions based on netconf requests
36 public class ActionHelper {
38 private static final Log log = LogFactory.getLog(ActionHelper.class);
39 private OutputStream out;
40 private Thread groovyCallerThread;
42 public ActionHelper(OutputStream out){
46 public boolean doActionForRPCRequest(NetconfMessage netconfMessage, Map<String, Boolean> connectionResetMap, boolean sessionClosedStatus){
47 // if the client is sending an rpc request for any protocol operation
48 RpcData rpcData = (RpcData) netconfMessage;
49 if (null != rpcData.getTargetName()) {
50 // the connectionResetMap is a shared between multiple client connections coming in for a single netconf server instance
51 // true value for a target indicates that sending events to that target needs to be stopped
52 // edit-config operation with a target determines the termination of sending events to that target.
53 if (rpcData.getOperation().equals("edit-config")) {
54 connectionResetMap.put(rpcData.getTargetName(), true);
56 connectionResetMap.put(rpcData.getTargetName(), false);
59 if (rpcData.getOperation().equals("\"close-session\"")) {
60 sessionClosedStatus = true;
62 String operation = rpcData.getOperation();
63 log.info("Received query");
65 // on getting a get-schema request from client, the server sends back the yang
66 // model of the particular functionality/capability asked for.
67 if (operation.equals("get-schema")) {
68 doActionForGetSchemaOperation(rpcData);
70 doActionForOperations(rpcData, operation, connectionResetMap);
72 return sessionClosedStatus;
75 public void doActionForOperations(RpcData rpcData, String operation, Map<String, Boolean> connectionResetMap) {
76 groovyCallerThread = new Thread("Groovy caller") {
79 rpcData.setTimeDelayForSendingEvents(Server.getTimeDelayForSendingEvents());
81 // pick up and execute the correct groovy file based on the operation and configuration datastore identified from the rpc request
82 result = HelperUtils.executeGroovy(Server.getNetconfTemplatesDirectory()
83 + "/netconftemplates/"+operation+"/"+rpcData.getConfigurationDatastore()+"/response.groovy", rpcData, connectionResetMap);
84 if (!result.equals("ignore")) {
85 result = result.replaceAll("<MID>", rpcData.getMessageId());
86 HelperUtils.sendAsBytesToOutput(result.getBytes(), out);
87 log.info("Finished writing " + result);
91 groovyCallerThread.start();
92 log.info("groovyCallerThread started");
95 public void doActionForGetSchemaOperation(RpcData rpcData){
96 log.info("get-schema received.");
97 if (null != rpcData.getSchemaDetails()
98 && null != rpcData.getSchemaDetails().getIdentifier()) {
99 log.info("Sending schema for capability...");
100 String schemaString = HelperUtils.readFile(Server.getNetconfTemplatesDirectory()
101 + "/netconftemplates/"+rpcData.getSchemaDetails().getIdentifier()+"-schema.yang"); // the yang model has to be in the netconftemplates directory with the proper naming format : '<operation>-schema.yang'
102 String schemaStringValue = schemaString.replaceAll("<MID>", rpcData.getMessageId()); // Put the correct message id in the response as it is in the request.
103 HelperUtils.sendAsBytesToOutput(schemaStringValue.getBytes(), out);
104 log.info("Finished writing the schema information");
108 // the method sends a hello message to the client, hello.xml in the netconftemplates directory is used to send the hello message
109 public void sendHelloMessage(){
110 log.info("Send hello message.");
111 String helloMessage = HelperUtils.readFile(Server.getNetconfTemplatesDirectory()+"/netconftemplates/hello.xml");
112 HelperUtils.sendAsBytesToOutput(helloMessage.getBytes(), out);
113 log.info("Hello message sent.");
116 public void interruptGroovyCallerThread() {
117 // kill thread if it don't finish naturally
118 if (groovyCallerThread!=null && groovyCallerThread.isAlive()) {
119 log.info("Killing groovy caller thread");
120 groovyCallerThread.interrupt();