suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 22 | static grpc_c_server_t *test_server; |
| 23 | |
| 24 | static void sigint_handler (int x) { |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 25 | grpc_c_server_destroy(test_server); |
| 26 | exit(0); |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | /* |
| 30 | * This functions gets invoked whenever bal RPC gets called |
| 31 | */ |
| 32 | void bal__bal_cfg_get_cb(grpc_c_context_t *context) |
| 33 | { |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 34 | BalCfg *get_cfg; |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 35 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 36 | /* |
| 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 | |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* |
| 46 | * This functions gets invoked whenever bal RPC gets called |
| 47 | */ |
| 48 | void bal__bal_cfg_set_cb(grpc_c_context_t *context) |
| 49 | { |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 50 | BalCfg *set_cfg; |
| 51 | BalErr bal_err; |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 52 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 53 | /* |
| 54 | * Read incoming message into set_cfg |
| 55 | */ |
| 56 | if (context->gcc_payload) { |
| 57 | context->gcc_stream->read(context, (void **)&set_cfg, 0); |
| 58 | } |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 59 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 60 | /* |
| 61 | * send it to BAL |
| 62 | */ |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 63 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 64 | bal_err__init(&bal_err); |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 65 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 66 | bal_err.err= 0; |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 67 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 68 | /* |
| 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 | } |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 76 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 77 | grpc_c_status_t status; |
| 78 | status.gcs_code = 0; |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 79 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 80 | /* |
| 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 | } |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 87 | |
| 88 | #ifdef BAL_STUB |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 89 | 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: |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 126 | { |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 127 | 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 | } |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 146 | } |
| 147 | break; |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 148 | 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 | } |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 177 | #endif |
| 178 | } |
| 179 | |
| 180 | |
| 181 | /* |
| 182 | * This functions gets invoked whenever bal clear RPC gets called |
| 183 | */ |
| 184 | void bal__bal_cfg_clear_cb(grpc_c_context_t *context) |
| 185 | { |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 186 | BalKey *clear_key; |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 187 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 188 | /* |
| 189 | * Read incoming message into clear_key |
| 190 | */ |
| 191 | if (context->gcc_payload) { |
| 192 | context->gcc_stream->read(context, (void **)&clear_key, 0); |
| 193 | } |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 194 | |
| 195 | } |
| 196 | |
| 197 | |
| 198 | /* |
| 199 | * This functions gets invoked whenever bal Init RPC gets called |
| 200 | */ |
| 201 | void bal__bal_api_init_cb(grpc_c_context_t *context) |
| 202 | { |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 203 | BalInit *bal_init; |
| 204 | BalErr bal_err; |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 205 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 206 | /* |
| 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 | } |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 245 | |
| 246 | } |
| 247 | |
| 248 | |
| 249 | /* |
| 250 | * This functions gets invoked whenever bal finish RPC gets called |
| 251 | */ |
| 252 | void bal__bal_api_finish_cb(grpc_c_context_t *context) |
| 253 | { |
| 254 | #if 0 |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 255 | void *finish_init; |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 256 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 257 | /* |
| 258 | * Read incoming message into set_cfg |
| 259 | */ |
| 260 | if (context->gcc_payload) { |
| 261 | context->gcc_stream->read(context, (void **)&finish_init); |
| 262 | } |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 263 | #endif |
| 264 | } |
| 265 | |
| 266 | |
| 267 | /* |
| 268 | * This functions gets invoked whenever bal finish RPC gets called |
| 269 | */ |
| 270 | void bal_ind__bal_ind_info_cb(grpc_c_context_t *context) |
| 271 | { |
| 272 | #if 0 |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 273 | void *finish_init; |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 274 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 275 | /* |
| 276 | * Read incoming message into set_cfg |
| 277 | */ |
| 278 | if (context->gcc_payload) { |
| 279 | context->gcc_stream->read(context, (void **)&finish_init); |
| 280 | } |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 281 | #endif |
| 282 | } |
| 283 | |
| 284 | |
| 285 | /* |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 286 | * Takes socket path as argument |
| 287 | */ |
| 288 | int main (int argc, char **argv) |
| 289 | { |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 290 | int i = 0; |
| 291 | grpc_c_server_t *server = NULL; |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 292 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 293 | 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 | } |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 300 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 301 | signal(SIGINT, sigint_handler); |
| 302 | /* |
| 303 | * Initialize grpc-c library to be used with vanilla gRPC |
| 304 | */ |
| 305 | grpc_c_init(GRPC_THREADS, NULL); |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 306 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 307 | /* |
| 308 | * Create server object |
| 309 | */ |
| 310 | test_server = grpc_c_server_create(argv[1]); |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 311 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 312 | if (test_server == NULL) { |
| 313 | printf("Failed to create server\n"); |
| 314 | exit(1); |
| 315 | } |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 316 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 317 | /* |
| 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); |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 327 | |
| 328 | #ifdef BAL_STUB |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 329 | 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); |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 333 | #endif |
| 334 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 335 | /* |
| 336 | * Blocks server to wait to completion |
| 337 | */ |
| 338 | grpc_c_server_wait(test_server); |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 339 | |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 340 | /* code added for example Makefile to compile grpc-c along with edgecore driver */ |
| 341 | bal__service_init(server); |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 342 | |
| 343 | } |