2 * Copyright 2020 AT&T Intellectual Property
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.
19 // Created by adi ENZEL on 4/5/20.
28 #include "pugixml/src/pugixml.hpp"
31 #include <mdclog/mdclog.h>
37 * Copied from pugixml samples
39 struct xml_string_writer : pugi::xml_writer {
42 void write(const void *data, size_t size) override {
43 result.append(static_cast<const char *>(data), size);
47 std::string node_to_string(pugi::xml_node node) {
48 xml_string_writer writer;
54 string buildXmlData(const string &messageName,
56 vector<string> &RANfunctionsAdded,
57 vector<string> &RANfunctionsModified,
58 unsigned char *buffer,
60 pugi::xml_document *doc = new pugi::xml_document();
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);
69 pugi::xml_parse_result result = doc->load_buffer((const char *) buffer, size);
71 for (auto tool : doc->child("E2AP-PDU")
72 .child("initiatingMessage")
74 .child(messageName.c_str())
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
80 mdclog_write(MDCLOG_ERR, "Failed to find ID node in the XML. File %s, line %d",
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");
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());
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());
114 } else if (strcmp(node.name(), "id") == 0 && strcmp(node.child_value(), GlobalE2nodeID) == 0) {
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());
124 mdclog_write(MDCLOG_ERR,
125 "Error loading xml string");
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());
137 #endif //E2_BUILDXML_H