blob: 457ef8bee1728119b9468c4bad421d3253bd0f73 [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
24#include <iostream>
25#include <iosfwd>
26#include <vector>
27#include "pugixml/src/pugixml.hpp"
28#include <string.h>
29#include <sstream>
30
aa7133@att.comc5a15202020-04-05 19:57:01 +030031using namespace std;
32
aa7133@att.com0793fcb2020-04-16 11:34:26 +030033/*
34 * Copied from pugixml samples
35 */
36struct xml_string_writer : pugi::xml_writer {
37 std::string result;
38
39 virtual void write(const void *data, size_t size) {
40 result.append(static_cast<const char *>(data), size);
41 }
42};
43// end::code[]
44
45struct xml_memory_writer : pugi::xml_writer {
46 char *buffer;
47 size_t capacity;
48 size_t result;
49
50 xml_memory_writer() : buffer(0), capacity(0), result(0) {
51 }
52
53 xml_memory_writer(char *buffer, size_t capacity) : buffer(buffer), capacity(capacity), result(0) {
54 }
55
56 size_t written_size() const {
57 return result < capacity ? result : capacity;
58 }
59
60 virtual void write(const void *data, size_t size) {
61 if (result < capacity) {
62 size_t chunk = (capacity - result < size) ? capacity - result : size;
63
64 memcpy(buffer + result, data, chunk);
65 }
66 result += size;
67 }
68};
69
70std::string node_to_string(pugi::xml_node node) {
71 xml_string_writer writer;
72 node.print(writer);
73
74 return writer.result;
75}
76
77
78void buildXmlData(const string &messageName, const string &ieName, vector<string> &repValues, unsigned char *buffer) {
aa7133@att.comc5a15202020-04-05 19:57:01 +030079 pugi::xml_document doc;
80
aa7133@att.com0793fcb2020-04-16 11:34:26 +030081 pugi::xml_parse_result result = doc.load_string((const char *)buffer);
aa7133@att.comc5a15202020-04-05 19:57:01 +030082 if (result) {
83 unsigned int index = 0;
84 for (auto tool : doc.child("E2AP-PDU")
85 .child("initiatingMessage")
86 .child("value")
aa7133@att.com0793fcb2020-04-16 11:34:26 +030087 .child(messageName.c_str())
aa7133@att.comc5a15202020-04-05 19:57:01 +030088 .child("protocolIEs")
aa7133@att.com0793fcb2020-04-16 11:34:26 +030089 .children(ieName.c_str())) {
aa7133@att.comc5a15202020-04-05 19:57:01 +030090 for (auto n : tool.child("value").child("RANfunctions-List").child(
91 "ProtocolIE-SingleContainer").children()) {
92//ProtocolIE-SingleContainer
93//cout << "\t1 " << n.name() << endl;
94 if (strcmp(n.name(), "value") == 0) {
95 for (auto l : tool.child("value").children()) {
96//cout << "\t\t2 " << l.name() << endl;
97 for (auto f : l.children()) {
98//cout << "\t\t\t3 " << f.name() << endl;
99 for (auto g : f.child("value").children()) {
100//cout << "\t\t\t\t4 " << g.name() << endl;
101 for (auto a : g.children()) {
102 if (strcmp(a.name(), "ranFunctionDefinition") == 0) {
103 if (repValues.size() > index) {
104 a.remove_children();
105 string val = repValues.at(index++);
106// here we get vector with counter
107 a.append_child(pugi::node_pcdata).set_value(val.c_str());
108
109 }
110 }
111//cout << "\t\t\t\t\t5 " << a.name() << " " << a.child_value() << endl;
112 }
113 }
114 }
115 }
116 }
117 }
118 }
119
aa7133@att.com0793fcb2020-04-16 11:34:26 +0300120 auto res = node_to_string(doc);
121 memcpy(buffer, res.c_str(), res.length());
122
123// streambuf *oldCout = cout.rdbuf();
124// ostringstream memCout;
125//// create new cout
126// cout.rdbuf(memCout.rdbuf());
127// doc.save(std::cout);
128////return to the normal cout
129// cout.rdbuf(oldCout);
130// memcpy(buffer, memCout.str().c_str(), memCout.str().length());
aa7133@att.comc5a15202020-04-05 19:57:01 +0300131 }
132}
133
134#endif //E2_BUILDXML_H