2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 // Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved.
5 // Copyright 2023 Capgemini
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
20 // platform project (RICP).
22 package rmrmsghandlers
27 "e2mgr/e2managererrors"
33 "e2mgr/services/rmrsender"
42 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
43 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
46 const cleanUpDurationNanoSec uint64 = 10000000000 // cleanUpDuration = 10sec (value in nanoSecond=10000000000)
49 emptyTagsToReplaceToSelfClosingTags = []string{"reject", "ignore", "transport-resource-unavailable", "om-intervention", "request-id-unknown",
50 "unspecified", "message-not-compatible-with-receiver-state", "control-processing-overload",
51 "v60s", "v20s", "v10s", "v5s", "v2s", "v1s", "ng", "xn", "e1", "f1", "w1", "s1", "x2", "success", "failure"}
52 gnbTypesMap = map[string]entities.GnbType{
53 "gnb": entities.GnbType_GNB,
54 "en_gnb": entities.GnbType_EN_GNB,
56 enbTypesMap = map[string]entities.EnbType{
57 "enB_macro": entities.EnbType_MACRO_ENB,
58 "enB_home": entities.EnbType_HOME_ENB,
59 "enB_shortmacro": entities.EnbType_SHORT_MACRO_ENB,
60 "enB_longmacro": entities.EnbType_LONG_MACRO_ENB,
61 "ng_enB_macro": entities.EnbType_MACRO_NG_ENB,
62 "ng_enB_shortmacro": entities.EnbType_SHORT_MACRO_NG_ENB,
63 "ng_enB_longmacro": entities.EnbType_LONG_MACRO_NG_ENB,
67 type E2SetupRequestNotificationHandler struct {
69 config *configuration.Configuration
70 e2tInstancesManager managers.IE2TInstancesManager
71 rmrSender *rmrsender.RmrSender
72 rNibDataService services.RNibDataService
73 e2tAssociationManager *managers.E2TAssociationManager
74 ranConnectStatusChangeManager managers.IRanConnectStatusChangeManager
75 ranListManager managers.RanListManager
78 func NewE2SetupRequestNotificationHandler(logger *logger.Logger, config *configuration.Configuration, e2tInstancesManager managers.IE2TInstancesManager, rmrSender *rmrsender.RmrSender, rNibDataService services.RNibDataService, e2tAssociationManager *managers.E2TAssociationManager, ranConnectStatusChangeManager managers.IRanConnectStatusChangeManager, ranListManager managers.RanListManager) *E2SetupRequestNotificationHandler {
79 return &E2SetupRequestNotificationHandler{
82 e2tInstancesManager: e2tInstancesManager,
84 rNibDataService: rNibDataService,
85 e2tAssociationManager: e2tAssociationManager,
86 ranConnectStatusChangeManager: ranConnectStatusChangeManager,
87 ranListManager: ranListManager,
91 func (h *E2SetupRequestNotificationHandler) Handle(request *models.NotificationRequest) {
92 ranName := request.RanName
93 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - received E2_SETUP_REQUEST. Payload: %x", ranName, request.Payload)
95 generalConfiguration, err := h.rNibDataService.GetGeneralConfiguration()
98 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - Failed retrieving e2m general configuration - quitting e2 setup flow. error: %s", err)
102 setupRequest, e2tIpAddress, err := h.parseSetupRequest(request.Payload)
104 h.logger.Errorf(err.Error())
108 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - E2T Address: %s - handling E2_SETUP_REQUEST", e2tIpAddress)
109 h.logger.Debugf("#E2SetupRequestNotificationHandler.Handle - E2_SETUP_REQUEST has been parsed successfully %+v", setupRequest)
111 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - got general configuration from rnib - enableRic: %t", generalConfiguration.EnableRic)
113 if !generalConfiguration.EnableRic {
114 cause := models.Cause{Misc: &models.CauseMisc{OmIntervention: &struct{}{}}}
115 h.handleUnsuccessfulResponse(ranName, request, cause, setupRequest)
119 _, err = h.e2tInstancesManager.GetE2TInstance(e2tIpAddress)
122 h.logger.Errorf("#E2TermInitNotificationHandler.Handle - Failed retrieving E2TInstance. error: %s", err)
126 nodebInfo, err := h.rNibDataService.GetNodeb(ranName)
128 var functionsModified bool
132 if _, ok := err.(*common.ResourceNotFoundError); !ok {
133 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to retrieve nodebInfo entity. Error: %s", ranName, err)
137 if nodebInfo, err = h.handleNewRan(ranName, e2tIpAddress, setupRequest); err != nil {
138 if _, ok := err.(*e2managererrors.UnknownSetupRequestRanNameError); ok {
139 cause := models.Cause{RicRequest: &models.CauseRic{RequestIdUnknown: &struct{}{}}}
140 h.handleUnsuccessfulResponse(ranName, request, cause, setupRequest)
147 functionsModified, err = h.handleExistingRan(ranName, nodebInfo, setupRequest)
150 h.fillCauseAndSendUnsuccessfulResponse(nodebInfo, request, setupRequest)
155 ranStatusChangePublished, err := h.e2tAssociationManager.AssociateRan(e2tIpAddress, nodebInfo)
159 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to associate E2T to nodeB entity. Error: %s", ranName, err)
160 if _, ok := err.(*e2managererrors.RoutingManagerError); ok {
162 if err = h.handleUpdateAndPublishNodebInfo(functionsModified, ranStatusChangePublished, nodebInfo); err != nil {
166 cause := models.Cause{Transport: &models.CauseTransport{TransportResourceUnavailable: &struct{}{}}}
167 h.handleUnsuccessfulResponse(nodebInfo.RanName, request, cause, setupRequest)
172 if err = h.handleUpdateAndPublishNodebInfo(functionsModified, ranStatusChangePublished, nodebInfo); err != nil {
176 h.handleSuccessfulResponse(ranName, request, setupRequest)
179 func (h *E2SetupRequestNotificationHandler) handleUpdateAndPublishNodebInfo(functionsModified bool, ranStatusChangePublished bool, nodebInfo *entities.NodebInfo) error {
181 if ranStatusChangePublished || !functionsModified {
185 err := h.rNibDataService.UpdateNodebInfoAndPublish(nodebInfo)
188 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleUpdateAndPublishNodebInfo - RAN name: %s - Failed at UpdateNodebInfoAndPublish. error: %s", nodebInfo.RanName, err)
192 h.logger.Infof("#E2SetupRequestNotificationHandler.handleUpdateAndPublishNodebInfo - RAN name: %s - Successfully executed UpdateNodebInfoAndPublish", nodebInfo.RanName)
197 func (h *E2SetupRequestNotificationHandler) handleNewRan(ranName string, e2tIpAddress string, setupRequest *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
199 nodebInfo, err := h.buildNodebInfo(ranName, e2tIpAddress, setupRequest)
201 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleNewRan - RAN name: %s - failed building nodebInfo. Error: %s", ranName, err)
205 err = h.rNibDataService.SaveNodeb(nodebInfo)
207 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleNewRan - RAN name: %s - failed saving nodebInfo. Error: %s", ranName, err)
211 nbIdentity := h.buildNbIdentity(ranName, setupRequest)
213 err = h.ranListManager.AddNbIdentity(nodebInfo.GetNodeType(), nbIdentity)
219 return nodebInfo, nil
222 func (h *E2SetupRequestNotificationHandler) handleExistingRan(ranName string, nodebInfo *entities.NodebInfo, setupRequest *models.E2SetupRequestMessage) (bool, error) {
223 if nodebInfo.GetConnectionStatus() == entities.ConnectionStatus_DISCONNECTED {
224 delta_in_nano := uint64(time.Now().UnixNano()) - nodebInfo.StatusUpdateTimeStamp
225 //The duration from last Disconnection for which a new request is to be rejected (currently 10 sec)
226 if delta_in_nano < cleanUpDurationNanoSec {
227 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s, connection status: %s - nodeB entity disconnection in progress", ranName, nodebInfo.ConnectionStatus)
228 return false, errors.New("nodeB entity disconnection in progress")
230 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - RAN name: %s, connection status: %s - nodeB entity in disconnected state", ranName, nodebInfo.ConnectionStatus)
231 } else if nodebInfo.GetConnectionStatus() == entities.ConnectionStatus_SHUTTING_DOWN {
232 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s, connection status: %s - nodeB entity in incorrect state", ranName, nodebInfo.ConnectionStatus)
233 return false, errors.New("nodeB entity in incorrect state")
236 nodebInfo.SetupFromNetwork = true
238 e2NodeConfig := setupRequest.ExtractE2NodeConfigList()
239 if e2NodeConfig == nil {
240 return false, errors.New("Empty E2nodeComponentConfigAddition-List")
243 if nodebInfo.NodeType == entities.Node_ENB {
244 if len(e2NodeConfig) == 0 && len(nodebInfo.GetEnb().GetNodeConfigs()) == 0 {
245 return false, errors.New("Empty E2nodeComponentConfigAddition-List")
247 nodebInfo.GetEnb().NodeConfigs = e2NodeConfig
252 if len(e2NodeConfig) == 0 && len(nodebInfo.GetGnb().GetNodeConfigs()) == 0 {
253 return false, errors.New("Empty E2nodeComponentConfigAddition-List")
255 nodebInfo.GetGnb().NodeConfigs = e2NodeConfig
257 setupMessageRanFuncs := setupRequest.ExtractRanFunctionsList()
259 if setupMessageRanFuncs == nil || (len(setupMessageRanFuncs) == 0 && len(nodebInfo.GetGnb().RanFunctions) == 0) {
263 nodebInfo.GetGnb().RanFunctions = setupMessageRanFuncs
267 func (h *E2SetupRequestNotificationHandler) handleUnsuccessfulResponse(ranName string, req *models.NotificationRequest, cause models.Cause, setupRequest *models.E2SetupRequestMessage) {
268 failureResponse := models.NewE2SetupFailureResponseMessage(models.TimeToWaitEnum.V60s, cause, setupRequest)
269 h.logger.Debugf("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - E2_SETUP_RESPONSE has been built successfully %+v", failureResponse)
271 responsePayload, err := xml.Marshal(&failureResponse.E2APPDU)
273 h.logger.Warnf("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - RAN name: %s - Error marshalling RIC_E2_SETUP_RESP. Payload: %s", ranName, responsePayload)
276 responsePayload = utils.ReplaceEmptyTagsWithSelfClosing(responsePayload, emptyTagsToReplaceToSelfClosingTags)
278 h.logger.Infof("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - payload: %s", responsePayload)
279 msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_FAILURE, ranName, responsePayload, req.TransactionId, req.GetMsgSrc())
280 h.logger.Infof("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - RAN name: %s - RIC_E2_SETUP_RESP message has been built successfully. Message: %x", ranName, msg)
281 _ = h.rmrSender.WhSend(msg)
285 func (h *E2SetupRequestNotificationHandler) handleSuccessfulResponse(ranName string, req *models.NotificationRequest, setupRequest *models.E2SetupRequestMessage) {
287 plmnId := buildPlmnId(h.config.GlobalRicId.Mcc, h.config.GlobalRicId.Mnc)
289 ricNearRtId, err := convertTo20BitString(h.config.GlobalRicId.RicId)
293 successResponse := models.NewE2SetupSuccessResponseMessage(plmnId, ricNearRtId, setupRequest)
294 h.logger.Debugf("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - E2_SETUP_RESPONSE has been built successfully %+v", successResponse)
296 responsePayload, err := xml.Marshal(&successResponse.E2APPDU)
298 h.logger.Warnf("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - RAN name: %s - Error marshalling RIC_E2_SETUP_RESP. Payload: %s", ranName, responsePayload)
301 responsePayload = utils.ReplaceEmptyTagsWithSelfClosing(responsePayload, emptyTagsToReplaceToSelfClosingTags)
303 h.logger.Infof("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - payload: %s", responsePayload)
305 msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_RESP, ranName, responsePayload, req.TransactionId, req.GetMsgSrc())
306 h.logger.Infof("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - RAN name: %s - RIC_E2_SETUP_RESP message has been built successfully. Message: %x", ranName, msg)
307 _ = h.rmrSender.Send(msg)
310 func buildPlmnId(mmc string, mnc string) string {
311 var b strings.Builder
327 func convertTo20BitString(ricNearRtId string) (string, error) {
328 r, err := strconv.ParseUint(ricNearRtId, 16, 32)
332 return fmt.Sprintf("%020b", r)[:20], nil
335 func (h *E2SetupRequestNotificationHandler) parseSetupRequest(payload []byte) (*models.E2SetupRequestMessage, string, error) {
337 pipInd := bytes.IndexByte(payload, '|')
339 return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Error parsing E2 Setup Request failed extract Payload: no | separator found")
342 e2tIpAddress := string(payload[:pipInd])
343 if len(e2tIpAddress) == 0 {
344 return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Empty E2T Address received")
347 h.logger.Infof("#E2SetupRequestNotificationHandler.parseSetupRequest - payload: %s", payload[pipInd+1:])
349 setupRequest := &models.E2SetupRequestMessage{}
350 err := xml.Unmarshal(utils.NormalizeXml(payload[pipInd+1:]), &setupRequest.E2APPDU)
352 return nil, "", errors.New(fmt.Sprintf("#E2SetupRequestNotificationHandler.parseSetupRequest - Error unmarshalling E2 Setup Request payload: %x", payload))
355 return setupRequest, e2tIpAddress, nil
358 func (h *E2SetupRequestNotificationHandler) buildNodebInfo(ranName string, e2tAddress string, request *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
359 nodebInfo := &entities.NodebInfo{
360 AssociatedE2TInstanceAddress: e2tAddress,
362 GlobalNbId: h.buildGlobalNbId(request),
363 SetupFromNetwork: true,
365 err := h.setNodeTypeAndConfiguration(nodebInfo)
370 e2NodeConfig := request.ExtractE2NodeConfigList()
371 if e2NodeConfig == nil {
372 return nil, errors.New("Empty E2nodeComponentConfigAddition-List")
375 if nodebInfo.NodeType == entities.Node_ENB {
376 if len(e2NodeConfig) == 0 && len(nodebInfo.GetEnb().GetNodeConfigs()) == 0 {
377 return nil, errors.New("Empty E2nodeComponentConfigAddition-List")
379 nodebInfo.GetEnb().NodeConfigs = e2NodeConfig
381 return nodebInfo, nil
384 if len(e2NodeConfig) == 0 && len(nodebInfo.GetGnb().GetNodeConfigs()) == 0 {
385 return nil, errors.New("Empty E2nodeComponentConfigAddition-List")
387 nodebInfo.GetGnb().NodeConfigs = e2NodeConfig
389 if nodebInfo.NodeType == entities.Node_GNB {
390 h.logger.Debugf("#E2SetupRequestNotificationHandler buildNodebInfo - entities.Node_GNB %d", entities.Node_GNB)
392 gnbNodetype := h.setGnbNodeType(request)
393 h.logger.Debugf("#E2SetupRequestNotificationHandler buildNodebInfo -gnbNodetype %s", gnbNodetype)
394 nodebInfo.GnbNodeType = gnbNodetype
395 nodebInfo.CuUpId = request.GetCuupId()
396 nodebInfo.DuId = request.GetDuId()
397 h.logger.Debugf("#E2SetupRequestNotificationHandler buildNodebInfo -cuupid%s", request.GetCuupId())
398 h.logger.Debugf("#E2SetupRequestNotificationHandler buildNodebInfo -duid %s", request.GetDuId())
401 ranFuncs := request.ExtractRanFunctionsList()
404 nodebInfo.GetGnb().RanFunctions = ranFuncs
407 return nodebInfo, nil
411 func (h *E2SetupRequestNotificationHandler) setGnbNodeType(setupRequest *models.E2SetupRequestMessage) string {
413 /*Note: Deployment where CU-UP and DU are combined
414 (but do not include the CP-CP) and have a single E2 connection
415 is not supported. The combination of CU-CP, CU-UP, and DU will be
416 treated as a single gNB and expect it to have only the
417 global gNB ID in its E2 Setup ID*/
418 if setupRequest.GetCuupId() != "" && setupRequest.GetDuId() != "" {
420 } else if setupRequest.GetCuupId() != "" {
421 gnbNodetype = "gNB_CU_UP"
422 } else if setupRequest.GetDuId() != "" {
423 gnbNodetype = "gNB_DU"
428 func (h *E2SetupRequestNotificationHandler) setNodeTypeAndConfiguration(nodebInfo *entities.NodebInfo) error {
429 for k, v := range gnbTypesMap {
430 if strings.HasPrefix(nodebInfo.RanName, k) {
431 nodebInfo.NodeType = entities.Node_GNB
432 nodebInfo.Configuration = &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{GnbType: v}}
436 for k, v := range enbTypesMap {
437 if strings.HasPrefix(nodebInfo.RanName, k) {
438 nodebInfo.NodeType = entities.Node_ENB
439 nodebInfo.Configuration = &entities.NodebInfo_Enb{Enb: &entities.Enb{EnbType: v}}
444 return e2managererrors.NewUnknownSetupRequestRanNameError(nodebInfo.RanName)
447 func (h *E2SetupRequestNotificationHandler) buildGlobalNbId(setupRequest *models.E2SetupRequestMessage) *entities.GlobalNbId {
448 return &entities.GlobalNbId{
449 PlmnId: setupRequest.GetPlmnId(),
450 NbId: setupRequest.GetNbId(),
454 func (h *E2SetupRequestNotificationHandler) buildNbIdentity(ranName string, setupRequest *models.E2SetupRequestMessage) *entities.NbIdentity {
455 return &entities.NbIdentity{
456 InventoryName: ranName,
457 GlobalNbId: h.buildGlobalNbId(setupRequest),
461 func (h *E2SetupRequestNotificationHandler) fillCauseAndSendUnsuccessfulResponse(nodebInfo *entities.NodebInfo, request *models.NotificationRequest, setupRequest *models.E2SetupRequestMessage) {
462 if nodebInfo.GetConnectionStatus() == entities.ConnectionStatus_DISCONNECTED {
463 cause := models.Cause{Misc: &models.CauseMisc{ControlProcessingOverload: &struct{}{}}}
464 h.handleUnsuccessfulResponse(nodebInfo.RanName, request, cause, setupRequest)