2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
20 package rmrmsghandlers
25 "e2mgr/e2managererrors"
31 "e2mgr/services/rmrsender"
35 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
36 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
42 emptyTagsToReplaceToSelfClosingTags = []string{"reject", "ignore", "transport-resource-unavailable", "om-intervention", "request-id-unknown",
43 "v60s", "v20s", "v10s", "v5s", "v2s", "v1s"}
44 gnbTypesMap = map[string]entities.GnbType{
45 "gnb": entities.GnbType_GNB,
46 "en_gnb": entities.GnbType_EN_GNB,
48 enbTypesMap = map[string]entities.EnbType{
49 "enB_macro": entities.EnbType_MACRO_ENB,
50 "enB_home": entities.EnbType_HOME_ENB,
51 "enB_shortmacro": entities.EnbType_SHORT_MACRO_ENB,
52 "enB_longmacro": entities.EnbType_LONG_MACRO_ENB,
53 "ng_enB_macro": entities.EnbType_MACRO_NG_ENB,
54 "ng_enB_shortmacro": entities.EnbType_SHORT_MACRO_NG_ENB,
55 "ng_enB_longmacro": entities.EnbType_LONG_MACRO_NG_ENB,
59 type E2SetupRequestNotificationHandler struct {
61 config *configuration.Configuration
62 e2tInstancesManager managers.IE2TInstancesManager
63 rmrSender *rmrsender.RmrSender
64 rNibDataService services.RNibDataService
65 e2tAssociationManager *managers.E2TAssociationManager
66 ranConnectStatusChangeManager managers.IRanConnectStatusChangeManager
67 ranListManager managers.RanListManager
70 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 {
71 return &E2SetupRequestNotificationHandler{
74 e2tInstancesManager: e2tInstancesManager,
76 rNibDataService: rNibDataService,
77 e2tAssociationManager: e2tAssociationManager,
78 ranConnectStatusChangeManager: ranConnectStatusChangeManager,
79 ranListManager: ranListManager,
83 func (h *E2SetupRequestNotificationHandler) Handle(request *models.NotificationRequest) {
84 ranName := request.RanName
85 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - received E2_SETUP_REQUEST. Payload: %x", ranName, request.Payload)
87 generalConfiguration, err := h.rNibDataService.GetGeneralConfiguration()
90 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - Failed retrieving e2m general configuration - quitting e2 setup flow. error: %s", err)
94 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - got general configuration from rnib - enableRic: %t", generalConfiguration.EnableRic)
96 if !generalConfiguration.EnableRic {
97 cause := models.Cause{Misc: &models.CauseMisc{OmIntervention: &struct{}{}}}
98 h.handleUnsuccessfulResponse(ranName, request, cause)
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 _, err = h.e2tInstancesManager.GetE2TInstance(e2tIpAddress)
114 h.logger.Errorf("#E2TermInitNotificationHandler.Handle - Failed retrieving E2TInstance. error: %s", err)
118 nodebInfo, err := h.rNibDataService.GetNodeb(ranName)
120 var functionsModified bool
124 if _, ok := err.(*common.ResourceNotFoundError); !ok {
125 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to retrieve nodebInfo entity. Error: %s", ranName, err)
129 if nodebInfo, err = h.handleNewRan(ranName, e2tIpAddress, setupRequest); err != nil {
130 if _, ok := err.(*e2managererrors.UnknownSetupRequestRanNameError); ok {
131 cause := models.Cause{RicRequest: &models.CauseRic{RequestIdUnknown: &struct{}{}}}
132 h.handleUnsuccessfulResponse(ranName, request, cause)
139 functionsModified, err = h.handleExistingRan(ranName, nodebInfo, setupRequest)
146 ranStatusChangePublished, err := h.e2tAssociationManager.AssociateRan(e2tIpAddress, nodebInfo)
150 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to associate E2T to nodeB entity. Error: %s", ranName, err)
151 if _, ok := err.(*e2managererrors.RoutingManagerError); ok {
153 if err = h.handleUpdateAndPublishNodebInfo(functionsModified, ranStatusChangePublished, nodebInfo); err != nil {
157 cause := models.Cause{Transport: &models.CauseTransport{TransportResourceUnavailable: &struct{}{}}}
158 h.handleUnsuccessfulResponse(nodebInfo.RanName, request, cause)
163 if err = h.handleUpdateAndPublishNodebInfo(functionsModified, ranStatusChangePublished, nodebInfo); err != nil {
167 h.handleSuccessfulResponse(ranName, request, setupRequest)
170 func (h *E2SetupRequestNotificationHandler) handleUpdateAndPublishNodebInfo(functionsModified bool, ranStatusChangePublished bool, nodebInfo *entities.NodebInfo) error {
172 if ranStatusChangePublished || !functionsModified {
176 err := h.rNibDataService.UpdateNodebInfoAndPublish(nodebInfo)
179 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleUpdateAndPublishNodebInfo - RAN name: %s - Failed at UpdateNodebInfoAndPublish. error: %s", nodebInfo.RanName, err)
183 h.logger.Infof("#E2SetupRequestNotificationHandler.handleUpdateAndPublishNodebInfo - RAN name: %s - Successfully executed UpdateNodebInfoAndPublish", nodebInfo.RanName)
188 func (h *E2SetupRequestNotificationHandler) handleNewRan(ranName string, e2tIpAddress string, setupRequest *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
190 nodebInfo, err := h.buildNodebInfo(ranName, e2tIpAddress, setupRequest)
192 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleNewRan - RAN name: %s - failed building nodebInfo. Error: %s", ranName, err)
196 err = h.rNibDataService.SaveNodeb(nodebInfo)
198 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleNewRan - RAN name: %s - failed saving nodebInfo. Error: %s", ranName, err)
202 nbIdentity := h.buildNbIdentity(ranName, setupRequest)
204 err = h.ranListManager.AddNbIdentity(nodebInfo.GetNodeType(), nbIdentity)
210 return nodebInfo, nil
213 func (h *E2SetupRequestNotificationHandler) handleExistingRan(ranName string, nodebInfo *entities.NodebInfo, setupRequest *models.E2SetupRequestMessage) (bool, error) {
214 if nodebInfo.GetConnectionStatus() == entities.ConnectionStatus_SHUTTING_DOWN {
215 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s, connection status: %s - nodeB entity in incorrect state", ranName, nodebInfo.ConnectionStatus)
216 return false, errors.New("nodeB entity in incorrect state")
219 if nodebInfo.NodeType == entities.Node_ENB {
223 setupMessageRanFuncs := setupRequest.ExtractRanFunctionsList()
225 if setupMessageRanFuncs == nil || (len(setupMessageRanFuncs) == 0 && len(nodebInfo.GetGnb().RanFunctions) == 0) {
229 nodebInfo.GetGnb().RanFunctions = setupMessageRanFuncs
233 func (h *E2SetupRequestNotificationHandler) handleUnsuccessfulResponse(ranName string, req *models.NotificationRequest, cause models.Cause) {
234 failureResponse := models.NewE2SetupFailureResponseMessage(models.TimeToWaitEnum.V60s, cause)
235 h.logger.Debugf("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - E2_SETUP_RESPONSE has been built successfully %+v", failureResponse)
237 responsePayload, err := xml.Marshal(&failureResponse.E2APPDU)
239 h.logger.Warnf("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - RAN name: %s - Error marshalling RIC_E2_SETUP_RESP. Payload: %s", ranName, responsePayload)
242 responsePayload = replaceEmptyTagsWithSelfClosing(responsePayload)
244 h.logger.Infof("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - payload: %s", responsePayload)
245 msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_FAILURE, ranName, responsePayload, req.TransactionId, req.GetMsgSrc())
246 h.logger.Infof("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - RAN name: %s - RIC_E2_SETUP_RESP message has been built successfully. Message: %x", ranName, msg)
247 _ = h.rmrSender.WhSend(msg)
251 func (h *E2SetupRequestNotificationHandler) handleSuccessfulResponse(ranName string, req *models.NotificationRequest, setupRequest *models.E2SetupRequestMessage) {
253 plmnId := buildPlmnId(h.config.GlobalRicId.Mcc, h.config.GlobalRicId.Mnc)
255 ricNearRtId, err := convertTo20BitString(h.config.GlobalRicId.RicId)
259 successResponse := models.NewE2SetupSuccessResponseMessage(plmnId, ricNearRtId, setupRequest)
260 h.logger.Debugf("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - E2_SETUP_RESPONSE has been built successfully %+v", successResponse)
262 responsePayload, err := xml.Marshal(&successResponse.E2APPDU)
264 h.logger.Warnf("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - RAN name: %s - Error marshalling RIC_E2_SETUP_RESP. Payload: %s", ranName, responsePayload)
267 responsePayload = replaceEmptyTagsWithSelfClosing(responsePayload)
269 h.logger.Infof("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - payload: %s", responsePayload)
271 msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_RESP, ranName, responsePayload, req.TransactionId, req.GetMsgSrc())
272 h.logger.Infof("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - RAN name: %s - RIC_E2_SETUP_RESP message has been built successfully. Message: %x", ranName, msg)
273 _ = h.rmrSender.Send(msg)
276 func buildPlmnId(mmc string, mnc string) string {
277 var b strings.Builder
293 func replaceEmptyTagsWithSelfClosing(responsePayload []byte) []byte {
295 emptyTagVsSelfClosingTagPairs := make([]string, len(emptyTagsToReplaceToSelfClosingTags)*2)
299 for i := 0; i < len(emptyTagsToReplaceToSelfClosingTags); i++ {
300 emptyTagVsSelfClosingTagPairs[j] = fmt.Sprintf("<%[1]s></%[1]s>", emptyTagsToReplaceToSelfClosingTags[i])
301 emptyTagVsSelfClosingTagPairs[j+1] = fmt.Sprintf("<%s/>", emptyTagsToReplaceToSelfClosingTags[i])
304 responseString := strings.NewReplacer(emptyTagVsSelfClosingTagPairs...).Replace(string(responsePayload))
305 return []byte(responseString)
308 func convertTo20BitString(ricNearRtId string) (string, error) {
309 r, err := strconv.ParseUint(ricNearRtId, 16, 32)
313 return fmt.Sprintf("%020b", r)[:20], nil
316 func (h *E2SetupRequestNotificationHandler) parseSetupRequest(payload []byte) (*models.E2SetupRequestMessage, string, error) {
318 pipInd := bytes.IndexByte(payload, '|')
320 return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Error parsing E2 Setup Request failed extract Payload: no | separator found")
323 e2tIpAddress := string(payload[:pipInd])
324 if len(e2tIpAddress) == 0 {
325 return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Empty E2T Address received")
328 h.logger.Infof("#E2SetupRequestNotificationHandler.parseSetupRequest - payload: %s", payload[pipInd+1:])
330 setupRequest := &models.E2SetupRequestMessage{}
331 err := xml.Unmarshal(normalizeXml(payload[pipInd+1:]), &setupRequest.E2APPDU)
333 return nil, "", errors.New(fmt.Sprintf("#E2SetupRequestNotificationHandler.parseSetupRequest - Error unmarshalling E2 Setup Request payload: %x", payload))
336 return setupRequest, e2tIpAddress, nil
339 func normalizeXml(payload []byte) []byte {
340 xmlStr := string(payload)
341 normalized := strings.NewReplacer("<", "<", ">", ">").Replace(xmlStr)
342 return []byte(normalized)
345 func (h *E2SetupRequestNotificationHandler) buildNodebInfo(ranName string, e2tAddress string, request *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
346 nodebInfo := &entities.NodebInfo{
347 AssociatedE2TInstanceAddress: e2tAddress,
349 GlobalNbId: h.buildGlobalNbId(request),
350 SetupFromNetwork: true,
352 err := h.setNodeTypeAndConfiguration(nodebInfo)
357 if nodebInfo.NodeType == entities.Node_ENB {
358 return nodebInfo, nil
361 ranFuncs := request.ExtractRanFunctionsList()
364 nodebInfo.GetGnb().RanFunctions = ranFuncs
367 return nodebInfo, nil
370 func (h *E2SetupRequestNotificationHandler) setNodeTypeAndConfiguration(nodebInfo *entities.NodebInfo) error {
371 for k, v := range gnbTypesMap {
372 if strings.HasPrefix(nodebInfo.RanName, k) {
373 nodebInfo.NodeType = entities.Node_GNB
374 nodebInfo.Configuration = &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{GnbType: v}}
378 for k, v := range enbTypesMap {
379 if strings.HasPrefix(nodebInfo.RanName, k) {
380 nodebInfo.NodeType = entities.Node_ENB
381 nodebInfo.Configuration = &entities.NodebInfo_Enb{Enb: &entities.Enb{EnbType: v}}
386 return e2managererrors.NewUnknownSetupRequestRanNameError(nodebInfo.RanName)
389 func (h *E2SetupRequestNotificationHandler) buildGlobalNbId(setupRequest *models.E2SetupRequestMessage) *entities.GlobalNbId {
390 return &entities.GlobalNbId{
391 PlmnId: setupRequest.GetPlmnId(),
392 NbId: setupRequest.GetNbId(),
396 func (h *E2SetupRequestNotificationHandler) buildNbIdentity(ranName string, setupRequest *models.E2SetupRequestMessage) *entities.NbIdentity {
397 return &entities.NbIdentity{
398 InventoryName: ranName,
399 GlobalNbId: h.buildGlobalNbId(setupRequest),