blob: b169e508a69cf37e4e70ce17fd1cd7cab860e4f0 [file] [log] [blame]
suhasgrao76e8f8c2017-07-12 16:24:33 +05301#ifdef BAL_STUB
2#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
5#include <signal.h>
6#include "bal_stub.h"
7#include "bal_msg_type.grpc-c.h"
8#include "bal_osmsg.grpc-c.h"
9#include "bal_model_ids.grpc-c.h"
10#include "bal_obj.grpc-c.h"
11#include "bal_model_types.grpc-c.h"
12#include "bal_errno.grpc-c.h"
13#include "bal_indications.grpc-c.h"
14#include "bal.grpc-c.h"
15
16void *stub_thread(void *v)
17{
suhasgrao4b31f402017-07-31 00:01:54 +053018 int status;
19 grpc_c_client_t *client = grpc_c_client_init("172.24.150.167:60001", "bal_client", NULL);
20 pthread_mutex_lock(&lock);
21 pthread_cond_wait(&cv, &lock);
22 while(NULL != shared_queue->front)
23 {
24 BalObjId prevObjType;
25 char vendor_id[20];
26 char vendor_specific[20];
27 struct QNode *front = deQueue(shared_queue);
28 /* prepare and send rpc response */
29 BalIndications balIndCfg;
30 memset(&balIndCfg, 0, sizeof(BalIndications));
31 bal_indications__init(&balIndCfg);
32 balIndCfg.has_objtype = 1;
33 balIndCfg.objtype = front->obj_type;
34 balIndCfg.device_id = front->device_id;
35 printf("Device Id = %s\n", front->device_id);
36 switch(front->obj_type)
37 {
38 case BAL_OBJ_ID__BAL_OBJ_ID_ACCESS_TERMINAL:
39 {
40 printf("***************************************************\n");
41 printf("Successful Indication sent for ACCESS_TERMINAL\n");
42 printf("***************************************************\n");
43 balIndCfg.u_case = BAL_INDICATIONS__U_ACCESS_TERM_IND;
44 prevObjType = front->obj_type;
45 }
46 break;
47 case BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE:
48 {
49 printf("***************************************************\n");
50 printf("Successful Indication sent for PON INTERFACE UP\n");
51 printf("***************************************************\n");
52 balIndCfg.u_case = BAL_INDICATIONS__U_INTERFACE_IND;
53 prevObjType = front->obj_type;
54 }
55 break;
56 case BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL:
57 {
58 BalSubscriberTerminalKey subTermKey;
59 memset(&subTermKey, 0, sizeof(BalSubscriberTerminalKey));
60 bal_subscriber_terminal_key__init(&subTermKey);
suhasgrao17d65922017-07-13 16:55:29 +053061
suhasgrao4b31f402017-07-31 00:01:54 +053062 BalSerialNumber serial_number;
63 memset(&serial_number, 0, sizeof(BalSerialNumber));
64 bal_serial_number__init(&serial_number);
suhasgrao17d65922017-07-13 16:55:29 +053065
suhasgrao4b31f402017-07-31 00:01:54 +053066 char vendor_id[20];
67 memset(&vendor_id, 0, 20);
68 strcpy(vendor_id,"4252434D");
69 char vendor_specific[20];
70 memset(&vendor_specific, 0, 20);
71 strcpy(vendor_specific,"12345678");
72
73 if(BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE == prevObjType)
74 {
75 balIndCfg.u_case = BAL_INDICATIONS__U_TERMINAL_DISC;
76 BalSubscriberTerminalSubTermDisc terminal_disc;
77 memset(&terminal_disc, 0, sizeof(BalSubscriberTerminalSubTermDisc));
78 bal_subscriber_terminal_sub_term_disc__init(&terminal_disc);
79 balIndCfg.terminal_disc = &terminal_disc;
80
81 balIndCfg.terminal_disc->key = &subTermKey;
82 balIndCfg.terminal_disc->key->has_sub_term_id = 1;
83 balIndCfg.terminal_disc->key->sub_term_id = front->onu_id;
84 balIndCfg.terminal_disc->key->has_intf_id = 1;
85 balIndCfg.terminal_disc->key->intf_id = front->intf_id;
86
87 BalSubscriberTerminalSubTermDiscData subTermCfgData;
88 memset(&subTermCfgData, 0, sizeof(BalSubscriberTerminalSubTermDiscData));
89 bal_subscriber_terminal_sub_term_disc_data__init(&subTermCfgData);
90 balIndCfg.terminal_disc->data = &subTermCfgData;
91 balIndCfg.terminal_disc->data->serial_number = &serial_number;
92 balIndCfg.terminal_disc->data->serial_number->vendor_id = vendor_id;
93 printf("\n***************************************************\n");
94 printf("Sending ONU discovery message\n");
95 printf("***************************************************\n");
96 }
97 else
98 {
99 balIndCfg.u_case = BAL_INDICATIONS__U_TERMINAL_IND;
100 BalSubscriberTerminalInd terminal_ind;
101 memset(&terminal_ind, 0, sizeof(BalSubscriberTerminalInd));
102 bal_subscriber_terminal_ind__init(&terminal_ind);
103 balIndCfg.terminal_ind = &terminal_ind;
104
105 balIndCfg.terminal_ind->key = &subTermKey;
106 balIndCfg.terminal_ind->key->has_sub_term_id = 1;
107 balIndCfg.terminal_ind->key->sub_term_id = front->onu_id;
108 balIndCfg.terminal_ind->key->has_intf_id = 1;
109 balIndCfg.terminal_ind->key->intf_id = front->intf_id;
110
111 BalSubscriberTerminalIndData subTermCfgData;
112 memset(&subTermCfgData, 0, sizeof(BalSubscriberTerminalIndData));
113 bal_subscriber_terminal_ind_data__init(&subTermCfgData);
114 balIndCfg.terminal_ind->data = &subTermCfgData;
115 balIndCfg.terminal_ind->data->has_admin_state = 1;
116 balIndCfg.terminal_ind->data->admin_state = BAL_STATE__BAL_STATE_UP;
117 balIndCfg.terminal_ind->data->has_oper_status = 1;
118 balIndCfg.terminal_ind->data->oper_status = BAL_STATUS__BAL_STATUS_UP;
119 balIndCfg.terminal_ind->data->serial_number = &serial_number;
120 balIndCfg.terminal_ind->data->serial_number->vendor_id = vendor_id;
121 balIndCfg.terminal_ind->data->serial_number->vendor_specific = vendor_specific;
122 printf("***************************************************\n");
123 printf("ONU Activation Successful\n");
124 printf("***************************************************\n");
125 }
126 prevObjType = front->obj_type;
127 }
128 break;
129 case BAL_OBJ_ID__BAL_OBJ_ID_PACKET:
130 {
131 balIndCfg.u_case = BAL_INDICATIONS__U_BAL_OMCI_RESP;
132 BalPacketItuOmciChannelRx balomciresp;
133 memset(&balomciresp, 0, sizeof(BalPacketItuOmciChannelRx));
134 bal_packet_itu_omci_channel_rx__init(&balomciresp);
135
136 BalPacketKey balomcirespkey;
137 memset(&balomcirespkey, 0, sizeof(BalPacketKey));
138 bal_packet_key__init(&balomcirespkey);
139 balomciresp.key = &balomcirespkey;
140
141 BalDest balomcirespkeydest;
142 memset(&balomcirespkeydest, 0, sizeof(BalDest));
143 bal_dest__init(&balomcirespkeydest);
144 balomciresp.key->packet_send_dest = &balomcirespkeydest;
145 balomciresp.key->packet_send_dest->has_type = 1;
146 balomciresp.key->packet_send_dest->type = BAL_DEST_TYPE__BAL_DEST_TYPE_ITU_OMCI_CHANNEL;
147 balomciresp.key->packet_send_dest->u_case = BAL_DEST__U_ITU_OMCI_CHANNEL;
148
149 BalItuOmciChannel itu_omci_channel;
150 memset(&itu_omci_channel, 0, sizeof(BalItuOmciChannel));
151 bal_itu_omci_channel__init(&itu_omci_channel);
152 balomciresp.key->packet_send_dest->itu_omci_channel = &itu_omci_channel;
153 balomciresp.key->packet_send_dest->itu_omci_channel->has_sub_term_id = 1;
154 balomciresp.key->packet_send_dest->itu_omci_channel->sub_term_id = front->onu_id;
155 balomciresp.key->packet_send_dest->itu_omci_channel->has_int_id = 1;
156 balomciresp.key->packet_send_dest->itu_omci_channel->int_id = front->intf_id;
157 }
158 break;
159 default:
160 {
161 balIndCfg.u_case = BAL_INDICATIONS__U__NOT_SET;
162 prevObjType = front->obj_type;
163 }
164 break;
165 }
166 BalErr *output;
167 status = bal_ind__bal_ind_info(client, NULL, &balIndCfg, &output, NULL, 0);
168 free(front);
169 pthread_mutex_unlock(&lock);
170 pthread_mutex_lock(&lock);
171 pthread_cond_wait(&cv, &lock);
172 }
173 return NULL;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530174}
175
176void create_stub_thread()
177{
suhasgrao4b31f402017-07-31 00:01:54 +0530178 pthread_t threadId = 0;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530179
suhasgrao4b31f402017-07-31 00:01:54 +0530180 /* create shared queue */
181 shared_queue = createQueue();
suhasgrao76e8f8c2017-07-12 16:24:33 +0530182
suhasgrao4b31f402017-07-31 00:01:54 +0530183 pthread_create(&threadId, NULL, stub_thread, NULL);
suhasgrao76e8f8c2017-07-12 16:24:33 +0530184
185}
186
187/* A utility function to create an empty queue */
188bal_queue *createQueue()
189{
suhasgrao4b31f402017-07-31 00:01:54 +0530190 shared_queue = (struct Queue*)malloc(sizeof(struct Queue));
191 shared_queue->front = shared_queue->rear = NULL;
192 return shared_queue;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530193}
194
195/* A utility function to create a new linked list node */
suhasgrao4b31f402017-07-31 00:01:54 +0530196struct QNode* newNode(int objKey, int status, char *device_id)
suhasgrao76e8f8c2017-07-12 16:24:33 +0530197{
suhasgrao4b31f402017-07-31 00:01:54 +0530198 struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode));
199 temp->obj_type = objKey;
200 temp->status = status;
201 if(device_id != NULL)
202 {
203 memset(temp->device_id, 0, BAL_DEVICE_STR_LEN);
204 memcpy(temp->device_id, device_id, strlen(device_id));
205 }
206 temp->next = NULL;
207 return temp;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530208}
209
210/* The function to add data to shared_queue - Add end of the queue */
suhasgrao4b31f402017-07-31 00:01:54 +0530211void enQueue(int objKey, struct QNode *temp)
suhasgrao76e8f8c2017-07-12 16:24:33 +0530212{
suhasgrao4b31f402017-07-31 00:01:54 +0530213 /* Create a new LL node */
suhasgrao76e8f8c2017-07-12 16:24:33 +0530214
suhasgrao4b31f402017-07-31 00:01:54 +0530215 /* If queue is empty, then new node is front and rear both */
216 if (shared_queue->rear == NULL)
217 {
218 shared_queue->front = shared_queue->rear = temp;
219 return;
220 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530221
suhasgrao4b31f402017-07-31 00:01:54 +0530222 /* Add the new node at the end of queue and change rear */
223 shared_queue->rear->next = temp;
224 shared_queue->rear = temp;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530225}
226
227/* Function to remove data from shared_queue - FIFO */
228struct QNode *deQueue()
229{
suhasgrao4b31f402017-07-31 00:01:54 +0530230 /* If queue is empty, return NULL */
231 if (shared_queue->front == NULL)
232 {
233 return NULL;
234 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530235
suhasgrao4b31f402017-07-31 00:01:54 +0530236 /* Store previous front and move front one node ahead */
237 struct QNode *temp = shared_queue->front;
238 shared_queue->front = shared_queue->front->next;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530239
suhasgrao4b31f402017-07-31 00:01:54 +0530240 /* If front becomes NULL, then change rear also as NULL */
241 if (shared_queue->front == NULL)
242 {
243 shared_queue->rear = NULL;
244 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530245
suhasgrao4b31f402017-07-31 00:01:54 +0530246 return temp;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530247}
248
249#endif