[Epic-ID: ODUHIGH-516][Task-ID: 526] Implementation of ric service update
[o-du/l2.git] / src / du_app / du_e2ap_msg_hdl.c
index adedf73..49586c5 100644 (file)
 #include "E2nodeComponentInterfaceF1.h"
 #include "E2setupRequest.h"
 #include "du_e2_conversions.h"
+#include "E2SM-KPM-RANfunction-Description.h"
+#include "RANfunction-Name.h"
+#include "RIC-EventTriggerStyle-Item.h"
+#include "RIC-ReportStyle-Item.h"
+#include "MeasurementInfo-Action-Item.h"
 
 /*******************************************************************
  *
@@ -61,10 +66,10 @@ uint8_t BuildGlobalgNBId(GlobalE2node_gNB_ID_t *gNbId)
 {
    uint8_t unused = 0;
    uint8_t byteSize = 4;
-   uint8_t gnbId = 1;
+   uint8_t gnbId = duCb.gnbId;
    uint8_t ret = ROK;
 
-   /* Allocate Buffer size */
+   /* fill Global gNB ID Id */
    gNbId->global_gNB_ID.plmn_id.size = 3 * sizeof(uint8_t);
    gNbId->global_gNB_ID.plmn_id.buf = NULLP;
    DU_ALLOC(gNbId->global_gNB_ID.plmn_id.buf , gNbId->global_gNB_ID.plmn_id.size);
@@ -77,7 +82,6 @@ uint8_t BuildGlobalgNBId(GlobalE2node_gNB_ID_t *gNbId)
    {
       buildPlmnId(duCfgParam.srvdCellLst[0].duCellInfo.cellInfo.nrCgi.plmn, \
             gNbId->global_gNB_ID.plmn_id.buf);
-      /* fill gND Id */
       gNbId->global_gNB_ID.gnb_id.present = GNB_ID_Choice_PR_gnb_ID;
       /* Allocate Buffer size */
       gNbId->global_gNB_ID.gnb_id.choice.gnb_ID.size = byteSize * sizeof(uint8_t);
@@ -94,6 +98,29 @@ uint8_t BuildGlobalgNBId(GlobalE2node_gNB_ID_t *gNbId)
          fillBitString(&gNbId->global_gNB_ID.gnb_id.choice.gnb_ID, unused, byteSize, gnbId);
       }
    }
+
+   /* fill gNB-DU ID */ 
+   DU_ALLOC( gNbId->gNB_DU_ID, sizeof(GNB_DU_ID_t));
+   if(gNbId->gNB_DU_ID == NULLP)
+   {
+      DU_LOG("\nERROR  -->  E2AP: Memory allocation failed for gNB_DU_ID ");
+      ret = RFAILED;
+   }
+   else
+   {
+      gNbId->gNB_DU_ID->size = sizeof(uint8_t);
+      DU_ALLOC( gNbId->gNB_DU_ID->buf, sizeof(uint8_t));
+      if(gNbId->gNB_DU_ID->buf)
+      {
+         gNbId->gNB_DU_ID->buf[0] =duCb.e2apDb.e2NodeId;
+      }
+      else
+      {
+         DU_LOG("\nERROR  -->  E2AP: Memory allocation failed for gNB_DU_ID buffer");
+         ret = RFAILED;
+      }
+   }
+
    return ret;
 }
 
@@ -131,6 +158,7 @@ CmLList *searchE2NodeComponentInfo(InterfaceType interfaceType, uint8_t componen
    }
    return node; 
 }
