2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 // Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved.
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
18 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 // platform project (RICP).
21 package rmrmsghandlers
26 "e2mgr/e2managererrors"
32 "e2mgr/services/rmrsender"
41 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
42 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
45 const cleanUpDurationNanoSec uint64 = 10000000000 // cleanUpDuration = 10sec (value in nanoSecond=10000000000)
48 emptyTagsToReplaceToSelfClosingTags = []string{"reject", "ignore", "transport-resource-unavailable", "om-intervention", "request-id-unknown",
49 "unspecified", "message-not-compatible-with-receiver-state", "control-processing-overload",
50 "v60s", "v20s", "v10s", "v5s", "v2s", "v1s", "ng", "xn", "e1", "f1", "w1", "s1", "x2", "success", "failure"}
51 gnbTypesMap = map[string]entities.GnbType{
52 "gnb": entities.GnbType_GNB,
53 "en_gnb": entities.GnbType_EN_GNB,
55 enbTypesMap = map[string]entities.EnbType{
56 "enB_macro": entities.EnbType_MACRO_ENB,
57 "enB_home": entities.EnbType_HOME_ENB,
58 "enB_shortmacro": entities.EnbType_SHORT_MACRO_ENB,
59 "enB_longmacro": entities.EnbType_LONG_MACRO_ENB,
60 "ng_enB_macro": entities.EnbType_MACRO_NG_ENB,
61 "ng_enB_shortmacro": entities.EnbType_SHORT_MACRO_NG_ENB,
62 "ng_enB_longmacro": entities.EnbType_LONG_MACRO_NG_ENB,
66 type E2SetupRequestNotificationHandler struct {
68 config *configuration.Configuration
69 e2tInstancesManager managers.IE2TInstancesManager
70 rmrSender *rmrsender.RmrSender
71 rNibDataService services.RNibDataService
72 e2tAssociationManager *managers.E2TAssociationManager
73 ranConnectStatusChangeManager managers.IRanConnectStatusChangeManager
74 ranListManager managers.RanListManager
77 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 {
78 return &E2SetupRequestNotificationHandler{
81 e2tInstancesManager: e2tInstancesManager,
83 rNibDataService: rNibDataService,
84 e2tAssociationManager: e2tAssociationManager,
85 ranConnectStatusChangeManager: ranConnectStatusChangeManager,
86 ranListManager: ranListManager,
90 func (h *E2SetupRequestNotificationHandler) Handle(request *models.NotificationRequest) {
91 ranName := request.RanName
92 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - received E2_SETUP_REQUEST. Payload: %x", ranName, request.Payload)
94 generalConfiguration, err := h.rNibDataService.GetGeneralConfiguration()
97 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - Failed retrieving e2m general configuration - quitting e2 setup flow. error: %s", err)
101 setupRequest, e2tIpAddress, err := h.parseSetupRequest(request.Payload)
103 h.logger.Errorf(err.Error())
107 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - E2T Address: %s - handling E2_SETUP_REQUEST", e2tIpAddress)
108 h.logger.Debugf("#E2SetupRequestNotificationHandler.Handle - E2_SETUP_REQUEST has been parsed successfully %+v", setupRequest)
110 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - got general configuration from rnib - enableRic: %t", generalConfiguration.EnableRic)
112 if !generalConfiguration.EnableRic {
113 cause := models.Cause{Misc: &models.CauseMisc{OmIntervention: &struct{}{}}}
114 h.handleUnsuccessfulResponse(ranName, request, cause, setupRequest)
118 _, err = h.e2tInstancesManager.GetE2TInstance(e2tIpAddress)
121 h.logger.Errorf("#E2TermInitNotificationHandler.Handle - Failed retrieving E2TInstance. error: %s", err)
125 nodebInfo, err := h.rNibDataService.GetNodeb(ranName)
127 var functionsModified bool
131 if _, ok := err.(*common.ResourceNotFoundError); !ok {
132 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to retrieve nodebInfo entity. Error: %s", ranName, err)
136 if nodebInfo, err = h.handleNewRan(ranName, e2tIpAddress, setupRequest); err != nil {
137 if _, ok := err.(*e2managererrors.UnknownSetupRequestRanNameError); ok {
138 cause := models.Cause{RicRequest: &models.CauseRic{RequestIdUnknown: &struct{}{}}}
139 h.handleUnsuccessfulResponse(ranName, request, cause, setupRequest)
146 functionsModified, err = h.handleExistingRan(ranName, nodebInfo, setupRequest)
149 h.fillCauseAndSendUnsuccessfulResponse(nodebInfo, request, setupRequest)
154 ranStatusChangePublished, err := h.e2tAssociationManager.AssociateRan(e2tIpAddress, nodebInfo)
158 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to associate E2T to nodeB entity. Error: %s", ranName, err)
159 if _, ok := err.(*e2managererrors.RoutingManagerError); ok {
161 if err = h.handleUpdateAndPublishNodebInfo(functionsModified, ranStatusChangePublished, nodebInfo); err != nil {
165 cause := models.Cause{Transport: &models.CauseTransport{TransportResourceUnavailable: &struct{}{}}}
166 h.handleUnsuccessfulResponse(nodebInfo.RanName, request, cause, setupRequest)
171 if err = h.handleUpdateAndPublishNodebInfo(functionsModified, ranStatusChangePublished, nodebInfo); err != nil {
175 h.handleSuccessfulResponse(ranName, request, setupRequest)
178 func (h *E2SetupRequestNotificationHandler) handleUpdateAndPublishNodebInfo(functionsModified bool, ranStatusChangePublished bool, nodebInfo *entities.NodebInfo) error {
180 if ranStatusChangePublished || !functionsModified {
184 err := h.rNibDataService.UpdateNodebInfoAndPublish(nodebInfo)
187 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleUpdateAndPublishNodebInfo - RAN name: %s - Failed at UpdateNodebInfoAndPublish. error: %s", nodebInfo.RanName, err)
191 h.logger.Infof("#E2SetupRequestNotificationHandler.handleUpdateAndPublishNodebInfo - RAN name: %s - Successfully executed UpdateNodebInfoAndPublish", nodebInfo.RanName)
196 func (h *E2SetupRequestNotificationHandler) handleNewRan(ranName string, e2tIpAddress string, setupRequest *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
198 nodebInfo, err := h.buildNodebInfo(ranName, e2tIpAddress, setupRequest)
200 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleNewRan - RAN name: %s - failed building nodebInfo. Error: %s", ranName, err)
204 err = h.rNibDataService.SaveNodeb(nodebInfo)
206 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleNewRan - RAN name: %s - failed saving nodebInfo. Error: %s", ranName, err)
210 nbIdentity := h.buildNbIdentity(ranName, setupRequest)
212 err = h.ranListManager.AddNbIdentity(nodebInfo.GetNodeType(), nbIdentity)
218 return nodebInfo, nil
221 func (h *E2SetupRequestNotificationHandler) handleExistingRan(ranName string, nodebInfo *entities.NodebInfo, setupRequest *models.E2SetupRequestMessage) (bool, error) {
222 if nodebInfo.GetConnectionStatus() == entities.ConnectionStatus_DISCONNECTED {
223 delta_in_nano := uint64(time.Now().UnixNano()) - nodebInfo.StatusUpdateTimeStamp
224 //The duration from last Disconnection for which a new request is to be rejected (currently 10 sec)
225 if delta_in_nano < cleanUpDurationNanoSec {
226 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s, connection status: %s - nodeB entity disconnection in progress", ranName, nodebInfo.ConnectionStatus)
227 return false, errors.New("nodeB entity disconnection in progress")
229 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - RAN name: %s, connection status: %s - nodeB entity in disconnected state", ranName, nodebInfo.ConnectionStatus)
230 } else if nodebInfo.GetConnectionStatus() == entities.ConnectionStatus_SHUTTING_DOWN {
231 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s, connection status: %s - nodeB entity in incorrect state", ranName, nodebInfo.ConnectionStatus)
232 return false, errors.New("nodeB entity in incorrect state")
235 nodebInfo.SetupFromNetwork = true
237 e2NodeConfig := setupRequest.ExtractE2NodeConfigList()
238 if e2NodeConfig == nil {
239 return false, errors.New("Empty E2nodeComponentConfigAddition-List")
242 if nodebInfo.NodeType == entities.Node_ENB {
243 if len(e2NodeConfig) == 0 && len(nodebInfo.GetEnb().GetNodeConfigs()) == 0 {
244 return false, errors.New("Empty E2nodeComponentConfigAddition-List")
246 nodebInfo.GetEnb().NodeConfigs = e2NodeConfig
251 if len(e2NodeConfig) == 0 && len(nodebInfo.GetGnb().GetNodeConfigs()) == 0 {
252 return false, errors.New("Empty E2nodeComponentConfigAddition-List")
254 nodebInfo.GetGnb().NodeConfigs = e2NodeConfig
256 setupMessageRanFuncs := setupRequest.ExtractRanFunctionsList()
258 if setupMessageRanFuncs == nil || (len(setupMessageRanFuncs) == 0 && len(nodebInfo.GetGnb().RanFunctions) == 0) {
262 nodebInfo.GetGnb().RanFunctions = setupMessageRanFuncs
266 func (h *E2SetupRequestNotificationHandler) handleUnsuccessfulResponse(ranName string, req *models.NotificationRequest, cause models.Cause, setupRequest *models.E2SetupRequestMessage) {
267 failureResponse := models.NewE2SetupFailureResponseMessage(models.TimeToWaitEnum.V60s, cause, setupRequest)
268 h.logger.Debugf("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - E2_SETUP_RESPONSE has been built successfully %+v", failureResponse)
270 responsePayload, err := xml.Marshal(&failureResponse.E2APPDU)
272 h.logger.Warnf("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - RAN name: %s - Error marshalling RIC_E2_SETUP_RESP. Payload: %s", ranName, responsePayload)
275 responsePayload = utils.ReplaceEmptyTagsWithSelfClosing(responsePayload, emptyTagsToReplaceToSelfClosingTags)
277 h.logger.Infof("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - payload: %s", responsePayload)
278 msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_FAILURE, ranName, responsePayload, req.TransactionId, req.GetMsgSrc())
279 h.logger.Infof("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - RAN name: %s - RIC_E2_SETUP_RESP message has been built successfully. Message: %x", ranName, msg)
280 _ = h.rmrSender.WhSend(msg)
284 func (h *E2SetupRequestNotificationHandler) handleSuccessfulResponse(ranName string, req *models.NotificationRequest, setupRequest *models.E2SetupRequestMessage) {
286 plmnId := buildPlmnId(h.config.GlobalRicId.Mcc, h.config.GlobalRicId.Mnc)
288 ricNearRtId, err := convertTo20BitString(h.config.GlobalRicId.RicId)
292 successResponse := models.NewE2SetupSuccessResponseMessage(plmnId, ricNearRtId, setupRequest)
293 h.logger.Debugf("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - E2_SETUP_RESPONSE has been built successfully %+v", successResponse)
295 responsePayload, err := xml.Marshal(&successResponse.E2APPDU)
297 h.logger.Warnf("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - RAN name: %s - Error marshalling RIC_E2_SETUP_RESP. Payload: %s", ranName, responsePayload)
300 responsePayload = utils.ReplaceEmptyTagsWithSelfClosing(responsePayload, emptyTagsToReplaceToSelfClosingTags)
302 h.logger.Infof("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - payload: %s", responsePayload)
304 msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_RESP, ranName, responsePayload, req.TransactionId, req.GetMsgSrc())
305 h.logger.Infof("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - RAN name: %s - RIC_E2_SETUP_RESP message has been built successfully. Message: %x", ranName, msg)
306 _ = h.rmrSender.Send(msg)
309 func buildPlmnId(mmc string, mnc string) string {
310 var b strings.Builder
326 func convertTo20BitString(ricNearRtId string) (string, error) {
327 r, err := strconv.ParseUint(ricNearRtId, 16, 32)
331 return fmt.Sprintf("%020b", r)[:20], nil
334 func (h *E2SetupRequestNotificationHandler) parseSetupRequest(payload []byte) (*models.E2SetupRequestMessage, string, error) {
336 pipInd := bytes.IndexByte(payload, '|')
338 return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Error parsing E2 Setup Request failed extract Payload: no | separator found")
341 e2tIpAddress := string(payload[:pipInd])
342 if len(e2tIpAddress) == 0 {
343 return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Empty E2T Address received")
346 h.logger.Infof("#E2SetupRequestNotificationHandler.parseSetupRequest - payload: %s", payload[pipInd+1:])
348 setupRequest := &models.E2SetupRequestMessage{}
349 err := xml.Unmarshal(utils.NormalizeXml(payload[pipInd+1:]), &setupRequest.E2APPDU)
351 return nil, "", errors.New(fmt.Sprintf("#E2SetupRequestNotificationHandler.parseSetupRequest - Error unmarshalling E2 Setup Request payload: %x", payload))
354 return setupRequest, e2tIpAddress, nil
357 func (h *E2SetupRequestNotificationHandler) buildNodebInfo(ranName string, e2tAddress string, request *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
358 nodebInfo := &entities.NodebInfo{
359 AssociatedE2TInstanceAddress: e2tAddress,
361 GlobalNbId: h.buildGlobalNbId(request),
362 SetupFromNetwork: true,
364 err := h.setNodeTypeAndConfiguration(nodebInfo)
369 e2NodeConfig := request.ExtractE2NodeConfigList()
370 if e2NodeConfig == nil {
371 return nil, errors.New("Empty E2nodeComponentConfigAddition-List")
374 if nodebInfo.NodeType == entities.Node_ENB {
375 if len(e2NodeConfig) == 0 && len(nodebInfo.GetEnb().GetNodeConfigs()) == 0 {
376 return nil, errors.New("Empty E2nodeComponentConfigAddition-List")
378 nodebInfo.GetEnb().NodeConfigs = e2NodeConfig
380 return nodebInfo, nil
383 if len(e2NodeConfig) == 0 && len(nodebInfo.GetGnb().GetNodeConfigs()) == 0 {
384 return nil, errors.New("Empty E2nodeComponentConfigAddition-List")
386 nodebInfo.GetGnb().NodeConfigs = e2NodeConfig
388 ranFuncs := request.ExtractRanFunctionsList()
391 nodebInfo.GetGnb().RanFunctions = ranFuncs
394 return nodebInfo, nil
397 func (h *E2SetupRequestNotificationHandler) setNodeTypeAndConfiguration(nodebInfo *entities.NodebInfo) error {
398 for k, v := range gnbTypesMap {
399 if strings.HasPrefix(nodebInfo.RanName, k) {
400 nodebInfo.NodeType = entities.Node_GNB
401 nodebInfo.Configuration = &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{GnbType: v}}
405 for k, v := range enbTypesMap {
406 if strings.HasPrefix(nodebInfo.RanName, k) {
407 nodebInfo.NodeType = entities.Node_ENB
408 nodebInfo.Configuration = &entities.NodebInfo_Enb{Enb: &entities.Enb{EnbType: v}}
413 return e2managererrors.NewUnknownSetupRequestRanNameError(nodebInfo.RanName)
416 func (h *E2SetupRequestNotificationHandler) buildGlobalNbId(setupRequest *models.E2SetupRequestMessage) *entities.GlobalNbId {
417 return &entities.GlobalNbId{
418 PlmnId: setupRequest.GetPlmnId(),
419 NbId: setupRequest.GetNbId(),
423 func (h *E2SetupRequestNotificationHandler) buildNbIdentity(ranName string, setupRequest *models.E2SetupRequestMessage) *entities.NbIdentity {
424 return &entities.NbIdentity{
425 InventoryName: ranName,
426 GlobalNbId: h.buildGlobalNbId(setupRequest),
430 func (h *E2SetupRequestNotificationHandler) fillCauseAndSendUnsuccessfulResponse(nodebInfo *entities.NodebInfo, request *models.NotificationRequest, setupRequest *models.E2SetupRequestMessage) {
431 if nodebInfo.GetConnectionStatus() == entities.ConnectionStatus_DISCONNECTED {
432 cause := models.Cause{Misc: &models.CauseMisc{ControlProcessingOverload: &struct{}{}}}
433 h.handleUnsuccessfulResponse(nodebInfo.RanName, request, cause, setupRequest)