First release
[sim/ns3-o-ran-e2.git] / examples / oran-interface-example.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2022 Northeastern University
4  * Copyright (c) 2022 Sapienza, University of Rome
5  * Copyright (c) 2022 University of Padova
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation;
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * Author: Andrea Lacava <thecave003@gmail.com>
21  *                 Tommaso Zugno <tommasozugno@gmail.com>
22  *                 Michele Polese <michele.polese@gmail.com>
23  */
24
25 #include "ns3/core-module.h"
26 #include "ns3/oran-interface.h"
27 #include "encode_e2apv1.hpp"
28 #include <errno.h>
29
30
31 using namespace ns3;
32
33 Ptr<E2Termination> e2Term;
34 // TODO create getters for these parameters in e2Term
35 std::string plmId = "111";
36 uint16_t cellId = 1;
37 const std::string gnb = std::to_string (cellId);
38
39 /**
40 * Creates an empty RIC Report message and send it to the RIC
41 *
42 * \param params the RIC Subscription Request parameters
43 */
44 static void BuildAndSendReportMessage (E2Termination::RicSubscriptionRequest_rval_s params)
45 {
46   KpmIndicationHeader::KpmRicIndicationHeaderValues headerValues; 
47   headerValues.m_plmId = plmId;
48   headerValues.m_gnbId = cellId;
49   headerValues.m_nrCellId = cellId;
50
51   Ptr<KpmIndicationHeader> header = Create<KpmIndicationHeader> (KpmIndicationHeader::GlobalE2nodeType::gNB, headerValues);
52   
53   KpmIndicationMessage::KpmIndicationMessageValues msgValues;
54   
55   Ptr<OCuUpContainerValues> cuUpValues = Create<OCuUpContainerValues> ();
56   cuUpValues->m_plmId = plmId;
57   cuUpValues->m_pDCPBytesUL = 100;
58   cuUpValues->m_pDCPBytesDL = 100;
59   msgValues.m_pmContainerValues = cuUpValues;
60   
61   Ptr<MeasurementItemList> ue0DummyValues = Create<MeasurementItemList> ("UE-0");
62   ue0DummyValues->AddItem<long> ("DRB.PdcpSduVolumeDl_Filter.UEID", 6);
63   ue0DummyValues->AddItem<long> ("QosFlow.PdcpPduVolumeDL_Filter.UEID", 7);
64   ue0DummyValues->AddItem<long> ("Tot.PdcpSduNbrDl.UEID", 8);
65   ue0DummyValues->AddItem<long> ("DRB.PdcpPduNbrDl.Qos.UEID", 9);
66   ue0DummyValues->AddItem<double> ("DRB.IPThpDl.UEID", 10.0);
67   ue0DummyValues->AddItem<double> ("DRB.IPLateDl.UEID", 11.0);
68   msgValues.m_ueIndications.insert (ue0DummyValues);
69  
70   Ptr<MeasurementItemList> ue1DummyValues = Create<MeasurementItemList> ("UE-1");;
71   ue1DummyValues->AddItem<long> ("DRB.PdcpSduVolumeDl_Filter.UEID", 6);
72   ue1DummyValues->AddItem<long> ("QosFlow.PdcpPduVolumeDL_Filter.UEID", 7);
73   ue1DummyValues->AddItem<long> ("Tot.PdcpSduNbrDl.UEID", 8);
74   ue1DummyValues->AddItem<long> ("DRB.PdcpPduNbrDl.Qos.UEID", 9);
75   ue1DummyValues->AddItem<double> ("DRB.IPThpDl.UEID", 10.0);
76   ue1DummyValues->AddItem<double> ("DRB.IPLateDl.UEID", 11.0);
77   msgValues.m_ueIndications.insert (ue1DummyValues);
78   
79   Ptr<KpmIndicationMessage> msg = Create<KpmIndicationMessage> (msgValues);
80   
81   E2AP_PDU *pdu_cuup_ue = new E2AP_PDU; 
82   encoding::generate_e2apv1_indication_request_parameterized(pdu_cuup_ue, 
83                                                              params.requestorId,
84                                                              params.instanceId,
85                                                              params.ranFuncionId,
86                                                              params.actionId,
87                                                              1, // TODO sequence number  
88                                                              (uint8_t*) header->m_buffer, // buffer containing the encoded header
89                                                              header->m_size, // size of the encoded header
90                                                              (uint8_t*) msg->m_buffer, // buffer containing the encoded message
91                                                              msg->m_size); // size of the encoded message  
92   e2Term->SendE2Message (pdu_cuup_ue);
93   delete pdu_cuup_ue;
94   
95 }
96
97 /**
98 * KPM Subscription Request callback.
99 * This function is triggered whenever a RIC Subscription Request for 
100 * the KPM RAN Function is received.
101 *
102 * \param pdu request message
103 */
104 static void KpmSubscriptionCallback (E2AP_PDU_t* sub_req_pdu)
105 {
106   NS_LOG_UNCOND ("\n\nReceived RIC Subscription Request");
107   
108   E2Termination::RicSubscriptionRequest_rval_s params = e2Term->ProcessRicSubscriptionRequest (sub_req_pdu);
109   NS_LOG_UNCOND ("requestorId " << +params.requestorId << 
110                  ", instanceId " << +params.instanceId << 
111                  ", ranFuncionId " << +params.ranFuncionId << 
112                  ", actionId " << +params.actionId);  
113   
114   BuildAndSendReportMessage (params);
115 }
116
117 /**
118 * RIC Control Message callback.
119 * This function is triggered whenever a RIC Control Message is received.
120 *
121 * \param pdu request message
122 */
123 static void
124 RicControlMessageCallback (E2AP_PDU_t *ric_ctrl_pdu)
125 {
126   NS_LOG_UNCOND ("\n\nReceived RIC Control Message");
127
128   RicControlMessage msg = RicControlMessage (ric_ctrl_pdu);
129   // TODO log something
130 }
131
132
133 int 
134 main (int argc, char *argv[])
135 {
136   LogComponentEnable ("Asn1Types", LOG_LEVEL_ALL);
137   LogComponentEnable ("RicControlMessage", LOG_LEVEL_ALL);
138   e2Term = CreateObject<E2Termination> ("10.244.0.191", 36422, 38472, gnb, plmId);
139   Ptr<KpmFunctionDescription> kpmFd = Create<KpmFunctionDescription> ();
140   e2Term->RegisterKpmCallbackToE2Sm (200, kpmFd, &KpmSubscriptionCallback);    
141   Ptr<RicControlFunctionDescription> rcFd = Create<RicControlFunctionDescription> ();
142   e2Term->RegisterSmCallbackToE2Sm (300, rcFd, &RicControlMessageCallback);
143
144   return 0;
145 }