+
 /*******************************************************************
  *
  * @brief Builds E2 node config addition list 
@@ -259,6 +287,504 @@ uint8_t BuildE2NodeConfigAddList(E2nodeComponentConfigAddition_List_t *e2NodeAdd
 
 }
 
+/*******************************************************************
+ *
+ * @brief deallocation of E2SM_KPM_RANfunction_Description_t
+ *
+ * @details
+ *
+ *    Function : freeE2smKpmRanFunctionDefinition
+ *
+ *    Functionality: deallocation of E2SM_KPM_RANfunction_Description_t
+ *
+ * @params[in]  E2SM_KPM_RANfunction_Description_t *ranFunctionDefinition
+ * @return void
+ *
+ ******************************************************************/
+
+void freeE2smKpmRanFunctionDefinition(E2SM_KPM_RANfunction_Description_t *ranFunctionDefinition)
+{
+   MeasurementInfo_Action_Item_t *measInfoList;
+   uint8_t eventTriggerIdx, reportStyleIdx, measInfoIdx;
+   RANfunction_Name_t *ranFuncName;
+   struct E2SM_KPM_RANfunction_Description__ric_ReportStyle_List *ricReportStyle;
+   struct E2SM_KPM_RANfunction_Description__ric_EventTriggerStyle_List *eventTriggerStyle;
+   if(ranFunctionDefinition)
+   {
+      ranFuncName = &ranFunctionDefinition->ranFunction_Name;
+      /* Free RAN function Name */     
+      DU_FREE(ranFuncName->ranFunction_ShortName.buf,  ranFuncName->ranFunction_ShortName.size);
+      DU_FREE(ranFuncName->ranFunction_E2SM_OID.buf, ranFuncName->ranFunction_E2SM_OID.size);
+      DU_FREE(ranFuncName->ranFunction_Description.buf, ranFuncName->ranFunction_Description.size);
+
+      /* Sequence of Event Trigger styles */
+      eventTriggerStyle = ranFunctionDefinition->ric_EventTriggerStyle_List;
+      if(eventTriggerStyle)
+      {
+         if(eventTriggerStyle->list.array)
+         {
+            for(eventTriggerIdx =0;eventTriggerIdx<eventTriggerStyle->list.count; eventTriggerIdx++)
+            {
+               if(eventTriggerStyle->list.array[eventTriggerIdx])
+               {
+                  DU_FREE(eventTriggerStyle->list.array[eventTriggerIdx]->ric_EventTriggerStyle_Name.buf,\
+                        eventTriggerStyle->list.array[eventTriggerIdx]->ric_EventTriggerStyle_Name.size);
+                  DU_FREE(eventTriggerStyle->list.array[eventTriggerIdx], sizeof(RIC_EventTriggerStyle_Item_t ));
+               }
+            }
+            DU_FREE(eventTriggerStyle->list.array, eventTriggerStyle->list.size)
+         }
+         DU_FREE(eventTriggerStyle, sizeof(struct E2SM_KPM_RANfunction_Description__ric_EventTriggerStyle_List));
+      }
+      
+      /* Sequence of Report styles */
+      ricReportStyle = ranFunctionDefinition->ric_ReportStyle_List;
+      if(ricReportStyle)
+      {
+         if(ricReportStyle->list.array)
+         {
+            for(reportStyleIdx =0;reportStyleIdx<ricReportStyle->list.count; reportStyleIdx++)
+            {
+               if(ricReportStyle->list.array[reportStyleIdx])
+               {
+                  if(ricReportStyle->list.array[reportStyleIdx]->ric_ReportStyle_Name.buf)
+                  {
+                     DU_FREE(ricReportStyle->list.array[reportStyleIdx]->ric_ReportStyle_Name.buf,\
+                           ricReportStyle->list.array[reportStyleIdx]->ric_ReportStyle_Name.size);
+                  }
+                  if(ricReportStyle->list.array[reportStyleIdx]->measInfo_Action_List.list.array)
+                  {
+                     for(measInfoIdx=0; measInfoIdx<ricReportStyle->list.array[reportStyleIdx]->measInfo_Action_List.list.count; \
+                           measInfoIdx++)
+                     {
+                        measInfoList = ricReportStyle->list.array[reportStyleIdx]->measInfo_Action_List.list.array[measInfoIdx];
+                        if(measInfoList)
+                        {
+                           DU_FREE(measInfoList->measID, sizeof(long));
+                           DU_FREE(measInfoList->measName.buf, measInfoList->measName.size);
+                           DU_FREE(measInfoList,sizeof(MeasurementInfo_Action_Item_t)); 
+                        }
+                     }
+                     DU_FREE(measInfoList,ricReportStyle->list.array[reportStyleIdx]->measInfo_Action_List.list.size);
+                  }
+                  DU_FREE(ricReportStyle->list.array[reportStyleIdx], sizeof(RIC_ReportStyle_Item_t));
+               }
+            }
+            DU_FREE(ricReportStyle->list.array, ricReportStyle->list.size);
+         }
+         DU_FREE(ricReportStyle, sizeof(struct E2SM_KPM_RANfunction_Description__ric_ReportStyle_List));
+      }
+      DU_FREE(ranFunctionDefinition, sizeof(E2SM_KPM_RANfunction_Description_t)); 
+   }
+}
+
+/*******************************************************************
+ *
+ * @brief fill the e2sm ric report style
+ *
+ * @details
+ *
+ *    Function : fillRicReportStyle
+ *
+ *    Functionality: fill the report style
+ *
+ * @params[in]   RanFunction *ranFuncDb, struct
+ * E2SM_KPM_RANfunction_Description__ric_ReportStyle_List *ricReportStyle
+ * @return ROK     - success
+ *         RFAILED - failure
+ *
+ ******************************************************************/
+uint8_t fillRicReportStyle(RanFunction *ranFuncDb, struct E2SM_KPM_RANfunction_Description__ric_ReportStyle_List *ricReportStyle)
+{
+   uint8_t styleIdx, measInfoIdx;
+   MeasurementInfo_Action_List_t *measInfo;
+   CmLList  *node;
+   
+   ricReportStyle->list.count = ranFuncDb->numOfReportStyleSupported;
+   ricReportStyle->list.size = ricReportStyle->list.count * sizeof(RIC_ReportStyle_Item_t*);
+   DU_ALLOC(ricReportStyle->list.array, ricReportStyle->list.size);
+   if(!ricReportStyle->list.array)
+   {
+      DU_LOG("\nERROR  --> E2AP: Memory allocation failed for ranFuncDefinition %d",__LINE__);
+      return RFAILED;
+   }
+
+   for(styleIdx =0;styleIdx<ricReportStyle->list.count; styleIdx++)
+   {
+      DU_ALLOC(ricReportStyle->list.array[styleIdx], sizeof(RIC_ReportStyle_Item_t));
+      if(!ricReportStyle->list.array[styleIdx])
+      {
+         DU_LOG("\nERROR  --> E2AP: Memory allocation failed in function %s at %d",__func__,__LINE__);
+         return RFAILED;
+      }
+      
+      /* RIC Report Style Type */
+      ricReportStyle->list.array[styleIdx]->ric_ReportStyle_Type = ranFuncDb->reportStyleList[styleIdx].reportStyle.styleType;
+      
+      /* RIC Report Style Format Type */
+      ricReportStyle->list.array[styleIdx]->ric_ActionFormat_Type = ranFuncDb->reportStyleList[styleIdx].reportStyle.formatType;
+      
+      /* RIC Report Style Name */
+      ricReportStyle->list.array[styleIdx]->ric_ReportStyle_Name.size = strlen(ranFuncDb->reportStyleList[styleIdx].reportStyle.name);
+      DU_ALLOC(ricReportStyle->list.array[styleIdx]->ric_ReportStyle_Name.buf,\
+            ricReportStyle->list.array[styleIdx]->ric_ReportStyle_Name.size);
+      if(!ricReportStyle->list.array[styleIdx]->ric_ReportStyle_Name.buf)
+      {
+         DU_LOG("\nERROR  --> E2AP: Memory allocation failed in function %s at %d",__func__,__LINE__);
+         return RFAILED;
+      }
+      memcpy(ricReportStyle->list.array[styleIdx]->ric_ReportStyle_Name.buf, ranFuncDb->reportStyleList[styleIdx].reportStyle.name,\
+            ricReportStyle->list.array[styleIdx]->ric_ReportStyle_Name.size);
+
+      /* RIC Indication Header Format Type*/
+      ricReportStyle->list.array[styleIdx]->ric_IndicationHeaderFormat_Type = ranFuncDb->ricIndicationHeaderFormat;
+
+      /* RIC Indication Message Format Type*/
+      ricReportStyle->list.array[styleIdx]->ric_IndicationMessageFormat_Type = ranFuncDb->ricIndicationMessageFormat;
+      
+      /* Measurement Info Action List */
+      CmLListCp measInfoList =ranFuncDb->reportStyleList[styleIdx].measurementInfoList;
+      if(!measInfoList.count)
+      {
+         continue;      
+      }
+
+      CM_LLIST_FIRST_NODE(&ranFuncDb->reportStyleList[styleIdx].measurementInfoList, node);
+      measInfo = &ricReportStyle->list.array[styleIdx]->measInfo_Action_List;
+
+      measInfo->list.count = measInfoList.count; 
+      measInfo->list.size =  measInfoList.count*sizeof(MeasurementInfo_Action_Item_t*);
+      DU_ALLOC(measInfo->list.array, measInfo->list.size);
+      if(!measInfo->list.array)
+      {
+         DU_LOG("\nERROR  --> E2AP: Memory allocation failed in function %s at %d",__func__,__LINE__);
+         return RFAILED;
+      }
+
+      for(measInfoIdx=0; measInfoIdx<measInfo->list.count; measInfoIdx++)
+      {
+         if(!node)
+         {
+            DU_LOG("\nERROR  --> E2AP: Measurement info node is null");
+            return RFAILED;
+         }
+
+         DU_ALLOC(measInfo->list.array[measInfoIdx],sizeof(MeasurementInfo_Action_Item_t));  
+         if(!measInfo->list.array[measInfoIdx])
+         {
+            DU_LOG("\nERROR  --> E2AP: Memory allocation failed in function %s at %d",__func__,__LINE__);
+            return RFAILED;
+         }
+         MeasurementInfoForAction *measInfoForAction= (MeasurementInfoForAction*)node->node;
+         DU_ALLOC(measInfo->list.array[measInfoIdx]->measID, sizeof(long));
+         if(!measInfo->list.array[measInfoIdx]->measID)
+         {
+            DU_LOG("\nERROR  --> E2AP: Memory allocation failed in function %s at %d",__func__,__LINE__);
+            return RFAILED;
+         }
+         
+         memcpy(measInfo->list.array[measInfoIdx]->measID, &measInfoForAction->measurementTypeId,sizeof(long));
+         measInfo->list.array[measInfoIdx]->measName.size= strlen(measInfoForAction->measurementTypeName);
+         DU_ALLOC(measInfo->list.array[measInfoIdx]->measName.buf, measInfo->list.array[measInfoIdx]->measName.size);
+         if(!measInfo->list.array[measInfoIdx]->measName.size)
+         {
+            DU_LOG("\nERROR  --> E2AP: Memory allocation failed in function %s at %d",__func__,__LINE__);
+            return RFAILED;
+         }
+
+         memcpy(measInfo->list.array[measInfoIdx]->measName.buf, \
+               measInfoForAction->measurementTypeName,\
+               measInfo->list.array[measInfoIdx]->measName.size);
+         node = node->next;
+      }
+
+   }
+   return ROK;
+}
+/*******************************************************************
+ *
+ * @brief fill the ric event trigger style
+ *
+ * @details
+ *
+ *    Function : fillRicEventTriggerStyle
+ *
+ *    Functionality: fill the ric event trigger style
+ *
+ * @params[in]   
+ * @return ROK     - success
+ *         RFAILED - failure
+ *
+ ******************************************************************/
+uint8_t fillRicEventTriggerStyle(RanFunction *ranFuncDb, struct E2SM_KPM_RANfunction_Description__ric_EventTriggerStyle_List *ricEventTriggerStyle)
+{
+   uint8_t styleIdx;
+
+   ricEventTriggerStyle->list.count = ranFuncDb->numOfEventTriggerStyleSupported;
+   ricEventTriggerStyle->list.size = ricEventTriggerStyle->list.count*  sizeof(RIC_EventTriggerStyle_Item_t *);
+   DU_ALLOC(ricEventTriggerStyle->list.array, ricEventTriggerStyle->list.size);
+   if(!ricEventTriggerStyle->list.array)
+   {
+      DU_LOG("\nERROR  --> E2AP: Memory allocation failed for ric_EventTriggerStyle_List %d",__LINE__);
+      return RFAILED;
+   }
+
+   for(styleIdx =0;styleIdx<ricEventTriggerStyle->list.count; styleIdx++)
+   {
+      DU_ALLOC(ricEventTriggerStyle->list.array[styleIdx], sizeof(RIC_EventTriggerStyle_Item_t ));
+      if(!ricEventTriggerStyle->list.array[styleIdx])
+      {
+         DU_LOG("\nERROR  --> E2AP: Memory allocation failed in function %s at %d",__func__,__LINE__);
+         return RFAILED;
+      }
+      ricEventTriggerStyle->list.array[styleIdx]->ric_EventTriggerStyle_Type = ranFuncDb->eventTriggerStyleList[styleIdx].styleType;
+
+      ricEventTriggerStyle->list.array[styleIdx]->ric_EventTriggerFormat_Type = ranFuncDb->eventTriggerStyleList[styleIdx].formatType;
+
+      ricEventTriggerStyle->list.array[styleIdx]->ric_EventTriggerStyle_Name.size = strlen(ranFuncDb->eventTriggerStyleList[styleIdx].name);
+      DU_ALLOC(ricEventTriggerStyle->list.array[styleIdx]->ric_EventTriggerStyle_Name.buf,\
+            ricEventTriggerStyle->list.array[styleIdx]->ric_EventTriggerStyle_Name.size);
+      if(!ricEventTriggerStyle->list.array[styleIdx]->ric_EventTriggerStyle_Name.buf)
+      {
+         DU_LOG("\nERROR  --> E2AP: Memory allocation failed in function %s at %d",__func__,__LINE__);
+         return RFAILED;
+      }
+      memcpy(ricEventTriggerStyle->list.array[styleIdx]->ric_EventTriggerStyle_Name.buf,ranFuncDb->eventTriggerStyleList[styleIdx].name,\
+            ricEventTriggerStyle->list.array[styleIdx]->ric_EventTriggerStyle_Name.size);
+   
+   }
+   return ROK;
+}
+
+/*******************************************************************
+ *
+ * @brief Builds Ran function item
+ *
+ * @details
+ *
+ *    Function : BuildRanFunctionItem  
+ *
+ *    Functionality: Building RAN function item
+ *
+ * @params[in] 
+ *             RAN function item that has to be filled 
+ *             Stored RAN Function information
+ * @return ROK     - success
+ *         RFAILED - failure
+ *
+ ******************************************************************/
+
+uint8_t BuildRanFunctionItem(RANfunction_Item_t *ranFuncItem, RanFunction *ranFuncDb)
+{
+   uint8_t ret =RFAILED;
+   RANfunctionDefinition_t  *ranFunctionDefinition;
+   RANfunction_Name_t *ranFuncName;
+   asn_enc_rval_t encRetVal;
+   E2SM_KPM_RANfunction_Description_t *ranFuncDefinition;
+   
+   while(true)
+   {
+      /* RAN function Id*/
+      ranFuncItem->ranFunctionID = ranFuncDb->id;
+
+      /* RAN Function Revision*/
+      ranFuncItem->ranFunctionRevision = ranFuncDb->revisionCounter;
+
+      /* RAN function OID*/
+      ranFuncItem->ranFunctionOID.size = strlen(ranFuncDb->name.serviceModelOID);
+      DU_ALLOC(ranFuncItem->ranFunctionOID.buf, ranFuncItem->ranFunctionOID.size);
+      if(!ranFuncItem->ranFunctionOID.buf)
+      {
+         DU_LOG("\nERROR  --> E2AP: Memory allocation failed in function %s at %d",__func__,__LINE__);
+         break;
+      }
+      memcpy(ranFuncItem->ranFunctionOID.buf, ranFuncDb->name.serviceModelOID, ranFuncItem->ranFunctionOID.size);
+
+      /* RAN function Definition */
+      DU_ALLOC(ranFuncDefinition, sizeof(E2SM_KPM_RANfunction_Description_t));
+      if(!ranFuncDefinition)
+      {
+         DU_LOG("\nERROR  --> E2AP: Memory allocation failed in function %s at %d",__func__,__LINE__);
+         break;
+      }
+
+      /* RAN function Name */
+      ranFuncName = &ranFuncDefinition->ranFunction_Name;
+
+      /* RAN function ShortName */
+      ranFuncName->ranFunction_ShortName.size = strlen(ranFuncDb->name.shortName); 
+      DU_ALLOC(ranFuncName->ranFunction_ShortName.buf,  ranFuncName->ranFunction_ShortName.size);
+      if(!ranFuncName->ranFunction_ShortName.buf)
+      {
+         DU_LOG("\nERROR  --> E2AP: Memory allocation failed in function %s at %d",__func__,__LINE__);
+         break;
+      }
+      memcpy(ranFuncName->ranFunction_ShortName.buf, ranFuncDb->name.shortName, strlen(ranFuncDb->name.shortName));
+
+      /* RAN function E2SM_OID */
+      ranFuncName->ranFunction_E2SM_OID.size = strlen(ranFuncDb->name.serviceModelOID);
+      DU_ALLOC(ranFuncName->ranFunction_E2SM_OID.buf, ranFuncName->ranFunction_E2SM_OID.size);
+      if(!ranFuncName->ranFunction_E2SM_OID.buf)
+      {
+         DU_LOG("\nERROR  --> E2AP: Memory allocation failed in function %s at %d",__func__,__LINE__);
+         break;
+      }
+      memcpy(ranFuncName->ranFunction_E2SM_OID.buf, ranFuncDb->name.serviceModelOID, ranFuncName->ranFunction_E2SM_OID.size);
+
+      /* RAN Function Name Description */
+      ranFuncName->ranFunction_Description.size = strlen(ranFuncDb->name.description);
+      DU_ALLOC(ranFuncName->ranFunction_Description.buf, ranFuncName->ranFunction_Description.size);
+      if(!ranFuncName->ranFunction_Description.buf)
+      {
+         DU_LOG("\nERROR  --> E2AP: Memory allocation failed in function %s at %d",__func__,__LINE__);
+         break;
+      }
+      memcpy(ranFuncName->ranFunction_Description.buf, ranFuncDb->name.description, ranFuncName->ranFunction_Description.size);
+
+      /* RIC Event Trigger Style List */
+      DU_ALLOC(ranFuncDefinition->ric_EventTriggerStyle_List, sizeof(struct E2SM_KPM_RANfunction_Description__ric_EventTriggerStyle_List));
+      if(!ranFuncDefinition->ric_EventTriggerStyle_List)
+      {
+         DU_LOG("\nERROR  --> E2AP: Memory allocation failed in function %s at %d",__func__,__LINE__);
+         break;
+      }
+
+      if(fillRicEventTriggerStyle(ranFuncDb, ranFuncDefinition->ric_EventTriggerStyle_List)!=ROK)
+      {
+         DU_LOG("\nERROR  --> E2AP: failed to fill ric event trigger style");
+         break;
+      }
+
+      /* RIC Report Style List */
+      DU_ALLOC(ranFuncDefinition->ric_ReportStyle_List, sizeof(struct E2SM_KPM_RANfunction_Description__ric_ReportStyle_List));
+      if(!ranFuncDefinition->ric_ReportStyle_List)
+      {
+         DU_LOG("\nERROR  --> E2AP: Memory allocation failed in function %s at %d",__func__,__LINE__);
+         break;
+      }
+      if(fillRicReportStyle(ranFuncDb, ranFuncDefinition->ric_ReportStyle_List) != ROK)
+      {
+         DU_LOG("\nERROR  --> E2AP: failed to fill ric report style");
+         break;
+      }
+
+      /* Encode the F1SetupRequest type as APER */
+      xer_fprint(stdout, &asn_DEF_E2SM_KPM_RANfunction_Description, ranFuncDefinition);
+
+      memset(encBuf, 0, ENC_BUF_MAX_LEN);
+      encBufSize = 0;
+      encRetVal = aper_encode(&asn_DEF_E2SM_KPM_RANfunction_Description, 0, ranFuncDefinition, PrepFinalEncBuf, encBuf);
+
+      /* Encode results */
+      if(encRetVal.encoded == ENCODE_FAIL)
+      {
+         DU_LOG("\nERROR  -->  F1AP : Could not encode RAN function definition  (at %s)\n",\
+               encRetVal.failed_type ? encRetVal.failed_type->name : "unknown");
+         break;
+      }
+      else
+      {
+         DU_LOG("\nDEBUG   -->  F1AP : Created APER encoded buffer for RAN function definition \n");
+         for(uint8_t measIeIdx=0; measIeIdx< encBufSize; measIeIdx++)
+         {
+            printf("%x",encBuf[measIeIdx]);
+         }
+         ranFunctionDefinition = &ranFuncItem->ranFunctionDefinition; 
+         ranFunctionDefinition->size = encBufSize;
+         DU_ALLOC(ranFunctionDefinition->buf, encBufSize);
+         if(ranFunctionDefinition->buf == NULLP)
+         {
+            DU_LOG("\nERROR  -->  F1AP : Memory allocation failed for RAN function definition buffer");
+            break;
+         }
+         memcpy(ranFunctionDefinition->buf, &encBuf, encBufSize);
+         ret = ROK;
+         break;
+      }
+   }
+   freeE2smKpmRanFunctionDefinition(ranFuncDefinition);
+   return ret;
+}
+
+/*******************************************************************
+ *
+ * @brief Builds Ran function add list based on the procedure code
+ *
+ * @details
+ *
+ *    Function : BuildRanFunctionAddList 
+ *
+ *    Functionality: Building RAN addition addition list
+ *       In case of ProcedureCodeE2_id_E2setup we add all the RAN Function list
+ *       which is present in E2 database.
+ *       In the case of other procedures, we just fill the RAN functions whose ID 
+ *       is contained in recvList
+ *
+ * @params[in] 
+ *       RAN Function list
+ *       Procedure code
+ *       Count of ran functions to be added in the list
+ *       Received list of RAN functions
+ *
+ * @return ROK     - success
+ *         RFAILED - failure
+ *
+ ******************************************************************/
+
+uint8_t BuildRanFunctionAddList(RANfunctions_List_t *ranFunctionsList, uint8_t procedureCode, uint8_t count, RanFuncInfo *recvList)
+{
+   uint16_t id;
+   RanFunction *ranFuncDb;
+   uint8_t ranFuncIdx;
+   RANfunction_ItemIEs_t *ranFuncItemIe;
+   
+   /* For ProcedureCodeE2_id_E2setup, the number of RAN function list items is
+    * equal to the number of ran function entries stored in the database.
+    * For any other procedure, the RAN function list count is equal
+    * to the count of ran functions obtained from the function's caller */
+
+   if(procedureCode == ProcedureCodeE2_id_E2setup)
+      ranFunctionsList->list.count = duCb.e2apDb.numOfRanFunction;
+   else
+      ranFunctionsList->list.count = count;
+
+   ranFunctionsList->list.size = ranFunctionsList->list.count * sizeof(RANfunction_ItemIEs_t*);
+   DU_ALLOC(ranFunctionsList->list.array, ranFunctionsList->list.size);
+   if(ranFunctionsList->list.array == NULLP)
+   {
+      DU_LOG("\nERROR  --> E2AP: Memory allocation failed in %s at %d",__func__, __LINE__);
+      return RFAILED;
+   }
+
+   for(ranFuncIdx = 0; ranFuncIdx< ranFunctionsList->list.count; ranFuncIdx++)
+   {
+      DU_ALLOC(ranFunctionsList->list.array[ranFuncIdx], sizeof(RANfunction_ItemIEs_t));
+      if(ranFunctionsList->list.array[ranFuncIdx] == NULLP)
+      {
+         DU_LOG("\nERROR  --> E2AP: Memory allocation failed in %s at %d",__func__, __LINE__);
+         return RFAILED;
+      }
+      if(procedureCode == ProcedureCodeE2_id_E2setup) 
+      {
+         /* Getting all of the RAN function's information from DuCb one by one*/
+         ranFuncDb = &duCb.e2apDb.ranFunction[ranFuncIdx];
+      }
+      else
+      {
+         /* Getting only the RAN function information from DuCb whose Id is
+          * present in the received array */
+         id =recvList[ranFuncIdx].id;
+         ranFuncDb = &duCb.e2apDb.ranFunction[id-1];
+      }
+      ranFuncItemIe = (RANfunction_ItemIEs_t *) ranFunctionsList->list.array[ranFuncIdx];
+      ranFuncItemIe->id = ProtocolIE_IDE2_id_RANfunction_Item;
+      ranFuncItemIe->criticality = CriticalityE2_ignore;
+      ranFuncItemIe->value.present = RANfunction_ItemIEs__value_PR_RANfunction_Item;
+      BuildRanFunctionItem(&ranFuncItemIe->value.choice.RANfunction_Item, ranFuncDb);
+   }
+   return ROK;
+}   
+
 /*******************************************************************
  *
  * @brief De Allocate E2 Setup Request Message
@@ -278,10 +804,13 @@ uint8_t BuildE2NodeConfigAddList(E2nodeComponentConfigAddition_List_t *e2NodeAdd
 void FreeE2SetupReq(E2AP_PDU_t *e2apMsg)
 {
    uint8_t arrIdx = 0;
-   uint8_t e2NodeAddListIdx =0;
+   uint8_t e2NodeAddListIdx =0, ranFuncAddListIdx;
    E2setupRequest_t *e2SetupReq;
    E2nodeComponentConfigAddition_List_t *e2NodeAddList;
    E2nodeComponentConfigAddition_ItemIEs_t *e2NodeAddItem;
+   RANfunctions_List_t *ranFunctionsList;
+   RANfunction_ItemIEs_t *ranFuncItemIe;
+   RANfunction_Item_t  *ranFunItem;
 
    /* De-allocating Memory */
    if(e2apMsg != NULLP)
