# Integration between FlexRIC and OAI CU
###### tags: `ORAN`
[TOC]
:::success
* INFO:
* This document is described how to develop E2 interface between OAI CU and FlexRIC
* Resource
* [oai.patch from ChiehChun](https://gitlab.eurecom.fr/mosaic5g/flexric/-/blob/2851530501f701d2305c703d06d0135118e11deb/multiRAT/oai/mobsys2022.patch)
* Reference
* [E2AP and E2SM](https://www.techplayon.com/what-is-e2-interface-in-open-ran/)
:::
## Prerequisites
### Architecture

### Call Flow

### OAI FlexRIC
**Hardware**
| Items | Content |
| -------- | -------- |
| OS | ubuntu 18.04 LTS |
| CPU | Intel® Core™ i5-7500 CPU @ 3.40GHz |
| RAM | 16GB |
| Disk | 500GB |
| IP | 191.168.31.244 |
**Software**
| Items | Version |
| -------- | -------- |
| FlexRIC | branch "dev", commit "3a427b405ed9187a2ff0f85c5f20121f9f44bcd2" |
| FlexRIC | branch "e2ap-v2-kpm" |
:::success
建議使用"3a427b405ed9187a2ff0f85c5f20121f9f44bcd2"
:::
### OAI-CU
**Hardware**
| Items | Content |
| -------- | -------- |
| OS | ubuntu 18.04 LTS |
| CPU | Intel® Core™ i5-7500 CPU @ 3.40GHz |
| RAM | 16GB |
| Disk | 500GB |
| IP | 192.168.31.83 |
**Software**
| Items | Version |
| -------- | -------- |
| OSC CU | 2022.41 |
## Build FlexRIC
:::danger
gcc version需要10,如果安裝ubuntu22.04的話gcc version會是11,需要刪除後重裝。
```=
sudo apt remove gcc-11 g++-11 //移除ubuntu22.04 安裝的v11版本
sudo apt install gcc-10 g++-10 //安裝v10版本
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 100
sudo ln -s /usr/bin/gcc /usr/bin/cc //創建 cc 符號鏈接到 gcc
sudo ln -s /usr/bin/g++ /usr/bin/c++
```
:::
### Resource
[FlexRIC](https://gitlab.eurecom.fr/mosaic5g/flexric/-/tree/0d49ee71975fffe3d0edd7f078035aa2fcfae826)
### Install necessary tools
**Install cmake**
[Following this link to install](https://apt.kitware.com/)
:::info
3.Add the repository to your sources list and update.
For Ubuntu <font color="#f100">18.04</font> (bionic)
```
echo 'deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ bionic main' | sudo tee /etc/apt/sources.list.d/kitware.list >/dev/null
sudo apt-get update
```
:::
**Install automake**
```=
sudo apt-get install autotools-dev
sudo apt-get install automake
```
**Install bison for swig make**
```=
sudo apt-get install bison -y
sudo apt-get install byacc -y
```
<font color="#f100">bison版本會有問題請參考下面安裝</font>
[install bison](https://linuxreference.wordpress.com/2019/06/08/howto-install-bison-source-code-on-ubuntu-18-04/)
1. Step 1: Download Bison source code from GNU repository.
`wget http://ftp.gnu.org/gnu/bison/bison-3.5.tar.gz`
3. Step 2: Extract the gz with command tar -zxvf
`tar -zxvf bison-3.5.tar.gz`
5. Step 3: Change to the directory where the package extracted.
`cd bison-3.5/`
7. Step 4: Configure the Bison packages.
`sudo ./configure`
9. Step 5: Compile and install Bison packages.
```=
sudo make
sudo make install
```
6. Step 6: Create soft link for Bison binary file.
`sudo ln -s /usr/local/bin/bison /usr/bin/bison`
**Install swig**
We use SWIG as an interface generator to enable the multi-language feature (i.e., C/C++ and Python) for the xApps. Please, check your SWIG version (i.e, swig -version) and install it from scratch if necessary as described here: https://swig.org/svn.html or via the code below:
```=
git clone https://github.com/swig/swig.git
cd swig
./autogen.sh
./configure --prefix=/usr/
make
sudo make install
```
:::success
第四行會有問題需下載以下程式碼
```
sudo apt-get install libpcre2-dev
```
確認g++有沒有下載
```=
apt-get update gcc
sudo apt-get install g++
```
:::
**Install asn1c compiler**
```=
sudo apt-get -y install asn1c
```
### Download the required dependencies
**Install required dependencies**
```=
sudo apt install libsctp-dev python3.8 cmake-curses-gui libpcre2-dev python-dev
```
### Clone the FlexRIC project, build and install it
**Download the code**
```=
git clone https://gitlab.eurecom.fr/mosaic5g/flexric.git
git checkout <here put the release tag>
```
**Build**
```=
cd flexric && mkdir build && cd build && cmake .. && make
```
**Install**
```=
cd /flexric/build
sudo make install
```
### Test
**Execute Near-RT RIC**
```=
./build/examples/ric/nearRT-RIC
```
**Execute E2 agent simulator**
```=
./build/examples/emulator/agent/emu_agent_gnb_cu
```
**Log from RIC**

**Log from E2 agent**

## Build OAI CU
### Resource
[OAI-CU](https://gitlab.eurecom.fr/oai/openairinterface5g)
### Clone the OAI project, build and install it
**Download the code**
```=
git clone https://gitlab.eurecom.fr/oai/openairinterface5g.git oai
cd oai/
git checkout 2022.41
```
**Get oai.patch from FlexRIC**
:::warning
If your CU and RIC are in the different PC or VM, you have to download FlexRIC in your OAI PC
:::
```=
git am ~/flexric/multiRAT/oai/oai.patch --whitespace=nowarn
```
**Build**
```=
source oaienv
cd cmake_targets
./build_oai -I
cd ~/oai/cmake_targets
./build_oai --gNB --nrUE -w SIMU
```
**Modify configuration for CU**
```=
cd ~/flexric/multiRAT/oai
nano gnb.sa.band78.fr1.106PRB.usrpb210.conf
```
* Modify mcc、mnc、sst and sd refer to your amf

* Modify amf IP、CU interface name and IP

**Modify configuration for FlexRIC**
* Copy FlexRIC folder from FlexRIC PC to OAI PC

* Copy FlexRIC configuration from FlexRIC PC to OAI PC

```=
nano /usr/local/etc/flexric/flexric.conf
```
* Modify RIC IP

Using the Nano text editor, save and exit a file.
Click Ctrl+X to exit, and then click Y to save the file. It asks for file to write, and press Enter to use the default name because its already named.
Get this message.
**[Error writing /filename: Permission denied]**
**Add sudo to have the rights to write that file**
```=
sudo nano /usr/local/etc/flexric/flexric.conf
```
:::warning
OAI CU also need to modify RIC IP in the file.
:::
### Test E2AP Protocol
**Execute Near-RT RIC in FlexRIC PC**
```=
./build/examples/ric/nearRT-RIC
```
**Execute OAI-CU in OAI PC**
```=
cd ~/oai/cmake_targets/ran_build/build
sudo RFSIMULATOR=server ./nr-softmodem --rfsim --sa -O ~/flexric/multiRAT/oai/gnb.sa.band78.fr1.106PRB.usrpb210.conf --sa -E --continuous-tx
```
**Log from OAI-CU**

**Log from FlexRIC**

### Test E2SM Protocol
**Modify xapp_mac_rlc_pdcp_moni.c**
:::warning
For the oai cu, it gets the data related to the PDCP layer, so you need to run the xapp_mac_rlc_pdcp_moni to subscribe the PDCP RAN function.
:::
:::danger
**Remind:** you have to comment the codes which subscribe the MAC and RLC RAN function in the xapp_mac_rlc_pdcp_moni.c
:::
```=
nano ~/flexric/examples/xApp/c/monitor/xapp_mac_rlc_pdcp_moni.c
```



**Compile FlexRIC again**
```=
cd ~/flexric/build
cmake .. && make
sudo make install
```
**Execute FlexRIC and OAI CU**
```=
# RIC
./build/examples/ric/nearRT-RIC
# OAI CU
sudo RFSIMULATOR=server ./nr-softmodem --rfsim --sa -O ~/flexric/multiRAT/oai/gnb.sa.band78.fr1.106PRB.usrpb210.conf --sa -E --continuous-tx
```
**Execute xApp**
```=
cd ~/flexric
build/examples/xApp/c/monitor/xapp_mac_rlc_pdcp_moni
```
**Log from xApp**

**Log from FlexRIC**

**Log from OAI CU**

---
安裝OAI需要更改PVE CPU設置。

---
參考資料
https://apt.kitware.com/
https://github.com/swig/swig/issues/2120
https://linuxreference.wordpress.com/2019/06/08/howto-install-bison-source-code-on-ubuntu-18-04/