blob: adebd0ae2893cd89a997543d5751a332074b17ba [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{
18 int status;
19 grpc_c_client_t *client = grpc_c_client_init("172.24.150.114: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 struct QNode *front = deQueue(shared_queue);
26 /* prepare and send rpc response */
27 BalIndications balIndCfg;
28 memset(&balIndCfg, 0, sizeof(BalIndications));
29 bal_indications__init(&balIndCfg);
30 BalObjInd bal_obj_ind;
31 memset(&bal_obj_ind, 0, sizeof(BalObjInd));
32 bal_obj_ind__init(&bal_obj_ind);
33 balIndCfg.u_case = BAL_INDICATIONS__U_BAL_OBJ_INFO;
34 balIndCfg.balobjinfo = &bal_obj_ind;
35 balIndCfg.balobjinfo->has_objtype = 1;
36 balIndCfg.balobjinfo->objtype = front->obj_type;
37 balIndCfg.balobjinfo->has_status = 1;
38 balIndCfg.balobjinfo->status = front->status;
39 char keystr;
40 balIndCfg.balobjinfo->keystr = &keystr;
41 switch(front->obj_type)
42 {
43 case BAL_OBJ_ID__BAL_OBJ_ID_ACCESS_TERMINAL:
44 {
45 printf("***************************************************\n");
46 printf("Successful Indication sent for ACCESS_TERMINAL\n");
47 printf("***************************************************\n");
48 balIndCfg.balobjinfo->u_case = BAL_OBJ_IND__U__NOT_SET;
49 prevObjType = front->obj_type;
50 }
51 break;
52 case BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE:
53 {
54 printf("***************************************************\n");
55 printf("Successful Indication sent for PON INTERFACE UP\n");
56 printf("***************************************************\n");
57 balIndCfg.balobjinfo->u_case = BAL_OBJ_IND__U__NOT_SET;
58 prevObjType = front->obj_type;
59 }
60 break;
61 case BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL:
62 {
63 BalOnuDiscoveryInfo onuDiscoveryInfo;
64 memset(&onuDiscoveryInfo, 0, sizeof(BalOnuDiscoveryInfo));
65 bal_onu_discovery_info__init(&onuDiscoveryInfo);
66 balIndCfg.balobjinfo->onudiscoveryinfo = &onuDiscoveryInfo;
67 balIndCfg.balobjinfo->u_case = BAL_OBJ_IND__U_ONU_DISCOVERY_INFO;
68 BalSerialNumber serial_number;
69 memset(&serial_number, 0, sizeof(BalSerialNumber));
70 bal_serial_number__init(&serial_number);
71 balIndCfg.balobjinfo->onudiscoveryinfo->serial_number = &serial_number;
72 char vendor_id[20];
73 memset(&vendor_id, 0, 20);
74 strcpy(vendor_id,"4252434D");
75 balIndCfg.balobjinfo->onudiscoveryinfo->serial_number->vendor_id = vendor_id;
76 char vendor_specific[20];
77 memset(&vendor_specific, 0, 20);
78 strcpy(vendor_specific,"12345678");
79 balIndCfg.balobjinfo->onudiscoveryinfo->serial_number->vendor_specific = vendor_specific;
80 if(BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE == prevObjType)
81 {
82 balIndCfg.balobjinfo->onudiscoveryinfo->has_admin_state = 1;
83 balIndCfg.balobjinfo->onudiscoveryinfo->admin_state = BAL_STATE__BAL_STATE_DOWN;
84 balIndCfg.balobjinfo->onudiscoveryinfo->has_oper_status = 1;
85 balIndCfg.balobjinfo->onudiscoveryinfo->oper_status = BAL_STATUS__BAL_STATUS_DOWN;
86 printf("\n***************************************************\n");
87 printf("Sending ONU discovery message\n");
88 printf("***************************************************\n");
89 }
90 else
91 {
92 balIndCfg.balobjinfo->onudiscoveryinfo->has_admin_state = 1;
93 balIndCfg.balobjinfo->onudiscoveryinfo->admin_state = BAL_STATE__BAL_STATE_UP;
94 balIndCfg.balobjinfo->onudiscoveryinfo->has_oper_status = 1;
95 balIndCfg.balobjinfo->onudiscoveryinfo->oper_status = BAL_STATUS__BAL_STATUS_UP;
96 printf("***************************************************\n");
97 printf("ONU Activation Successful\n");
98 printf("***************************************************\n");
99 }
100 prevObjType = front->obj_type;
101 }
102 break;
103 case BAL_OBJ_ID__BAL_OBJ_ID_PACKET:
104 {
105 balIndCfg.balobjinfo->u_case = BAL_OBJ_IND__U_PKT_DATA;
106 }
107 break;
108 default:
109 {
110 balIndCfg.balobjinfo->u_case = BAL_OBJ_IND__U__NOT_SET;
111 prevObjType = front->obj_type;
112 }
113 break;
114 }
115 BalErr *output;
116 status = bal_ind__bal_ind_info(client, NULL, &balIndCfg, &output, NULL, 0);
117 free(front);
118 pthread_mutex_unlock(&lock);
119 pthread_mutex_lock(&lock);
120 pthread_cond_wait(&cv, &lock);
121 }
122 return NULL;
123}
124
125void create_stub_thread()
126{
127 pthread_t threadId = 0;
128
129 /* create shared queue */
130 shared_queue = createQueue();
131
132 pthread_create(&threadId, NULL, stub_thread, NULL);
133
134}
135
136/* A utility function to create an empty queue */
137bal_queue *createQueue()
138{
139 shared_queue = (struct Queue*)malloc(sizeof(struct Queue));
140 shared_queue->front = shared_queue->rear = NULL;
141 return shared_queue;
142}
143
144/* A utility function to create a new linked list node */
145struct QNode* newNode(int objKey, int status)
146{
147 struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode));
148 temp->obj_type = objKey;
149 temp->status = status;
150 temp->next = NULL;
151 return temp;
152}
153
154/* The function to add data to shared_queue - Add end of the queue */
155void enQueue(int objKey, int status)
156{
157 /* Create a new LL node */
158 struct QNode *temp = newNode(objKey, status);
159
160 /* If queue is empty, then new node is front and rear both */
161 if (shared_queue->rear == NULL)
162 {
163 shared_queue->front = shared_queue->rear = temp;
164 return;
165 }
166
167 /* Add the new node at the end of queue and change rear */
168 shared_queue->rear->next = temp;
169 shared_queue->rear = temp;
170}
171
172/* Function to remove data from shared_queue - FIFO */
173struct QNode *deQueue()
174{
175 /* If queue is empty, return NULL */
176 if (shared_queue->front == NULL)
177 {
178 return NULL;
179 }
180
181 /* Store previous front and move front one node ahead */
182 struct QNode *temp = shared_queue->front;
183 shared_queue->front = shared_queue->front->next;
184
185 /* If front becomes NULL, then change rear also as NULL */
186 if (shared_queue->front == NULL)
187 {
188 shared_queue->rear = NULL;
189 }
190
191 return temp;
192}
193
194#endif