@@ -310,10 +839,16 @@ void FreeE2SetupReq(E2AP_PDU_t *e2apMsg)
                               if(gNbId->global_gNB_ID.plmn_id.buf != NULLP)
                               {
                                  DU_FREE(gNbId->global_gNB_ID.gnb_id.choice.gnb_ID.buf,\
-                                          gNbId->global_gNB_ID.gnb_id.choice.gnb_ID.size);
+                                       gNbId->global_gNB_ID.gnb_id.choice.gnb_ID.size);
                                  DU_FREE(gNbId->global_gNB_ID.plmn_id.buf,\
                                        gNbId->global_gNB_ID.plmn_id.size);
                               }
+
+                              if(gNbId->gNB_DU_ID != NULLP)
+                              {
+                                 DU_FREE( gNbId->gNB_DU_ID->buf, gNbId->gNB_DU_ID->size);
+                                 DU_FREE(gNbId->gNB_DU_ID, sizeof(GNB_DU_ID_t));
+                              }
                               DU_FREE(e2SetupReq->protocolIEs.list.array[arrIdx]->value.\
                                     choice.GlobalE2node_ID.choice.gNB, sizeof(GlobalE2node_gNB_ID_t));
                            }
@@ -353,6 +888,27 @@ void FreeE2SetupReq(E2AP_PDU_t *e2apMsg)
                          }
                          break;
                      }
