1. Transitioned to using latest asn1c compiler
[ric-app/admin.git] / test / mock_a1_mediator.cc
1 /*
2 ==================================================================================
3
4         Copyright (c) 2018-2019 AT&T Intellectual Property.
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19 /* Author : Ashwin Sridharan
20
21    A sample test client to demonstrate A1 functionality.
22    Sends different kind of policy requests (valid/invalid) and prints out response 
23 */
24
25 #include <limits>
26 #include <map>
27 #include <getopt.h>
28 #include <csignal>
29 #include <time.h>
30 #include <xapp_utils.hpp>
31 #include <vector>
32 #include <rmr/RIC_message_types.h>
33
34 std::string gNodeB = "";
35
36 bool rcv_message(rmr_mbuf_t *message){
37   std::string response;
38   switch(message->mtype){
39   case DC_ADM_INT_CONTROL_ACK:
40     std::cout <<"Received response = " << (char *)message->payload << " of len = " << strlen((char *)message->payload) << "Actual = " << message->len << std::endl;
41     break;
42
43   case DC_ADM_GET_POLICY_ACK:
44     std::cout <<"Received Policy  = " << (char *)message->payload << " of len = " << strlen((char *)message->payload) << "Actual = " << message->len << std::endl;
45     break;
46     
47   default:
48     std::cout <<"Unknown RMR message of type " << message->mtype << " received" << std::endl;
49   }
50   
51   return false;
52 }
53
54
55 void usage(char *command){
56     std::cout <<"Usage : " << command << " ";
57     std::cout <<" --name[-n] xapp_instance_name ";
58     std::cout <<" --port[-p] port to listen on (default is tcp:4561) ";
59     std::cout <<"--schema[-s] schema file";
60     
61     std::cout << std::endl;
62 }
63
64
65 void msg_error(rmr_mbuf_t *message){
66   mdclog_write(MDCLOG_ERR, "Error sending message of length %d and type %d, Reason %d",  message->len,  message->mtype, errno );
67 };
68
69
70 int main(int argc, char *argv[]){
71
72   char name[128] = "test_a1_client";
73   char port[16] = "tcp:4560";
74   unsigned int num_threads = 1;
75   std::unique_ptr<XaPP> my_xapp;
76   
77   // Parse command line options
78   static struct option long_options[] = 
79     {
80
81         /* Thse options require arguments */
82         {"name", required_argument, 0, 'n'},
83         {"port", required_argument, 0, 'p'},
84         
85     };
86
87
88    while(1) {
89
90         int option_index = 0;
91         char c = getopt_long(argc, argv, "n:p:", long_options, &option_index);
92
93         if(c == -1){
94             break;
95          }
96
97         switch(c)
98         {
99
100         case 0:
101           /* An option flag was set. 
102              Do nothing for now */
103           break;
104           
105         case 'n':
106           strcpy(name, optarg);
107           break;
108           
109         case 'p':
110           strcpy(port, optarg);
111           break;
112           
113           
114         case 'h':
115           usage(argv[0]);
116           exit(0);
117           
118         default:
119           usage(argv[0]);
120           exit(1);
121         }
122    };
123
124    init_logger(name, MDCLOG_INFO);
125    
126    mdclog_write(MDCLOG_INFO, "XaPP name specified = %s", name);
127    mdclog_write(MDCLOG_INFO, "XaPP port specified = %s", port);
128
129    mdclog_write(MDCLOG_INFO,"XaPP listener threads specified = auto");
130    my_xapp = std::make_unique<XaPP>(name, port, 1024, 1);
131    
132    
133    // Start receiving loop ...
134    std::vector<int> thread_ids(num_threads);
135    for(unsigned int i = 0; i < num_threads; i++){
136      thread_ids[i] = (*my_xapp).StartThread(&rcv_message, msg_error);
137      i++;
138    };
139    
140    bool enforce = true;
141    int block_rate = 2;
142    int window_length = 20;
143    int trigger_threshold = 40;
144    char buffer[1024];
145    while(1){
146      
147     // Send a valid config
148      std::string message_string ;
149      std::string start = "{";
150      std::string end = "}";
151
152      message_string = start + "\"enforce\":" + (enforce? "true":"false") + ",";
153      message_string += std::string("\"blocking_rate\":") + std::to_string(block_rate) + ",";
154      message_string += std::string("\"window_length\":") + std::to_string(window_length) + ",";
155      message_string += std::string("\"trigger_threshold\":") + std::to_string(trigger_threshold) + end;
156      memcpy(buffer, message_string.c_str(), message_string.length());     
157      my_xapp.get()->Send(DC_ADM_INT_CONTROL,  message_string.length(),  buffer);
158
159      
160      sleep(2);
161      
162      // // Send an invalid config
163      message_string = start + "\"enfce\":" + (enforce? "true":"false") + ",";
164      message_string += std::string("\"blocking_rate\":") + std::to_string(block_rate) + ",";
165      message_string += std::string("\"window_length\":") + std::to_string(window_length) + end;  
166      memcpy(buffer, message_string.c_str(), message_string.length());     
167      my_xapp.get()->Send(DC_ADM_INT_CONTROL,  message_string.length(),  buffer);
168      sleep(2);
169      
170      // Send invalid JSON
171      message_string.assign("\"enforce\":false,");
172      message_string += std::string("\"blocking_rate\":") + std::to_string(block_rate) + ",";
173      message_string += std::string("\"window_length\":") + std::to_string(window_length) + end;  
174      memcpy(buffer, message_string.c_str(), message_string.length());
175      my_xapp.get()->Send(DC_ADM_INT_CONTROL, message_string.length(), buffer);
176      
177      sleep(2);
178
179
180      // Send request for policy
181      // we don't care about contents of request for now ...
182      std::cout <<"Sending request to get policy" << std::endl;
183      my_xapp.get()->Send(DC_ADM_GET_POLICY, message_string.length(), buffer);
184      sleep(2);
185
186     window_length += 1;
187      
188    }
189    
190    (*my_xapp).Stop();  
191    
192 }