Merge "version 4.0.8 Fix bug in the XML builder using string with using buffer to...
authorShuky Har-Noy <shuky.har-noy@intl.att.com>
Thu, 16 Apr 2020 17:29:34 +0000 (17:29 +0000)
committerGerrit Code Review <gerrit@o-ran-sc.org>
Thu, 16 Apr 2020 17:29:34 +0000 (17:29 +0000)
RIC-E2-TERMINATION/BuildXml.h
RIC-E2-TERMINATION/sctpThread.cpp

index 457ef8b..fe6d757 100644 (file)
 #include <iosfwd>
 #include <vector>
 #include "pugixml/src/pugixml.hpp"
-#include <string.h>
+#include <string>
 #include <sstream>
+#include <mdclog/mdclog.h>
+#include <cstdlib>
 
 using namespace std;
 
@@ -36,36 +38,36 @@ using namespace std;
 struct xml_string_writer : pugi::xml_writer {
     std::string result;
 
-    virtual void write(const void *data, size_t size) {
+    void write(const void *data, size_t size) override {
         result.append(static_cast<const char *>(data), size);
     }
 };
 // end::code[]
 
-struct xml_memory_writer : pugi::xml_writer {
-    char *buffer;
-    size_t capacity;
-    size_t result;
-
-    xml_memory_writer() : buffer(0), capacity(0), result(0) {
-    }
-
-    xml_memory_writer(char *buffer, size_t capacity) : buffer(buffer), capacity(capacity), result(0) {
-    }
-
-    size_t written_size() const {
-        return result < capacity ? result : capacity;
-    }
-
-    virtual void write(const void *data, size_t size) {
-        if (result < capacity) {
-            size_t chunk = (capacity - result < size) ? capacity - result : size;
-
-            memcpy(buffer + result, data, chunk);
-        }
-        result += size;
-    }
-};
+//struct xml_memory_writer : pugi::xml_writer {
+//    char *buffer;
+//    size_t capacity;
+//    size_t result;
+//
+//    xml_memory_writer() : buffer(nullptr), capacity(0), result(0) {
+//    }
+//
+//    xml_memory_writer(char *buffer, size_t capacity) : buffer(buffer), capacity(capacity), result(0) {
+//    }
+//
+//    [[nodiscard]] size_t written_size() const {
+//        return result < capacity ? result : capacity;
+//    }
+//
+//    void write(const void *data, size_t size) override {
+//        if (result < capacity) {
+//            size_t chunk = (capacity - result < size) ? capacity - result : size;
+//
+//            memcpy(buffer + result, data, chunk);
+//        }
+//        result += size;
+//    }
+//};
 
 std::string node_to_string(pugi::xml_node node) {
     xml_string_writer writer;
@@ -75,10 +77,11 @@ std::string node_to_string(pugi::xml_node node) {
 }
 
 
-void buildXmlData(const string &messageName, const string &ieName, vector<string> &repValues, unsigned char *buffer) {
+int buildXmlData(const string &messageName, const string &ieName, vector<string> &RANfunctionsAdded, unsigned char *buffer, size_t size) {
     pugi::xml_document doc;
 
-    pugi::xml_parse_result result = doc.load_string((const char *)buffer);
+    doc.reset();
+    pugi::xml_parse_result result = doc.load_buffer((const char *)buffer, size);
     if (result) {
         unsigned int index = 0;
         for (auto tool : doc.child("E2AP-PDU")
@@ -87,48 +90,38 @@ void buildXmlData(const string &messageName, const string &ieName, vector<string
                 .child(messageName.c_str())
                 .child("protocolIEs")
                 .children(ieName.c_str())) {
-            for (auto n : tool.child("value").child("RANfunctions-List").child(
-                    "ProtocolIE-SingleContainer").children()) {
-//ProtocolIE-SingleContainer
-//cout << "\t1 " << n.name() << endl;
-                if (strcmp(n.name(), "value") == 0) {
-                    for (auto l : tool.child("value").children()) {
-//cout << "\t\t2 " << l.name() << endl;
-                        for (auto f : l.children()) {
-//cout << "\t\t\t3 " << f.name() << endl;
-                            for (auto g : f.child("value").children()) {
-//cout << "\t\t\t\t4 " << g.name() << endl;
-                                for (auto a : g.children()) {
-                                    if (strcmp(a.name(), "ranFunctionDefinition") == 0) {
-                                        if (repValues.size() > index) {
-                                            a.remove_children();
-                                            string val = repValues.at(index++);
-// here we get vector with counter
-                                            a.append_child(pugi::node_pcdata).set_value(val.c_str());
-
-                                        }
-                                    }
-//cout << "\t\t\t\t\t5 " << a.name() << " " << a.child_value() << endl;
-                                }
-                            }
-                        }
+            auto node = tool.child("id");
+            if (strcmp(node.name(), "id") == 0 && strcmp(node.child_value(), "10") == 0) {
+                auto nodea = tool.child("value").
+                        child("RANfunctions-List").
+                        children("ProtocolIE-SingleContainer");
+                for (auto n1 : nodea) {
+                    auto n2 = n1.child("value").child("RANfunction-Item").child("ranFunctionDefinition");
+                    n2.remove_children();
+                    string val = RANfunctionsAdded.at(index++);
+                    // here we get vector with counter
+                    n2.append_child(pugi::node_pcdata).set_value(val.c_str());
+                    if (mdclog_level_get() >= MDCLOG_DEBUG) {
+                        mdclog_write(MDCLOG_DEBUG, "entry %s Replaced with : %s", n2.name(), n2.child_value());
                     }
                 }
+            } else {
+                if (mdclog_level_get() >= MDCLOG_DEBUG) {
+                    mdclog_write(MDCLOG_DEBUG, "Entry %s = value %s skipped", node.name(), node.child_value());
+                }
+                continue;
             }
         }
 
         auto res = node_to_string(doc);
         memcpy(buffer, res.c_str(), res.length());
-
-//        streambuf *oldCout = cout.rdbuf();
-//        ostringstream memCout;
-//// create new cout
-//        cout.rdbuf(memCout.rdbuf());
-//        doc.save(std::cout);
-////return to the normal cout
-//        cout.rdbuf(oldCout);
-//        memcpy(buffer, memCout.str().c_str(), memCout.str().length());
+        doc.reset();
+    } else {
+        mdclog_write(MDCLOG_ERR, "Error loading xml string");
+        return -1;
     }
+    return 0;
+
 }
 
 #endif //E2_BUILDXML_H
index c2ad2c5..f622d53 100644 (file)
@@ -1197,16 +1197,10 @@ static void buildAndsendSetupRequest(ReportingMessages_t &message,
                                      E2AP_PDU_t *pdu,
                                      vector<string> &repValues) {
     auto logLevel = mdclog_level_get();
-
     // now we can send the data to e2Mgr
     auto buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
-
-    auto *rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, buffer_size);
-    // add addrees to message
-
-
-    // unsigned char *buffer = &rmrMsg->payload[j];
     unsigned char buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
+    auto *rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, buffer_size);
     // encode to xml
     auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, buffer, buffer_size);
     if (er.encoded == -1) {
@@ -1218,7 +1212,9 @@ static void buildAndsendSetupRequest(ReportingMessages_t &message,
     } else {
         string messageType("E2setupRequest");
         string ieName("E2setupRequestIEs");
-        buildXmlData(messageType, ieName, repValues, buffer);
+        buffer[er.encoded] = '\0';
+        buildXmlData(messageType, ieName, repValues, buffer, (size_t)er.encoded);
+
 //        string xmlStr = (char *)buffer;
 //        auto removeSpaces = [] (string str) -> string {
 //            str.erase(remove(str.begin(), str.end(), ' '), str.end());
@@ -1348,10 +1344,11 @@ int RAN_Function_list_To_Vector(RANfunctions_List_t& list, vector <string> &runF
 
 
 
-int collectSetupRequestData(E2AP_PDU_t *pdu,
-                            Sctp_Map_t *sctpMap,
-                            ReportingMessages_t &message,
-                            vector <string> &runFunDEFXML_v) {
+int collectSetupAndServiceUpdate_RequestData(E2AP_PDU_t *pdu,
+                                             Sctp_Map_t *sctpMap,
+                                             ReportingMessages_t &message,
+                                             vector <string> &RANfunctionsAdded_v,
+                                             vector <string> &RANfunctionsModified_v) {
     memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
     for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) {
         auto *ie = pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.array[i];
@@ -1366,15 +1363,23 @@ int collectSetupRequestData(E2AP_PDU_t *pdu,
                 memcpy(message.message.enodbName, message.peerInfo->enodbName, strlen(message.peerInfo->enodbName));
                 sctpMap->setkey(message.message.enodbName, message.peerInfo);
             }
-        }
-        // reformat RANFUNCTION Definition to XML
-        if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
+        } else if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
             if (ie->value.present == E2setupRequestIEs__value_PR_RANfunctions_List) {
                 if (mdclog_level_get() >= MDCLOG_DEBUG) {
                     mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
                                  ie->value.choice.RANfunctions_List.list.count);
                 }
-                if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, runFunDEFXML_v) != 0 ) {
+                if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
+                    return -1;
+                }
+            }
+        } else if (ie->id == ProtocolIE_ID_id_RANfunctionsModified) {
+            if (ie->value.present == E2setupRequestIEs__value_PR_RANfunctions_List) {
+                if (mdclog_level_get() >= MDCLOG_DEBUG) {
+                    mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
+                                 ie->value.choice.RANfunctions_List.list.count);
+                }
+                if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsModified_v) != 0 ) {
                     return -1;
                 }
             }
@@ -1382,7 +1387,7 @@ int collectSetupRequestData(E2AP_PDU_t *pdu,
     }
     if (mdclog_level_get() >= MDCLOG_DEBUG) {
         mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
-                     runFunDEFXML_v.size());
+                     RANfunctionsAdded_v.size());
     }
     return 0;
 }
