2 // Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
16 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 // platform project (RICP).
19 package rmrmsghandlers
29 "e2mgr/services/rmrsender"
32 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
33 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
36 //type FunctionChange int
39 RAN_FUNCTIONS_ADDED int = 10 + iota
41 RAN_FUNCTIONS_MODIFIED
44 type functionDetails struct {
47 functionDefinition string
48 functionRevision uint32
51 type RicServiceUpdateHandler struct {
53 rmrSender *rmrsender.RmrSender
54 rNibDataService services.RNibDataService
55 ranListManager managers.RanListManager
58 func NewRicServiceUpdateHandler(logger *logger.Logger, rmrSender *rmrsender.RmrSender, rNibDataService services.RNibDataService, ranListManager managers.RanListManager) *RicServiceUpdateHandler {
59 return &RicServiceUpdateHandler {
62 rNibDataService: rNibDataService,
63 ranListManager: ranListManager,
67 func (h *RicServiceUpdateHandler) Handle(request *models.NotificationRequest) {
68 ranName := request.RanName
69 h.logger.Infof("#RicServiceUpdateHandler.Handle - RAN name: %s - received RIC_SERVICE_UPDATE. Payload: %s", ranName, request.Payload)
71 nodebInfo, err := h.rNibDataService.GetNodeb(ranName)
73 _, ok := err.(*common.ResourceNotFoundError)
75 h.logger.Errorf("#RicServiceUpdateHandler.Handle - failed to get nodeB entity for ran name: %v due to RNIB Error: %s", ranName, err)
77 h.logger.Errorf("#RicServiceUpdateHandler.Handle - nobeB entity of RanName:%s absent in RNIB. Error: %s", ranName, err)
82 ricServiceUpdate, err := h.parseSetupRequest(request.Payload)
84 h.logger.Errorf(err.Error())
87 h.logger.Infof("#RicServiceUpdateHandler.Handle - RIC_SERVICE_UPDATE has been parsed successfully %+v", ricServiceUpdate)
89 ackFunctionIds := h.updateFunctions(ricServiceUpdate.E2APPDU.InitiatingMessage.Value.RICServiceUpdate.ProtocolIEs.RICServiceUpdateIEs, nodebInfo)
90 if len(ricServiceUpdate.E2APPDU.InitiatingMessage.Value.RICServiceUpdate.ProtocolIEs.RICServiceUpdateIEs) != 0 {
91 err = h.rNibDataService.UpdateNodebInfoAndPublish(nodebInfo)
93 h.logger.Errorf("#RicServiceUpdateHandler.Handle - RAN name: %s - Failed at UpdateNodebInfoAndPublish. error: %s", nodebInfo.RanName, err)
98 oldNbIdentity, newNbIdentity := h.ranListManager.UpdateHealthcheckTimeStampReceived(nodebInfo.RanName)
99 err = h.ranListManager.UpdateNbIdentities(nodebInfo.NodeType, []*entities.NbIdentity{oldNbIdentity}, []*entities.NbIdentity{newNbIdentity})
101 h.logger.Errorf("#RicServiceUpdate.Handle - failed to Update NbIdentities: %s", err)
105 updateAck := models.NewServiceUpdateAck(ackFunctionIds)
106 err = h.sendUpdateAck(updateAck, nodebInfo, request)
108 h.logger.Errorf("#RicServiceUpdate.Handle - failed to send RIC_SERVICE_UPDATE_ACK message to RMR: %s", err)
112 h.logger.Infof("#RicServiceUpdate.Handle - Completed successfully")
115 func (h *RicServiceUpdateHandler) sendUpdateAck(updateAck models.RicServiceUpdateAckE2APPDU, nodebInfo *entities.NodebInfo, request *models.NotificationRequest) error {
116 payLoad, err := xml.Marshal(updateAck)
118 h.logger.Errorf("#RicServiceUpdate.sendUpdateAck - RAN name: %s - Error marshalling RIC_SERVICE_UPDATE_ACK. Payload: %s", nodebInfo.RanName, payLoad)
121 toReplaceTags := []string{"reject", "ignore", "procedureCode", "id", "RANfunctionID-Item", "RANfunctionsID-List"}
122 payLoad = utils.ReplaceEmptyTagsWithSelfClosing(payLoad,toReplaceTags)
124 h.logger.Infof("#RicServiceUpdate.sendUpdateAck - Sending RIC_SERVICE_UPDATE_ACK to RAN name: %s with payload %s",nodebInfo.RanName, payLoad)
125 msg := models.NewRmrMessage(rmrCgo.RIC_SERVICE_UPDATE_ACK, nodebInfo.RanName, payLoad, request.TransactionId, request.GetMsgSrc())
126 err = h.rmrSender.Send(msg)
130 func (h *RicServiceUpdateHandler) updateFunctions(RICServiceUpdateIEs []models.RICServiceUpdateIEs,nodebInfo *entities.NodebInfo) []models.RicServiceAckRANFunctionIDItem {
131 ranFunctions := nodebInfo.GetGnb().RanFunctions
132 RanFIdtoIdxMap := make(map[uint32]int)
133 var acceptedFunctionIds []models.RicServiceAckRANFunctionIDItem
134 functionsToBeDeleted := make(map[int]bool)
136 for index, ranFunction := range ranFunctions {
137 RanFIdtoIdxMap[ranFunction.RanFunctionId] = index
140 for _, ricServiceUpdateIE := range RICServiceUpdateIEs {
141 functionDetails, err := h.getFunctionDetails(ricServiceUpdateIE)
143 h.logger.Errorf("#RicServiceUpdate.updateFunctions- GetFunctionDetails returned err: %s", err)
146 for _, functionDetail := range functionDetails {
147 functionChange, functionId, functionDefinition, functionRevision := functionDetail.functionChange,
148 functionDetail.functionId, functionDetail.functionDefinition, functionDetail.functionRevision
149 ranFIndex, ok := RanFIdtoIdxMap[functionId]
151 switch functionChange {
152 case RAN_FUNCTIONS_ADDED,RAN_FUNCTIONS_MODIFIED :
153 ranFunctions = append(ranFunctions, &entities.RanFunction{RanFunctionId:functionId,
154 RanFunctionDefinition:functionDefinition, RanFunctionRevision:functionRevision})
155 case RAN_FUNCTIONS_DELETED:
159 switch functionChange {
160 case RAN_FUNCTIONS_ADDED, RAN_FUNCTIONS_MODIFIED:
161 ranFunctions[ranFIndex].RanFunctionDefinition = functionDefinition
162 ranFunctions[ranFIndex].RanFunctionRevision = functionRevision
163 case RAN_FUNCTIONS_DELETED:
164 functionsToBeDeleted[ranFIndex] = true
167 serviceupdateAckFunctionId := models.RicServiceAckRANFunctionIDItem{RanFunctionID:functionId, RanFunctionRevision:functionRevision}
168 acceptedFunctionIds = append(acceptedFunctionIds, serviceupdateAckFunctionId)
171 finalranFunctions := h.remove(ranFunctions, functionsToBeDeleted)
172 nodebInfo.GetGnb().RanFunctions = finalranFunctions
173 return acceptedFunctionIds
176 func (h *RicServiceUpdateHandler) remove(ranFunctions []*entities.RanFunction, functionsToBeDeleted map[int]bool) []*entities.RanFunction {
177 if len(functionsToBeDeleted) == 0 {
180 var finalranFunctions []*entities.RanFunction
181 for i := 0; i < len(ranFunctions); i++ {
182 _, ok := functionsToBeDeleted[i]
184 finalranFunctions = append(finalranFunctions, ranFunctions[i])
187 return finalranFunctions
190 func (h *RicServiceUpdateHandler) getFunctionDetails(ricServiceUpdateIE models.RICServiceUpdateIEs) ([]functionDetails , error) {
191 functionChange := ricServiceUpdateIE.ID
192 switch functionChange{
193 case RAN_FUNCTIONS_ADDED, RAN_FUNCTIONS_MODIFIED:
194 return h.getFunctionsAddedModifiedHandler(ricServiceUpdateIE)
195 case RAN_FUNCTIONS_DELETED:
196 return h.getFunctionsDeleteHandler(ricServiceUpdateIE)
198 return nil, common.NewInternalError(fmt.Errorf("#RicServiceUpdate.getFunctionDetails - Unknown change type %v", functionChange))
200 return nil, common.NewInternalError(fmt.Errorf("#RicServiceUpdate.getFunctionDetails - Internal Error"))
203 func (h *RicServiceUpdateHandler) getFunctionsAddedModifiedHandler(ricServiceUpdateIE models.RICServiceUpdateIEs) ([]functionDetails , error){
204 functionChange := ricServiceUpdateIE.ID
205 ranFunctionsIEList := ricServiceUpdateIE.Value.RANfunctionsList.RANfunctionsItemProtocolIESingleContainer
206 if len(ranFunctionsIEList) ==0 {
207 return nil, common.NewInternalError(fmt.Errorf("#RicServiceUpdate.getFunctionDetails - function change type is %v but Functions list is empty", functionChange))
210 functionDetailsList := make([]functionDetails, len(ranFunctionsIEList))
211 for index, ranFunctionIE := range ranFunctionsIEList {
212 ranFunction := ranFunctionIE.Value.RANfunctionItem
213 functionDetailsList[index] = functionDetails{functionChange:functionChange, functionId:ranFunction.RanFunctionID,
214 functionDefinition:ranFunction.RanFunctionDefinition, functionRevision:ranFunction.RanFunctionRevision}
216 return functionDetailsList, nil
219 func (h *RicServiceUpdateHandler) getFunctionsDeleteHandler(ricServiceUpdateIE models.RICServiceUpdateIEs) ([]functionDetails , error){
220 functionChange := ricServiceUpdateIE.ID
221 ranFunctionIdIEsList := ricServiceUpdateIE.Value.RANfunctionsIDList.RANfunctionsItemIDProtocolIESingleContainer
222 if len(ranFunctionIdIEsList) == 0 {
223 return nil, common.NewInternalError(fmt.Errorf("#RicServiceUpdate.getFunctionDetails - function change type is %v but FunctionIds list is empty", functionChange))
226 functionDetailsList := make([]functionDetails, len(ranFunctionIdIEsList))
227 for index, ranFunctionIdIE := range ranFunctionIdIEsList {
228 ranFunctionId := ranFunctionIdIE.Value.RANfunctionIDItem
229 functionDetailsList[index] = functionDetails{functionChange:functionChange, functionId:ranFunctionId.RanFunctionID,
230 functionDefinition:"", functionRevision:ranFunctionId.RanFunctionRevision}
232 return functionDetailsList, nil
235 func (h *RicServiceUpdateHandler) parseSetupRequest(payload []byte) (*models.RICServiceUpdateMessage, error) {
236 pipInd := bytes.IndexByte(payload, '|')
238 return nil, common.NewInternalError(fmt.Errorf("#RicServiceUpdateHandler.parseSetupRequest - Error parsing RIC SERVICE UPDATE failed extract Payload: no | separator found"))
241 ricServiceUpdate := &models.RICServiceUpdateMessage{}
242 err := xml.Unmarshal(utils.NormalizeXml(payload[pipInd+1:]), &ricServiceUpdate.E2APPDU)
244 return nil, common.NewInternalError(fmt.Errorf("#RicServiceUpdateHandler.parseSetupRequest - Error unmarshalling RIC SERVICE UPDATE payload: %x", payload))
246 return ricServiceUpdate, nil