blob: e71fa4dc21c718c2b17236c889607894b18a7674 [file] [log] [blame]
aa7133@att.comc5a15202020-04-05 19:57:01 +03001/*
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
ss412g4aeeda22020-06-16 14:12:41 +030024
aa7133@att.comc5a15202020-04-05 19:57:01 +030025#include <iostream>
26#include <iosfwd>
27#include <vector>
28#include "pugixml/src/pugixml.hpp"
aa7133@att.com0829b8b2020-04-16 19:27:42 +030029#include <string>
aa7133@att.comc5a15202020-04-05 19:57:01 +030030#include <sstream>
aa7133@att.com0829b8b2020-04-16 19:27:42 +030031#include <mdclog/mdclog.h>
32#include <cstdlib>
aa7133@att.comc5a15202020-04-05 19:57:01 +030033
aa7133@att.comc5a15202020-04-05 19:57:01 +030034using namespace std;
35
aa7133@att.com0793fcb2020-04-16 11:34:26 +030036/*
37 * Copied from pugixml samples
38 */
39struct xml_string_writer : pugi::xml_writer {
40 std::string result;
41
aa7133@att.com0829b8b2020-04-16 19:27:42 +030042 void write(const void *data, size_t size) override {
aa7133@att.com0793fcb2020-04-16 11:34:26 +030043 result.append(static_cast<const char *>(data), size);
44 }
45};
aa7133@att.com0793fcb2020-04-16 11:34:26 +030046
47std::string node_to_string(pugi::xml_node node) {
48 xml_string_writer writer;
49 node.print(writer);
50
51 return writer.result;
52}
53
ss412g4aeeda22020-06-16 14:12:41 +030054string 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();
aa7133@att.com0793fcb2020-04-16 11:34:26 +030061
ss412g4aeeda22020-06-16 14:12:41 +030062 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);
aa7133@att.comc5a15202020-04-05 19:57:01 +030068
ss412g4aeeda22020-06-16 14:12:41 +030069 pugi::xml_parse_result result = doc->load_buffer((const char *) buffer, size);
aa7133@att.comc5a15202020-04-05 19:57:01 +030070 if (result) {
ss412g4aeeda22020-06-16 14:12:41 +030071 for (auto tool : doc->child("E2AP-PDU")
aa7133@att.comc5a15202020-04-05 19:57:01 +030072 .child("initiatingMessage")
73 .child("value")
aa7133@att.com0793fcb2020-04-16 11:34:26 +030074 .child(messageName.c_str())
aa7133@att.comc5a15202020-04-05 19:57:01 +030075 .child("protocolIEs")
aa7133@att.com0793fcb2020-04-16 11:34:26 +030076 .children(ieName.c_str())) {
aa7133@att.comb95f70e2020-04-20 11:37:31 +030077 // 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",
ss412g4aeeda22020-06-16 14:12:41 +030081 __FILE__, __LINE__);
aa7133@att.comb95f70e2020-04-20 11:37:31 +030082 continue;
83 }
ss412g4aeeda22020-06-16 14:12:41 +030084 if (strcmp(node.name(), "id") == 0 && strcmp(node.child_value(), RANfunctionsAddedID) == 0) {
aa7133@att.com0829b8b2020-04-16 19:27:42 +030085 auto nodea = tool.child("value").
86 child("RANfunctions-List").
87 children("ProtocolIE-SingleContainer");
ss412g4aeeda22020-06-16 14:12:41 +030088 unsigned int index = 0;
aa7133@att.com0829b8b2020-04-16 19:27:42 +030089 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());
aa7133@att.comc5a15202020-04-05 19:57:01 +030097 }
98 }
ss412g4aeeda22020-06-16 14:12:41 +030099 } 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;
aa7133@att.com0829b8b2020-04-16 19:27:42 +0300116 } else {
117 if (mdclog_level_get() >= MDCLOG_DEBUG) {
ss412g4aeeda22020-06-16 14:12:41 +0300118 mdclog_write(MDCLOG_DEBUG, "Entry name :%s with entry of %s skipped", node.name(), node.child_value());
aa7133@att.com0829b8b2020-04-16 19:27:42 +0300119 }
120 continue;
aa7133@att.comc5a15202020-04-05 19:57:01 +0300121 }
122 }
aa7133@att.com0829b8b2020-04-16 19:27:42 +0300123 } else {
ss412g4aeeda22020-06-16 14:12:41 +0300124 mdclog_write(MDCLOG_ERR,
125 "Error loading xml string");
126 delete doc;
127 return string("");
aa7133@att.comc5a15202020-04-05 19:57:01 +0300128 }
ss412g4aeeda22020-06-16 14:12:41 +0300129 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;
aa7133@att.com0829b8b2020-04-16 19:27:42 +0300134
aa7133@att.comc5a15202020-04-05 19:57:01 +0300135}
136
137#endif //E2_BUILDXML_H