blob: 78b4823b23f90a1c81329c1debc3aff807ae187c [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);
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);
suhasgrao17d65922017-07-13 16:55:29 +053099
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530100 BalSerialNumber serial_number;
101 memset(&serial_number, 0, sizeof(BalSerialNumber));
102 bal_serial_number__init(&serial_number);
suhasgrao4b31f402017-07-31 00:01:54 +0530103
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530104/*
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");
suhasgrao4b31f402017-07-31 00:01:54 +0530111
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530112 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;
suhasgrao4b31f402017-07-31 00:01:54 +0530119
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530120 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;
suhasgrao4b31f402017-07-31 00:01:54 +0530125
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530126 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;
suhasgrao4b31f402017-07-31 00:01:54 +0530147
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530148 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);
Rajeswara Raoa3efbca2017-09-08 18:01:16 +0530208
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530209 BalPacketKey balomcirespkey;
210 memset(&balomcirespkey, 0, sizeof(BalPacketKey));
Rajeswara Raoa3efbca2017-09-08 18:01:16 +0530211 bal_packet_key__init(&balomcirespkey);
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530212 balomciresp.key = &balomcirespkey;
Rajeswara Raoa3efbca2017-09-08 18:01:16 +0530213 balIndCfg.balomciresp = &balomciresp;
suhasgrao4b31f402017-07-31 00:01:54 +0530214
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530215 BalDest balomcirespkeydest;
216 memset(&balomcirespkeydest, 0, sizeof(BalDest));
217 bal_dest__init(&balomcirespkeydest);
218 balomciresp.key->packet_send_dest = &balomcirespkeydest;
219 balomciresp.key->packet_send_dest->has_type = 1;
220 balomciresp.key->packet_send_dest->type = BAL_DEST_TYPE__BAL_DEST_TYPE_ITU_OMCI_CHANNEL;
221 balomciresp.key->packet_send_dest->u_case = BAL_DEST__U_ITU_OMCI_CHANNEL;
222
223 BalItuOmciChannel itu_omci_channel;
224 memset(&itu_omci_channel, 0, sizeof(BalItuOmciChannel));
225 bal_itu_omci_channel__init(&itu_omci_channel);
226 balomciresp.key->packet_send_dest->itu_omci_channel = &itu_omci_channel;
227 balomciresp.key->packet_send_dest->itu_omci_channel->has_sub_term_id = 1;
228 balomciresp.key->packet_send_dest->itu_omci_channel->sub_term_id = front->onu_id;
229 balomciresp.key->packet_send_dest->itu_omci_channel->has_intf_id = 1;
230 balomciresp.key->packet_send_dest->itu_omci_channel->intf_id = front->intf_id;
231 status = bal_ind__bal_pkt_omci_channel_rx_ind(client, NULL, &balIndCfg, &output, NULL, 0);
232 }
233 break;
234 default:
235 {
236 balIndCfg.u_case = BAL_INDICATIONS__U__NOT_SET;
237 prevObjType = front->obj_type;
238 }
239 break;
240 }
241 free(front);
242 pthread_mutex_unlock(&lock);
243 pthread_mutex_lock(&lock);
244 pthread_cond_wait(&cv, &lock);
245 }
246 return NULL;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530247}
248
Rajeswara Raoa3efbca2017-09-08 18:01:16 +0530249void create_stub_thread()
suhasgrao76e8f8c2017-07-12 16:24:33 +0530250{
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530251 pthread_t threadId = 0;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530252
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530253 /* create shared queue */
254 shared_queue = createQueue();
suhasgrao76e8f8c2017-07-12 16:24:33 +0530255
Rajeswara Raoa3efbca2017-09-08 18:01:16 +0530256 pthread_create(&threadId, NULL, stub_thread, NULL);
suhasgrao76e8f8c2017-07-12 16:24:33 +0530257
258}
259
260/* A utility function to create an empty queue */
261bal_queue *createQueue()
262{
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530263 shared_queue = (struct Queue*)malloc(sizeof(struct Queue));
264 shared_queue->front = shared_queue->rear = NULL;
265 return shared_queue;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530266}
267
268/* A utility function to create a new linked list node */
suhasgrao4b31f402017-07-31 00:01:54 +0530269struct QNode* newNode(int objKey, int status, char *device_id)
suhasgrao76e8f8c2017-07-12 16:24:33 +0530270{
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530271 struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode));
272 temp->obj_type = objKey;
273 temp->status = status;
274 if(device_id != NULL)
275 {
276 memset(temp->device_id, 0, BAL_DEVICE_STR_LEN);
277 memcpy(temp->device_id, device_id, strlen(device_id));
278 }
279 temp->next = NULL;
Rajeswara Raoa3efbca2017-09-08 18:01:16 +0530280 return temp;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530281}
282
283/* The function to add data to shared_queue - Add end of the queue */
suhasgrao4b31f402017-07-31 00:01:54 +0530284void enQueue(int objKey, struct QNode *temp)
suhasgrao76e8f8c2017-07-12 16:24:33 +0530285{
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530286 /* Create a new LL node */
suhasgrao76e8f8c2017-07-12 16:24:33 +0530287
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530288 /* If queue is empty, then new node is front and rear both */
289 if (shared_queue->rear == NULL)
290 {
291 shared_queue->front = shared_queue->rear = temp;
292 return;
293 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530294
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530295 /* Add the new node at the end of queue and change rear */
296 shared_queue->rear->next = temp;
297 shared_queue->rear = temp;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530298}
299
300/* Function to remove data from shared_queue - FIFO */
301struct QNode *deQueue()
302{
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530303 /* If queue is empty, return NULL */
304 if (shared_queue->front == NULL)
305 {
306 return NULL;
307 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530308
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530309 /* Store previous front and move front one node ahead */
310 struct QNode *temp = shared_queue->front;
311 shared_queue->front = shared_queue->front->next;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530312
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530313 /* If front becomes NULL, then change rear also as NULL */
314 if (shared_queue->front == NULL)
315 {
316 shared_queue->rear = NULL;
317 }
suhasgrao76e8f8c2017-07-12 16:24:33 +0530318
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530319 return temp;
320}
321
322void stub_bal_init(BalInit *bal_init)
323{
Rajeswara Raoa3efbca2017-09-08 18:01:16 +0530324 char *ip_and_port = NULL;
325 ip_and_port = bal_init->voltha_adapter_ip_port;
326 client = grpc_c_client_init(ip_and_port, "bal_client", NULL);
327}
328
329void stub_bal_stats_get(BalInterfaceStatData *statData)
330{
331 printf("Bal Stub - Get Stats In BalStubs : Got all the statistics\n");
332 statData->has_rx_bytes = 1;
333 statData->has_rx_packets=1;
334 statData->has_rx_ucast_packets=1;
335 statData->has_rx_mcast_packets=1;
336 statData->has_rx_bcast_packets=1;
337 statData->has_rx_error_packets=1;
338 statData->has_rx_unknown_protos=1;
339 statData->has_tx_bytes = 1;
340 statData->has_tx_packets=1;
341 statData->has_tx_ucast_packets=1;
342 statData->has_tx_mcast_packets=1;
343 statData->has_tx_bcast_packets=1;
344 statData->has_tx_error_packets=1;
345 statData->has_rx_crc_errors=1;
346 statData->has_bip_errors=1;
347
348 statData->rx_bytes = 1000; /**< RFC 2233 */
349 statData->rx_packets = 100; /**< RFC 1213 ucast + none-ucast */
350 statData->rx_ucast_packets = 5; /**< RFC 2233 */
351 statData->rx_mcast_packets = 10; /**< RFC 2233 */
352 statData->rx_bcast_packets = 15; /**< RFC 2233 */
353 statData->rx_error_packets = 20; /**< RFC 1213 */
354 statData->rx_unknown_protos = 45; /**< RFC 1213 */
355 statData->tx_bytes = 2000; /**< RFC 2233 */
356 statData->tx_packets = 190; /**< RFC 1213 ucast + none-ucast */
357 statData->tx_ucast_packets = 30; /**< RFC 2233 */
358 statData->tx_mcast_packets = 50; /**< RFC 2233 */
359 statData->tx_bcast_packets = 80; /**< RFC 2233 */
360 statData->tx_error_packets = 40; /**< RFC 1213 */
361 statData->rx_crc_errors = 5; /**< Received packets with CRC error. */
362 statData->bip_errors = 15;
suhasgrao76e8f8c2017-07-12 16:24:33 +0530363}
364
365#endif