Kim Kempf | 72cb33e | 2017-08-30 12:53:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2017-present Open Networking Foundation |
| 3 | ** |
| 4 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | ** you may not use this file except in compliance with the License. |
| 6 | ** You may obtain a copy of the License at |
| 7 | ** |
| 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | ** |
| 10 | ** Unless required by applicable law or agreed to in writing, software |
| 11 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | ** See the License for the specific language governing permissions and |
| 14 | ** limitations under the License. |
| 15 | */ |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 16 | |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 17 | #ifdef BAL_STUB |
| 18 | #include <stdlib.h> |
| 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | #include <signal.h> |
| 22 | #include "bal_stub.h" |
| 23 | #include "bal_msg_type.grpc-c.h" |
| 24 | #include "bal_osmsg.grpc-c.h" |
| 25 | #include "bal_model_ids.grpc-c.h" |
| 26 | #include "bal_obj.grpc-c.h" |
| 27 | #include "bal_model_types.grpc-c.h" |
| 28 | #include "bal_errno.grpc-c.h" |
| 29 | #include "bal_indications.grpc-c.h" |
| 30 | #include "bal.grpc-c.h" |
| 31 | |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 32 | char *voltha_ip_and_port = NULL; |
| 33 | |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 34 | void *stub_thread(void *v) |
| 35 | { |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 36 | int status; |
| 37 | pthread_mutex_lock(&lock); |
| 38 | pthread_cond_wait(&cv, &lock); |
| 39 | while(NULL != shared_queue->front) |
| 40 | { |
| 41 | BalErr *output; |
| 42 | BalObjId prevObjType; |
| 43 | char vendor_id[20]; |
| 44 | char vendor_specific[20]; |
| 45 | struct QNode *front = deQueue(shared_queue); |
| 46 | /* prepare and send rpc response */ |
| 47 | BalIndications balIndCfg; |
| 48 | memset(&balIndCfg, 0, sizeof(BalIndications)); |
| 49 | bal_indications__init(&balIndCfg); |
| 50 | balIndCfg.has_objtype = 1; |
| 51 | balIndCfg.objtype = front->obj_type; |
| 52 | balIndCfg.device_id = front->device_id; |
| 53 | printf("Device Id = %s\n", front->device_id); |
| 54 | switch(front->obj_type) |
| 55 | { |
| 56 | case BAL_OBJ_ID__BAL_OBJ_ID_ACCESS_TERMINAL: |
| 57 | { |
| 58 | printf("***************************************************\n"); |
| 59 | printf("Successful Indication sent for ACCESS_TERMINAL\n"); |
| 60 | printf("***************************************************\n"); |
| 61 | balIndCfg.u_case = BAL_INDICATIONS__U_ACCESS_TERM_IND; |
| 62 | prevObjType = front->obj_type; |
| 63 | BalAccessTerminalKey accTermKey; |
| 64 | memset(&accTermKey, 0, sizeof(BalAccessTerminalKey)); |
| 65 | bal_access_terminal_key__init(&accTermKey); |
| 66 | accTermKey.has_access_term_id = 1; |
| 67 | accTermKey.access_term_id = 1; |
| 68 | |
| 69 | BalAccessTerminalIndData data; |
| 70 | memset(&data, 0, sizeof(BalAccessTerminalIndData)); |
| 71 | bal_access_terminal_ind_data__init(&data); |
| 72 | data.has_admin_state = 1; |
| 73 | data.admin_state = BAL_STATE__BAL_STATE_UP; |
suhasgrao | 17d6592 | 2017-07-13 16:55:29 +0530 | [diff] [blame] | 74 | |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 75 | BalAccessTerminalInd access_term_ind; |
| 76 | memset(&access_term_ind, 0, sizeof(BalAccessTerminalInd)); |
| 77 | bal_access_terminal_ind__init(&access_term_ind); |
| 78 | access_term_ind.data = &data; |
| 79 | access_term_ind.key = &accTermKey; |
| 80 | balIndCfg.access_term_ind = &access_term_ind; |
| 81 | status = bal_ind__bal_acc_term_ind(client, NULL, &balIndCfg, &output, NULL, 0); |
| 82 | } |
| 83 | break; |
| 84 | case BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE: |
| 85 | { |
| 86 | printf("***************************************************\n"); |
| 87 | printf("Successful Indication sent for PON INTERFACE UP\n"); |
| 88 | printf("***************************************************\n"); |
| 89 | balIndCfg.u_case = BAL_INDICATIONS__U_INTERFACE_IND; |
| 90 | prevObjType = front->obj_type; |
| 91 | status = bal_ind__bal_iface_ind(client, NULL, &balIndCfg, &output, NULL, 0); |
| 92 | } |
| 93 | break; |
| 94 | case BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL: |
| 95 | { |
| 96 | BalSubscriberTerminalKey subTermKey; |
| 97 | memset(&subTermKey, 0, sizeof(BalSubscriberTerminalKey)); |
| 98 | bal_subscriber_terminal_key__init(&subTermKey); |
suhasgrao | 17d6592 | 2017-07-13 16:55:29 +0530 | [diff] [blame] | 99 | |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 100 | BalSerialNumber serial_number; |
| 101 | memset(&serial_number, 0, sizeof(BalSerialNumber)); |
| 102 | bal_serial_number__init(&serial_number); |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 103 | |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 104 | /* |
| 105 | char vendor_id[20]; |
| 106 | memset(&vendor_id, 0, 20); |
| 107 | strcpy(vendor_id,"4252434D"); |
| 108 | char vendor_specific[20]; |
| 109 | memset(&vendor_specific, 0, 20); |
| 110 | strcpy(vendor_specific,"12345678"); |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 111 | |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 112 | if(BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE == prevObjType) |
| 113 | { |
| 114 | balIndCfg.u_case = BAL_INDICATIONS__U_TERMINAL_DISC; |
| 115 | BalSubscriberTerminalSubTermDisc terminal_disc; |
| 116 | memset(&terminal_disc, 0, sizeof(BalSubscriberTerminalSubTermDisc)); |
| 117 | bal_subscriber_terminal_sub_term_disc__init(&terminal_disc); |
| 118 | balIndCfg.terminal_disc = &terminal_disc; |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 119 | |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 120 | balIndCfg.terminal_disc->key = &subTermKey; |
| 121 | balIndCfg.terminal_disc->key->has_sub_term_id = 1; |
| 122 | balIndCfg.terminal_disc->key->sub_term_id = front->onu_id; |
| 123 | balIndCfg.terminal_disc->key->has_intf_id = 1; |
| 124 | balIndCfg.terminal_disc->key->intf_id = front->intf_id; |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 125 | |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 126 | BalSubscriberTerminalSubTermDiscData subTermCfgData; |
| 127 | memset(&subTermCfgData, 0, sizeof(BalSubscriberTerminalSubTermDiscData)); |
| 128 | bal_subscriber_terminal_sub_term_disc_data__init(&subTermCfgData); |
| 129 | balIndCfg.terminal_disc->data = &subTermCfgData; |
| 130 | balIndCfg.terminal_disc->data->serial_number = &serial_number; |
| 131 | balIndCfg.terminal_disc->data->serial_number->vendor_id = vendor_id; |
| 132 | printf("\n***************************************************\n"); |
| 133 | printf("Sending ONU discovery message\n"); |
| 134 | printf("***************************************************\n"); |
| 135 | status = bal_ind__bal_subs_term_discovery_ind(client, NULL, &balIndCfg, &output, NULL, 0); |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | */ |
| 140 | balIndCfg.u_case = BAL_INDICATIONS__U_TERMINAL_DISC; |
| 141 | balIndCfg.has_sub_group = 1; |
| 142 | balIndCfg.sub_group = BAL_SUBSCRIBER_TERMINAL_AUTO_ID__BAL_SUBSCRIBER_TERMINAL_AUTO_ID_SUB_TERM_DISC; |
| 143 | BalSubscriberTerminalSubTermDisc terminal_disc; |
| 144 | memset(&terminal_disc, 0, sizeof(BalSubscriberTerminalSubTermDisc)); |
| 145 | bal_subscriber_terminal_sub_term_disc__init(&terminal_disc); |
| 146 | balIndCfg.terminal_disc = &terminal_disc; |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 147 | |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 148 | balIndCfg.terminal_disc->key = &subTermKey; |
| 149 | balIndCfg.terminal_disc->key->has_sub_term_id = 1; |
| 150 | balIndCfg.terminal_disc->key->sub_term_id = front->onu_id; |
| 151 | balIndCfg.terminal_disc->key->has_intf_id = 1; |
| 152 | balIndCfg.terminal_disc->key->intf_id = front->intf_id; |
| 153 | |
| 154 | BalSubscriberTerminalSubTermDiscData subTermDiscCfgData; |
| 155 | memset(&subTermDiscCfgData, 0, sizeof(BalSubscriberTerminalSubTermDiscData)); |
| 156 | bal_subscriber_terminal_sub_term_disc_data__init(&subTermDiscCfgData); |
| 157 | balIndCfg.terminal_disc->data = &subTermDiscCfgData; |
| 158 | balIndCfg.terminal_disc->data->serial_number = &serial_number; |
| 159 | balIndCfg.terminal_disc->data->serial_number->vendor_id = front->vendor_id; |
| 160 | balIndCfg.terminal_disc->data->serial_number->vendor_specific = front->vendor_specific; |
| 161 | printf("\n***************************************************\n"); |
| 162 | printf("Sending ONU discovery message\n"); |
| 163 | printf("***************************************************\n"); |
| 164 | status = bal_ind__bal_subs_term_discovery_ind(client, NULL, &balIndCfg, &output, NULL, 0); |
| 165 | |
| 166 | |
| 167 | balIndCfg.u_case = BAL_INDICATIONS__U_TERMINAL_IND; |
| 168 | balIndCfg.has_sub_group = 1; |
| 169 | balIndCfg.sub_group = BAL_SUBSCRIBER_TERMINAL_AUTO_ID__BAL_SUBSCRIBER_TERMINAL_AUTO_ID_IND; |
| 170 | BalSubscriberTerminalInd terminal_ind; |
| 171 | memset(&terminal_ind, 0, sizeof(BalSubscriberTerminalInd)); |
| 172 | bal_subscriber_terminal_ind__init(&terminal_ind); |
| 173 | balIndCfg.terminal_ind = &terminal_ind; |
| 174 | |
| 175 | balIndCfg.terminal_ind->key = &subTermKey; |
| 176 | balIndCfg.terminal_ind->key->has_sub_term_id = 1; |
| 177 | balIndCfg.terminal_ind->key->sub_term_id = front->onu_id; |
| 178 | balIndCfg.terminal_ind->key->has_intf_id = 1; |
| 179 | balIndCfg.terminal_ind->key->intf_id = front->intf_id; |
| 180 | |
| 181 | BalSubscriberTerminalIndData subTermCfgData; |
| 182 | memset(&subTermCfgData, 0, sizeof(BalSubscriberTerminalIndData)); |
| 183 | bal_subscriber_terminal_ind_data__init(&subTermCfgData); |
| 184 | balIndCfg.terminal_ind->data = &subTermCfgData; |
| 185 | balIndCfg.terminal_ind->data->has_admin_state = 1; |
| 186 | balIndCfg.terminal_ind->data->admin_state = BAL_STATE__BAL_STATE_UP; |
| 187 | balIndCfg.terminal_ind->data->has_oper_status = 1; |
| 188 | balIndCfg.terminal_ind->data->oper_status = BAL_STATUS__BAL_STATUS_UP; |
| 189 | balIndCfg.terminal_ind->data->serial_number = &serial_number; |
| 190 | balIndCfg.terminal_ind->data->serial_number->vendor_id = front->vendor_id; |
| 191 | balIndCfg.terminal_ind->data->serial_number->vendor_specific = front->vendor_specific; |
| 192 | printf("***************************************************\n"); |
| 193 | printf("ONU Activation Successful %s\n", balIndCfg.terminal_ind->data->serial_number->vendor_specific); |
| 194 | printf("***************************************************\n"); |
| 195 | status = bal_ind__bal_subs_term_ind(client, NULL, &balIndCfg, &output, NULL, 0); |
| 196 | /* |
| 197 | } |
| 198 | */ |
| 199 | prevObjType = front->obj_type; |
| 200 | } |
| 201 | break; |
| 202 | case BAL_OBJ_ID__BAL_OBJ_ID_PACKET: |
| 203 | { |
| 204 | balIndCfg.u_case = BAL_INDICATIONS__U_BAL_OMCI_RESP; |
| 205 | BalPacketItuOmciChannelRx balomciresp; |
| 206 | memset(&balomciresp, 0, sizeof(BalPacketItuOmciChannelRx)); |
| 207 | bal_packet_itu_omci_channel_rx__init(&balomciresp); |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 208 | |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 209 | BalPacketKey balomcirespkey; |
| 210 | memset(&balomcirespkey, 0, sizeof(BalPacketKey)); |
| 211 | bal_packet_key__init(&balomcirespkey); |
| 212 | balomciresp.key = &balomcirespkey; |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 213 | |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 214 | BalDest balomcirespkeydest; |
| 215 | memset(&balomcirespkeydest, 0, sizeof(BalDest)); |
| 216 | bal_dest__init(&balomcirespkeydest); |
| 217 | balomciresp.key->packet_send_dest = &balomcirespkeydest; |
| 218 | balomciresp.key->packet_send_dest->has_type = 1; |
| 219 | balomciresp.key->packet_send_dest->type = BAL_DEST_TYPE__BAL_DEST_TYPE_ITU_OMCI_CHANNEL; |
| 220 | balomciresp.key->packet_send_dest->u_case = BAL_DEST__U_ITU_OMCI_CHANNEL; |
| 221 | |
| 222 | BalItuOmciChannel itu_omci_channel; |
| 223 | memset(&itu_omci_channel, 0, sizeof(BalItuOmciChannel)); |
| 224 | bal_itu_omci_channel__init(&itu_omci_channel); |
| 225 | balomciresp.key->packet_send_dest->itu_omci_channel = &itu_omci_channel; |
| 226 | balomciresp.key->packet_send_dest->itu_omci_channel->has_sub_term_id = 1; |
| 227 | balomciresp.key->packet_send_dest->itu_omci_channel->sub_term_id = front->onu_id; |
| 228 | balomciresp.key->packet_send_dest->itu_omci_channel->has_intf_id = 1; |
| 229 | balomciresp.key->packet_send_dest->itu_omci_channel->intf_id = front->intf_id; |
| 230 | status = bal_ind__bal_pkt_omci_channel_rx_ind(client, NULL, &balIndCfg, &output, NULL, 0); |
| 231 | } |
| 232 | break; |
| 233 | default: |
| 234 | { |
| 235 | balIndCfg.u_case = BAL_INDICATIONS__U__NOT_SET; |
| 236 | prevObjType = front->obj_type; |
| 237 | } |
| 238 | break; |
| 239 | } |
| 240 | free(front); |
| 241 | pthread_mutex_unlock(&lock); |
| 242 | pthread_mutex_lock(&lock); |
| 243 | pthread_cond_wait(&cv, &lock); |
| 244 | } |
| 245 | return NULL; |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void create_stub_thread() |
| 249 | { |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 250 | pthread_t threadId = 0; |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 251 | |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 252 | /* create shared queue */ |
| 253 | shared_queue = createQueue(); |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 254 | |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 255 | pthread_create(&threadId, NULL, stub_thread, NULL); |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 256 | |
| 257 | } |
| 258 | |
| 259 | /* A utility function to create an empty queue */ |
| 260 | bal_queue *createQueue() |
| 261 | { |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 262 | shared_queue = (struct Queue*)malloc(sizeof(struct Queue)); |
| 263 | shared_queue->front = shared_queue->rear = NULL; |
| 264 | return shared_queue; |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* A utility function to create a new linked list node */ |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 268 | struct QNode* newNode(int objKey, int status, char *device_id) |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 269 | { |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 270 | struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode)); |
| 271 | temp->obj_type = objKey; |
| 272 | temp->status = status; |
| 273 | if(device_id != NULL) |
| 274 | { |
| 275 | memset(temp->device_id, 0, BAL_DEVICE_STR_LEN); |
| 276 | memcpy(temp->device_id, device_id, strlen(device_id)); |
| 277 | } |
| 278 | temp->next = NULL; |
| 279 | return temp; |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /* The function to add data to shared_queue - Add end of the queue */ |
suhasgrao | 4b31f40 | 2017-07-31 00:01:54 +0530 | [diff] [blame] | 283 | void enQueue(int objKey, struct QNode *temp) |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 284 | { |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 285 | /* Create a new LL node */ |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 286 | |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 287 | /* If queue is empty, then new node is front and rear both */ |
| 288 | if (shared_queue->rear == NULL) |
| 289 | { |
| 290 | shared_queue->front = shared_queue->rear = temp; |
| 291 | return; |
| 292 | } |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 293 | |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 294 | /* Add the new node at the end of queue and change rear */ |
| 295 | shared_queue->rear->next = temp; |
| 296 | shared_queue->rear = temp; |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* Function to remove data from shared_queue - FIFO */ |
| 300 | struct QNode *deQueue() |
| 301 | { |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 302 | /* If queue is empty, return NULL */ |
| 303 | if (shared_queue->front == NULL) |
| 304 | { |
| 305 | return NULL; |
| 306 | } |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 307 | |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 308 | /* Store previous front and move front one node ahead */ |
| 309 | struct QNode *temp = shared_queue->front; |
| 310 | shared_queue->front = shared_queue->front->next; |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 311 | |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 312 | /* If front becomes NULL, then change rear also as NULL */ |
| 313 | if (shared_queue->front == NULL) |
| 314 | { |
| 315 | shared_queue->rear = NULL; |
| 316 | } |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 317 | |
Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 318 | return temp; |
| 319 | } |
| 320 | |
| 321 | void stub_bal_init(BalInit *bal_init) |
| 322 | { |
| 323 | client = grpc_c_client_init(bal_init->voltha_adapter_ip_port, "bal_client", NULL); |
suhasgrao | 76e8f8c | 2017-07-12 16:24:33 +0530 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | #endif |