Fix extra nil termination char sent in RMR payload
[ric-app/ts.git] / test / app / ad_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:       ad_xapp.cpp
23         Abstract:   Simulates the AD xApp sending an anomaly dectection message to
24                 the TS xApp. It sends one message and exits.
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 <rmr/RIC_message_types.h>
37 #include "ricxfcpp/xapp.hpp"
38
39 using namespace std;
40 using namespace xapp;
41
42 unique_ptr<Xapp> xfw;
43
44 void ts_callback( Message& mbuf, int mtype, int subid, int len, Msg_component payload,  void* data ) {
45     string json ((char *)payload.get(), len);
46
47     cout << "[AD] TS Callback got a message, type=" << mtype << ", length=" << len << "\n";
48     cout << "[AD] Payload  is  " << json << endl;
49
50     // we only send one message, so we expect to receive only one as well
51     xfw->Stop();
52 }
53
54 // this thread just sends out one anomaly message to the TS xApp
55 void ad_loop() {
56     std::unique_ptr<Message> msg;
57     Msg_component payload;
58     int size;
59     int plen;
60
61     cout << "[AD] In Anomaly Detection ad_loop()\n";
62     sleep( 1 ); // just wait receiver thread starting up
63
64     msg = xfw->Alloc_msg(2048);
65     size = msg->Get_available_size();
66     if ( size < 2048 ) {
67         cout << "[ERROR] Message returned does not have enough size: " << size << " < 2048" << endl;
68         exit(1);
69     }
70
71     // the message we are sending
72     const char *ad_msg = "[{\"du-id\": 1010, \"ue-id\": \"Train passenger 2\", \"measTimeStampRf\": 1620835470108, \"Degradation\": \"RSRP RSSINR\"}]";
73
74     payload = msg->Get_payload();
75     snprintf( (char *) payload.get(), 2048, "%s", ad_msg );
76
77     plen = strlen( (char *) payload.get() );
78     cout << "[AD] Sending a message to TS, length: " << plen << "\n";
79     cout << "[AD] Message body " << payload.get() << endl;
80
81     // payload updated in place, nothing to copy from, so payload parm is nil
82     if ( ! msg->Send_msg( TS_ANOMALY_UPDATE, Message::NO_SUBID, plen, nullptr ) ) // msg type 30003
83         cout << "[ERROR] Unable to send a message to TS xApp, state: " << msg->Get_state() << endl;
84 }
85
86 int main(int argc, char const *argv[]) {
87     int nthreads = 1;
88
89     char* port = (char *) "4570";
90
91     cout << "[AD] Listening on port " << port << endl;
92     xfw = std::unique_ptr<Xapp>( new Xapp( port, true ) );
93
94     xfw->Add_msg_cb( TS_ANOMALY_ACK, ts_callback, NULL ); /*Register a callback function for msg type 30004*/
95
96     std::thread ad_thread;
97     ad_thread = std::thread(&ad_loop);
98
99     xfw->Run( nthreads );
100
101     ad_thread.join();
102
103     return 0;
104 }