Merge "version 4.0.8 change typo in sub_id replace redirection cout (<<) with buffer...
[ric-plt/e2.git] / RIC-E2-TERMINATION / BuildXml.h
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"
28 #include <string.h>
29 #include <sstream>
30
31 using namespace std;
32
33 /*
34  * Copied from pugixml samples
35  */
36 struct 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
45 struct 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
70 std::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
78 void buildXmlData(const string &messageName, const string &ieName, vector<string> &repValues, unsigned char *buffer) {
79     pugi::xml_document doc;
80
81     pugi::xml_parse_result result = doc.load_string((const char *)buffer);
82     if (result) {
83         unsigned int index = 0;
84         for (auto tool : doc.child("E2AP-PDU")
85                 .child("initiatingMessage")
86                 .child("value")
87                 .child(messageName.c_str())
88                 .child("protocolIEs")
89                 .children(ieName.c_str())) {
90             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
120         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());
131     }
132 }
133
134 #endif //E2_BUILDXML_H