cdf46a046ddffa7714cd0ad47a65fbf680319a21
[ric-app/ts.git] / test / app / rc_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:       rc_xapp.cpp
23         Abstract:   Implements a simple echo server just for testing gRPC calls
24                 from TS xApp.
25
26         Date:           08 Dec 2021
27         Author:         Alexandre Huff
28 */
29
30 #include <iostream>
31
32 #include <grpc/grpc.h>
33 #include <grpcpp/security/server_credentials.h>
34 #include <grpcpp/server.h>
35 #include <grpcpp/server_builder.h>
36
37 #include "../../ext/protobuf/api.grpc.pb.h"
38
39 using namespace std;
40
41 class ControlServiceImpl : public api::MsgComm::Service {
42     ::grpc::Status SendRICControlReqServiceGrpc(::grpc::ServerContext* context, const ::api::RicControlGrpcReq* request,
43                                                 ::api::RicControlGrpcRsp* response) override {
44         cout << "[RC] Received a new gRPC message\n";
45
46         /*
47             TODO check if this is related to RICControlAckEnum
48             if yes, then ACK value should be 2 (RIC_CONTROL_ACK)
49             api.proto assumes that 0 is an ACK
50         */
51         response->set_rspcode(0);
52         response->set_description("ACK");
53
54         return ::grpc::Status::OK;
55     }
56 };
57
58 void RunServer() {
59     string server_address("0.0.0.0:50051");
60     ControlServiceImpl service;
61
62     grpc::ServerBuilder builder;
63     builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
64     builder.RegisterService(&service);
65
66     unique_ptr<grpc::Server> server(builder.BuildAndStart());
67
68     cout << "[RC] Server listening on " << server_address << std::endl;
69     server->Wait();
70 }
71
72 int main(int argc, char const *argv[]) {
73     RunServer();
74
75     return 0;
76 }