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) { |
| 25 | grpc_c_server_destroy(test_server); |
| 26 | exit(0); |
| 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 | { |
| 34 | BalCfg *get_cfg; |
| 35 | |
| 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 | } |
| 43 | |
| 44 | /* |
| 45 | * This functions gets invoked whenever bal RPC gets called |
| 46 | */ |
| 47 | void bal__bal_cfg_set_cb(grpc_c_context_t *context) |
| 48 | { |
| 49 | BalCfg *set_cfg; |
| 50 | BalErr bal_err; |
| 51 | |
| 52 | /* |
| 53 | * Read incoming message into set_cfg |
| 54 | */ |
| 55 | if (context->gcc_payload) { |
| 56 | context->gcc_stream->read(context, (void **)&set_cfg, 0); |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * send it to BAL |
| 61 | */ |
| 62 | |
| 63 | bal_err__init(&bal_err); |
| 64 | |
| 65 | bal_err.err= 0; |
| 66 | |
| 67 | /* |
| 68 | * Write reply back to the client |
| 69 | */ |
| 70 | if (!context->gcc_stream->write(context, &bal_err, 0)) { |
| 71 | } else { |
| 72 | printf("Failed to write\n"); |
| 73 | exit(1); |
| 74 | } |
| 75 | |
| 76 | grpc_c_status_t status; |
| 77 | status.gcs_code = 0; |
| 78 | |
| 79 | /* |
| 80 | * Finish response for RPC |
| 81 | */ |
| 82 | if (context->gcc_stream->finish(context, &status)) { |
| 83 | printf("Failed to write status\n"); |
| 84 | exit(1); |
| 85 | } |
| 86 | |
| 87 | #ifdef BAL_STUB |
| 88 | pthread_mutex_lock(&lock); |
| 89 | if(set_cfg->hdr->has_obj_type) |
| 90 | { |
| 91 | enQueue(set_cfg->hdr->obj_type, BAL_ERRNO__BAL_ERR_OK); |
| 92 | switch(set_cfg->hdr->obj_type) |
| 93 | { |
| 94 | case BAL_OBJ_ID__BAL_OBJ_ID_ACCESS_TERMINAL: |
| 95 | { |
| 96 | printf("\n***************************************************\n"); |
| 97 | printf("Received Access Terminal Configuration msg\n"); |
| 98 | printf("***************************************************\n"); |
| 99 | } |
| 100 | break; |
| 101 | case BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE: |
| 102 | { |
| 103 | printf("\n***************************************************\n"); |
| 104 | printf("Received PON Interface Configuration msg\n"); |
| 105 | printf("***************************************************\n"); |
| 106 | } |
| 107 | break; |
| 108 | case BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL: |
| 109 | { |
| 110 | printf("\n*****************************************************\n"); |
| 111 | printf("Received ONU Activation msg\n"); |
| 112 | printf("*****************************************************\n"); |
| 113 | } |
| 114 | default: |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | printf("BALSTUB:Cfg Set recevied without object type"); |
| 121 | } |
| 122 | pthread_mutex_unlock(&lock); |
| 123 | sleep(2); |
| 124 | pthread_cond_signal(&cv); |
| 125 | if(BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE == set_cfg->hdr->obj_type) |
| 126 | { |
| 127 | sleep(5); |
| 128 | enQueue(BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL, BAL_ERRNO__BAL_ERR_OK); |
| 129 | pthread_cond_signal(&cv); |
| 130 | } |
| 131 | #endif |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /* |
| 136 | * This functions gets invoked whenever bal clear RPC gets called |
| 137 | */ |
| 138 | void bal__bal_cfg_clear_cb(grpc_c_context_t *context) |
| 139 | { |
| 140 | BalKey *clear_key; |
| 141 | |
| 142 | /* |
| 143 | * Read incoming message into clear_key |
| 144 | */ |
| 145 | if (context->gcc_payload) { |
| 146 | context->gcc_stream->read(context, (void **)&clear_key, 0); |
| 147 | } |
| 148 | |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /* |
| 153 | * This functions gets invoked whenever bal Init RPC gets called |
| 154 | */ |
| 155 | void bal__bal_api_init_cb(grpc_c_context_t *context) |
| 156 | { |
| 157 | BalInit *bal_init; |
| 158 | |
| 159 | /* |
| 160 | * Read incoming message into set_cfg |
| 161 | */ |
| 162 | if (context->gcc_payload) { |
| 163 | context->gcc_stream->read(context, (void **)&bal_init, 0); |
| 164 | } |
| 165 | |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /* |
| 170 | * This functions gets invoked whenever bal finish RPC gets called |
| 171 | */ |
| 172 | void bal__bal_api_finish_cb(grpc_c_context_t *context) |
| 173 | { |
| 174 | #if 0 |
| 175 | void *finish_init; |
| 176 | |
| 177 | /* |
| 178 | * Read incoming message into set_cfg |
| 179 | */ |
| 180 | if (context->gcc_payload) { |
| 181 | context->gcc_stream->read(context, (void **)&finish_init); |
| 182 | } |
| 183 | #endif |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /* |
| 188 | * This functions gets invoked whenever bal finish RPC gets called |
| 189 | */ |
| 190 | void bal_ind__bal_ind_info_cb(grpc_c_context_t *context) |
| 191 | { |
| 192 | #if 0 |
| 193 | void *finish_init; |
| 194 | |
| 195 | /* |
| 196 | * Read incoming message into set_cfg |
| 197 | */ |
| 198 | if (context->gcc_payload) { |
| 199 | context->gcc_stream->read(context, (void **)&finish_init); |
| 200 | } |
| 201 | #endif |
| 202 | } |
| 203 | |
| 204 | |
| 205 | /* |
| 206 | * This functions gets invoked whenever bal RPC gets called |
| 207 | */ |
| 208 | void bal__bal_omci_msg_cb(grpc_c_context_t *context) |
| 209 | { |
| 210 | char *omci_msg; |
| 211 | |
| 212 | /* |
| 213 | * Read incoming message into omci_msg |
| 214 | */ |
| 215 | if (context->gcc_payload) { |
| 216 | context->gcc_stream->read(context, (void **)&omci_msg, 0); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * Takes socket path as argument |
| 222 | */ |
| 223 | int main (int argc, char **argv) |
| 224 | { |
| 225 | int i = 0; |
| 226 | grpc_c_server_t *server = NULL; |
| 227 | |
| 228 | printf("*************\n"); |
| 229 | printf("BAL STUB\n"); |
| 230 | printf("*************\n"); |
| 231 | if (argc < 2) { |
| 232 | fprintf(stderr, "Missing socket path argument\n"); |
| 233 | exit(1); |
| 234 | } |
| 235 | |
| 236 | signal(SIGINT, sigint_handler); |
| 237 | /* |
| 238 | * Initialize grpc-c library to be used with vanilla gRPC |
| 239 | */ |
| 240 | grpc_c_init(GRPC_THREADS, NULL); |
| 241 | |
| 242 | /* |
| 243 | * Create server object |
| 244 | */ |
| 245 | test_server = grpc_c_server_create("172.24.150.117:50060"); |
| 246 | if (test_server == NULL) { |
| 247 | printf("Failed to create server\n"); |
| 248 | exit(1); |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Initialize greeter service |
| 253 | */ |
| 254 | printf("\nCreating a test server\n"); |
| 255 | bal__service_init(test_server); |
| 256 | |
| 257 | /* |
| 258 | * Start server |
| 259 | */ |
| 260 | grpc_c_server_start(test_server); |
| 261 | |
| 262 | #ifdef BAL_STUB |
| 263 | printf("\nCreating a stub thread\n"); |
| 264 | create_stub_thread(); |
| 265 | #endif |
| 266 | |
| 267 | /* |
| 268 | * Blocks server to wait to completion |
| 269 | */ |
| 270 | grpc_c_server_wait(test_server); |
| 271 | |
| 272 | bal__service_init(server); |
| 273 | |
| 274 | } |