RIC:1060: Change in PTL
[ric-plt/e2.git] / RIC-E2-TERMINATION / BuildXml.h
1 /*
2  * Copyright 2020 AT&T Intellectual Property
3  * Copyright 2020 Nokia
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
18 //
19 // Created by adi ENZEL on 4/5/20.
20 //
21
22 #ifndef E2_BUILDXML_H
23 #define E2_BUILDXML_H
24
25 #include <iostream>
26 #include <iosfwd>
27 #include <vector>
28 #include "pugixml/src/pugixml.hpp"
29 #include <string>
30 #include <sstream>
31 #include <mdclog/mdclog.h>
32 #include <cstdlib>
33
34 using namespace std;
35
36 /*
37  * Copied from pugixml samples
38  */
39 struct xml_string_writer : pugi::xml_writer {
40     std::string result;
41
42     void write(const void *data, size_t size) override {
43         result.append(static_cast<const char *>(data), size);
44     }
45 };
46
47 std::string node_to_string(pugi::xml_node node) {
48     xml_string_writer writer;
49     node.print(writer);
50
51     return writer.result;
52 }
53
54 string buildXmlData(const string &messageName,
55         const string &ieName,
56         vector<string> &RANfunctionsAdded,
57         vector<string> &RANfunctionsModified,
58         unsigned char *buffer,
59         size_t size) {
60     pugi::xml_document *doc = new pugi::xml_document();
61
62     char RANfunctionsAddedID[8];
63     snprintf(RANfunctionsAddedID, 8, "%d", (int) ProtocolIE_ID_id_RANfunctionsAdded);
64     char RANfunctionsModifiedID[8];
65     snprintf(RANfunctionsModifiedID, 8, "%d", (int) ProtocolIE_ID_id_RANfunctionsModified);
66     char GlobalE2nodeID[8];
67     snprintf(GlobalE2nodeID, 8, "%d", (int) ProtocolIE_ID_id_GlobalE2node_ID);
68
69     pugi::xml_parse_result result = doc->load_buffer((const char *) buffer, size);
70     if (result) {
71         for (auto tool : doc->child("E2AP-PDU")
72                 .child("initiatingMessage")
73                 .child("value")
74                 .child(messageName.c_str())
75                 .child("protocolIEs")
76                 .children(ieName.c_str())) {
77             // there can be many ieName entries in the messageName so we need only the ones that containes E2SM continers
78             auto node = tool.child("id");  // get the id to identify the type of the contained message
79             if (node.empty()) {
80                 mdclog_write(MDCLOG_ERR, "Failed to find ID node in the XML. File %s, line %d",
81                              __FILE__, __LINE__);
82                 continue;
83             }
84             if (strcmp(node.name(), "id") == 0 && strcmp(node.child_value(), RANfunctionsAddedID) == 0) {
85                 auto nodea = tool.child("value").
86                         child("RANfunctions-List").
87                         children("ProtocolIE-SingleContainer");
88                 unsigned int index = 0;
89                 for (auto n1 : nodea) {
90                     auto n2 = n1.child("value").child("RANfunction-Item").child("ranFunctionDefinition");
91                     n2.remove_children();
92                     string val = RANfunctionsAdded.at(index++);
93                     // here we get vector with counter
94                     n2.append_child(pugi::node_pcdata).set_value(val.c_str());
95                     if (mdclog_level_get() >= MDCLOG_DEBUG) {
96                         mdclog_write(MDCLOG_DEBUG, "entry %s Replaced with : %s", n2.name(), n2.child_value());
97                     }
98                 }
99             } else if (strcmp(node.name(), "id") == 0 && strcmp(node.child_value(), RANfunctionsModifiedID) == 0) {
100                 auto nodea = tool.child("value").
101                         child("RANfunctions-List").
102                         children("ProtocolIE-SingleContainer");
103                 unsigned int index = 0;
104                 for (auto n1 : nodea) {
105                     auto n2 = n1.child("value").child("RANfunction-Item").child("ranFunctionDefinition");
106                     n2.remove_children();
107                     string val = RANfunctionsModified.at(index++);
108                     // here we get vector with counter
109                     n2.append_child(pugi::node_pcdata).set_value(val.c_str());
110                     if (mdclog_level_get() >= MDCLOG_DEBUG) {
111                         mdclog_write(MDCLOG_DEBUG, "entry %s Replaced with : %s", n2.name(), n2.child_value());
112                     }
113                 }
114             } else if (strcmp(node.name(), "id") == 0 && strcmp(node.child_value(), GlobalE2nodeID) == 0) {
115                 continue;
116             } else {
117                 if (mdclog_level_get() >= MDCLOG_DEBUG) {
118                     mdclog_write(MDCLOG_DEBUG, "Entry name :%s  with entry of %s skipped", node.name(), node.child_value());
119                 }
120                 continue;
121             }
122         }
123     } else {
124         mdclog_write(MDCLOG_ERR,
125                      "Error loading xml string");
126         delete doc;
127         return string("");
128     }
129     auto res = node_to_string(*doc);
130     res.erase(std::remove(res.begin(), res.end(), '\n'), res.end());
131     res.erase(std::remove(res.begin(), res.end(), '\t'), res.end());
132     delete doc;
133     return res;
134
135 }
136
137 #endif //E2_BUILDXML_H