Adding TestVNF netconf server
[demo.git] / vnfs / TestVNF / netconftemplates / netconftemplates / send-event / NA / response.groovy
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 import com.ericsson.testvnf.server.models.RpcData;
22 import groovy.sql.Sql;
23 import groovy.util.XmlParser
24 import java.util.logging.Logger;
25 Logger logger = Logger.getLogger("")
26
27 println "send-event!"
28 def RpcData data = binding.getVariable("RpcData")
29 result = "ignore"
30 try{
31         def operationsData = new XmlParser().parseText(data.getOperationTagContent())
32         println "operations data::::" + operationsData
33         
34         def targetName
35         if (!operationsData.config.'target-name'.isEmpty()){
36                 targetName = operationsData.config.'target-name'.text()
37                 println "target-name ::::" + operationsData.config.'target-name'.text()
38
39                 logger.info("in the groovy file. going to open a database session.")
40                 def db = [url: "jdbc:mariadb://localhost:3306/netconf_db?useSSL=false",
41                                   user: "root", password: "root", driver: 'org.mariadb.jdbc.Driver']
42         //      def db = [url: "jdbc:mariadb://mariadb:3306/netconf_db?useSSL=false",
43         //                user: "root", password: "root", driver: 'org.mariadb.jdbc.Driver']
44                 def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)
45                 println "DB connection ready"
46                 logger.info("DB connection ready")
47                 def metadata = sql.connection.getMetaData()
48                 def tables = metadata.getTables(null, null, "AddressTable", null)
49                 def targetURL = ""
50                 if (!tables.next()) {
51                         logger.info("table not found");
52                         println "table not found"
53                         sql.execute("CREATE TABLE AddressTable (Name varchar(255), HTTPAddress varchar(255), PRIMARY KEY (Name))")
54                 }else{
55                         def query = "select HTTPAddress from AddressTable where Name=\"".concat(targetName).concat("\"")
56                         println "query" + query
57                         logger.info(query)
58                         sql.eachRow(query, { res ->
59                                 println "sending JSON data to "+res.HTTPAddress
60                                 targetURL = res.HTTPAddress
61                         })
62                 }
63                 println "targetURL:"+targetURL
64                 logger.info(targetURL)
65                 if(targetURL!="") {
66                         def post = new URL(targetURL).openConnection();
67                         def message = operationsData.content.text()
68                         post.setRequestMethod("POST")
69                         post.setDoOutput(true)
70                         post.setRequestProperty("Content-Type", "application/json")
71                         post.getOutputStream().write(message.getBytes("UTF-8"));
72                         def postRC = post.getResponseCode();
73                         println(postRC);
74                         if(postRC.equals(200)) {
75                                 println(post.getInputStream().getText());
76                                 result = "<rpc-reply message-id=\"<MID>\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"> <ok/> </rpc-reply>";
77                         }
78                 }else {
79                         println "No url found in database.."
80                         logger.info("no url found in database")
81                         result = "<rpc-reply message-id=\\\"<MID>\\\" xmlns=\\\"urn:ietf:params:xml:ns:netconf:base:1.0\\\"> <rpc-error><error-type>application</error-type><error-message xml:lang=\"en\">No url found in database</error-message></rpc-error></rpc-reply>"
82                 }
83         } else{
84                 logger.info("targetName not found");
85                 println("targetName not found")
86                 result = "<rpc-reply message-id=\\\"<MID>\\\" xmlns=\\\"urn:ietf:params:xml:ns:netconf:base:1.0\\\"> <rpc-error><error-type>application</error-type><error-message xml:lang=\"en\">No target name in request</error-message></rpc-error></rpc-reply>"
87         }
88 }
89 catch (Exception e)
90 {
91  e.printStackTrace();
92 }
93
94 return result;