blob: d89a37c83926b643633c623ecbdef495e20ce42b [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*/
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +053016
suhasgrao76e8f8c2017-07-12 16:24:33 +053017#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 Raof6b4e6c2017-08-31 17:26:27 +053032char *voltha_ip_and_port = NULL;
33
Rajeswara Raoa3efbca2017-09-08 18:01:16 +053034void *stub_thread(void *v)
suhasgrao76e8f8c2017-07-12 16:24:33 +053035{
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +053036 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;
Rajeswara Raoa3efbca2017-09-08 18:01:16 +053068
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +053069 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;
suhasgrao17d65922017-07-13 16:55:29 +053074
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +053075 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);
Rajeswara Rao9f1cea12017-10-10 18:25:29 +053082 if(GRPC_C_FAIL == status)
83 {
84 printf("\n Failed To Send Access Terminal Indication: Please verify the VOLTHA IP Address configured\n");
85 }
86 is_stub_reboot = 1;
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +053087 }
88 break;
89 case BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE:
90 {
91 printf("***************************************************\n");
92 printf("Successful Indication sent for PON INTERFACE UP\n");
93 printf("***************************************************\n");
94 balIndCfg.u_case = BAL_INDICATIONS__U_INTERFACE_IND;
95 prevObjType = front->obj_type;
96 status = bal_ind__bal_iface_ind(client, NULL, &balIndCfg, &output, NULL, 0);
Rajeswara Rao9f1cea12017-10-10 18:25:29 +053097 if(GRPC_C_FAIL == status)
98 {
99 printf("\n Failed To Send Interface Indication: Please verify the VOLTHA IP Address configured\n");
100 }
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530101 }
102 break;
103 case BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL:
104 {
105 BalSubscriberTerminalKey subTermKey;
106 memset(&subTermKey, 0, sizeof(BalSubscriberTerminalKey));
107 bal_subscriber_terminal_key__init(&subTermKey);
suhasgrao17d65922017-07-13 16:55:29 +0530108
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530109 BalSerialNumber serial_number;
110 memset(&serial_number, 0, sizeof(BalSerialNumber));
111 bal_serial_number__init(&serial_number);
suhasgrao4b31f402017-07-31 00:01:54 +0530112
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530113/*
114 char vendor_id[20];
115 memset(&vendor_id, 0, 20);
116 strcpy(vendor_id,"4252434D");
117 char vendor_specific[20];
118 memset(&vendor_specific, 0, 20);
119 strcpy(vendor_specific,"12345678");
suhasgrao4b31f402017-07-31 00:01:54 +0530120
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530121 if(BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE == prevObjType)
122 {
123 balIndCfg.u_case = BAL_INDICATIONS__U_TERMINAL_DISC;
124 BalSubscriberTerminalSubTermDisc terminal_disc;
125 memset(&terminal_disc, 0, sizeof(BalSubscriberTerminalSubTermDisc));
126 bal_subscriber_terminal_sub_term_disc__init(&terminal_disc);
127 balIndCfg.terminal_disc = &terminal_disc;
suhasgrao4b31f402017-07-31 00:01:54 +0530128
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530129 balIndCfg.terminal_disc->key = &subTermKey;
130 balIndCfg.terminal_disc->key->has_sub_term_id = 1;
131 balIndCfg.terminal_disc->key->sub_term_id = front->onu_id;
132 balIndCfg.terminal_disc->key->has_intf_id = 1;
133 balIndCfg.terminal_disc->key->intf_id = front->intf_id;
suhasgrao4b31f402017-07-31 00:01:54 +0530134
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530135 BalSubscriberTerminalSubTermDiscData subTermCfgData;
136 memset(&subTermCfgData, 0, sizeof(BalSubscriberTerminalSubTermDiscData));
137 bal_subscriber_terminal_sub_term_disc_data__init(&subTermCfgData);
138 balIndCfg.terminal_disc->data = &subTermCfgData;
139 balIndCfg.terminal_disc->data->serial_number = &serial_number;
140 balIndCfg.terminal_disc->data->serial_number->vendor_id = vendor_id;
141 printf("\n***************************************************\n");
142 printf("Sending ONU discovery message\n");
143 printf("***************************************************\n");
144 status = bal_ind__bal_subs_term_discovery_ind(client, NULL, &balIndCfg, &output, NULL, 0);
145 }
146 else
147 {
148*/
149 balIndCfg.u_case = BAL_INDICATIONS__U_TERMINAL_DISC;
150 balIndCfg.has_sub_group = 1;
151 balIndCfg.sub_group = BAL_SUBSCRIBER_TERMINAL_AUTO_ID__BAL_SUBSCRIBER_TERMINAL_AUTO_ID_SUB_TERM_DISC;
152 BalSubscriberTerminalSubTermDisc terminal_disc;
153 memset(&terminal_disc, 0, sizeof(BalSubscriberTerminalSubTermDisc));
154 bal_subscriber_terminal_sub_term_disc__init(&terminal_disc);
155 balIndCfg.terminal_disc = &terminal_disc;
suhasgrao4b31f402017-07-31 00:01:54 +0530156
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530157 balIndCfg.terminal_disc->key = &subTermKey;
158 balIndCfg.terminal_disc->key->has_sub_term_id = 1;
159 balIndCfg.terminal_disc->key->sub_term_id = front->onu_id;
160 balIndCfg.terminal_disc->key->has_intf_id = 1;
161 balIndCfg.terminal_disc->key->intf_id = front->intf_id;
162
163 BalSubscriberTerminalSubTermDiscData subTermDiscCfgData;
164 memset(&subTermDiscCfgData, 0, sizeof(BalSubscriberTerminalSubTermDiscData));
165 bal_subscriber_terminal_sub_term_disc_data__init(&subTermDiscCfgData);
166 balIndCfg.terminal_disc->data = &subTermDiscCfgData;
167 balIndCfg.terminal_disc->data->serial_number = &serial_number;
168 balIndCfg.terminal_disc->data->serial_number->vendor_id = front->vendor_id;
169 balIndCfg.terminal_disc->data->serial_number->vendor_specific = front->vendor_specific;
170 printf("\n***************************************************\n");
171 printf("Sending ONU discovery message\n");
172 printf("***************************************************\n");
173 status = bal_ind__bal_subs_term_discovery_ind(client, NULL, &balIndCfg, &output, NULL, 0);
174
175
176 balIndCfg.u_case = BAL_INDICATIONS__U_TERMINAL_IND;
177 balIndCfg.has_sub_group = 1;
178 balIndCfg.sub_group = BAL_SUBSCRIBER_TERMINAL_AUTO_ID__BAL_SUBSCRIBER_TERMINAL_AUTO_ID_IND;
179 BalSubscriberTerminalInd terminal_ind;
180 memset(&terminal_ind, 0, sizeof(BalSubscriberTerminalInd));
181 bal_subscriber_terminal_ind__init(&terminal_ind);
182 balIndCfg.terminal_ind = &terminal_ind;
183
184 balIndCfg.terminal_ind->key = &subTermKey;
185 balIndCfg.terminal_ind->key->has_sub_term_id = 1;
186 balIndCfg.terminal_ind->key->sub_term_id = front->onu_id;
187 balIndCfg.terminal_ind->key->has_intf_id = 1;
188 balIndCfg.terminal_ind->key->intf_id = front->intf_id;
189
190 BalSubscriberTerminalIndData subTermCfgData;
191 memset(&subTermCfgData, 0, sizeof(BalSubscriberTerminalIndData));
192 bal_subscriber_terminal_ind_data__init(&subTermCfgData);
193 balIndCfg.terminal_ind->data = &subTermCfgData;
194 balIndCfg.terminal_ind->data->has_admin_state = 1;
195 balIndCfg.terminal_ind->data->admin_state = BAL_STATE__BAL_STATE_UP;
196 balIndCfg.terminal_ind->data->has_oper_status = 1;
197 balIndCfg.terminal_ind->data->oper_status = BAL_STATUS__BAL_STATUS_UP;
198 balIndCfg.terminal_ind->data->serial_number = &serial_number;
199 balIndCfg.terminal_ind->data->serial_number->vendor_id = front->vendor_id;
200 balIndCfg.terminal_ind->data->serial_number->vendor_specific = front->vendor_specific;
201 printf("***************************************************\n");
202 printf("ONU Activation Successful %s\n", balIndCfg.terminal_ind->data->serial_number->vendor_specific);
203 printf("***************************************************\n");
204 status = bal_ind__bal_subs_term_ind(client, NULL, &balIndCfg, &output, NULL, 0);
205/*
206 }
207*/
208 prevObjType = front->obj_type;
209 }
210 break;
211 case BAL_OBJ_ID__BAL_OBJ_ID_PACKET:
212 {
213 balIndCfg.u_case = BAL_INDICATIONS__U_BAL_OMCI_RESP;
214 BalPacketItuOmciChannelRx balomciresp;
215 memset(&balomciresp, 0, sizeof(BalPacketItuOmciChannelRx));
216 bal_packet_itu_omci_channel_rx__init(&balomciresp);
Rajeswara Raoa3efbca2017-09-08 18:01:16 +0530217
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530218 BalPacketKey balomcirespkey;
219 memset(&balomcirespkey, 0, sizeof(BalPacketKey));
Rajeswara Raoa3efbca2017-09-08 18:01:16 +0530220 bal_packet_key__init(&balomcirespkey);
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530221 balomciresp.key = &balomcirespkey;
Rajeswara Raoa3efbca2017-09-08 18:01:16 +0530222 balIndCfg.balomciresp = &balomciresp;
suhasgrao4b31f402017-07-31 00:01:54 +0530223
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530224 BalDest balomcirespkeydest;
225 memset(&balomcirespkeydest, 0, sizeof(BalDest));
226 bal_dest__init(&balomcirespkeydest);
227 balomciresp.key->packet_send_dest = &balomcirespkeydest;
228 balomciresp.key->packet_send_dest->has_type = 1;
229 balomciresp.key->packet_send_dest->type = BAL_DEST_TYPE__BAL_DEST_TYPE_ITU_OMCI_CHANNEL;
230 balomciresp.key->packet_send_dest->u_case = BAL_DEST__U_ITU_OMCI_CHANNEL;
231
232 BalItuOmciChannel itu_omci_channel;
233 memset(&itu_omci_channel, 0, sizeof(BalItuOmciChannel));
234 bal_itu_omci_channel__init(&itu_omci_channel);
235 balomciresp.key->packet_send_dest->itu_omci_channel = &itu_omci_channel;
236 balomciresp.key->packet_send_dest->itu_omci_channel->has_sub_term_id = 1;
237 balomciresp.key->packet_send_dest->itu_omci_channel->sub_term_id = front->onu_id;
238 balomciresp.key->packet_send_dest->itu_omci_channel->has_intf_id = 1;
239 balomciresp.key->packet_send_dest->itu_omci_channel->intf_id = front->intf_id;
240 status = bal_ind__bal_pkt_omci_channel_rx_ind(client, NULL, &balIndCfg, &output, NULL, 0);
241 }
242 break;
243 default:
244 {
245 balIndCfg.u_case = BAL_INDICATIONS__U__NOT_SET;
246 prevObjType = front->obj_type;
247 }
248 break;
249 }
250 free(front);
251 pthread_mutex_unlock(&lock);
252 pthread_mutex_lock(&lock);
253 pthread_cond_wait(&cv, &lock);
254 }
255 return NULL;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530256}
257
Rajeswara Raoa3efbca2017-09-08 18:01:16 +0530258void create_stub_thread()
suhasgrao76e8f8c2017-07-12 16:24:33 +0530259{
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530260 pthread_t threadId = 0;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530261
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530262 /* create shared queue */
263 shared_queue = createQueue();
suhasgrao76e8f8c2017-07-12 16:24:33 +0530264
Rajeswara Raoa3efbca2017-09-08 18:01:16 +0530265 pthread_create(&threadId, NULL, stub_thread, NULL);
suhasgrao76e8f8c2017-07-12 16:24:33 +0530266
267}
268
269/* A utility function to create an empty queue */
270bal_queue *createQueue()
271{
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530272 shared_queue = (struct Queue*)malloc(sizeof(struct Queue));
273 shared_queue->front = shared_queue->rear = NULL;
274 return shared_queue;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530275}
276
277/* A utility function to create a new linked list node */
suhasgrao4b31f402017-07-31 00:01:54 +0530278struct QNode* newNode(int objKey, int status, char *device_id)
suhasgrao76e8f8c2017-07-12 16:24:33 +0530279{
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530280 struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode));
281 temp->obj_type = objKey;
282 temp->status = status;
283 if(device_id != NULL)
284 {
285 memset(temp->device_id, 0, BAL_DEVICE_STR_LEN);
286 memcpy(temp->device_id, device_id, strlen(device_id));
287 }
288 temp->next = NULL;
Rajeswara Raoa3efbca2017-09-08 18:01:16 +0530289 return temp;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530290}
291
292/* The function to add data to shared_queue - Add end of the queue */
suhasgrao4b31f402017-07-31 00:01:54 +0530293void enQueue(int objKey, struct QNode *temp)
suhasgrao76e8f8c2017-07-12 16:24:33 +0530294{
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530295 /* Create a new LL node */
suhasgrao76e8f8c2017-07-12 16:24:33 +0530296
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530297 /* If queue is empty, then new node is front and rear both */
298 if (shared_queue->rear == NULL)
299 {
300 shared_queue->front = shared_queue->rear = temp;
301 return;
302 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530303
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530304 /* Add the new node at the end of queue and change rear */
305 shared_queue->rear->next = temp;
306 shared_queue->rear = temp;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530307}
308
309/* Function to remove data from shared_queue - FIFO */
310struct QNode *deQueue()
311{
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530312 /* If queue is empty, return NULL */
313 if (shared_queue->front == NULL)
314 {
315 return NULL;
316 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530317
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530318 /* Store previous front and move front one node ahead */
319 struct QNode *temp = shared_queue->front;
320 shared_queue->front = shared_queue->front->next;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530321
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530322 /* If front becomes NULL, then change rear also as NULL */
323 if (shared_queue->front == NULL)
324 {
325 shared_queue->rear = NULL;
326 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530327
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530328 return temp;
329}
330
331void stub_bal_init(BalInit *bal_init)
332{
Rajeswara Raoa3efbca2017-09-08 18:01:16 +0530333 char *ip_and_port = NULL;
334 ip_and_port = bal_init->voltha_adapter_ip_port;
335 client = grpc_c_client_init(ip_and_port, "bal_client", NULL);
336}
337
338void stub_bal_stats_get(BalInterfaceStatData *statData)
339{
340 printf("Bal Stub - Get Stats In BalStubs : Got all the statistics\n");
341 statData->has_rx_bytes = 1;
342 statData->has_rx_packets=1;
343 statData->has_rx_ucast_packets=1;
344 statData->has_rx_mcast_packets=1;
345 statData->has_rx_bcast_packets=1;
346 statData->has_rx_error_packets=1;
347 statData->has_rx_unknown_protos=1;
348 statData->has_tx_bytes = 1;
349 statData->has_tx_packets=1;
350 statData->has_tx_ucast_packets=1;
351 statData->has_tx_mcast_packets=1;
352 statData->has_tx_bcast_packets=1;
353 statData->has_tx_error_packets=1;
354 statData->has_rx_crc_errors=1;
355 statData->has_bip_errors=1;
356
357 statData->rx_bytes = 1000; /**< RFC 2233 */
358 statData->rx_packets = 100; /**< RFC 1213 ucast + none-ucast */
359 statData->rx_ucast_packets = 5; /**< RFC 2233 */
360 statData->rx_mcast_packets = 10; /**< RFC 2233 */
361 statData->rx_bcast_packets = 15; /**< RFC 2233 */
362 statData->rx_error_packets = 20; /**< RFC 1213 */
363 statData->rx_unknown_protos = 45; /**< RFC 1213 */
364 statData->tx_bytes = 2000; /**< RFC 2233 */
365 statData->tx_packets = 190; /**< RFC 1213 ucast + none-ucast */
366 statData->tx_ucast_packets = 30; /**< RFC 2233 */
367 statData->tx_mcast_packets = 50; /**< RFC 2233 */
368 statData->tx_bcast_packets = 80; /**< RFC 2233 */
369 statData->tx_error_packets = 40; /**< RFC 1213 */
370 statData->rx_crc_errors = 5; /**< Received packets with CRC error. */
371 statData->bip_errors = 15;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530372}
373
374#endif