+                     case ProtocolIE_IDE2_id_RANfunctionsAdded:
+                     {
+                        ranFunctionsList = &(e2SetupReq->protocolIEs.list.array[arrIdx]->value.choice.RANfunctions_List);  
+                        if(ranFunctionsList->list.array)
+                        {  
+                           for(ranFuncAddListIdx= 0; ranFuncAddListIdx< ranFunctionsList->list.count; ranFuncAddListIdx++)
+                           {
+                              if(ranFunctionsList->list.array[ranFuncAddListIdx])
+                              {
+                                 ranFuncItemIe = (RANfunction_ItemIEs_t *) ranFunctionsList->list.array[ranFuncAddListIdx];
+                                 ranFunItem = &ranFuncItemIe->value.choice.RANfunction_Item;
+                                 DU_FREE(ranFunItem->ranFunctionOID.buf, ranFunItem->ranFunctionOID.size);
+                                 DU_FREE(ranFunItem->ranFunctionDefinition.buf, ranFunItem->ranFunctionDefinition.size);
+                                 DU_FREE(ranFunctionsList->list.array[ranFuncAddListIdx], sizeof(RANfunction_ItemIEs_t));
+                              }
+                           }
+                           DU_FREE(ranFunctionsList->list.array, ranFunctionsList->list.size);
+                        }
+                        break;
+                     }
+
                      default:
                         DU_LOG("\nERROR  --> E2AP: Invalid event at e2SetupRequet %ld ",\
                               (e2SetupReq->protocolIEs.list.array[arrIdx]->id));
@@ -414,7 +970,7 @@ uint8_t BuildAndSendE2SetupReq()
       e2apMsg->choice.initiatingMessage->value.present = InitiatingMessageE2__value_PR_E2setupRequest;
       e2SetupReq = &e2apMsg->choice.initiatingMessage->value.choice.E2setupRequest;
 
-      elementCnt = 3;
+      elementCnt = 4;
       e2SetupReq->protocolIEs.list.count = elementCnt;
       e2SetupReq->protocolIEs.list.size = elementCnt * sizeof(E2setupRequestIEs_t*);
 
@@ -475,14 +1031,25 @@ uint8_t BuildAndSendE2SetupReq()
          }
       }
       
