Update TS xApp for Release D use case
[ric-app/ts.git] / test / app / qp_xapp.cpp
1 // vi: ts=4 sw=4 noet:
2 /*
3 ==================================================================================
4         Copyright (c) 2021 AT&T Intellectual Property.
5         Copyright (c) 2021 Alexandre Huff.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20
21 /*
22         Mnemonic:       qp_xapp.cpp
23         Abstract:   Simulates both, the QP Driver and QP xApp for testing the behavior
24                 of the TS xApp.
25
26         Date:           20 May 2021
27         Author:         Alexandre Huff
28 */
29
30 #include <iostream>
31 #include <memory>
32 #include <thread>
33 #include <unistd.h>
34 #include <string.h>
35
36 #include <cstdlib>
37 #include <ctime>
38
39 #include <rapidjson/document.h>
40 #include <rapidjson/writer.h>
41 #include <rapidjson/stringbuffer.h>
42 #include <rapidjson/schema.h>
43 #include <rapidjson/reader.h>
44
45 #include <rmr/RIC_message_types.h>
46 #include "ricxfcpp/xapp.hpp"
47
48 using namespace std;
49 using namespace xapp;
50 using namespace rapidjson;
51
52 unique_ptr<Xapp> xfw;
53
54
55 void prediction_callback( Message& mbuf, int mtype, int subid, int len, Msg_component payload,  void* data ) {
56     cout << "[QP] Prediction Callback got a message, type=" << mtype << ", length=" << len << "\n";
57     cout << "[QP] Payload is " << payload.get() << endl;
58
59     int randomNumber;
60     srand( (unsigned int) time(0) );
61
62     const char *json = (const char *) payload.get();
63     Document document;
64     document.Parse(json);
65
66     const Value& uePred = document["UEPredictionSet"];
67     if ( uePred.Size() > 0 ) {
68         string ueid = uePred[0].GetString();
69         // we want to create "{"ueid-user1": {"CID1": [10, 20], "CID2": [30, 40], "CID3": [50, 60]}}";
70         string body = "{\"" + ueid + "\": {";
71         for ( int i = 1; i <= 3; i++ ) {
72             int down = rand() % 100;
73             int up = rand() % 100;
74             if ( i != 3 ) {
75                 body += "\"CID" + to_string(i) + "\": [" + to_string(down) + ", " + to_string(up) + "], ";
76             } else {
77                 body += "\"CID" + to_string(i) + "\": [" + to_string(down) + ", " + to_string(up) + "]}}";
78             }
79         }
80
81         /*
82             we are sending a string, so we have to include the nil byte to send with RMR and keep
83             things simple in the receiver side
84         */
85         int len = body.size() + 1;
86
87         cout << "[QP] Sending a message to TS, length=" << len << "\n";
88         cout << "[QP] Message body " << body << endl;
89
90         // payload updated in place, nothing to copy from, so payload parm is nil
91         if ( ! mbuf.Send_response( TS_QOE_PREDICTION, Message::NO_SUBID, len, (unsigned char *) body.c_str() ) ) // msg type 30002
92             cout << "[ERROR] unable to send a message to TS xApp, state: " << mbuf.Get_state() << endl;
93     }
94 }
95
96 int main(int argc, char const *argv[]) {
97     int nthreads = 1;
98
99     char* port = (char *) "4580";
100
101     cout << "[QP] listening on port " << port << endl;
102     xfw = std::unique_ptr<Xapp>( new Xapp( port, true ) );
103
104     xfw->Add_msg_cb( TS_UE_LIST, prediction_callback, NULL ); /*Register a callback function for msg type 30000*/
105
106     xfw->Run( nthreads );
107
108     return 0;
109 }