version 4.0.7
[ric-plt/e2.git] / RIC-E2-TERMINATION / sctpThread.cpp
index 95b0e46..a6a18ea 100644 (file)
 #include "sctpThread.h"
 #include "BuildRunName.h"
 
-//#define asn_DEF_E2SM_gNB_NRT_RANfunction_Definition e2sm_asn_DEF_E2SM_gNB_NRT_RANfunction_Definition
-//#define __gcov_init e2sm___gcov_init
-//#define __gcov_merge_add e2sm___gcov_merge_add
-//#define calloc e2sm_calloc
-
 #include "3rdparty/oranE2SM/E2SM-gNB-NRT-RANfunction-Definition.h"
 
-//#undef calloc
-//#undef __gcov_merge_add
-//#undef __gcov_init
-//#undef asn_DEF_E2SM_gNB_NRT_RANfunction_Definition
+#include "pugixml/src/pugixml.hpp"
 
 using namespace std;
 //using namespace std::placeholders;
@@ -429,7 +421,8 @@ void sendTermInit(sctp_params_t &sctpParams) {
         rmr_bytes2xact(msg, tx, txLen);
         msg = rmr_send_msg(sctpParams.rmrCtx, msg);
         if (msg == nullptr) {
-            msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.myIP.length());
+            //TODO fix it
+            msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
         } else if (msg->state == 0) {
             rmr_free_msg(msg);
             if (mdclog_level_get() >=  MDCLOG_INFO) {
@@ -726,12 +719,16 @@ void handleConfigChange(sctp_params_t *sctpParams) {
                 // not the directory
             }
             if (event->len) {
-                if (!(sctpParams->configFileName.compare(event->name))) {
+                auto  retVal = strcmp(sctpParams->configFileName.c_str(), event->name);
+                if (retVal != 0) {
                     continue;
                 }
             }
             // only the file we want
             if (event->mask & (uint32_t)IN_CLOSE_WRITE) {
+                if (mdclog_level_get() >= MDCLOG_INFO) {
+                    mdclog_write(MDCLOG_INFO, "Configuration file changed");
+                }
                 if (exists(p)) {
                     const int size = 2048;
                     auto fileSize = file_size(p);
@@ -987,7 +984,7 @@ int sendSctpMsg(ConnectedCU_t *peerInfo, ReportingMessages_t &message, Sctp_Map_
             m->erase(key);
             return -1;
         }
-        message.statCollector->incSentMessage(string(message.message.enodbName));
+        //message.statCollector->incSentMessage(string(message.message.enodbName));
         message.message.direction = 'D';
         // send report.buffer of size
         buildJsonMessage(message);
@@ -1207,7 +1204,8 @@ int receiveDataFromSctp(struct epoll_event *events,
 
 static void buildAndsendSetupRequest(ReportingMessages_t &message,
                                      RmrMessagesBuffer_t &rmrMessageBuffer,
-                                     E2AP_PDU_t *pdu) {
+                                     E2AP_PDU_t *pdu,
+                                     vector<string> repValues) {
     auto logLevel = mdclog_level_get();
 
     // now we can send the data to e2Mgr
@@ -1224,10 +1222,64 @@ static void buildAndsendSetupRequest(ReportingMessages_t &message,
     if (er.encoded == -1) {
         mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
     } else if (er.encoded > (ssize_t) buffer_size) {
-        mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s",
+        mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
                      (int) buffer_size,
-                     asn_DEF_E2AP_PDU.name);
+                     asn_DEF_E2AP_PDU.name, __func__, __LINE__);
     } else {
+        // we have the XML
+        pugi::xml_document doc;
+        pugi::xml_parse_result result = doc.load_string((const char *)buffer);
+        if (result) {
+            unsigned int index = 0;
+            for (auto tool : doc.child("E2AP-PDU")
+                    .child("initiatingMessage")
+                    .child("value")
+                    .child("E2setupRequest")
+                    .child("protocolIEs")
+                    .children("E2setupRequestIEs")) {
+                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;
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+
+//            memstream strinBuf(buffer, RECEIVE_SCTP_BUFFER_SIZE * 2);
+//
+//            strinBuf.read(, RECEIVE_SCTP_BUFFER_SIZE * 2);
+
+            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());
+        }
         rmrMsg->len = snprintf((char *)rmrMsg->payload, RECEIVE_SCTP_BUFFER_SIZE * 2, "%s:%d|%s",
                                message.peerInfo->sctpParams->myIP.c_str(),
                                message.peerInfo->sctpParams->rmrPort,
@@ -1309,12 +1361,15 @@ void asnInitiatingRequest(E2AP_PDU_t *pdu,
             if (er.encoded == -1) {
                 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
             } else if (er.encoded > (ssize_t) setup_xml_buffer_size) {
-                mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s",
+                mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
                              (int)setup_xml_buffer_size,
-                             asn_DEF_E2AP_PDU.name);
+                             asn_DEF_E2AP_PDU.name, __func__, __LINE__);
             }
             std::string xmlString(setup_xml_buffer_size,  setup_xml_buffer_size + er.encoded);
 
+            vector <string> runFunDEFXML_v;
+            runFunDEFXML_v.clear();
+            string runFuncStr = {};
             auto failed = false;
             memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
             for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) {
@@ -1336,18 +1391,17 @@ void asnInitiatingRequest(E2AP_PDU_t *pdu,
                     if (ie->value.present == E2setupRequestIEs__value_PR_RANfunctions_List) {
                         for (auto j = 0; i < ie->value.choice.RANfunctions_List.list.count; i++) {
                             auto *raNfunctionItemIEs = (RANfunction_ItemIEs_t *)ie->value.choice.RANfunctions_List.list.array[j];
-                            if (raNfunctionItemIEs->id == ProtocolIE_ID_id_RANfunction_Item) {
-                                auto buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
-                                unsigned char buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
+                            if (raNfunctionItemIEs->id == ProtocolIE_ID_id_RANfunction_Item &&
+                                    (raNfunctionItemIEs->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item)) {
                                 // encode to xml
                                 E2SM_gNB_NRT_RANfunction_Definition_t *ranFunDef = nullptr;
                                 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER,
                                         &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
                                         (void **)&ranFunDef,
-                                        buffer,
-                                        buffer_size);
+                                        raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.buf,
+                                        raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.size);
                                 if (rval.code != RC_OK) {
-                                    mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2AP PDU from E2MGR : %s",
+                                    mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2SM message from : %s",
                                                  rval.code,
                                                  asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name);
                                     failed = true;
@@ -1358,8 +1412,10 @@ void asnInitiatingRequest(E2AP_PDU_t *pdu,
                                     char *printBuffer;
                                     size_t size;
                                     FILE *stream = open_memstream(&printBuffer, &size);
-                                    asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu);
-                                    mdclog_write(MDCLOG_DEBUG, "Encoding E2AP PDU past : %s", printBuffer);
+                                    asn_fprint(stream, &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition, ranFunDef);
+                                    mdclog_write(MDCLOG_DEBUG, "Encoding E2SM %s PDU past : %s",
+                                                 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
+                                                 printBuffer);
                                 }
                                 auto xml_buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
                                 unsigned char xml_buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
@@ -1374,12 +1430,19 @@ void asnInitiatingRequest(E2AP_PDU_t *pdu,
                                     mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s",
                                                  asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
                                                  strerror(errno));
-                                } else if (er.encoded > (ssize_t) buffer_size) {
-                                    mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s",
+                                } else if (er.encoded > (ssize_t)xml_buffer_size) {
+                                    mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
                                                  (int) xml_buffer_size,
-                                                 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name);
+                                                 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name, __func__, __LINE__);
                                 } else {
-                                    // we have the xml
+                                    if (mdclog_level_get() >= MDCLOG_DEBUG) {
+                                        mdclog_write(MDCLOG_DEBUG, "Encoding E2SM %s PDU past : %s",
+                                                     asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
+                                                     xml_buffer);
+                                    }
+                                    //TODO replace the ranFunctionDefinition with the XML
+                                    string runFuncs = (char *)(xml_buffer);
+                                    runFunDEFXML_v.emplace_back(runFuncs);
                                 }
 
                             }
@@ -1396,7 +1459,7 @@ void asnInitiatingRequest(E2AP_PDU_t *pdu,
 
             //build all parts and send the XML (need to copy the XML with the header to the rmrMessageBuffer payload
             //TODO replace with new function
-            buildAndsendSetupRequest(message, rmrMessageBuffer, pdu);
+            buildAndsendSetupRequest(message, rmrMessageBuffer, pdu, runFunDEFXML_v);
             break;
         }
         case ProcedureCode_id_ErrorIndication: {
@@ -1895,26 +1958,34 @@ void getRmrContext(sctp_params_t &pSctpParams) {
 
 int BuildPERSetupResponseMessaeFromXML(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
     E2AP_PDU_t *pdu;
+
+    if (mdclog_level_get() >= MDCLOG_DEBUG) {
+        mdclog_write(MDCLOG_DEBUG, "got xml setup response \n %s\n", rmrMessageBuffer.rcvMessage->payload);
+    }
     auto rval = asn_decode(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, (void **) &pdu,
                            rmrMessageBuffer.rcvMessage->payload, rmrMessageBuffer.rcvMessage->len);
     if (rval.code != RC_OK) {
-        mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2AP PDU from E2MGR : %s",
+        mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response  from E2MGR : %s",
                      rval.code,
                      message.message.enodbName);
         return -1;
     }
 
-    auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu,
-                                   rmrMessageBuffer.rcvMessage->payload, rmrMessageBuffer.rcvMessage->len);
+    int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
+    auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu,
+                                   rmrMessageBuffer.rcvMessage->payload, buff_size);
     if (er.encoded == -1) {
         mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
         return -1;
-    } else if (er.encoded > (ssize_t)rmrMessageBuffer.rcvMessage->len) {
-        mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s",
+    } else if (er.encoded > (ssize_t)buff_size) {
+        mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
                      (int)rmrMessageBuffer.rcvMessage->len,
-                     asn_DEF_E2AP_PDU.name);
+                     asn_DEF_E2AP_PDU.name,
+                     __func__,
+                     __LINE__);
         return -1;
     }
+    rmrMessageBuffer.rcvMessage->len = er.encoded;
     return 0;
 }