+      /* RAN Functions Added List */
       arrIdx++;
+      e2SetupReq->protocolIEs.list.array[arrIdx]->id = ProtocolIE_IDE2_id_RANfunctionsAdded;
+      e2SetupReq->protocolIEs.list.array[arrIdx]->criticality = CriticalityE2_reject;
+      e2SetupReq->protocolIEs.list.array[arrIdx]->value.present = E2setupRequestIEs__value_PR_RANfunctions_List;
+      if(BuildRanFunctionAddList(&(e2SetupReq->protocolIEs.list.array[arrIdx]->value.choice.RANfunctions_List), ProcedureCodeE2_id_E2setup, 0, NULL)!=ROK)      
+      {
+         DU_LOG("\nERROR  -->  E2AP : Failed to create RAN Function");
+         break;
+      }
+
       /* E2 Node Component Configuration Addition List */
+      arrIdx++;
       e2SetupReq->protocolIEs.list.array[arrIdx]->id = ProtocolIE_IDE2_id_E2nodeComponentConfigAddition;
       e2SetupReq->protocolIEs.list.array[arrIdx]->criticality = CriticalityE2_reject;
       e2SetupReq->protocolIEs.list.array[arrIdx]->value.present = E2setupRequestIEs__value_PR_E2nodeComponentConfigAddition_List;
       if(BuildE2NodeConfigAddList(&(e2SetupReq->protocolIEs.list.array[arrIdx]->value.choice.E2nodeComponentConfigAddition_List))!=ROK)
       {
-         DU_LOG("\nERROR  -->  E2AP : Failed to E2 Node config addition list");
+         DU_LOG("\nERROR  -->  E2AP : Failed to create E2 Node config list");
          break;
       }
 
@@ -518,8 +1085,8 @@ uint8_t BuildAndSendE2SetupReq()
       break;
    }while(true);
 
-   duCb.e2apDb.e2TransInfo.onGoingTransaction[transId].transactionId = transId;
-   duCb.e2apDb.e2TransInfo.onGoingTransaction[transId].procedureCode = e2apMsg->choice.initiatingMessage->procedureCode;
+   duCb.e2apDb.e2TransInfo.e2InitTransaction[transId].transactionId = transId;
+   duCb.e2apDb.e2TransInfo.e2InitTransaction[transId].procedureCode = e2apMsg->choice.initiatingMessage->procedureCode;
    
    FreeE2SetupReq(e2apMsg);
    return ret;
@@ -1000,9 +1567,11 @@ uint8_t procE2SetupRsp(E2AP_PDU_t *e2apMsg)
          case ProtocolIE_IDE2_id_TransactionID:
             {
                transId = e2SetRspMsg->protocolIEs.list.array[arrIdx]->value.choice.TransactionID;
-               if((duCb.e2apDb.e2TransInfo.onGoingTransaction[transId].transactionId == transId) &&\
-                     (duCb.e2apDb.e2TransInfo.onGoingTransaction[transId].procedureCode == e2apMsg->choice.successfulOutcome->procedureCode))
-                  memset(&duCb.e2apDb.e2TransInfo.onGoingTransaction[transId], 0, sizeof(E2TransInfo));
+               if((duCb.e2apDb.e2TransInfo.e2InitTransaction[transId].transactionId == transId) &&\
+                     (duCb.e2apDb.e2TransInfo.e2InitTransaction[transId].procedureCode == e2apMsg->choice.successfulOutcome->procedureCode))
+               {
+                  memset(&duCb.e2apDb.e2TransInfo.e2InitTransaction[transId], 0, sizeof(E2TransInfo));
+               }
                else
                {
                   DU_LOG("\nERROR  -->  E2AP : Invalid transaction id [%d]", transId);
@@ -1144,6 +1713,15 @@ uint8_t procRicSubsReq(E2AP_PDU_t *e2apMsg)
                      }
                      free(ricSubsReq->protocolIEs.list.array[idx]->value.choice.RICsubscriptionDetails.ricAction_ToBeSetup_List.\
                            list.array);
+
+#ifdef KPI_CALCULATION 
+                     /* This is a dummy trigger for statistics request. It will
+                      * be removed in next gerrit and actual statistics request
+                      * will be sent when RIC subscription request is received
+                      * from RIC */
+                     ricSubscriptionInfo->actionSequence[0].definition.styleType = 1;
+                     BuildAndSendStatsReq(ricSubscriptionInfo->actionSequence[0].definition);
+#endif
                   }
                   break;
                }
