### Python xApp Creation Example Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp/diff_version_id=de5975b87004fe57a5342766c83905e9a148b9c7&view=inline This section provides instructions and an example for creating a new xApp using Python. It guides the user to create a Python main file within a specific directory structure, facilitating the development of Python-based xApps. ```python # Create a new xApp in Python # Example: create a new xApp in Python create a python main file in this path ```flexric/examples/xApp/python3``` ``` -------------------------------- ### FlexRIC Service Model Unit Test Example Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-service-model Provides an example of a unit test `main.c` for a FlexRIC service model. It demonstrates initializing a service model agent and RIC, then performing checks for function equality, subscription, and message reception. Finally, it cleans up allocated resources. ```c // In src/sm/new_sm/test/main.c int main() { // init agent with read write RAN function sm_io_ag_t io_ag = {.read = read_RAN, .write = write_RAN}; // Simulate fake service model agent in RAN sm_agent_t* sm_ag = make_new_sm_agent(io_ag); // Create FlexRIC service model sm_ric_t* sm_ric = make_new_sm_ric(); // Check if service model id is correct // between RAN and RIC check_eq_ran_function(sm_ag, sm_ric); // check subcription with defined time interval check_subscription(sm_ag, sm_ric); // Check if RIC recieve message or not check_indication(sm_ag, sm_ric); sm_ag->free_sm(sm_ag); sm_ric->free_sm(sm_ric); printf("Success\n"); return EXIT_SUCCESS; } ``` -------------------------------- ### C xApp Example: Monitoring New Data Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp/diff_version_id=32d3c2d3a93744251a7df634f015acf1f8cabc35&view=parallel This C code snippet demonstrates the basic structure of an xApp, including the main function and a success message. It serves as a starting point for developing monitoring xApps. ```c #include int main(int argc, char *argv[]) { printf("Test xApp run SUCCESSFULLY\n"); return 0; } ``` -------------------------------- ### Example xApp for Monitoring Data (C) Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp/diff_version_id=82c568a853998bc756e3adfdbce98aa6aab77ea6 This C code example demonstrates how to implement a callback function within a generic xApp to monitor new data arriving from FlexRIC. It's intended as a starting point for developing monitoring functionalities. ```c // Example in a generic xapp - xapp_new_moni.c // Call back function ``` -------------------------------- ### Running FlexRIC Components Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create%20a%20xApp Instructions on how to run the necessary components for a FlexRIC xApp. This includes starting the agent, the nearRT-RIC, and finally the xApp itself, either in C or Python. ```bash # Go to flexric/build/examples/emulator/agent ./agent_1 ``` ```bash # go to flexric/build/examples/ric ./nearRT-RIC ``` ```bash ## in C cd flexric/build/examples/xApp/c/monitor ./xapp_mysm_moni or ## in Python cd flexric/build/examples/xApp/python3 python3 mysm_moni.py ``` -------------------------------- ### Create xApp in C using FlexRIC API Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp_version_id=504c39f41ced28923b0a1050929d8586de6c77d1 Example C code demonstrating how to create and manage an xApp using the FlexRIC API. It covers initialization, E2 node subscription, data reporting, and cleanup. Requires FlexRIC environment setup. ```c // Example in a generic xapp - xapp_new_moni.c // Call back function // Implement what you're going to read from your service model static void sm_cb_new(sm_ag_if_rd_t const* rd) { assert(rd != NULL); assert(rd->type == NEW_STATS_V0); int64_t now = time_now_us(); printf("NEW ind_msg latency = %ld \n", now - rd->new_stats.msg.tstamp); } int main(int argc, char *argv[]) { // init the config of xApp fr_args_t args = init_fr_args(argc, argv); // init xApp init_xapp_api(&args); sleep(1); // Get the state of connection E2 nodes e2_node_arr_t nodes = e2_nodes_xapp_api(); defer({ free_e2_node_arr(&nodes); }); assert(nodes.len > 0); printf("Connected E2 nodes = %d\n", nodes.len); // new indication // Set the interval time of indication message inter_xapp_e i_3 = ms_1; sm_ans_xapp_t* new_handle = NULL; // if there is more than one E2 node connected to the RIC if(nodes.len > 0){ // Create an array of with length of nodes new_handle = calloc( nodes.len, sizeof(sm_ans_xapp_t)); assert(new_handle != NULL); } for (int i = 0; i < nodes.len; i++) { e2_node_connected_t* n = &nodes.n[i]; for (size_t j = 0; j < n->len_rf; j++) printf("Registered node %d ran func id = %d \n ", i, n->ack_rf[j].id); // generate subscription request new_handle[i] = report_sm_xapp_api(&nodes.n[i].id, n->ack_rf[6].id, i_3, sm_cb_new); assert(new_handle[i].success == true); } sleep(2); for(int i = 0; i < nodes.len; ++i){ // Remove the handle previously returned rm_report_sm_xapp_api(new_handle[i].u.handle); } if(nodes.len > 0){ free(new_handle } //Stop the xApp while(try_stop_xapp_api() == false) usleep(1000); printf("Test xApp run SUCCESSFULLY\n"); } ``` -------------------------------- ### C: Example xApp for Monitoring New Data Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp/diff_version_id=82c568a853998bc756e3adfdbce98aa6aab77ea6&view=inline A C code snippet demonstrating a generic xApp's callback function for monitoring new data. This example is intended for developers creating monitoring functionalities within their xApps. ```c // Example in a generic xapp - xapp_new_moni.c // Call back function ``` -------------------------------- ### C xApp API: init_xapp_api and report_sm_xapp_api Functions Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp/diff_version_id=32d3c2d3a93744251a7df634f015acf1f8cabc35&view=inline Demonstrates the usage of `init_xapp_api` for E2 subscription and SQLite database initialization, and `report_sm_xapp_api` for handling service model callbacks in C. It includes example parameters and a generated subscription request. ```clike // report_sm_xapp_api parameters // global_e2_node_id_t* id: e2 node id // uint32_t sm_id: service model id from node // inter_xapp_e i: Interval time // sm_cb handler: Service model callback handler // generate subscription request new_handle[i] = report_sm_xapp_api(&nodes.n[i].id, n->ack_rf[6].id, i_3, sm_cb_new); assert(new_handle[i].success == true); ``` -------------------------------- ### Run FlexRIC Agent Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp/diff_version_id=32d3c2d3a93744251a7df634f015acf1f8cabc35 This command navigates to the agent's directory within the FlexRIC build examples and executes the agent. Ensure you are in the correct directory before running. ```bash # Go to flexric/build/examples/emulator/agent cd ../../emulator/agent ./agent ``` -------------------------------- ### Unit Test Initialization for Service Model Agent Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-service-model/diff_version_id=2212396fd3ae0c21d9d964a597bc3bf5fc365957&view=inline Demonstrates the initialization of a service model I/O agent and the creation of a new service model agent for testing purposes. This setup is used to verify the communication between the E2 Node, RIC, and xApp. ```clike // In flexric/src/sm/new_sm/test/main.c int main() { // init agent with read write RAN function sm_io_ag_t io_ag = {.read = read_RAN, .write = write_RAN}; // Simulate fake service model agent in RAN sm_agent_t* sm_ag = make_new_sm_agent(io_ag); ``` -------------------------------- ### Running C xApp Monitor Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp_version_id=504c39f41ced28923b0a1050929d8586de6c77d1 Command to execute the C-based xApp monitor example. Navigate to the build directory for C xApps and run the `xapp_mysm_moni` executable. ```bash ## in C cd flexric/build/examples/xApp/c/monitor ./xapp_mysm_moni ``` -------------------------------- ### Run Monitoring xApp in C Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp/diff_version_id=23b7af682845137c37668c8223278661f1df34da&view=parallel Execute the monitoring xApp using C. This requires changing to the C examples directory and running the compiled C executable. The output shows the xApp initializing, connecting to the RIC, and managing subscriptions. ```c cd flexric/build/examples/xApp/c/monitor ./nearRT-RIC ``` -------------------------------- ### Create New xApp in C (Monitor Example) Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp_version_id=14f1460c4906d41a27d6c0f7d2e188cc3d676e8f This C code demonstrates how to create a new xApp that monitors data. It initializes the xApp API, connects to E2 nodes, sets up a callback function for new indications, and manages subscriptions and unsubscriptions. Dependencies include standard C libraries and FlexRIC SDK headers. ```c // Example in a generic xapp - xapp_new_moni.c // Call back function // Implement what you're going to read from your service model static void sm_cb_new(sm_ag_if_rd_t const* rd) { assert(rd != NULL); assert(rd->type == NEW_STATS_V0); int64_t now = time_now_us(); printf("NEW ind_msg latency = %ld \n", now - rd->new_stats.msg.tstamp); } int main(int argc, char *argv[]) { // init the config of xApp fr_args_t args = init_fr_args(argc, argv); // init xApp init_xapp_api(&args); sleep(1); // Get the state of connection E2 nodes e2_node_arr_t nodes = e2_nodes_xapp_api(); defer({ free_e2_node_arr(&nodes); }); assert(nodes.len > 0); printf("Connected E2 nodes = %d\n", nodes.len); // new indication // Set the interval time of indication message inter_xapp_e i_3 = ms_1; sm_ans_xapp_t* new_handle = NULL; // if there is more than one E2 node connected to the RIC if(nodes.len > 0){ // Create an array of with length of nodes new_handle = calloc( nodes.len, sizeof(sm_ans_xapp_t)); assert(new_handle != NULL); } for (int i = 0; i < nodes.len; i++) { e2_node_connected_t* n = &nodes.n[i]; for (size_t j = 0; j < n->len_rf; j++) printf("Registered node %d ran func id = %d \n ", i, n->ack_rf[j].id); // generate subscription request new_handle[i] = report_sm_xapp_api(&nodes.n[i].id, n->ack_rf[6].id, i_3, sm_cb_new); assert(new_handle[i].success == true); } sleep(2); for(int i = 0; i < nodes.len; ++i){ // Remove the handle previously returned rm_report_sm_xapp_api(new_handle[i].u.handle); } if(nodes.len > 0){ free(new_handle); } //Stop the xApp while(try_stop_xapp_api() == false) usleep(1000); printf("Test xApp run SUCCESSFULLY\n"); } ``` -------------------------------- ### Running xApps (C and Python) Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp This section outlines the steps to run the developed xApps. It involves starting the agent, the nearRT-RIC, and then executing the xApp either in C or Python from their respective build directories. It also mentions that statistic data is stored in an SQLite database. ```bash # Go to flexric/build/examples/emulator/agent ./agent_1 ``` ```bash # go to flexric/build/examples/ric ./nearRT-RIC ``` ```bash ## in C cd flexric/build/examples/xApp/c/monitor ./xapp_mysm_moni or ## in Python cd flexric/build/examples/xApp/python3 python3 mysm_moni.py ``` -------------------------------- ### Unit Test Example for Service Model in FlexRIC Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-service-model/diff_version_id=159a4650e0511cab01c8768df0112ae5b8fccf28&view=parallel Illustrates a basic C code structure for a unit test within the FlexRIC service model testing directory. This example shows the initialization of an agent for read-write RAN functions, a prerequisite for testing service models. ```c // In src/sm/new_sm/test/main.c int main() { // init agent with read write RAN function // ... rest of the test logic ... return 0; } ``` -------------------------------- ### Service Model File Structure Generated by gen_sm.py Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-service-model_version_id=de8d7401bb66d264754279f0f6281183f3323d36 An example of the directory structure and files created by the `gen_sm.py` script for a new service model. This includes directories for decoding, encoding, information elements, and testing, along with core C files and CMakeLists.txt for compilation. ```text Files generated: Inside flexric/src/sm/new_sm/ ├──dec/ # For decoding service model output ├──enc/ # For encoding service model input ├──ie/ # Pre-defined service model information element ├──test/ # Simulate connection between E2 agent and nearRT-RIC with corresponding service model ├──CMakeLists.txt # Compiler C file for your service model ├──new_sm_agent.c # Create and setup functionality for service model agent ├──new_sm_agent.h ├──new_sm_id.h # Register service model ID ├──new_sm_ric.c # Implement service ric fucntion ├──new_sm_ric.h Inside flexric/test/sm/new_sm ├──CMakeLists.txt ├──main.c ``` -------------------------------- ### C xApp Example Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp/diff_version_id=23b7af682845137c37668c8223278661f1df34da&view=inline This C code snippet demonstrates the basic structure for creating an xApp, including the main function and how to integrate with the RIC. It shows how to receive data and manage internal state with an embedded database. ```c #include #include // Assume RIC and xApp related headers are included here // #include "ric.h" // #include "xapp.h" int main(int argc, char *argv[]) { // Initialization code for xApp and RIC connection printf("Initializing xApp...\n"); // Initialize RIC connection // ric_connect(); // Main loop to process messages from RIC printf("Entering main loop...\n"); // while (ric.try_stop == 0) { // Process incoming messages // xapp_process_messages(); // } // Cleanup and exit printf("Shutting down xApp...\n"); // ric_disconnect(); return 0; } ``` -------------------------------- ### Create Python xApp Main File Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp/diff_version_id=de5975b87004fe57a5342766c83905e9a148b9c7&view=parallel Instructions and example for creating a main Python file for a new xApp. This involves placing the file in a specific directory within the flexric project structure. ```python # Example: create a new xApp in Python create a python main file in this path ```flexric/examples/xApp/python3``` ``` -------------------------------- ### C xApp Example: Monitoring New Data Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp/diff_version_id=32d3c2d3a93744251a7df634f015acf1f8cabc35 An example of creating a new xApp in C for monitoring new data within the FlexRIC framework. This snippet demonstrates the main function structure and a success message. ```c int main(int argc, char *argv[]) { printf("Test xApp run SUCCESSFULLY\n"); } ``` -------------------------------- ### Bash: Monitoring xApp Execution Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp/diff_version_id=626984d239b47be1f2a70377a17667707e032718&view=inline This section provides bash commands to monitor the execution of xApps, both in C and Python. It shows the directory navigation and the command to launch the monitoring xApp. The output examples illustrate the initialization process and the loading of service model plugins. ```bash cd flexric/build/examples/xApp/c/monitor ./xapp_mysm_moni ``` ```bash cd flexric/build/examples/xApp/python3 python3 mysm_moni.py ``` ```bash Setting the config -c file to /usr/local/flexric/flexric.conf Setting path -p for the shared libraries to /usr/local/flexric/ [xAapp]: Initializing ... [xApp]: RIC IP Address = 127.0.0.1 [E2 AGENT]: Opening plugin from path = /usr/local/flexric/libmac_sm.so [E2 AGENT]: Opening plugin from path = /usr/local/flexric/libtc_sm.so [E2 AGENT]: Opening plugin from path = /usr/local/flexric/libpdcp_sm.so [E2 AGENT]: Opening plugin from path = /usr/local/flexric/libgtp_sm.so ``` -------------------------------- ### C xApp Initialization and Reporting API Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp/diff_version_id=32d3c2d3a93744251a7df634f015acf1f8cabc35 Demonstrates essential C functions for initializing the xApp API, setting up E2 subscriptions, and handling service model reports. It includes example usage for `report_sm_xapp_api` and highlights a caveat regarding service model ID indexing. ```clike // report_sm_xapp_api parameters // global_e2_node_id_t* id: e2 node id // uint32_t sm_id: service model id from node // inter_xapp_e i: Interval time // sm_cb handler: Service model callback handler // generate subscription request new_handle[i] = report_sm_xapp_api(&nodes.n[i].id, n->ack_rf[6].id, i_3, sm_cb_new); assert(new_handle[i].success == true); ``` ```c typedef struct{ union { mac_ind_data_t mac_stats; rlc_ind_data_t rlc_stats; pdcp_ind_data_t pdcp_stats; slice_ind_data_t slice_stats; tc_ind_data_t tc_stats; gtp_ind_data_t gtp_stats; new_ind_data_t new_stats; }; sm_ag_if_rd_e type; } sm_ag_if_rd_t; ``` -------------------------------- ### Example Unit Test Main Function Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-service-model/diff_version_id=159a4650e0511cab01c8768df0112ae5b8fccf28 Illustrates a basic 'main' function for unit testing a service model, specifically demonstrating the initialization of an agent with read/write RAN function capabilities. ```c int main() { // init agent with read write RAN function ``` -------------------------------- ### Service Model File Structure (Generated Example) Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-service-model_version_id=cebc5cc2d23280bb0c7fed55c2b26015949bc650 This illustrates the typical file and directory structure created by the `gen_sm.py` script for a new service model. It includes directories for encoding/decoding, information elements, testing, and core implementation files like CMakeLists.txt and agent/RIC C files. ```text Files generated: Inside flexric/src/sm/new_sm/ ├──dec/ # For decoding service model output ├──enc/ # For encoding service model input ├──ie/ # Pre-defined service model information element ├──test/ # Simulate connection between E2 agent and nearRT-RIC with corresponding service model ├──CMakeLists.txt # Compiler C file for your service model ├──new_sm_agent.c # Create and setup functionality for service model agent ├──new_sm_agent.h ├──new_sm_id.h # Register service model ID ├──new_sm_ric.c # Implement service ric function ├──new_sm_ric.h Inside flexric/test/sm/new_sm ├──CMakeLists.txt ├──main.c ``` -------------------------------- ### Running nearRT-RIC Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp_version_id=504c39f41ced28923b0a1050929d8586de6c77d1 Command to start the near Real-Time RIC component of FlexRIC. This executable is found in the `flexric/build/examples/ric` directory. ```bash # go to flexric/build/examples/ric ./nearRT-RIC ``` -------------------------------- ### Example: Create New Service Model Directory Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-service-model_version_id=f242f4e3fe4697b2d5cb2986e9156b1269e968e5 Demonstrates the command to execute the `gen_sm.py` script to create a new service model, typically by copying the 'rlc_sm' template. ```bash # Example of step creating new service model by copying rlc_sm files and folders cd flexric/src/sm python3 gen_sm.py ``` -------------------------------- ### Python xApp Example for FlexRIC SDK Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp_version_id=504c39f41ced28923b0a1050929d8586de6c77d1 Example Python script for an xApp demonstrating interaction with the FlexRIC SDK using SWIG bindings. It includes setting up a callback for new indications, connecting to E2 nodes, and reporting new service model indications with a specified interval. It also handles the cleanup of reporting. ```python # For example xapp_mysm_moni.py import xapp_sdk as ric import time import os import pdb #################### #### NEW INDICATION CALLBACK #################### # Create a callback for NEW which derived it from C++ class new_cb class NEWCallback(ric.new_cb): def __init__(self): # Inherit C++ new_cb class ric.new_cb.__init__(self) # Create an override C++ method def handle(self, ind): if len(ind.ngu_stats) > 0: t_now = time.time_ns() / 1000.0 t_new = ind.tstamp / 1.0 t_diff = t_now - t_new print('new Indication tstamp = ' + str(ind.tstamp) + ' diff = ' + str(t_diff)) #################### #### GENERAL #################### ric.init() conn = ric.conn_e2_nodes() assert(len(conn) > 0) for i in range(0, len(conn)): print("Global E2 Node [" + str(i) + "]: PLMN MCC = " + str(conn[i].id.plmn.mcc)) print("Global E2 Node [" + str(i) + "]: PLMN MNC = " + str(conn[i].id.plmn.mnc)) #################### #### NEW INDICATION #################### for i in range(0, len(conn)): new_cb = NEWCallback() ric.report_new_sm(conn[i].id, ric.Interval_ms_1, new_cb) time.sleep(1) time.sleep(10) ### End ric.rm_report_new_sm() # Avoid deadlock. ToDo revise architecture while ric.try_stop == 0: time.sleep(1) ``` -------------------------------- ### Python xApp Example using SWIG Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp/diff_version_id=23b7af682845137c37668c8223278661f1df34da&view=inline This Python code demonstrates creating an xApp using SWIG bindings, allowing interaction with the RIC. It showcases the typical structure of a Python xApp, including initialization and message handling. ```python import time # Assume RIC and xApp related modules are imported here # from ric_sdk import RICConnector, XAppServiceModels # Initialize RIC connector and xApp service models # ric = RICConnector() # models = XAppServiceModels() # Placeholder for actual RIC object and loop class MockRIC: def __init__(self): self.try_stop = 0 ric = MockRIC() print("Initializing Python xApp...") # ric.connect() print("Entering main loop...") # while ric.try_stop == 0: # Process messages or perform actions # ric.process_messages() # time.sleep(1) # Add a sleep to prevent busy-waiting print("Shutting down Python xApp...") # ric.disconnect() ``` -------------------------------- ### Create a xApp in C Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp_version_id=69470ec67743f19fefe9fedf339d97040a9073cb This section provides an example C code for creating a monitoring xApp. It demonstrates how to initialize the xApp API, subscribe to service models, and handle incoming data. ```APIDOC ## POST /xapp/c/monitor ### Description Example C code for creating a monitoring xApp. This includes initializing the xApp API, subscribing to service models, and handling data callbacks. ### Method POST ### Endpoint /xapp/c/monitor ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c // Example in a generic xapp - xapp_new_moni.c // Call back function // Implement what you're going to read from your service model static void sm_cb_new(sm_ag_if_rd_t const* rd) { assert(rd != NULL); assert(rd->type == NEW_STATS_V0); int64_t now = time_now_us(); printf("NEW ind_msg latency = %ld \n", now - rd->new_stats.msg.tstamp); } int main(int argc, char *argv[]) { // init the config of xApp fr_args_t args = init_fr_args(argc, argv); // init xApp init_xapp_api(&args); sleep(1); // Get the state of connection E2 nodes e2_node_arr_t nodes = e2_nodes_xapp_api(); defer({ free_e2_node_arr(&nodes); }); assert(nodes.len > 0); printf("Connected E2 nodes = %d\n", nodes.len); // new indication // Set the interval time of indication message inter_xapp_e i_3 = ms_1; sm_ans_xapp_t* new_handle = NULL; // if there is more than one E2 node connected to the RIC if(nodes.len > 0){ // Create an array of with length of nodes new_handle = calloc( nodes.len, sizeof(sm_ans_xapp_t)); assert(new_handle != NULL); } for (int i = 0; i < nodes.len; i++) { e2_node_connected_t* n = &nodes.n[i]; for (size_t j = 0; j < n->len_rf; j++) printf("Registered node %d ran func id = %d \n ", i, n->ack_rf[j].id); // generate subscription request new_handle[i] = report_sm_xapp_api(&nodes.n[i].id, n->ack_rf[6].id, i_3, sm_cb_new); assert(new_handle[i].success == true); } sleep(2); for(int i = 0; i < nodes.len; ++i){ // Remove the handle previously returned rm_report_sm_xapp_api(new_handle[i].u.handle); } if(nodes.len > 0){ free(new_handle); } //Stop the xApp while(try_stop_xapp_api() == false) usleep(1000); printf("Test xApp run SUCCESSFULLY\n"); } ``` ### Response #### Success Response (200) - **message** (string) - Success message indicating xApp execution. #### Response Example ```json { "message": "Test xApp run SUCCESSFULLY" } ``` ``` -------------------------------- ### Create a xApp in C Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp_version_id=23b7af682845137c37668c8223278661f1df34da This section details how to create a monitoring xApp using C bindings within the FlexRIC framework. It includes an example C code snippet demonstrating initialization, subscription, and data handling. ```APIDOC ## POST /examples/xApp/c/monitor ### Description This endpoint provides an example of a C xApp that monitors new data and demonstrates the callback function implementation for handling service model data. ### Method POST ### Endpoint `/examples/xApp/c/monitor` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c // Example in a generic xapp - xapp_new_moni.c // Call back function // Implement what you're going to read from your service model static void sm_cb_new(sm_ag_if_rd_t const* rd) { assert(rd != NULL); assert(rd->type == RLC_STATS_V0); int64_t now = time_now_us(); printf("RLC ind_msg latency = %ld \n", now - rd->new_stats.msg.tstamp); } int main(int argc, char *argv[]) { // init the config of xApp fr_args_t args = init_fr_args(argc, argv); // init xApp init_xapp_api(&args); sleep(1); // Get the state of connection E2 nodes e2_node_arr_t nodes = e2_nodes_xapp_api(); defer({ free_e2_node_arr(&nodes); }); assert(nodes.len > 0); printf("Connected E2 nodes = %d\n", nodes.len); // new indication // Set the interval time of indication message inter_xapp_e i_3 = ms_1; sm_ans_xapp_t* new_handle = NULL; // if there is more than one E2 node connected to the RIC if(nodes.len > 0){ // Create an array of with length of nodes new_handle = calloc( nodes.len, sizeof(sm_ans_xapp_t)); assert(new_handle != NULL); } for (int i = 0; i < nodes.len; i++) { e2_node_connected_t* n = &nodes.n[i]; for (size_t j = 0; j < n->len_rf; j++) printf("Registered node %d ran func id = %d \n ", i, n->ack_rf[j].id); // generate subscription request new_handle[i] = report_sm_xapp_api(&nodes.n[i].id, n->ack_rf[6].id, i_3, sm_cb_new); assert(new_handle[i].success == true); } sleep(2); for(int i = 0; i < nodes.len; ++i){ // Remove the handle previously returned rm_report_sm_xapp_api(new_handle[i].u.handle); } if(nodes.len > 0){ free(new_handle); } //Stop the xApp while(try_stop_xapp_api() == false) usleep(1000); printf("Test xApp run SUCCESSFULLY\n"); } ``` ``` -------------------------------- ### Install Service Models Globally Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/mosaic5g/flexric Installs the service models globally. By default, libraries are placed in /usr/local/lib/flexric and configuration files in /usr/local/etc/flexric. This command requires superuser privileges. ```bash sudo make install ``` -------------------------------- ### Update CMakeLists.txt for New Service Model Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-service-model/diff_version_id=2212396fd3ae0c21d9d964a597bc3bf5fc365957&view=inline These examples show modifications required in the CMakeLists.txt files to integrate a newly generated service model into the FlexRIC build system. This involves adding the new service model's directory and defining build-time configurations. ```bash # In flexric/src/sm/CMakeLists.txt .... add_subdirectory(new_sm) ``` ```bash # In flexric/CMakeLists.txt ... # NEW Service Model set(SM_ENCODING_NEW "PLAIN" CACHE STRING "The NEW SM encoding to use") set_property(CACHE SM_ENCODING_NEW PROPERTY STRINGS "PLAIN" "ASN" "FLATBUFFERS") message(STATUS "Selected NEW SM_ENCODING: ${SM_ENCODING_NEW}") ... install(TARGETS new_sm DESTINATION ${CMAKE_INSTALL_PREFIX}/flexric) ``` -------------------------------- ### Bash: Running FlexRIC Agent and NearRT-RIC Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp/diff_version_id=626984d239b47be1f2a70377a17667707e032718&view=inline These bash commands outline the steps to run the FlexRIC agent and the near real-time RIC. Navigate to the specified build directories and execute the respective binaries. Ensure that the agent and RIC are started in the correct order for successful operation. ```bash # Go to flexric/build/examples/emulator/agent ./agent_1 ``` ```bash # go to flexric/build/examples/ric ./nearRT-RIC ``` -------------------------------- ### Install Service Models to Custom Path Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/mosaic5g/flexric Installs service models to a custom path specified by the CMAKE_INSTALL_PREFIX variable. This allows users to control the installation location of service model libraries and configuration files. ```bash mkdir build && cd build && cmake -DCMAKE_INSTALL_PREFIX= .. && make -j8 ``` -------------------------------- ### Example Indication Message IE Implementation (C) Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-service-model/diff_version_id=3eddd2f52dd327a8b2774e8b2ab08326de681082 Provides an example C implementation for an indication message Information Element (IE). This includes function declarations for creating and comparing indication messages. ```c // flexric/src/sm/new_sm/ie/new_data_ie.h ... new_ind_msg_t cp_new_ind_msg(new_ind_msg_t const* src); bool eq_new_ind_msg(new_ind_msg_t* m0, new_ind_msg_t* m1); ``` -------------------------------- ### C Example: Monitoring New Data xApp for FlexRIC Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp/diff_version_id=626984d239b47be1f2a70377a17667707e032718&view=inline This C code demonstrates how to create a monitoring xApp. It initializes the xApp API, connects to E2 nodes, sets up a subscription for new data indications, and defines a callback function to process these indications. It includes error handling and cleanup routines. ```c // Example in a generic xapp - xapp_new_moni.c // Call back function // Implement what you're going to read from your service model static void sm_cb_new(sm_ag_if_rd_t const* rd) { assert(rd != NULL); assert(rd->type == RLC_STATS_V0); int64_t now = time_now_us(); printf("RLC ind_msg latency = %ld \n", now - rd->new_stats.msg.tstamp); } int main(int argc, char *argv[]) { // init the config of xApp fr_args_t args = init_fr_args(argc, argv); // init xApp init_xapp_api(&args); sleep(1); // Get the state of connection E2 nodes e2_node_arr_t nodes = e2_nodes_xapp_api(); defer({ free_e2_node_arr(&nodes); }); assert(nodes.len > 0); printf("Connected E2 nodes = %d\n", nodes.len); // new indication // Set the interval time of indication message inter_xapp_e i_3 = ms_1; sm_ans_xapp_t* new_handle = NULL; // if there is more than one E2 node connected to the RIC if(nodes.len > 0){ // Create an array of with length of nodes new_handle = calloc( nodes.len, sizeof(sm_ans_xapp_t)); assert(new_handle != NULL); } for (int i = 0; i < nodes.len; i++) { e2_node_connected_t* n = &nodes.n[i]; for (size_t j = 0; j < n->len_rf; j++) printf("Registered node %d ran func id = %d \n ", i, n->ack_rf[j].id); // generate subscription request new_handle[i] = report_sm_xapp_api(&nodes.n[i].id, n->ack_rf[6].id, i_3, sm_cb_new); assert(new_handle[i].success == true); } sleep(2); for(int i = 0; i < nodes.len; ++i){ // Remove the handle previously returned rm_report_sm_xapp_api(new_handle[i].u.handle); } if(nodes.len > 0){ free(new_handle } //Stop the xApp while(try_stop_xapp_api() == false) usleep(1000); printf("Test xApp run SUCCESSFULLY\n"); } ``` -------------------------------- ### Running Python xApp Monitor Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp_version_id=504c39f41ced28923b0a1050929d8586de6c77d1 Command to run the Python 3 xApp monitor example. Navigate to the build directory for Python 3 xApps and execute the `mysm_moni.py` script using the python3 interpreter. ```bash ## in Python cd flexric/build/examples/xApp/python3 python3 mysm_moni.py ``` -------------------------------- ### xApp Reporting Functionality (Python) Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp/diff_version_id=d1a6d8e6d7494224d88b6a30c024512c49a63ee0&view=parallel Illustrates how to implement xApp reporting mechanisms in Python, likely involving similar logic to the C examples for handling service model callbacks and data processing. ```python # Example on message flow typical of a xApp conversation with NearRT Ric. # As the figure describes, the xApp will receive the data and store them in SQLite. Since some more complex operations may be required by the user, we decided to embed a DB into the xApp in a separate thread, so that queries can be made directly to a DB, rather than maintaining the states in the code. In addition, these are the essential functions for developing xApp. # 1. init_xapp_api # Initial E2 subscription request and database SQLite # 2. report_sm_xapp_api function # Report service model external application handled by developers sm_cb handler function. Example from above code: ``` -------------------------------- ### Example xApp CMakeLists.txt Configuration (CMake) Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Files-need-to-be-modified-for-developing-xApp-C-binding/diff_version_id=1015bdbf0b88f58f3ff67eceec40487030efc19a&view=parallel Modifies the CMakeLists.txt file for an xApp example. This involves adding an executable line to compile the 'xapp_new_moni' executable, linking it with 'xapp_new_moni.c' and other necessary source files. ```cmake add_executable(xapp_new_moni xapp_new_moni.c ../../../../src/util/alg_ds/alg/defer.c ``` -------------------------------- ### FlexRIC Testing Architecture Overview Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-service-model/diff_version_id=5f2b620ca98329c278c42870aa1cd6ece3856027 The deployment system for testing FlexRIC consists of three nodes. The provided documentation includes an architectural diagram illustrating the setup that needs to be tested. ```markdown The system to be deployed is made of 3 nodes. Below is the architecture we need to test: ``` -------------------------------- ### Create and Run Python xApp for Monitoring Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create%20a%20xApp Provides an example of creating a new xApp in Python for monitoring. It utilizes the xapp_sdk to initialize the RIC connection, retrieve E2 node information, and set up a callback for new indications to process and print latency data. ```python # For example xapp_mysm_moni.py import xapp_sdk as ric import time import os import pdb #################### #### NEW INDICATION CALLBACK #################### # Create a callback for NEW which derived it from C++ class new_cb class NEWCallback(ric.new_cb): def __init__(self): # Inherit C++ new_cb class ric.new_cb.__init__(self) # Create an override C++ method def handle(self, ind): if len(ind.rb_stats) > 0: t_now = time.time_ns() / 1000.0 t_new = ind.tstamp / 1.0 t_diff = t_now - t_new print('new Indication tstamp = ' + str(ind.tstamp) + ' diff = ' + str(t_diff)) #################### #### GENERAL #################### ric.init() conn = ric.conn_e2_nodes() assert(len(conn) > 0) for i in range(0, len(conn)): print("Global E2 Node [" + str(i) + "]: PLMN MCC = " + str(conn[i].id.plmn.mcc)) print("Global E2 Node [" + str(i) + "]: PLMN MNC = " + str(conn[i].id.plmn.mnc)) #################### #### NEW INDICATION #################### for i in range(0, len(conn)): new_cb = NEWCallback() ric.report_new_sm(conn[i].id, ric.Interval_ms_1, new_cb) time.sleep(1) time.sleep(10) ### End ric.rm_report_new_sm() # Avoid deadlock. ToDo revise architecture while ric.try_stop == 0: time.sleep(1) ``` -------------------------------- ### Create new xApp in C for Monitoring Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp This C code demonstrates how to create a new xApp that monitors network statistics. It initializes the xApp API, connects to E2 nodes, sets up a callback for new indications, and then removes the subscription. Ensure the necessary build files are modified as per the guide. ```c // Example in a generic xapp - xapp_new_moni.c // Call back function // Implement what you're going to read from your service model static void sm_cb_new(sm_ag_if_rd_t const* rd) { assert(rd != NULL); assert(rd->type == NEW_STATS_V0); int64_t now = time_now_us(); printf("NEW ind_msg latency = %ld \n", now - rd->new_stats.msg.tstamp); } int main(int argc, char *argv[]) { // init the config of xApp fr_args_t args = init_fr_args(argc, argv); // init xApp init_xapp_api(&args); sleep(1); // Get the state of connection E2 nodes e2_node_arr_t nodes = e2_nodes_xapp_api(); defer({ free_e2_node_arr(&nodes); }); assert(nodes.len > 0); printf("Connected E2 nodes = %d\n", nodes.len); // new indication // Set the interval time of indication message inter_xapp_e i_3 = ms_1; sm_ans_xapp_t* new_handle = NULL; // if there is more than one E2 node connected to the RIC if(nodes.len > 0){ // Create an array of with length of nodes new_handle = calloc( nodes.len, sizeof(sm_ans_xapp_t)); assert(new_handle != NULL); } for (int i = 0; i < nodes.len; i++) { e2_node_connected_t* n = &nodes.n[i]; for (size_t j = 0; j < n->len_rf; j++) printf("Registered node %d ran func id = %d \n ", i, n->ack_rf[j].id); // generate subscription request new_handle[i] = report_sm_xapp_api(&nodes.n[i].id, n->ack_rf[6].id, i_3, sm_cb_new); assert(new_handle[i].success == true); } sleep(2); for(int i = 0; i < nodes.len; ++i){ // Remove the handle previously returned rm_report_sm_xapp_api(new_handle[i].u.handle); } if(nodes.len > 0){ free(new_handle); } //Stop the xApp while(try_stop_xapp_api() == false) usleep(1000); printf("Test xApp run SUCCESSFULLY\n"); } ``` -------------------------------- ### C xApp API: report_sm_xapp_api Function Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp/diff_version_id=82c568a853998bc756e3adfdbce98aa6aab77ea6&view=parallel Demonstrates the 'report_sm_xapp_api' function used for service model reporting in C. It outlines the parameters and provides an example structure, highlighting dependencies on service model IDs defined in 'sm_ag_if_rd.h'. ```c // report_sm_xapp_api parameters // global_e2_node_id_t* id: e2 node id // uint32_t sm_id: service model id from node ``` ```c typedef struct{ union { mac_ind_data_t mac_stats; ``` -------------------------------- ### Python xApp Example: Basic Structure Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp/diff_version_id=32d3c2d3a93744251a7df634f015acf1f8cabc35&view=inline This Python code snippet shows a basic structure for a new xApp in Python. It includes a loop that continues as long as a stop condition is not met, with a sleep interval. ```python # For example xapp_mysm_moni.py while ric.try_stop == 0: time.sleep(1) ``` -------------------------------- ### Add xapp_new_moni executable in CMakeLists.txt Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Files-need-to-be-modified-for-developing-xApp-C-binding/diff_version_id=1015bdbf0b88f58f3ff67eceec40487030efc19a&view=inline This CMakeLists.txt file snippet is for an xApp example. It defines an executable named 'xapp_new_moni' and links it with necessary libraries and source files, including the new data IE and other relevant modules. ```c add_executable(xapp_new_moni xapp_new_moni.c ../../../../src/util/alg_ds/alg/defer.c ../../../../src/sm/mac_sm/ie/mac_data_ie.c ../../../../src/sm/rlc_sm/ie/rlc_data_ie.c ../../../../src/sm/pdcp_sm/ie/pdcp_data_ie.c ../../../../src/sm/slice_sm/ie/slice_data_ie.c ../../../../src/sm/tc_sm/ie/tc_data_ie.c ../../../../src/sm/gtp_sm/ie/gtp_data_ie.c ../../../../src/sm/new_sm/ie/new_data_ie.c ) target_link_libraries(xapp_new_moni PUBLIC e42_xapp -pthread -lsctp -ldl ) ``` -------------------------------- ### C: Create and Monitor xApp Data Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp_version_id=7c1e9aada5fa7f51feede72fd73e056ffb72057d Example C code for creating and running an xApp that monitors new data from a service model. It initializes the xApp API, subscribes to E2 nodes for indications, processes callbacks, and cleans up resources. ```c // Example in a generic xapp - xapp_new_moni.c // Call back function // Implement what you're going to read from your service model static void sm_cb_new(sm_ag_if_rd_t const* rd) { assert(rd != NULL); assert(rd->type == RLC_STATS_V0); int64_t now = time_now_us(); printf("RLC ind_msg latency = %ld \n", now - rd->new_stats.msg.tstamp); } int main(int argc, char *argv[]) { // init the config of xApp fr_args_t args = init_fr_args(argc, argv); // init xApp init_xapp_api(&args); sleep(1); // Get the state of connection E2 nodes e2_node_arr_t nodes = e2_nodes_xapp_api(); defer({ free_e2_node_arr(&nodes); }); assert(nodes.len > 0); printf("Connected E2 nodes = %d\n", nodes.len); // new indication // Set the interval time of indication message inter_xapp_e i_3 = ms_1; sm_ans_xapp_t* new_handle = NULL; // if there is more than one E2 node connected to the RIC if(nodes.len > 0){ // Create an array of with length of nodes new_handle = calloc( nodes.len, sizeof(sm_ans_xapp_t)); assert(new_handle != NULL); } for (int i = 0; i < nodes.len; i++) { e2_node_connected_t* n = &nodes.n[i]; for (size_t j = 0; j < n->len_rf; j++) printf("Registered node %d ran func id = %d \n ", i, n->ack_rf[j].id); // generate subscription request new_handle[i] = report_sm_xapp_api(&nodes.n[i].id, n->ack_rf[6].id, i_3, sm_cb_new); assert(new_handle[i].success == true); } sleep(2); for(int i = 0; i < nodes.len; ++i){ // Remove the handle previously returned rm_report_sm_xapp_api(new_handle[i].u.handle); } if(nodes.len > 0){ free(new_handle } //Stop the xApp while(try_stop_xapp_api() == false) usleep(1000); printf("Test xApp run SUCCESSFULLY\n"); } ``` -------------------------------- ### Shell Commands to Run C or Python xApp Source: https://gitlab.eurecom.fr/mosaic5g/flexric/-/wikis/Create-a-xApp_version_id=935769b5de169ce10977033d4e692a81b549f605 Provides two distinct shell commands for running xApps. The first navigates to the C xApp build directory and executes it. The second navigates to the Python 3 xApp build directory and runs the Python script. These are the final steps in the FlexRIC example execution. ```shell ## in C cd flexric/build/examples/xApp/c/monitor ./xapp_mysm_moni or ## in Python cd flexric/build/examples/xApp/python3 python3 mysm_moni.py ```