Adding TestVNF netconf server
[demo.git] / vnfs / TestVNF / netconfserver / src / main / java / com / ericsson / testvnf / server / helper / ActionHelper.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.OutputStream;
24 import java.util.Map;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import com.ericsson.testvnf.server.Server;
30 import com.ericsson.testvnf.server.models.NetconfMessage;
31 import com.ericsson.testvnf.server.models.RpcData;
32
33 /*
34  * The helper class which performs actions based on netconf requests
35  */
36 public class ActionHelper {
37         
38         private static final Log log = LogFactory.getLog(ActionHelper.class);
39         private OutputStream                                    out;
40         private Thread groovyCallerThread;
41         
42         public ActionHelper(OutputStream out){
43                 this.out = out;
44         }
45         
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);
55                         } else {
56                                 connectionResetMap.put(rpcData.getTargetName(), false);
57                         }
58                 }
59                 if (rpcData.getOperation().equals("\"close-session\"")) {
60                         sessionClosedStatus = true;
61                 }
62                 String operation = rpcData.getOperation();
63                 log.info("Received query");
64
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);
69                 } else {
70                         doActionForOperations(rpcData, operation, connectionResetMap);
71                 }
72                 return sessionClosedStatus;
73         }
74         
75         public void doActionForOperations(RpcData rpcData, String operation, Map<String, Boolean> connectionResetMap) {
76                 groovyCallerThread = new Thread("Groovy caller") {
77                         @Override
78                         public void run() {
79                                 rpcData.setTimeDelayForSendingEvents(Server.getTimeDelayForSendingEvents());
80                                 String result ="";
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);
88                                 }
89                         }
90                 };
91                 groovyCallerThread.start();
92                 log.info("groovyCallerThread started");
93         }
94         
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");
105                 }
106         }
107         
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.");
114         }
115
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();
121                 }
122                 
123         }
124 }