@@ -1781,8 +2359,8 @@ uint8_t BuildAndSendE2ResetRequest(E2CauseType failureType, E2Cause failureCause
 
       /* In case the message is sent successfully, store the transaction info to
        * be used when response is received */
-      duCb.e2apDb.e2TransInfo.onGoingTransaction[transId].transactionId = transId;
-      duCb.e2apDb.e2TransInfo.onGoingTransaction[transId].procedureCode = e2apMsg->choice.initiatingMessage->procedureCode;
+      duCb.e2apDb.e2TransInfo.e2InitTransaction[transId].transactionId = transId;
+      duCb.e2apDb.e2TransInfo.e2InitTransaction[transId].procedureCode = e2apMsg->choice.initiatingMessage->procedureCode;
 
       ret = ROK;
       break;
@@ -1865,9 +2443,11 @@ uint8_t procResetResponse(E2AP_PDU_t *e2apMsg)
       {
          case ProtocolIE_IDE2_id_TransactionID:
             transId = resetResponse->protocolIEs.list.array[ieIdx]->value.choice.TransactionID;
-            if((duCb.e2apDb.e2TransInfo.onGoingTransaction[transId].transactionId == transId) && \
-                  (duCb.e2apDb.e2TransInfo.onGoingTransaction[transId].procedureCode == e2apMsg->choice.successfulOutcome->procedureCode))
-              memset(&duCb.e2apDb.e2TransInfo.onGoingTransaction[transId], 0, sizeof(E2TransInfo));
+            if((duCb.e2apDb.e2TransInfo.e2InitTransaction[transId].transactionId == transId) && \
+                  (duCb.e2apDb.e2TransInfo.e2InitTransaction[transId].procedureCode == e2apMsg->choice.successfulOutcome->procedureCode))
+            {
+               memset(&duCb.e2apDb.e2TransInfo.e2InitTransaction[transId], 0, sizeof(E2TransInfo));
+            }
             else
             {
                DU_LOG("\nERROR  -->  E2AP : Invalid transaction id [%d]", transId);
@@ -1958,9 +2538,11 @@ void procE2SetupFailure(E2AP_PDU_t *e2apMsg)
          case ProtocolIE_IDE2_id_TransactionID:
          {
             transId = e2SetupFailure->protocolIEs.list.array[arrIdx]->value.choice.TransactionID;
-            if((duCb.e2apDb.e2TransInfo.onGoingTransaction[transId].transactionId == transId) &&\
-            (duCb.e2apDb.e2TransInfo.onGoingTransaction[transId].procedureCode == e2apMsg->choice.unsuccessfulOutcome->procedureCode))
-              memset(&duCb.e2apDb.e2TransInfo.onGoingTransaction[transId], 0, sizeof(E2TransInfo));
+            if((duCb.e2apDb.e2TransInfo.e2InitTransaction[transId].transactionId == transId) &&\
+                  (duCb.e2apDb.e2TransInfo.e2InitTransaction[transId].procedureCode == e2apMsg->choice.unsuccessfulOutcome->procedureCode))
+            {
+               memset(&duCb.e2apDb.e2TransInfo.e2InitTransaction[transId], 0, sizeof(E2TransInfo));
+            }
             else
             {
                DU_LOG("\nERROR  -->  E2AP : Invalid transaction id [%d]", transId);
@@ -1971,9 +2553,9 @@ void procE2SetupFailure(E2AP_PDU_t *e2apMsg)
          case ProtocolIE_IDE2_id_TimeToWaitE2:
             {
                timerValue = covertE2WaitTimerEnumToValue(e2SetupFailure->protocolIEs.list.array[arrIdx]->value.choice.TimeToWaitE2);
-               if((duChkTmr((PTR)&(duCb.e2apDb), EVENT_E2_SETUP_TMR)) == FALSE)
+               if((duChkTmr((PTR)&(duCb.e2apDb.e2TimersInfo.e2Timers.e2SetupTimer), EVENT_E2_SETUP_TMR)) == FALSE)
                {
-                  duStartTmr((PTR)&(duCb.e2apDb), EVENT_E2_SETUP_TMR, timerValue);
+                  duStartTmr((PTR)&(duCb.e2apDb.e2TimersInfo.e2Timers.e2SetupTimer), EVENT_E2_SETUP_TMR, timerValue);
                }
                else
                {
@@ -1987,6 +2569,497 @@ void procE2SetupFailure(E2AP_PDU_t *e2apMsg)
 
    freeAperDecodingOfE2SetupFailure(e2SetupFailure);
 }
+/******************************************************************
+ *
+ * @brief Deallocation of memory allocated bu aper decoder for RIC service Query
+ *
+ * @details
+ *
+ *    Function : freeAperDecodingOfRicServiceQuery
+ *
+ *    Functionality: Deallocation of memory allocated bu aper decoder for RIC
+ *    service Query
+ *
+ * @params[in] RICserviceQuery_t *ricServiceQuery;
+ * @return void
+ *
+ * ****************************************************************/
+
+void freeAperDecodingOfRicServiceQuery(RICserviceQuery_t *ricServiceQuery)
+{
+   uint8_t arrIdx,ranFuncIdx;
+    RANfunctionsID_List_t *ranFuncAddedList;
+
+   if(ricServiceQuery)
+   {
+      if(ricServiceQuery->protocolIEs.list.array)
+      {
+         for(arrIdx=0; arrIdx<ricServiceQuery->protocolIEs.list.count; arrIdx++)
+         {
+            if(ricServiceQuery->protocolIEs.list.array[arrIdx])
+            {
+               switch(ricServiceQuery->protocolIEs.list.array[arrIdx]->id)
+               {
+                  case ProtocolIE_IDE2_id_RANfunctionsAccepted:
+                  {
+                     ranFuncAddedList= &ricServiceQuery->protocolIEs.list.array[arrIdx]->value.choice.RANfunctionsID_List;
+                     if(ranFuncAddedList->list.array)
+                     {
+                        for(ranFuncIdx=0;ranFuncIdx<ranFuncAddedList->list.count; ranFuncIdx++)
+                        {
+                           free(ranFuncAddedList->list.array[ranFuncIdx]);
+                        }
+                        free(ranFuncAddedList->list.array);;
+                     }
+                     break;
+                  }
+                  default:
+                     break;
+               }
+               free(ricServiceQuery->protocolIEs.list.array[arrIdx]);
+            }
+         }
+         free(ricServiceQuery->protocolIEs.list.array);
+      }
+   }
+}
+/*******************************************************************
+ *
+ * @brief Build RanFunction Delete List
+ *
+ * @details
+ *
+ *    Function : BuildRanFunctionDeleteList
+ *
+ * Functionality:  Build RanFunction Delete List
+ *
+ * @params[in]
+ *    RANfunctionsID List
+ *    Count of the RAN function
+ *    Received RAN function list
+ *
+ * @return ROK     - success
+ *         RFAILED - failure
+ *
+ ******************************************************************/
+
+uint8_t BuildRanFunctionDeleteList(RANfunctionsID_List_t *deleteList, uint8_t count, RanFuncInfo *recvdRanFunc)
+{
+   uint8_t ranFuncIdx=0;
+   RANfunctionID_ItemIEs_t *delRanFuncItem;
+
+   if(count)
+   {
+      deleteList->list.count = count;
+      deleteList->list.size = deleteList->list.count * sizeof(RANfunctionID_ItemIEs_t*);
+      DU_ALLOC(deleteList->list.array, deleteList->list.size);
+      if(deleteList->list.array == NULLP)
+      {
+         DU_LOG("\nERROR  --> E2AP: Memory allocation failed in %s at %d",__func__, __LINE__);
+         return RFAILED;
+      }
+      for(ranFuncIdx = 0; ranFuncIdx< deleteList->list.count; ranFuncIdx++)
+      {
+         DU_ALLOC(deleteList->list.array[ranFuncIdx], sizeof(RANfunctionID_ItemIEs_t));
+         if(deleteList->list.array[ranFuncIdx] == NULLP)
+         {
+            DU_LOG("\nERROR  --> E2AP: Memory allocation failed in %s at %d",__func__, __LINE__);
+            return RFAILED;
+         }
+         delRanFuncItem= (RANfunctionID_ItemIEs_t *) deleteList->list.array[ranFuncIdx];
+         delRanFuncItem->id = ProtocolIE_IDE2_id_RANfunctionID_Item;
+         delRanFuncItem->criticality = CriticalityE2_ignore;
+         delRanFuncItem->value.choice.RANfunctionID_Item.ranFunctionID = recvdRanFunc[ranFuncIdx].id;
+         delRanFuncItem->value.choice.RANfunctionID_Item.ranFunctionRevision = recvdRanFunc[ranFuncIdx].revisionCounter;
+
+      }
+   }
+   return ROK;
+}
+/*******************************************************************
+ *
+ * @brief De Allocate  Ric Service Update message
+ *
+ * @details
+ *
+ *    Function : FreeRicServiceUpdate
+ *
+ *    Functionality: De-Allocating Ric Service Update message
+ *
+ * @params[in] E2AP_PDU_t *e2apMsg
+
+ * @return void
+ *
+ * ****************************************************************/
+
+void FreeRicServiceUpdate(E2AP_PDU_t *e2apMsg)
+{
+   uint8_t arrIdx = 0;
+   uint8_t ranFuncAddListIdx=0, ranFuncDelIdx=0;
+   RICserviceUpdate_t *ricServiceUpdate;
+   RANfunctions_List_t *ranFunctionsList;
+   RANfunction_ItemIEs_t *ranFuncItemIe;
+   RANfunction_Item_t  *ranFunItem;
+   RANfunctionsID_List_t *deleteList;
+
+   /* De-allocating Memory */
+   if(e2apMsg != NULLP)
+   {
+      if(e2apMsg->choice.initiatingMessage != NULLP)
+      {
+         ricServiceUpdate = &e2apMsg->choice.initiatingMessage->value.choice.RICserviceUpdate;
+         if(ricServiceUpdate->protocolIEs.list.array != NULLP)
+         {
+            for(arrIdx = 0; arrIdx < ricServiceUpdate->protocolIEs.list.count; arrIdx++)
+            {
+               if(ricServiceUpdate->protocolIEs.list.array[arrIdx] != NULLP)
+               {
+                  switch(ricServiceUpdate->protocolIEs.list.array[arrIdx]->id)
+                  {
+                     case ProtocolIE_IDE2_id_TransactionID:
+                        break;
+
+                     case ProtocolIE_IDE2_id_RANfunctionsAdded:
+                     case ProtocolIE_IDE2_id_RANfunctionsModified:
+                        {
+                           ranFunctionsList = &(ricServiceUpdate->protocolIEs.list.array[arrIdx]->value.choice.RANfunctions_List);
+                           if(ranFunctionsList->list.array)
+                           {
+                              for(ranFuncAddListIdx= 0; ranFuncAddListIdx< ranFunctionsList->list.count; ranFuncAddListIdx++)
+                              {
+                                 if(ranFunctionsList->list.array[ranFuncAddListIdx])
+                                 {
+                                    ranFuncItemIe = (RANfunction_ItemIEs_t *) ranFunctionsList->list.array[ranFuncAddListIdx];
+                                    ranFunItem = &ranFuncItemIe->value.choice.RANfunction_Item;
+                                    DU_FREE(ranFunItem->ranFunctionOID.buf, ranFunItem->ranFunctionOID.size);
+                                    DU_FREE(ranFunItem->ranFunctionDefinition.buf, ranFunItem->ranFunctionDefinition.size);
+                                    DU_FREE(ranFunctionsList->list.array[ranFuncAddListIdx], sizeof(RANfunction_ItemIEs_t));
+                                 }
+                              }
+                              DU_FREE(ranFunctionsList->list.array, ranFunctionsList->list.size);
+                           }
+                           break;
+                        }
+                     case ProtocolIE_IDE2_id_RANfunctionsDeleted:
+                        {
+                           deleteList= &ricServiceUpdate->protocolIEs.list.array[arrIdx]->value.choice.RANfunctionsID_List;
+                           if(deleteList->list.array)
+                           {
+                              for(ranFuncDelIdx = 0; ranFuncDelIdx< deleteList->list.count; ranFuncDelIdx++)
+                              {
+                                 DU_FREE(deleteList->list.array[ranFuncDelIdx], sizeof(RANfunctionID_ItemIEs_t));
+                              }
+                              DU_FREE(deleteList->list.array, deleteList->list.size);
+  
+                           }
+                           break;
+                        }
+                     default:
+                        DU_LOG("\nERROR  --> E2AP: Invalid event at ricServiceUpdate %ld ",\
+                              (ricServiceUpdate->protocolIEs.list.array[arrIdx]->id));
+                        break;
+                  }
+                  DU_FREE(ricServiceUpdate->protocolIEs.list.array[arrIdx], sizeof(RICserviceUpdate_IEs_t));
+               }
+            }
+            DU_FREE(ricServiceUpdate->protocolIEs.list.array, ricServiceUpdate->protocolIEs.list.size);
+         }
+         DU_FREE(e2apMsg->choice.initiatingMessage, sizeof(InitiatingMessageE2_t));
+      }
+      DU_FREE(e2apMsg, sizeof(E2AP_PDU_t));
+   }
+}
+
+/*******************************************************************
+ *
+ * @brief Builds and Send the RicServiceUpdateuest
+ *
+ * @details
+ *
+ *    Function : BuildAndSendRicServiceUpdate
+ *
+ * Functionality:Fills the RicServiceUpdateuest
+ *
+ * @return ROK     - success
+ *         RFAILED - failure
+ *
+ ******************************************************************/
+
+uint8_t BuildAndSendRicServiceUpdate(RicServiceUpdate serviceUpdate)
+{
+   uint8_t arrIdx = 0, elementCnt=0;
+   uint8_t transId = 0, ret = RFAILED;
+   bool memAllocFailed =false;
+   E2AP_PDU_t        *e2apMsg = NULLP;
+   RICserviceUpdate_t  *ricServiceUpdate = NULLP;
+   asn_enc_rval_t     encRetVal;       /* Encoder return value */
+
+   DU_LOG("\nINFO   -->  E2AP : Building Ric Service Update\n");
+   do
+   {
+      DU_ALLOC(e2apMsg, sizeof(E2AP_PDU_t));
+      if(e2apMsg == NULLP)
+      {
+         DU_LOG("\nERROR  -->  E2AP : Memory allocation for E2AP-PDU failed");
+         break;
+      }
+      e2apMsg->present = E2AP_PDU_PR_initiatingMessage;
+      DU_ALLOC(e2apMsg->choice.initiatingMessage, sizeof(InitiatingMessageE2_t));
+      if(e2apMsg->choice.initiatingMessage == NULLP)
+      {
+         DU_LOG("\nERROR  -->  E2AP : Memory allocation for E2AP-PDU failed");
+         break;
+      }
+      e2apMsg->choice.initiatingMessage->criticality = CriticalityE2_reject;
+      e2apMsg->choice.initiatingMessage->procedureCode = ProcedureCodeE2_id_RICserviceUpdate;
+      e2apMsg->choice.initiatingMessage->value.present = InitiatingMessageE2__value_PR_RICserviceUpdate;
+      ricServiceUpdate = &e2apMsg->choice.initiatingMessage->value.choice.RICserviceUpdate;
+      
+      /* For TransId IE, set elementCnt to 1.
+      If there is any item in the RAN function add list, RAN function modification list, or RAN function delete list, increment the elementCnt.*/
+
+      elementCnt =1;
+      if(serviceUpdate.recvRanFuncList.numOfRanFunToBeAdded)
+        elementCnt++;
+      if(serviceUpdate.recvRanFuncList.numOfRanFunToBeModified)
+         elementCnt++;
+      if(serviceUpdate.recvRanFuncList.numOfRanFunToBeDeleted)
+         elementCnt++;
+       
+      ricServiceUpdate->protocolIEs.list.count = elementCnt;
+      ricServiceUpdate->protocolIEs.list.size = elementCnt * sizeof(RICserviceUpdate_IEs_t*);
+
+      /* Initialize the E2Setup members */
+      DU_ALLOC(ricServiceUpdate->protocolIEs.list.array, ricServiceUpdate->protocolIEs.list.size);
+      if(ricServiceUpdate->protocolIEs.list.array == NULLP)
+      {
+         DU_LOG("\nERROR  -->  E2AP : Memory allocation failed for array elements");
+         break;
+      }
+      
+      for(arrIdx = 0; arrIdx < elementCnt; (arrIdx)++)
+      {
+         DU_ALLOC(ricServiceUpdate->protocolIEs.list.array[arrIdx], sizeof(RICserviceUpdate_IEs_t));
+         if(ricServiceUpdate->protocolIEs.list.array[arrIdx] == NULLP)
+         {
+            memAllocFailed = true;
+            DU_LOG("\nERROR  -->  E2AP : Memory allocation failed for arrayIdx [%d]", arrIdx);
+            break;
+         }
+      }
+      if(memAllocFailed == true)
+         break;
+
+      arrIdx = 0;
+
+      /* TransactionID */
+      ricServiceUpdate->protocolIEs.list.array[arrIdx]->id = ProtocolIE_IDE2_id_TransactionID;
+      ricServiceUpdate->protocolIEs.list.array[arrIdx]->criticality = CriticalityE2_reject;
+      ricServiceUpdate->protocolIEs.list.array[arrIdx]->value.present = E2setupRequestIEs__value_PR_TransactionID;
+      if(serviceUpdate.dir == E2_NODE_INITIATED)
+         transId = assignTransactionId();
+      else
+        transId = serviceUpdate.transId;
+      ricServiceUpdate->protocolIEs.list.array[arrIdx]->value.choice.TransactionID = transId;
+
+      if(serviceUpdate.recvRanFuncList.numOfRanFunToBeAdded)
+      {
+         arrIdx++;
+         ricServiceUpdate->protocolIEs.list.array[arrIdx]->id = ProtocolIE_IDE2_id_RANfunctionsAdded;
+         ricServiceUpdate->protocolIEs.list.array[arrIdx]->criticality = CriticalityE2_reject;
+         ricServiceUpdate->protocolIEs.list.array[arrIdx]->value.present = RICserviceUpdate_IEs__value_PR_RANfunctions_List;
+         if(BuildRanFunctionAddList(&ricServiceUpdate->protocolIEs.list.array[arrIdx]->value.choice.RANfunctions_List,\
+         e2apMsg->choice.initiatingMessage->procedureCode, serviceUpdate.recvRanFuncList.numOfRanFunToBeAdded, serviceUpdate.recvRanFuncList.ranFunToBeAdded) !=ROK)
+         {
+            break;
+         }
+      }
+
+      if(serviceUpdate.recvRanFuncList.numOfRanFunToBeModified)
+      {
+         arrIdx++;
+         ricServiceUpdate->protocolIEs.list.array[arrIdx]->id = ProtocolIE_IDE2_id_RANfunctionsModified;
+         ricServiceUpdate->protocolIEs.list.array[arrIdx]->criticality = CriticalityE2_reject;
+         ricServiceUpdate->protocolIEs.list.array[arrIdx]->value.present = RICserviceUpdate_IEs__value_PR_RANfunctions_List;
+         if(BuildRanFunctionAddList(&ricServiceUpdate->protocolIEs.list.array[arrIdx]->value.choice.RANfunctions_List,
+         e2apMsg->choice.initiatingMessage->procedureCode, serviceUpdate.recvRanFuncList.numOfRanFunToBeModified, serviceUpdate.recvRanFuncList.ranFunToBeModified) !=ROK)
+         {
+            break;
+         }
+      }
+
+      if(serviceUpdate.recvRanFuncList.numOfRanFunToBeDeleted)
+      {
+         arrIdx++;
+         ricServiceUpdate->protocolIEs.list.array[arrIdx]->id = ProtocolIE_IDE2_id_RANfunctionsDeleted;
+         ricServiceUpdate->protocolIEs.list.array[arrIdx]->criticality = CriticalityE2_reject;
+         ricServiceUpdate->protocolIEs.list.array[arrIdx]->value.present = RICserviceUpdate_IEs__value_PR_RANfunctionsID_List;
+         if(BuildRanFunctionDeleteList(&ricServiceUpdate->protocolIEs.list.array[arrIdx]->value.choice.RANfunctionsID_List,\
+         serviceUpdate.recvRanFuncList.numOfRanFunToBeDeleted, serviceUpdate.recvRanFuncList.ranFunToBeDeleted) != ROK)
+         {
+            break;
+         }
+      }
+      /* Prints the Msg formed */
+      xer_fprint(stdout, &asn_DEF_E2AP_PDU, e2apMsg);
+
+      memset(encBuf, 0, ENC_BUF_MAX_LEN);
+      encBufSize = 0;
+      encRetVal = aper_encode(&asn_DEF_E2AP_PDU, 0, e2apMsg, PrepFinalEncBuf, encBuf);
+      if(encRetVal.encoded == ENCODE_FAIL)
+      {
+         DU_LOG("\nERROR  -->  E2AP : Could not encode RicServiceUpdateuest structure (at %s)\n",\
+               encRetVal.failed_type ? encRetVal.failed_type->name : "unknown");
+         break;
+      }
+      else
+      {
+         DU_LOG("\nDEBUG   -->  E2AP : Created APER encoded buffer for RicServiceUpdateuest\n");
+#ifdef DEBUG_ASN_PRINT
+         for(int i=0; i< encBufSize; i++)
+         {
+            printf("%x",encBuf[i]);
+         }
+#endif
+      }
+      if(SendE2APMsg(DU_APP_MEM_REGION, DU_POOL, encBuf, encBufSize) != ROK)
+      {
+         DU_LOG("\nERROR  -->  E2AP : Sending E2 Setup request failed");
+         break;
+      }
+      ret = ROK;
+      break;
+   }while(true);
+   
+   if(ret == ROK)
+   {
+      if(serviceUpdate.dir == E2_NODE_INITIATED)
+      {
+         duCb.e2apDb.e2TransInfo.e2InitTransaction[transId].transactionId = transId;
+         duCb.e2apDb.e2TransInfo.e2InitTransaction[transId].procedureCode = e2apMsg->choice.initiatingMessage->procedureCode;
+      }
+      else
+      {
+         duCb.e2apDb.e2TransInfo.ricInitTransaction[transId].transactionId = transId;
+         duCb.e2apDb.e2TransInfo.ricInitTransaction[transId].procedureCode = e2apMsg->choice.initiatingMessage->procedureCode;
+      }
+      duCb.e2apDb.e2TimersInfo.e2Timers.ricServiceUpdateTimer.ricService.dir = serviceUpdate.dir;
+      duCb.e2apDb.e2TimersInfo.e2Timers.ricServiceUpdateTimer.ricService.transId =transId;
+   }
+   FreeRicServiceUpdate(e2apMsg);
+   return ret;
+}
+/******************************************************************
+ *
+ * @brief Processes RIC service Query sent by RIC
+ *
+ * @details
+ *
+ *    Function : procRicServiceQuery
+ *
+ *    Functionality: Processes RIC service Query sent by RIC
+ *
+ * @params[in] E2AP_PDU_t ASN decoded E2AP message
+ * @return ROK     - success
+ *         RFAILED - failure
+ *
+ * ****************************************************************/
+
+void procRicServiceQuery(E2AP_PDU_t *e2apMsg)
+{
+   ConfigType action;
+   uint8_t arrIdx =0, ranFuncIdx=0,tmpIdx=0;
+   uint16_t id,revisionCcounter;
+   bool tmpArray[MAX_RAN_FUNCTION] = {false};
+   RICserviceQuery_t *ricServiceQuery=NULL;
+   RicServiceUpdate ricUpdate;
+   RANfunctionID_ItemIEs_t *ranFuncAddedItemIe;
+   RANfunctionsID_List_t *ranFuncAddedList;
+
+   DU_LOG("\nINFO   -->  E2AP : RIC Service Query received");
+   memset(&ricUpdate, 0, sizeof(RicServiceUpdate));
+   ricUpdate.dir = RIC_INITIATED;
+   ricServiceQuery = &e2apMsg->choice.initiatingMessage->value.choice.RICserviceQuery;
+
+   for(arrIdx=0; arrIdx<ricServiceQuery->protocolIEs.list.count; arrIdx++)
+   {
+      switch(ricServiceQuery->protocolIEs.list.array[arrIdx]->id)
+      {
+         /* TODO completing in next patch/gerrit */
+         case ProtocolIE_IDE2_id_TransactionID:
+         {
+            ricUpdate.transId = ricServiceQuery->protocolIEs.list.array[arrIdx]->value.choice.TransactionID;
+            break;
+         }
+
+         case ProtocolIE_IDE2_id_RANfunctionsAccepted:
+         {
+            ranFuncAddedList= &ricServiceQuery->protocolIEs.list.array[arrIdx]->value.choice.RANfunctionsID_List;
+            if(ranFuncAddedList->list.array)
+            {
+               for(ranFuncIdx=0;ranFuncIdx<ranFuncAddedList->list.count; ranFuncIdx++)
+               {
+                  if(ranFuncAddedList->list.array[ranFuncIdx])
+                  {
+                     /* Using the RAN function Id, identify the RAN function to be modified or deleted.  */
+                     
+                     ranFuncAddedItemIe = (RANfunctionID_ItemIEs_t*)ranFuncAddedList->list.array[ranFuncIdx];
+                     id = ranFuncAddedItemIe->value.choice.RANfunctionID_Item.ranFunctionID;
+                     revisionCcounter = ranFuncAddedItemIe->value.choice.RANfunctionID_Item.ranFunctionRevision;
+                     
+                     if((id != duCb.e2apDb.ranFunction[id-1].id))
+                     {
+                        action = CONFIG_DEL;
+                     }
+                     else if((id == duCb.e2apDb.ranFunction[id-1].id)&&(revisionCcounter!=duCb.e2apDb.ranFunction[id-1].revisionCounter))
+                     {
+                        action = CONFIG_MOD;
+                     }
+
+                     if(action == CONFIG_DEL)
+                     {
+                        ricUpdate.recvRanFuncList.ranFunToBeDeleted[ricUpdate.recvRanFuncList.numOfRanFunToBeDeleted].id = id;
+                        ricUpdate.recvRanFuncList.ranFunToBeDeleted[ricUpdate.recvRanFuncList.numOfRanFunToBeDeleted].revisionCounter = revisionCcounter;
+                        ricUpdate.recvRanFuncList.numOfRanFunToBeDeleted++;
+                     }
+                     else if(action == CONFIG_MOD)
+                     {
+                        ricUpdate.recvRanFuncList.ranFunToBeModified[ricUpdate.recvRanFuncList.numOfRanFunToBeModified].id = id;
+                        ricUpdate.recvRanFuncList.ranFunToBeModified[ricUpdate.recvRanFuncList.numOfRanFunToBeModified].revisionCounter = revisionCcounter;
+                        ricUpdate.recvRanFuncList.numOfRanFunToBeModified++;
+                     }
+
+                     /* If any ID is set to true, it means that the ID has been used in either modification or deletion list. 
+                      * Else we will add the IDs into the added list */
+                     tmpArray[id-1] = true;
+                  }
+               }
+            }
+            break;
+         }
+      }
+   }
+
+   /*  Traversing the whole RAN function list in ducb to check if any new Ran function ids have been added. */
+   for(arrIdx =0; arrIdx<MAX_RAN_FUNCTION; arrIdx++)
+   {
+      tmpIdx= ricUpdate.recvRanFuncList.numOfRanFunToBeAdded;
+      if((duCb.e2apDb.ranFunction[arrIdx].id >0)&&(!tmpArray[arrIdx]))
+      {
+         ricUpdate.recvRanFuncList.ranFunToBeAdded[tmpIdx].id = duCb.e2apDb.ranFunction[arrIdx].id;
+         ricUpdate.recvRanFuncList.ranFunToBeAdded[tmpIdx].revisionCounter = duCb.e2apDb.ranFunction[arrIdx].revisionCounter;
+         ricUpdate.recvRanFuncList.numOfRanFunToBeAdded++;
+      }
+   }
+   if(BuildAndSendRicServiceUpdate(ricUpdate)!= ROK)
+   {
+      DU_LOG("\nERROR  -->  E2AP : Failed to build and send ric service update message");
+   }
+
+   freeAperDecodingOfRicServiceQuery(ricServiceQuery);
+}
+
+
 /*******************************************************************
  *
  * @brief Handles received E2AP message and sends back response  
@@ -2117,6 +3190,11 @@ void E2APMsgHdlr(Buffer *mBuf)
                      procRicSubsReq(e2apMsg);
                      break;
                   }
+               case InitiatingMessageE2__value_PR_RICserviceQuery:
+                  {
+                     procRicServiceQuery(e2apMsg);
+                     break;
+                  }
                default:
                   {
                      DU_LOG("\nERROR  -->  E2AP : Invalid type of E2AP_PDU_PR_initiatingMessage [%d]",\