blob: 9d6402fa17f2c3ad3822106f365ef70853a8e26a [file] [log] [blame]
suhasgrao76e8f8c2017-07-12 16:24:33 +05301/*
2 * Copyright (c) 2016, Juniper Networks, Inc.
3 * All rights reserved.
4 */
5
6#include <signal.h>
7#include <stdio.h>
8#include <unistd.h>
9#include <sys/time.h>
10#include "bal_msg_type.grpc-c.h"
11#include "bal_osmsg.grpc-c.h"
12#include "bal_model_ids.grpc-c.h"
13#include "bal_obj.grpc-c.h"
14#include "bal_model_types.grpc-c.h"
15#include "bal_errno.grpc-c.h"
16#include "bal.grpc-c.h"
17
18#ifdef BAL_STUB
19#include "bal_stub.h"
20#endif
21
22static grpc_c_server_t *test_server;
23
24static void sigint_handler (int x) {
suhasgrao4b31f402017-07-31 00:01:54 +053025 grpc_c_server_destroy(test_server);
26 exit(0);
suhasgrao76e8f8c2017-07-12 16:24:33 +053027}
28
29/*
30 * This functions gets invoked whenever bal RPC gets called
31 */
32void bal__bal_cfg_get_cb(grpc_c_context_t *context)
33{
suhasgrao4b31f402017-07-31 00:01:54 +053034 BalCfg *get_cfg;
suhasgrao76e8f8c2017-07-12 16:24:33 +053035
suhasgrao4b31f402017-07-31 00:01:54 +053036 /*
37 * Read incoming message into get_cfg
38 */
39 if (context->gcc_payload) {
40 context->gcc_stream->read(context, (void **)&get_cfg, 0);
41 }
42
suhasgrao76e8f8c2017-07-12 16:24:33 +053043}
44
45/*
46 * This functions gets invoked whenever bal RPC gets called
47 */
48void bal__bal_cfg_set_cb(grpc_c_context_t *context)
49{
suhasgrao4b31f402017-07-31 00:01:54 +053050 BalCfg *set_cfg;
51 BalErr bal_err;
suhasgrao76e8f8c2017-07-12 16:24:33 +053052
suhasgrao4b31f402017-07-31 00:01:54 +053053 /*
54 * Read incoming message into set_cfg
55 */
56 if (context->gcc_payload) {
57 context->gcc_stream->read(context, (void **)&set_cfg, 0);
58 }
suhasgrao76e8f8c2017-07-12 16:24:33 +053059
suhasgrao4b31f402017-07-31 00:01:54 +053060 /*
61 * send it to BAL
62 */
suhasgrao76e8f8c2017-07-12 16:24:33 +053063
suhasgrao4b31f402017-07-31 00:01:54 +053064 bal_err__init(&bal_err);
suhasgrao76e8f8c2017-07-12 16:24:33 +053065
suhasgrao4b31f402017-07-31 00:01:54 +053066 bal_err.err= 0;
suhasgrao76e8f8c2017-07-12 16:24:33 +053067
suhasgrao4b31f402017-07-31 00:01:54 +053068 /*
69 * Write reply back to the client
70 */
71 if (!context->gcc_stream->write(context, &bal_err, 0)) {
72 } else {
73 printf("Failed to write\n");
74 exit(1);
75 }
suhasgrao76e8f8c2017-07-12 16:24:33 +053076
suhasgrao4b31f402017-07-31 00:01:54 +053077 grpc_c_status_t status;
78 status.gcs_code = 0;
suhasgrao76e8f8c2017-07-12 16:24:33 +053079
suhasgrao4b31f402017-07-31 00:01:54 +053080 /*
81 * Finish response for RPC
82 */
83 if (context->gcc_stream->finish(context, &status)) {
84 printf("Failed to write status\n");
85 exit(1);
86 }
suhasgrao76e8f8c2017-07-12 16:24:33 +053087
88#ifdef BAL_STUB
suhasgrao4b31f402017-07-31 00:01:54 +053089 pthread_mutex_lock(&lock);
90
91 struct QNode *temp = newNode(set_cfg->hdr->obj_type,
92 BAL_ERRNO__BAL_ERR_OK,
93 set_cfg->device_id);
94
95 if(set_cfg->hdr->has_obj_type)
96 {
97
98 switch(set_cfg->hdr->obj_type)
99 {
100 case BAL_OBJ_ID__BAL_OBJ_ID_ACCESS_TERMINAL:
101 {
102 printf("\n***************************************************\n");
103 printf("Received Access Terminal Configuration msg\n");
104 printf("***************************************************\n");
105 }
106 break;
107 case BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE:
108 {
109 printf("\n***************************************************\n");
110 printf("Received PON Interface Configuration msg\n");
111 printf("***************************************************\n");
112 temp->intf_id = set_cfg->interface->key->intf_id;
113 printf("Pon ID = %d\n", temp->intf_id);
114 }
115 break;
116 case BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL:
117 {
118 printf("\n*****************************************************\n");
119 printf("Received ONU Activation msg\n");
120 printf("*****************************************************\n");
121 temp->intf_id = set_cfg->terminal->key->intf_id;
122 temp->onu_id = set_cfg->terminal->key->sub_term_id;
123 }
124 break;
125 case BAL_OBJ_ID__BAL_OBJ_ID_PACKET:
suhasgrao76e8f8c2017-07-12 16:24:33 +0530126 {
suhasgrao4b31f402017-07-31 00:01:54 +0530127 switch(set_cfg->packet->key->packet_send_dest->type)
128 {
129 case BAL_DEST_TYPE__BAL_DEST_TYPE_ITU_OMCI_CHANNEL:
130 {
131 printf("\n*****************************************************\n");
132 printf("Received OMCI msg\n");
133 printf("*****************************************************\n");
134 temp->intf_id = set_cfg->terminal->key->intf_id;
135 temp->onu_id = set_cfg->terminal->key->sub_term_id;
136 }
137 break;
138 default:
139 {
140 ("\n*****************************************************\n");
141 printf("Dest type invalid\n");
142 printf("*****************************************************\n");
143 }
144 break;
145 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530146 }
147 break;
suhasgrao4b31f402017-07-31 00:01:54 +0530148 default:
149 {
150 ("\n*****************************************************\n");
151 printf("Received Invalid msg\n");
152 printf("*****************************************************\n");
153 }
154 break;
155 }
156 enQueue(set_cfg->hdr->obj_type, temp);
157 }
158 else
159 {
160 printf("BALSTUB:Cfg Set recevied without object type");
161 }
162 pthread_mutex_unlock(&lock);
163 sleep(2);
164 pthread_cond_signal(&cv);
165 if(BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE == set_cfg->hdr->obj_type)
166 {
167 sleep(5);
168 struct QNode *temp1 = newNode(BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL,
169 BAL_ERRNO__BAL_ERR_OK,
170 set_cfg->device_id);
171 temp1->intf_id = set_cfg->interface->key->intf_id;
172 temp1->onu_id = 65535;
173 printf("sending _onu_discovery_indiaction\n");
174 enQueue(BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL, temp1);
175 pthread_cond_signal(&cv);
176 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530177#endif
178}
179
180
181/*
182 * This functions gets invoked whenever bal clear RPC gets called
183 */
184void bal__bal_cfg_clear_cb(grpc_c_context_t *context)
185{
suhasgrao4b31f402017-07-31 00:01:54 +0530186 BalKey *clear_key;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530187
suhasgrao4b31f402017-07-31 00:01:54 +0530188 /*
189 * Read incoming message into clear_key
190 */
191 if (context->gcc_payload) {
192 context->gcc_stream->read(context, (void **)&clear_key, 0);
193 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530194
195}
196
197
198/*
199 * This functions gets invoked whenever bal Init RPC gets called
200 */
201void bal__bal_api_init_cb(grpc_c_context_t *context)
202{
suhasgrao4b31f402017-07-31 00:01:54 +0530203 BalInit *bal_init;
204 BalErr bal_err;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530205
suhasgrao4b31f402017-07-31 00:01:54 +0530206 /*
207 * Read incoming message into set_cfg
208 */
209 if (context->gcc_payload) {
210 context->gcc_stream->read(context, (void **)&bal_init, 0);
211 }
212
213 /*
214 * send it to BAL
215 */
216
217
218 ("\n*****************************************************\n");
219 printf("Received API Init msg\n");
220 printf("*****************************************************\n");
221
222 bal_err__init(&bal_err);
223
224 bal_err.err= 0;
225
226 /*
227 * Write reply back to the client
228 */
229 if (!context->gcc_stream->write(context, &bal_err, 0)) {
230 } else {
231 printf("Failed to write\n");
232 exit(1);
233 }
234
235 grpc_c_status_t status;
236 status.gcs_code = 0;
237
238 /*
239 * Finish response for RPC
240 */
241 if (context->gcc_stream->finish(context, &status)) {
242 printf("Failed to write status\n");
243 exit(1);
244 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530245
246}
247
248
249/*
250 * This functions gets invoked whenever bal finish RPC gets called
251 */
252void bal__bal_api_finish_cb(grpc_c_context_t *context)
253{
254#if 0
suhasgrao4b31f402017-07-31 00:01:54 +0530255 void *finish_init;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530256
suhasgrao4b31f402017-07-31 00:01:54 +0530257 /*
258 * Read incoming message into set_cfg
259 */
260 if (context->gcc_payload) {
261 context->gcc_stream->read(context, (void **)&finish_init);
262 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530263#endif
264}
265
266
267/*
268 * This functions gets invoked whenever bal finish RPC gets called
269 */
270void bal_ind__bal_ind_info_cb(grpc_c_context_t *context)
271{
272#if 0
suhasgrao4b31f402017-07-31 00:01:54 +0530273 void *finish_init;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530274
suhasgrao4b31f402017-07-31 00:01:54 +0530275 /*
276 * Read incoming message into set_cfg
277 */
278 if (context->gcc_payload) {
279 context->gcc_stream->read(context, (void **)&finish_init);
280 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530281#endif
282}
283
284
285/*
suhasgrao76e8f8c2017-07-12 16:24:33 +0530286 * Takes socket path as argument
287 */
288int main (int argc, char **argv)
289{
suhasgrao4b31f402017-07-31 00:01:54 +0530290 int i = 0;
291 grpc_c_server_t *server = NULL;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530292
suhasgrao4b31f402017-07-31 00:01:54 +0530293 printf("*************\n");
294 printf("BAL STUB\n");
295 printf("*************\n");
296 if (argc < 2) {
297 fprintf(stderr, "Missing socket path argument\n");
298 exit(1);
299 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530300
suhasgrao4b31f402017-07-31 00:01:54 +0530301 signal(SIGINT, sigint_handler);
302 /*
303 * Initialize grpc-c library to be used with vanilla gRPC
304 */
305 grpc_c_init(GRPC_THREADS, NULL);
suhasgrao76e8f8c2017-07-12 16:24:33 +0530306
suhasgrao4b31f402017-07-31 00:01:54 +0530307 /*
308 * Create server object
309 */
310 test_server = grpc_c_server_create(argv[1]);
suhasgrao76e8f8c2017-07-12 16:24:33 +0530311
suhasgrao4b31f402017-07-31 00:01:54 +0530312 if (test_server == NULL) {
313 printf("Failed to create server\n");
314 exit(1);
315 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530316
suhasgrao4b31f402017-07-31 00:01:54 +0530317 /*
318 * Initialize greeter service
319 */
320 printf("\nCreating a test server\n");
321 bal__service_init(test_server);
322
323 /*
324 * Start server
325 */
326 grpc_c_server_start(test_server);
suhasgrao76e8f8c2017-07-12 16:24:33 +0530327
328#ifdef BAL_STUB
suhasgrao4b31f402017-07-31 00:01:54 +0530329 printf("\nCreating a stub thread\n");
330 create_stub_thread();
331#else
332 grpc_c_client_t *client = grpc_c_client_init("172.24.150.114:60001", "bal_client", NULL);
suhasgrao76e8f8c2017-07-12 16:24:33 +0530333#endif
334
suhasgrao4b31f402017-07-31 00:01:54 +0530335 /*
336 * Blocks server to wait to completion
337 */
338 grpc_c_server_wait(test_server);
suhasgrao76e8f8c2017-07-12 16:24:33 +0530339
suhasgrao4b31f402017-07-31 00:01:54 +0530340 /* code added for example Makefile to compile grpc-c along with edgecore driver */
341 bal__service_init(server);
suhasgrao76e8f8c2017-07-12 16:24:33 +0530342
343}