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 |
| 24 | #include <iostream> |
| 25 | #include <iosfwd> |
| 26 | #include <vector> |
| 27 | #include "pugixml/src/pugixml.hpp" |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 28 | #include <string> |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 29 | #include <sstream> |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 30 | #include <mdclog/mdclog.h> |
| 31 | #include <cstdlib> |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 32 | |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 33 | using namespace std; |
| 34 | |
aa7133@att.com | 0793fcb | 2020-04-16 11:34:26 +0300 | [diff] [blame] | 35 | /* |
| 36 | * Copied from pugixml samples |
| 37 | */ |
| 38 | struct xml_string_writer : pugi::xml_writer { |
| 39 | std::string result; |
| 40 | |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 41 | void write(const void *data, size_t size) override { |
aa7133@att.com | 0793fcb | 2020-04-16 11:34:26 +0300 | [diff] [blame] | 42 | result.append(static_cast<const char *>(data), size); |
| 43 | } |
| 44 | }; |
| 45 | // end::code[] |
| 46 | |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 47 | //struct xml_memory_writer : pugi::xml_writer { |
| 48 | // char *buffer; |
| 49 | // size_t capacity; |
| 50 | // size_t result; |
| 51 | // |
| 52 | // xml_memory_writer() : buffer(nullptr), capacity(0), result(0) { |
| 53 | // } |
| 54 | // |
| 55 | // xml_memory_writer(char *buffer, size_t capacity) : buffer(buffer), capacity(capacity), result(0) { |
| 56 | // } |
| 57 | // |
| 58 | // [[nodiscard]] size_t written_size() const { |
| 59 | // return result < capacity ? result : capacity; |
| 60 | // } |
| 61 | // |
| 62 | // void write(const void *data, size_t size) override { |
| 63 | // if (result < capacity) { |
| 64 | // size_t chunk = (capacity - result < size) ? capacity - result : size; |
| 65 | // |
| 66 | // memcpy(buffer + result, data, chunk); |
| 67 | // } |
| 68 | // result += size; |
| 69 | // } |
| 70 | //}; |
aa7133@att.com | 0793fcb | 2020-04-16 11:34:26 +0300 | [diff] [blame] | 71 | |
| 72 | std::string node_to_string(pugi::xml_node node) { |
| 73 | xml_string_writer writer; |
| 74 | node.print(writer); |
| 75 | |
| 76 | return writer.result; |
| 77 | } |
| 78 | |
| 79 | |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 80 | int buildXmlData(const string &messageName, const string &ieName, vector<string> &RANfunctionsAdded, unsigned char *buffer, size_t size) { |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 81 | pugi::xml_document doc; |
| 82 | |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 83 | doc.reset(); |
| 84 | 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] | 85 | if (result) { |
| 86 | unsigned int index = 0; |
| 87 | for (auto tool : doc.child("E2AP-PDU") |
| 88 | .child("initiatingMessage") |
| 89 | .child("value") |
aa7133@att.com | 0793fcb | 2020-04-16 11:34:26 +0300 | [diff] [blame] | 90 | .child(messageName.c_str()) |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 91 | .child("protocolIEs") |
aa7133@att.com | 0793fcb | 2020-04-16 11:34:26 +0300 | [diff] [blame] | 92 | .children(ieName.c_str())) { |
aa7133@att.com | b95f70e | 2020-04-20 11:37:31 +0300 | [diff] [blame^] | 93 | // there can be many ieName entries in the messageName so we need only the ones that containes E2SM continers |
| 94 | auto node = tool.child("id"); // get the id to identify the type of the contained message |
| 95 | if (node.empty()) { |
| 96 | mdclog_write(MDCLOG_ERR, "Failed to find ID node in the XML. File %s, line %d", |
| 97 | __FILE__, __LINE__); |
| 98 | continue; |
| 99 | } |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 100 | if (strcmp(node.name(), "id") == 0 && strcmp(node.child_value(), "10") == 0) { |
| 101 | auto nodea = tool.child("value"). |
| 102 | child("RANfunctions-List"). |
| 103 | children("ProtocolIE-SingleContainer"); |
aa7133@att.com | b95f70e | 2020-04-20 11:37:31 +0300 | [diff] [blame^] | 104 | |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 105 | for (auto n1 : nodea) { |
| 106 | auto n2 = n1.child("value").child("RANfunction-Item").child("ranFunctionDefinition"); |
| 107 | n2.remove_children(); |
| 108 | string val = RANfunctionsAdded.at(index++); |
| 109 | // here we get vector with counter |
| 110 | n2.append_child(pugi::node_pcdata).set_value(val.c_str()); |
| 111 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 112 | 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] | 113 | } |
| 114 | } |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 115 | } else { |
| 116 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 117 | mdclog_write(MDCLOG_DEBUG, "Entry %s = value %s skipped", node.name(), node.child_value()); |
| 118 | } |
| 119 | continue; |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
aa7133@att.com | 0793fcb | 2020-04-16 11:34:26 +0300 | [diff] [blame] | 123 | auto res = node_to_string(doc); |
| 124 | memcpy(buffer, res.c_str(), res.length()); |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 125 | doc.reset(); |
| 126 | } else { |
| 127 | mdclog_write(MDCLOG_ERR, "Error loading xml string"); |
| 128 | return -1; |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 129 | } |
aa7133@att.com | 0829b8b | 2020-04-16 19:27:42 +0300 | [diff] [blame] | 130 | return 0; |
| 131 | |
aa7133@att.com | c5a1520 | 2020-04-05 19:57:01 +0300 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | #endif //E2_BUILDXML_H |