aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 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 |
ss412g | 4aeeda2 | 2020-06-16 14:12:41 +0300 | [diff] [blame] | 24 | |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 25 | #include <iostream> |
| 26 | #include <iosfwd> |
| 27 | #include <vector> |
| 28 | #include "pugixml/src/pugixml.hpp" |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 29 | #include <string> |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 30 | #include <sstream> |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 31 | #include <mdclog/mdclog.h> |
| 32 | #include <cstdlib> |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 33 | |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 34 | using namespace std; |
| 35 | |
aa7133@att.com | 0793fcb | 2020-04-16 11:34:26 +0300 | [diff] [blame] | 36 | /* |
| 37 | * Copied from pugixml samples |
| 38 | */ |
| 39 | struct xml_string_writer : pugi::xml_writer { |
| 40 | std::string result; |
| 41 | |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 42 | void write(const void *data, size_t size) override { |
aa7133@att.com | 0793fcb | 2020-04-16 11:34:26 +0300 | [diff] [blame] | 43 | result.append(static_cast<const char *>(data), size); |
| 44 | } |
| 45 | }; |
aa7133@att.com | 0793fcb | 2020-04-16 11:34:26 +0300 | [diff] [blame] | 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 | |
ss412g | 4aeeda2 | 2020-06-16 14:12:41 +0300 | [diff] [blame] | 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(); |
aa7133@att.com | 0793fcb | 2020-04-16 11:34:26 +0300 | [diff] [blame] | 61 | |
ss412g | 4aeeda2 | 2020-06-16 14:12:41 +0300 | [diff] [blame] | 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); |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 68 | |
ss412g | 4aeeda2 | 2020-06-16 14:12:41 +0300 | [diff] [blame] | 69 | pugi::xml_parse_result result = doc->load_buffer((const char *) buffer, size); |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 70 | if (result) { |
ss412g | 4aeeda2 | 2020-06-16 14:12:41 +0300 | [diff] [blame] | 71 | for (auto tool : doc->child("E2AP-PDU") |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 72 | .child("initiatingMessage") |
| 73 | .child("value") |
aa7133@att.com | 0793fcb | 2020-04-16 11:34:26 +0300 | [diff] [blame] | 74 | .child(messageName.c_str()) |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 75 | .child("protocolIEs") |
aa7133@att.com | 0793fcb | 2020-04-16 11:34:26 +0300 | [diff] [blame] | 76 | .children(ieName.c_str())) { |
aa7133@att.com | b95f70e | 2020-04-20 11:37:31 +0300 | [diff] [blame] | 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", |
ss412g | 4aeeda2 | 2020-06-16 14:12:41 +0300 | [diff] [blame] | 81 | __FILE__, __LINE__); |
aa7133@att.com | b95f70e | 2020-04-20 11:37:31 +0300 | [diff] [blame] | 82 | continue; |
| 83 | } |
ss412g | 4aeeda2 | 2020-06-16 14:12:41 +0300 | [diff] [blame] | 84 | if (strcmp(node.name(), "id") == 0 && strcmp(node.child_value(), RANfunctionsAddedID) == 0) { |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 85 | auto nodea = tool.child("value"). |
| 86 | child("RANfunctions-List"). |
| 87 | children("ProtocolIE-SingleContainer"); |
ss412g | 4aeeda2 | 2020-06-16 14:12:41 +0300 | [diff] [blame] | 88 | unsigned int index = 0; |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 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()); |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 97 | } |
| 98 | } |
ss412g | 4aeeda2 | 2020-06-16 14:12:41 +0300 | [diff] [blame] | 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; |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 116 | } else { |
| 117 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
ss412g | 4aeeda2 | 2020-06-16 14:12:41 +0300 | [diff] [blame] | 118 | mdclog_write(MDCLOG_DEBUG, "Entry name :%s with entry of %s skipped", node.name(), node.child_value()); |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 119 | } |
| 120 | continue; |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 121 | } |
| 122 | } |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 123 | } else { |
ss412g | 4aeeda2 | 2020-06-16 14:12:41 +0300 | [diff] [blame] | 124 | mdclog_write(MDCLOG_ERR, |
| 125 | "Error loading xml string"); |
| 126 | delete doc; |
| 127 | return string(""); |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 128 | } |
ss412g | 4aeeda2 | 2020-06-16 14:12:41 +0300 | [diff] [blame] | 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; |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 134 | |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | #endif //E2_BUILDXML_H |