@@ -1423,13 +1428,16 @@ void asnInitiatingRequest(E2AP_PDU_t *pdu,
             }
             std::string xmlString(setup_xml_buffer_size,  setup_xml_buffer_size + er.encoded);
 
-            vector <string> runFunDEFXML_v;
-            runFunDEFXML_v.clear();
-            if (collectSetupRequestData(pdu, sctpMap,  message, runFunDEFXML_v) != 0) {
+            vector <string> RANfunctionsAdded_v;
+            vector <string> RANfunctionsModified_v;
+            RANfunctionsAdded_v.clear();
+            RANfunctionsModified_v.clear();
+            if (collectSetupAndServiceUpdate_RequestData(pdu, sctpMap, message,
+                    RANfunctionsAdded_v, RANfunctionsModified_v) != 0) {
                 break;
             }
 
-            buildAndsendSetupRequest(message, rmrMessageBuffer, pdu, runFunDEFXML_v);
+            buildAndsendSetupRequest(message, rmrMessageBuffer, pdu, RANfunctionsAdded_v);
             break;
         }
         case ProcedureCode_id_RICserviceUpdate: {
@@ -1643,7 +1651,6 @@ void asnSuccsesfulMsg(E2AP_PDU_t *pdu,
                                        (unsigned char *)message.message.enodbName,
                                        strlen(message.message.enodbName));
                         rmrMessageBuffer.sendMessage->state = 0;
-//                        rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricRequestorID;
                         rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
                         if (mdclog_level_get() >= MDCLOG_DEBUG) {
                             mdclog_write(MDCLOG_DEBUG, "RIC sub id = %d, message type = %d",