blob: 4b61bd573bbfb2c0009c0fa89cc36f2a33a3837d [file] [log] [blame]
Kim Kempf72cb33e2017-08-30 12:53:35 -07001/*
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*/
suhasgrao76e8f8c2017-07-12 16:24:33 +053016#ifdef BAL_STUB
17#include <stdlib.h>
18#include <stdio.h>
19#include <string.h>
20#include <signal.h>
21#include "bal_stub.h"
22#include "bal_msg_type.grpc-c.h"
23#include "bal_osmsg.grpc-c.h"
24#include "bal_model_ids.grpc-c.h"
25#include "bal_obj.grpc-c.h"
26#include "bal_model_types.grpc-c.h"
27#include "bal_errno.grpc-c.h"
28#include "bal_indications.grpc-c.h"
29#include "bal.grpc-c.h"
30
31void *stub_thread(void *v)
32{
suhasgrao4b31f402017-07-31 00:01:54 +053033 int status;
34 grpc_c_client_t *client = grpc_c_client_init("172.24.150.167:60001", "bal_client", NULL);
35 pthread_mutex_lock(&lock);
36 pthread_cond_wait(&cv, &lock);
37 while(NULL != shared_queue->front)
38 {
39 BalObjId prevObjType;
40 char vendor_id[20];
41 char vendor_specific[20];
42 struct QNode *front = deQueue(shared_queue);
43 /* prepare and send rpc response */
44 BalIndications balIndCfg;
45 memset(&balIndCfg, 0, sizeof(BalIndications));
46 bal_indications__init(&balIndCfg);
47 balIndCfg.has_objtype = 1;
48 balIndCfg.objtype = front->obj_type;
49 balIndCfg.device_id = front->device_id;
50 printf("Device Id = %s\n", front->device_id);
51 switch(front->obj_type)
52 {
53 case BAL_OBJ_ID__BAL_OBJ_ID_ACCESS_TERMINAL:
54 {
55 printf("***************************************************\n");
56 printf("Successful Indication sent for ACCESS_TERMINAL\n");
57 printf("***************************************************\n");
58 balIndCfg.u_case = BAL_INDICATIONS__U_ACCESS_TERM_IND;
59 prevObjType = front->obj_type;
60 }
61 break;
62 case BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE:
63 {
64 printf("***************************************************\n");
65 printf("Successful Indication sent for PON INTERFACE UP\n");
66 printf("***************************************************\n");
67 balIndCfg.u_case = BAL_INDICATIONS__U_INTERFACE_IND;
68 prevObjType = front->obj_type;
69 }
70 break;
71 case BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL:
72 {
73 BalSubscriberTerminalKey subTermKey;
74 memset(&subTermKey, 0, sizeof(BalSubscriberTerminalKey));
75 bal_subscriber_terminal_key__init(&subTermKey);
suhasgrao17d65922017-07-13 16:55:29 +053076
suhasgrao4b31f402017-07-31 00:01:54 +053077 BalSerialNumber serial_number;
78 memset(&serial_number, 0, sizeof(BalSerialNumber));
79 bal_serial_number__init(&serial_number);
suhasgrao17d65922017-07-13 16:55:29 +053080
suhasgrao4b31f402017-07-31 00:01:54 +053081 char vendor_id[20];
82 memset(&vendor_id, 0, 20);
83 strcpy(vendor_id,"4252434D");
84 char vendor_specific[20];
85 memset(&vendor_specific, 0, 20);
86 strcpy(vendor_specific,"12345678");
87
88 if(BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE == prevObjType)
89 {
90 balIndCfg.u_case = BAL_INDICATIONS__U_TERMINAL_DISC;
91 BalSubscriberTerminalSubTermDisc terminal_disc;
92 memset(&terminal_disc, 0, sizeof(BalSubscriberTerminalSubTermDisc));
93 bal_subscriber_terminal_sub_term_disc__init(&terminal_disc);
94 balIndCfg.terminal_disc = &terminal_disc;
95
96 balIndCfg.terminal_disc->key = &subTermKey;
97 balIndCfg.terminal_disc->key->has_sub_term_id = 1;
98 balIndCfg.terminal_disc->key->sub_term_id = front->onu_id;
99 balIndCfg.terminal_disc->key->has_intf_id = 1;
100 balIndCfg.terminal_disc->key->intf_id = front->intf_id;
101
102 BalSubscriberTerminalSubTermDiscData subTermCfgData;
103 memset(&subTermCfgData, 0, sizeof(BalSubscriberTerminalSubTermDiscData));
104 bal_subscriber_terminal_sub_term_disc_data__init(&subTermCfgData);
105 balIndCfg.terminal_disc->data = &subTermCfgData;
106 balIndCfg.terminal_disc->data->serial_number = &serial_number;
107 balIndCfg.terminal_disc->data->serial_number->vendor_id = vendor_id;
108 printf("\n***************************************************\n");
109 printf("Sending ONU discovery message\n");
110 printf("***************************************************\n");
111 }
112 else
113 {
114 balIndCfg.u_case = BAL_INDICATIONS__U_TERMINAL_IND;
115 BalSubscriberTerminalInd terminal_ind;
116 memset(&terminal_ind, 0, sizeof(BalSubscriberTerminalInd));
117 bal_subscriber_terminal_ind__init(&terminal_ind);
118 balIndCfg.terminal_ind = &terminal_ind;
119
120 balIndCfg.terminal_ind->key = &subTermKey;
121 balIndCfg.terminal_ind->key->has_sub_term_id = 1;
122 balIndCfg.terminal_ind->key->sub_term_id = front->onu_id;
123 balIndCfg.terminal_ind->key->has_intf_id = 1;
124 balIndCfg.terminal_ind->key->intf_id = front->intf_id;
125
126 BalSubscriberTerminalIndData subTermCfgData;
127 memset(&subTermCfgData, 0, sizeof(BalSubscriberTerminalIndData));
128 bal_subscriber_terminal_ind_data__init(&subTermCfgData);
129 balIndCfg.terminal_ind->data = &subTermCfgData;
130 balIndCfg.terminal_ind->data->has_admin_state = 1;
131 balIndCfg.terminal_ind->data->admin_state = BAL_STATE__BAL_STATE_UP;
132 balIndCfg.terminal_ind->data->has_oper_status = 1;
133 balIndCfg.terminal_ind->data->oper_status = BAL_STATUS__BAL_STATUS_UP;
134 balIndCfg.terminal_ind->data->serial_number = &serial_number;
135 balIndCfg.terminal_ind->data->serial_number->vendor_id = vendor_id;
136 balIndCfg.terminal_ind->data->serial_number->vendor_specific = vendor_specific;
137 printf("***************************************************\n");
138 printf("ONU Activation Successful\n");
139 printf("***************************************************\n");
140 }
141 prevObjType = front->obj_type;
142 }
143 break;
144 case BAL_OBJ_ID__BAL_OBJ_ID_PACKET:
145 {
146 balIndCfg.u_case = BAL_INDICATIONS__U_BAL_OMCI_RESP;
147 BalPacketItuOmciChannelRx balomciresp;
148 memset(&balomciresp, 0, sizeof(BalPacketItuOmciChannelRx));
149 bal_packet_itu_omci_channel_rx__init(&balomciresp);
150
151 BalPacketKey balomcirespkey;
152 memset(&balomcirespkey, 0, sizeof(BalPacketKey));
153 bal_packet_key__init(&balomcirespkey);
154 balomciresp.key = &balomcirespkey;
155
156 BalDest balomcirespkeydest;
157 memset(&balomcirespkeydest, 0, sizeof(BalDest));
158 bal_dest__init(&balomcirespkeydest);
159 balomciresp.key->packet_send_dest = &balomcirespkeydest;
160 balomciresp.key->packet_send_dest->has_type = 1;
161 balomciresp.key->packet_send_dest->type = BAL_DEST_TYPE__BAL_DEST_TYPE_ITU_OMCI_CHANNEL;
162 balomciresp.key->packet_send_dest->u_case = BAL_DEST__U_ITU_OMCI_CHANNEL;
163
164 BalItuOmciChannel itu_omci_channel;
165 memset(&itu_omci_channel, 0, sizeof(BalItuOmciChannel));
166 bal_itu_omci_channel__init(&itu_omci_channel);
167 balomciresp.key->packet_send_dest->itu_omci_channel = &itu_omci_channel;
168 balomciresp.key->packet_send_dest->itu_omci_channel->has_sub_term_id = 1;
169 balomciresp.key->packet_send_dest->itu_omci_channel->sub_term_id = front->onu_id;
170 balomciresp.key->packet_send_dest->itu_omci_channel->has_int_id = 1;
171 balomciresp.key->packet_send_dest->itu_omci_channel->int_id = front->intf_id;
172 }
173 break;
174 default:
175 {
176 balIndCfg.u_case = BAL_INDICATIONS__U__NOT_SET;
177 prevObjType = front->obj_type;
178 }
179 break;
180 }
181 BalErr *output;
182 status = bal_ind__bal_ind_info(client, NULL, &balIndCfg, &output, NULL, 0);
183 free(front);
184 pthread_mutex_unlock(&lock);
185 pthread_mutex_lock(&lock);
186 pthread_cond_wait(&cv, &lock);
187 }
188 return NULL;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530189}
190
191void create_stub_thread()
192{
suhasgrao4b31f402017-07-31 00:01:54 +0530193 pthread_t threadId = 0;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530194
suhasgrao4b31f402017-07-31 00:01:54 +0530195 /* create shared queue */
196 shared_queue = createQueue();
suhasgrao76e8f8c2017-07-12 16:24:33 +0530197
suhasgrao4b31f402017-07-31 00:01:54 +0530198 pthread_create(&threadId, NULL, stub_thread, NULL);
suhasgrao76e8f8c2017-07-12 16:24:33 +0530199
200}
201
202/* A utility function to create an empty queue */
203bal_queue *createQueue()
204{
suhasgrao4b31f402017-07-31 00:01:54 +0530205 shared_queue = (struct Queue*)malloc(sizeof(struct Queue));
206 shared_queue->front = shared_queue->rear = NULL;
207 return shared_queue;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530208}
209
210/* A utility function to create a new linked list node */
suhasgrao4b31f402017-07-31 00:01:54 +0530211struct QNode* newNode(int objKey, int status, char *device_id)
suhasgrao76e8f8c2017-07-12 16:24:33 +0530212{
suhasgrao4b31f402017-07-31 00:01:54 +0530213 struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode));
214 temp->obj_type = objKey;
215 temp->status = status;
216 if(device_id != NULL)
217 {
218 memset(temp->device_id, 0, BAL_DEVICE_STR_LEN);
219 memcpy(temp->device_id, device_id, strlen(device_id));
220 }
221 temp->next = NULL;
222 return temp;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530223}
224
225/* The function to add data to shared_queue - Add end of the queue */
suhasgrao4b31f402017-07-31 00:01:54 +0530226void enQueue(int objKey, struct QNode *temp)
suhasgrao76e8f8c2017-07-12 16:24:33 +0530227{
suhasgrao4b31f402017-07-31 00:01:54 +0530228 /* Create a new LL node */
suhasgrao76e8f8c2017-07-12 16:24:33 +0530229
suhasgrao4b31f402017-07-31 00:01:54 +0530230 /* If queue is empty, then new node is front and rear both */
231 if (shared_queue->rear == NULL)
232 {
233 shared_queue->front = shared_queue->rear = temp;
234 return;
235 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530236
suhasgrao4b31f402017-07-31 00:01:54 +0530237 /* Add the new node at the end of queue and change rear */
238 shared_queue->rear->next = temp;
239 shared_queue->rear = temp;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530240}
241
242/* Function to remove data from shared_queue - FIFO */
243struct QNode *deQueue()
244{
suhasgrao4b31f402017-07-31 00:01:54 +0530245 /* If queue is empty, return NULL */
246 if (shared_queue->front == NULL)
247 {
248 return NULL;
249 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530250
suhasgrao4b31f402017-07-31 00:01:54 +0530251 /* Store previous front and move front one node ahead */
252 struct QNode *temp = shared_queue->front;
253 shared_queue->front = shared_queue->front->next;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530254
suhasgrao4b31f402017-07-31 00:01:54 +0530255 /* If front becomes NULL, then change rear also as NULL */
256 if (shared_queue->front == NULL)
257 {
258 shared_queue->rear = NULL;
259 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530260
suhasgrao4b31f402017-07-31 00:01:54 +0530261 return temp;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530262}
263
264#endif