6c28f460c730b1ff5e7c440263097886761935de
[o-du/l2.git] / src / du_app / du_cell_mgr.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18
19 /* This file contains message handling functionality for DU cell management */
20 #include "common_def.h"
21 #include "lrg.h"
22 #include "legtp.h"
23 #include "lrg.x"
24 #include "lkw.x"
25 #include "rgr.h"
26 #include "rgr.x"
27 #include "du_app_mac_inf.h"
28 #include "du_app_rlc_inf.h"
29 #include "du_cfg.h"
30 #include "du_mgr.h"
31 #include "du_utils.h"
32 #include "du_cell_mgr.h"
33
34 #ifdef O1_ENABLE
35
36 #include "AlarmInterface.h"
37 #include "ConfigInterface.h"
38
39 #endif
40
41 DuMacCellDeleteReq packMacCellDeleteReqOpts[] =
42 {
43    packDuMacCellDeleteReq,       /* Loose coupling */
44    MacProcCellDeleteReq,         /* TIght coupling */
45    packDuMacCellDeleteReq        /* Light weight-loose coupling */
46 };
47
48 /*******************************************************************
49  *
50  * @brief Processes cells to be activated
51  *
52  * @details
53  *
54  *    Function : duProcCellsToBeActivated
55  *
56  *    Functionality:
57  *      - Processes cells to be activated list received in F1SetupRsp
58  *
59  * @params[in] void
60  * @return ROK     - success
61  *         RFAILED - failure
62  *
63  * ****************************************************************/
64 uint8_t duProcCellsToBeActivated(uint8_t *plmnStr, uint16_t nci, uint16_t nRPci)
65 {
66    uint8_t ret = ROK;
67    DuCellCb *cellCb = NULLP;
68    uint8_t cfgIdx, tmpPlmn[4];
69
70    for(cfgIdx=0; cfgIdx<duCb.numCfgCells; cfgIdx++)
71    {
72       memset(tmpPlmn, 0, 4);
73       buildPlmnId(duCb.cfgCellLst[cfgIdx]->cellInfo.nrEcgi.plmn, tmpPlmn);
74       if(duCb.cfgCellLst[cfgIdx]->cellInfo.nrEcgi.cellId == nci &&
75             (strcmp((const char*)tmpPlmn, (const char*)plmnStr) == 0))
76       {
77          cellCb = duCb.cfgCellLst[cfgIdx];
78          break;
79       }
80       else
81       {
82          DU_LOG("\nERROR  -->  DU APP : No Cell found for NCI %d", nci);
83          return RFAILED;
84       }
85    }
86
87    cellCb->cellStatus = ACTIVATION_IN_PROGRESS; 
88    cellCb->cellInfo.nrPci = nRPci;
89
90    duCb.actvCellLst[duCb.numActvCells++] = cellCb;
91
92    if(duBuildAndSendMacCellCfg(cellCb->cellId) != ROK)
93    {
94       DU_LOG("\nERROR  -->  DU APP : macCellCfg build and send failed");
95       /* Delete cell from actvCellList */
96       duCb.actvCellLst[--(duCb.numActvCells)] = NULLP;
97       ret = RFAILED;
98    }
99    return ret;
100 }
101
102 /*******************************************************************
103  *
104  * @brief Handles DU F1Setup Rsp received in F1AP
105  *
106  * @details
107  *
108  *    Function : duProcF1SetupRsp
109  *
110  *    Functionality:
111  *      - Handles DU F1Setup Rsp received in F1AP
112  *
113  * @params[in] Pointer to F1SetupRsp 
114  * @return void
115  *
116  ******************************************************************/
117 void duProcF1SetupRsp()
118 {
119    DU_LOG("\nINFO   -->  DU_APP : F1 Setup Response received");
120    duCb.f1Status = TRUE; //Set F1 status as true
121 }
122
123 /*******************************************************************
124 *
125 * @brief Returns cellCb based on cell ID
126 *
127 * @details
128 *
129 *    Function : duGetCellCb
130 *
131 *    Functionality: Returns DU APP CellCb based on cell ID
132 *
133 * @params[in] F1AP_PDU_t ASN decoded F1AP message
134 * @return ROK     - success
135 *         RFAILED - failure
136 *
137 * ****************************************************************/
138 uint8_t duGetCellCb(uint16_t cellId, DuCellCb **cellCb)
139 {
140    uint8_t cellIdx = 0;
141    for(cellIdx=0; cellIdx<duCb.numActvCells; cellIdx++)
142    {
143       if(duCb.actvCellLst[cellIdx]->cellId == cellId)
144          *cellCb = duCb.actvCellLst[cellIdx];
145          break;
146    }
147
148    if(!*cellCb)
149    {
150       DU_LOG("\nERROR  -->  DU APP : Cell Id %d not found in DU APP", cellId);
151       return RFAILED;
152    }
153
154    return ROK;
155 }
156
157 /*******************************************************************
158  *
159  * @brief Handles cell up indication from MAC
160  *
161  * @details
162  *
163  *    Function : duHandleCellUpInd
164  *
165  *    Functionality:
166  *      Handles cell up indication from MAC
167  *
168  * @params[in] Post structure pointer
169  *             cell Up info
170  * @return ROK     - success
171  *         RFAILED - failure
172  *
173  * ****************************************************************/
174 uint8_t duHandleCellUpInd(Pst *pst, OduCellId *cellId)
175 {
176    DuCellCb *cellCb = NULLP;
177
178    if(cellId->cellId <=0 || cellId->cellId > MAX_NUM_CELL)
179    {
180       DU_LOG("\nERROR  -->  DU APP : Invalid Cell Id %d in duHandleCellUpInd()", cellId->cellId);
181       return RFAILED;
182    }
183
184    if(duGetCellCb(cellId->cellId, &cellCb) != ROK)
185       return RFAILED;
186
187    if((cellCb != NULL) && (cellCb->cellStatus == ACTIVATION_IN_PROGRESS))
188    {
189       DU_LOG("\nINFO   -->  DU APP : 5G-NR Cell %d is UP", cellId->cellId);
190       cellCb->cellStatus = ACTIVATED;
191       gCellStatus = CELL_UP;
192
193 #ifdef O1_ENABLE
194       DU_LOG("\nINFO   -->  DU APP : Raise cell UP alarm for cell id=%d", cellId->cellId);
195       raiseCellAlrm(CELL_UP_ALARM_ID, cellId->cellId);
196       setCellOpState(cellId->cellId, ENABLED, ACTIVE);
197 #endif
198    }
199
200    if((pst->selector == ODU_SELECTOR_LWLC) || (pst->selector == ODU_SELECTOR_TC))
201       DU_FREE_SHRABL_BUF(pst->region, pst->pool, cellId, sizeof(OduCellId));
202    return ROK;
203 }
204
205 /*******************************************************************
206  *
207  * @brief Handle Cell delete response from MAC
208  *
209  * @details
210  *
211  *    Function : DuProcMacCellDeleteRsp
212  *
213  *    Functionality: Handle Cell delete response from MAC
214  *
215  * @params[in] Pointer to MacCellDeleteRsp and Pst
216  * @return ROK     - success
217  *         RFAILED - failure
218  *
219  * ****************************************************************/
220
221 uint8_t DuProcMacCellDeleteRsp(Pst *pst, MacCellDeleteRsp *deleteRsp)
222 {
223    uint8_t ret = ROK;
224    uint16_t cellIdx=0;
225    if(deleteRsp)
226    {
227       if(deleteRsp->result == SUCCESSFUL_RSP)
228       {
229          GET_CELL_IDX(deleteRsp->cellId, cellIdx);
230          DU_LOG("\nINFO   -->  DU APP : MAC CELL Delete Response : SUCCESS [CELL IDX : %d]", deleteRsp->cellId);
231          if(duCb.actvCellLst[cellIdx]->cellId == deleteRsp->cellId)
232          {
233             memset(duCb.actvCellLst[cellIdx], 0, sizeof(DuCellCb));
234             gCellStatus = CELL_DOWN;
235
236 #ifdef O1_ENABLE
237             DU_LOG("\nINFO   -->  DU APP : Raise cell down alarm for cell id=%d", deleteRsp->cellId);
238             raiseCellAlrm(CELL_DOWN_ALARM_ID, deleteRsp->cellId);
239             setCellOpState(deleteRsp->cellId, DISABLED, INACTIVE);
240 #endif
241
242             duCb.numActvCells--;
243             duCb.numCfgCells--;
244             DU_FREE(duCb.actvCellLst[cellIdx], sizeof(DuCellCb));
245
246          }
247          else
248          {
249             DU_LOG("\nERROR  -->  DU APP : DuProcMacCellDeleteRsp(): CellId [%d] doesnot exist", deleteRsp->cellId);
250             ret = RFAILED;
251          }
252       }
253       else
254       {
255          DU_LOG("\nERROR  -->  DU APP : DuProcMacCellDeleteRsp(): MAC CELL Delete Response : FAILED\
256          [CELL IDX : %d]", deleteRsp->cellId);
257          ret = RFAILED;
258       }
259       DU_FREE_SHRABL_BUF(pst->region, pst->pool, deleteRsp, sizeof(MacCellDeleteRsp));
260    }
261    else
262    {
263       DU_LOG("\nERROR  -->  DU APP : DuProcMacCellDeleteRsp(): Received MAC cell delete response is NULL");
264       ret = RFAILED;
265    }
266    return ret;
267 }
268
269 /*******************************************************************
270  *
271  * @brief Sending Cell Delete Req To Mac
272  *
273  * @details
274  *
275  *    Function : sendCellDeleteReqToMac
276  *
277  *    Functionality:
278  *     sending Cell Delete Req To Mac
279  *
280  *  @params[in]    uint16_t cellId
281  *  @return ROK     - success
282  *          RFAILED - failure
283  *
284  *
285  *****************************************************************/
286
287 uint8_t sendCellDeleteReqToMac(uint16_t cellId)
288 {
289    Pst pst;
290    uint8_t ret=ROK;
291    MacCellDelete *cellDelete = NULLP;
292    
293    DU_ALLOC_SHRABL_BUF(cellDelete, sizeof(MacCellDelete));
294    if(cellDelete)
295    {
296       cellDelete->cellId = cellId;
297       FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_CELL_DELETE_REQ);
298
299       DU_LOG("\nINFO   -->  DU APP : Sending Cell Delete Request to MAC");  
300       /* Processing one Cell at a time to MAC */
301       ret = (*packMacCellDeleteReqOpts[pst.selector])(&pst, cellDelete);
302       if(ret == RFAILED)
303       {
304          DU_LOG("\nERROR  -->  DU APP : sendCellDeleteReqToMac(): Failed to send Cell delete Req to MAC");
305          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cellDelete, sizeof(MacCellDelete));
306       }
307    }
308    else
309    {
310       DU_LOG("\nERROR  -->   DU APP : sendCellDeleteReqToMac(): Failed to allocate memory"); 
311       ret = RFAILED;
312    }
313    return ret;
314 }
315
316 /*******************************************************************
317  *
318  * @brief DU preocess Cell Delete Req to MAC 
319  *
320  * @details
321  *
322  *    Function : duSendCellDeletReq 
323  *
324  *    Functionality: DU process Cell Delete Req to MAC 
325  *
326  * @params[in] uint16_t cellId
327  * @return ROK     - success
328  *         RFAILED - failure
329  *
330  * ****************************************************************/
331
332 uint8_t duSendCellDeletReq(uint16_t cellId)
333 {
334    uint16_t cellIdx = 0;
335    DU_LOG("\nINFO   -->  DU APP : Processing Cell Delete Request ");
336    GET_CELL_IDX(cellId, cellIdx);
337
338    if(duCb.actvCellLst[cellIdx] == NULLP)
339    {
340       DU_LOG("\nERROR  -->  DU APP : duSendCellDeletReq(): CellId[%d] is not found", cellId);
341       return RFAILED;
342    }
343    
344    if(duCb.actvCellLst[cellIdx]->cellId != cellId)
345    {
346       DU_LOG("\nERROR  -->  DU APP : duSendCellDeletReq(): CellId[%d] is not found", cellId);
347       return RFAILED;
348
349    }  
350    
351    if(duCb.actvCellLst[cellIdx]->cellStatus != DELETION_IN_PROGRESS)
352    {
353       DU_LOG("\nERROR  -->  DU APP : duSendCellDeletReq(): CellStatus[%d] of cellId[%d] is not correct.\
354       Expected CellStatus is DELETION_IN_PROGRESS",duCb.actvCellLst[cellIdx]->cellStatus, cellId);
355       return RFAILED;  
356    }
357
358    if(duCb.actvCellLst[cellIdx]->numActvUes)
359    {
360       DU_LOG("\nERROR  -->  DU APP : duSendCellDeletReq(): Active UEs still present in cellId[%d].\
361       Failed to delete cell", cellId);
362       return RFAILED;
363    }
364
365    if(duBuildAndSendMacCellStop(cellId) == RFAILED)
366    {
367       DU_LOG("\nERROR  -->  DU APP : duSendCellDeletReq(): Failed to build and send cell stop request to MAC for\
368             cellId[%d]",cellId);
369       return RFAILED;
370    }
371
372    return ROK;
373 }
374
375 /**********************************************************************
376   End of file
377  **********************************************************************/