Adding TestVNF netconf server
[demo.git] / vnfs / TestVNF / netconftemplates / netconftemplates / edit-config / candidate / 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 java.util.concurrent.TimeUnit;
23 import groovy.sql.Sql
24 import groovy.util.XmlParser
25 import java.util.logging.Logger;
26 Logger logger = Logger.getLogger("")
27 println "edit config - candidate!"
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         def targetAddress
36         
37         if (!operationsData.config.'target-name'.isEmpty()){
38                 targetName = operationsData.config.'target-name'.text()
39                 println "target-name ::::" + operationsData.config.'target-name'.text()
40         } else{
41                 logger.info("targetName not found");
42                 println("targetName not found")
43         }
44         if (!operationsData.config.'target-address'.isEmpty()){
45                 targetAddress = operationsData.config.'target-address'.text()
46                 println "target-address ::::" + operationsData.config.'target-address'.text()
47         } else{
48                 logger.info("targetAddress not found");
49                 println ("targetAddress not found");
50         }
51
52         def db = [url: "jdbc:mariadb://localhost:3306/netconf_db?useSSL=false",
53                   user: "root", password: "root", driver: 'org.mariadb.jdbc.Driver']
54 //      def db = [url: "jdbc:mariadb://mariadb:3306/netconf_db?useSSL=false",
55 //                user: "root", password: "root", driver: 'org.mariadb.jdbc.Driver']
56         def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)
57         println "DB connection ready"
58         logger.info("DB connection ready")
59         if (targetName != null && targetAddress != null){
60                 def metadata = sql.connection.getMetaData()
61                 def tables = metadata.getTables(null, null, "AddressTable", null)
62                 if (!tables.next()) {
63                         logger.info("table not found");
64                         println "table not found"
65                         sql.execute("CREATE TABLE AddressTable (Name varchar(255), HTTPAddress varchar(255), PRIMARY KEY (Name))")
66                 }
67                 def query = "INSERT INTO AddressTable (NAME, HTTPAddress) VALUES(\""+targetName+"\",\""+targetAddress+"\") ON DUPLICATE KEY UPDATE Name=\""+targetName+"\", HTTPAddress=\""+targetAddress+"\""
68                 println "query" + query
69                 logger.info(query)
70                 sql.execute(query)
71                 result = "<rpc-reply message-id=\"<MID>\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"> <ok/> </rpc-reply>"
72         }else{
73          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\">edit-conf failed. Target name or address not found in request.</error-message></rpc-error></rpc-reply>"
74         }
75 }
76 catch (Exception e)
77 {
78  e.printStackTrace();
79  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\">edit-conf failed</error-message></rpc-error></rpc-reply>"
80 }
81
82
83 return result;