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 import com.ericsson.testvnf.server.models.RpcData;
23 import java.util.concurrent.TimeUnit;
24 import groovy.sql.Sql;
25 import groovy.util.XmlParser
26 import java.util.logging.Logger;
27 import groovy.json.JsonSlurper
28 import groovy.json.JsonOutput
30 Logger logger = Logger.getLogger("")
32 println "send-models!"
33 def RpcData data = binding.getVariable("RpcData")
34 Object connectionResetMap = binding.getVariable("connectionResetMap")
35 println "connectionResetMap:" + connectionResetMap.get(data.getTargetName())
38 def operationsData = new XmlParser().parseText(data.getOperationTagContent())
39 println "operations data::::" + operationsData
42 if (!operationsData.config.'target-name'.isEmpty()){
43 targetName = operationsData.config.'target-name'.text()
44 println "target-name ::::" + operationsData.config.'target-name'.text()
46 logger.info("in the groovy file. going to open a database session.")
47 def db = [url: "jdbc:mariadb://localhost:3306/netconf_db?useSSL=false",
48 user: "root", password: "root", driver: 'org.mariadb.jdbc.Driver']
49 // def db = [url: "jdbc:mariadb://mariadb:3306/netconf_db?useSSL=false",
50 // user: "root", password: "root", driver: 'org.mariadb.jdbc.Driver']
51 def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)
52 println "DB connection ready"
53 logger.info("DB connection ready")
54 def metadata = sql.connection.getMetaData()
55 def tables = metadata.getTables(null, null, "AddressTable", null)
58 logger.info("table not found");
59 println "table not found"
60 sql.execute("CREATE TABLE AddressTable (Name varchar(255), HTTPAddress varchar(255), PRIMARY KEY (Name))")
62 def query = "select HTTPAddress from AddressTable where Name=\"".concat(targetName).concat("\"")
63 println "query" + query
65 sql.eachRow(query, { res ->
66 println "sending JSON data to "+res.HTTPAddress
67 targetURL = res.HTTPAddress
70 println "targetURL:"+targetURL
71 logger.info(targetURL)
73 def url = url = new URL(targetURL)
75 def message = operationsData.content.text()
76 def jsonSlurper = new JsonSlurper()
77 Object jsonData = jsonSlurper.parseText(message)
78 for (i=0; i<jsonData.size(); i++){
79 def post = url.openConnection();
80 post.setRequestMethod("POST")
81 post.setDoOutput(true)
82 post.setRequestProperty("Content-Type", "application/json")
84 println "connectionResetMap:" + connectionResetMap.get(data.getTargetName())
85 if (connectionResetMap.get(data.getTargetName())){
86 println "ConfigUpdate received. Sending a final event."
87 post.getOutputStream().write('{"event":{"commonEventHeader":{"startEpochMicrosec":1.53456715899E+12,"sourceId":"mmisVnfName","sequence":1910,"eventId":"ManagedElement=1","domain":"other","lastEpochMicrosec":1.53805815899E+12,"eventName":"PM_ManagedElement=1","internalHeaderFields":{"collectorTimeStamp":"Thu, 09 27 2018 02:22:48 GMT"},"sourceName":"000.111.222.333:4002","priority":"Low","version":2,"reportingEntityName":"ManagedElement=1"},"otherField":{"nameValuePairs":[{"name":"StartTime","value":"2018-1-26 8:49:50"},{"name":"WEB04NY_tej","value":"406"}],"otherFieldVersion":2}}}'.getBytes("UTF-8"))
88 def postRC = post.getResponseCode();
90 if(postRC.equals(200)) {
91 println(post.getInputStream().getText());
92 result = "<rpc-reply message-id=\"<MID>\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"> <ok/> </rpc-reply>";
94 println "Final event not accepted. Respnse code "+ postRC+ " received."
99 def eventMessage = JsonOutput.toJson(jsonData[i])
100 println "sending event " + i + ":" + eventMessage
101 post.getOutputStream().write(eventMessage.getBytes("UTF-8"));
102 def postRC = post.getResponseCode();
104 if(postRC.equals(200)) {
105 println(post.getInputStream().getText());
106 result = "<rpc-reply message-id=\"<MID>\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"> <ok/> </rpc-reply>";
108 println "Event not accepted. Response code "+ postRC+ " received."
110 if(i==jsonData.size()-1){
114 println "sleeping for "+Long.valueOf(data.getTimeDelayForSendingEvents())+" milliseconds.."
115 sleep(Long.valueOf(data.getTimeDelayForSendingEvents()))
119 println "No url found in database.."
120 logger.info("no url found in database")
121 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>"
124 logger.info("targetName not found");
125 println("targetName not found")
126 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>"