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"
40 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
41 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
45 emptyTagsToReplaceToSelfClosingTags = []string{"reject", "ignore", "transport-resource-unavailable", "om-intervention", "request-id-unknown",
46 "v60s", "v20s", "v10s", "v5s", "v2s", "v1s", "ng", "xn", "e1", "f1", "w1", "s1", "x2", "success", "failure"}
47 gnbTypesMap = map[string]entities.GnbType{
48 "gnb": entities.GnbType_GNB,
49 "en_gnb": entities.GnbType_EN_GNB,
51 enbTypesMap = map[string]entities.EnbType{
52 "enB_macro": entities.EnbType_MACRO_ENB,
53 "enB_home": entities.EnbType_HOME_ENB,
54 "enB_shortmacro": entities.EnbType_SHORT_MACRO_ENB,
55 "enB_longmacro": entities.EnbType_LONG_MACRO_ENB,
56 "ng_enB_macro": entities.EnbType_MACRO_NG_ENB,
57 "ng_enB_shortmacro": entities.EnbType_SHORT_MACRO_NG_ENB,
58 "ng_enB_longmacro": entities.EnbType_LONG_MACRO_NG_ENB,
62 type E2SetupRequestNotificationHandler struct {
64 config *configuration.Configuration
65 e2tInstancesManager managers.IE2TInstancesManager
66 rmrSender *rmrsender.RmrSender
67 rNibDataService services.RNibDataService
68 e2tAssociationManager *managers.E2TAssociationManager
69 ranConnectStatusChangeManager managers.IRanConnectStatusChangeManager
70 ranListManager managers.RanListManager
73 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 {
74 return &E2SetupRequestNotificationHandler{
77 e2tInstancesManager: e2tInstancesManager,
79 rNibDataService: rNibDataService,
80 e2tAssociationManager: e2tAssociationManager,
81 ranConnectStatusChangeManager: ranConnectStatusChangeManager,
82 ranListManager: ranListManager,
86 func (h *E2SetupRequestNotificationHandler) Handle(request *models.NotificationRequest) {
87 ranName := request.RanName
88 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - received E2_SETUP_REQUEST. Payload: %x", ranName, request.Payload)
90 generalConfiguration, err := h.rNibDataService.GetGeneralConfiguration()
93 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - Failed retrieving e2m general configuration - quitting e2 setup flow. error: %s", err)
97 setupRequest, e2tIpAddress, err := h.parseSetupRequest(request.Payload)
99 h.logger.Errorf(err.Error())
103 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - E2T Address: %s - handling E2_SETUP_REQUEST", e2tIpAddress)
104 h.logger.Debugf("#E2SetupRequestNotificationHandler.Handle - E2_SETUP_REQUEST has been parsed successfully %+v", setupRequest)
106 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - got general configuration from rnib - enableRic: %t", generalConfiguration.EnableRic)
108 if !generalConfiguration.EnableRic {
109 cause := models.Cause{Misc: &models.CauseMisc{OmIntervention: &struct{}{}}}
110 h.handleUnsuccessfulResponse(ranName, request, cause, setupRequest)
114 _, err = h.e2tInstancesManager.GetE2TInstance(e2tIpAddress)
117 h.logger.Errorf("#E2TermInitNotificationHandler.Handle - Failed retrieving E2TInstance. error: %s", err)
121 nodebInfo, err := h.rNibDataService.GetNodeb(ranName)
123 var functionsModified bool
127 if _, ok := err.(*common.ResourceNotFoundError); !ok {
128 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to retrieve nodebInfo entity. Error: %s", ranName, err)
132 if nodebInfo, err = h.handleNewRan(ranName, e2tIpAddress, setupRequest); err != nil {
133 if _, ok := err.(*e2managererrors.UnknownSetupRequestRanNameError); ok {
134 cause := models.Cause{RicRequest: &models.CauseRic{RequestIdUnknown: &struct{}{}}}
135 h.handleUnsuccessfulResponse(ranName, request, cause, setupRequest)
142 functionsModified, err = h.handleExistingRan(ranName, nodebInfo, setupRequest)
149 ranStatusChangePublished, err := h.e2tAssociationManager.AssociateRan(e2tIpAddress, nodebInfo)
153 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to associate E2T to nodeB entity. Error: %s", ranName, err)
154 if _, ok := err.(*e2managererrors.RoutingManagerError); ok {
156 if err = h.handleUpdateAndPublishNodebInfo(functionsModified, ranStatusChangePublished, nodebInfo); err != nil {
160 cause := models.Cause{Transport: &models.CauseTransport{TransportResourceUnavailable: &struct{}{}}}
161 h.handleUnsuccessfulResponse(nodebInfo.RanName, request, cause, setupRequest)
166 if err = h.handleUpdateAndPublishNodebInfo(functionsModified, ranStatusChangePublished, nodebInfo); err != nil {
170 h.handleSuccessfulResponse(ranName, request, setupRequest)
173 func (h *E2SetupRequestNotificationHandler) handleUpdateAndPublishNodebInfo(functionsModified bool, ranStatusChangePublished bool, nodebInfo *entities.NodebInfo) error {
175 if ranStatusChangePublished || !functionsModified {
179 err := h.rNibDataService.UpdateNodebInfoAndPublish(nodebInfo)
182 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleUpdateAndPublishNodebInfo - RAN name: %s - Failed at UpdateNodebInfoAndPublish. error: %s", nodebInfo.RanName, err)
186 h.logger.Infof("#E2SetupRequestNotificationHandler.handleUpdateAndPublishNodebInfo - RAN name: %s - Successfully executed UpdateNodebInfoAndPublish", nodebInfo.RanName)
191 func (h *E2SetupRequestNotificationHandler) handleNewRan(ranName string, e2tIpAddress string, setupRequest *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
193 nodebInfo, err := h.buildNodebInfo(ranName, e2tIpAddress, setupRequest)
195 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleNewRan - RAN name: %s - failed building nodebInfo. Error: %s", ranName, err)
199 err = h.rNibDataService.SaveNodeb(nodebInfo)
201 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleNewRan - RAN name: %s - failed saving nodebInfo. Error: %s", ranName, err)
205 nbIdentity := h.buildNbIdentity(ranName, setupRequest)
207 err = h.ranListManager.AddNbIdentity(nodebInfo.GetNodeType(), nbIdentity)
213 return nodebInfo, nil
216 func (h *E2SetupRequestNotificationHandler) handleExistingRan(ranName string, nodebInfo *entities.NodebInfo, setupRequest *models.E2SetupRequestMessage) (bool, error) {
217 if nodebInfo.GetConnectionStatus() == entities.ConnectionStatus_SHUTTING_DOWN {
218 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s, connection status: %s - nodeB entity in incorrect state", ranName, nodebInfo.ConnectionStatus)
219 return false, errors.New("nodeB entity in incorrect state")
222 nodebInfo.SetupFromNetwork = true
224 e2NodeConfig := setupRequest.ExtractE2NodeConfigList()
225 if e2NodeConfig == nil {
226 return false, errors.New("Empty E2nodeComponentConfigAddition-List")
229 if nodebInfo.NodeType == entities.Node_ENB {
230 if len(e2NodeConfig) == 0 && len(nodebInfo.GetEnb().GetNodeConfigs()) == 0 {
231 return false, errors.New("Empty E2nodeComponentConfigAddition-List")
233 nodebInfo.GetEnb().NodeConfigs = e2NodeConfig
238 if len(e2NodeConfig) == 0 && len(nodebInfo.GetGnb().GetNodeConfigs()) == 0 {
239 return false, errors.New("Empty E2nodeComponentConfigAddition-List")
241 nodebInfo.GetGnb().NodeConfigs = e2NodeConfig
243 setupMessageRanFuncs := setupRequest.ExtractRanFunctionsList()
245 if setupMessageRanFuncs == nil || (len(setupMessageRanFuncs) == 0 && len(nodebInfo.GetGnb().RanFunctions) == 0) {
249 nodebInfo.GetGnb().RanFunctions = setupMessageRanFuncs
253 func (h *E2SetupRequestNotificationHandler) handleUnsuccessfulResponse(ranName string, req *models.NotificationRequest, cause models.Cause, setupRequest *models.E2SetupRequestMessage) {
254 failureResponse := models.NewE2SetupFailureResponseMessage(models.TimeToWaitEnum.V60s, cause, setupRequest)
255 h.logger.Debugf("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - E2_SETUP_RESPONSE has been built successfully %+v", failureResponse)
257 responsePayload, err := xml.Marshal(&failureResponse.E2APPDU)
259 h.logger.Warnf("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - RAN name: %s - Error marshalling RIC_E2_SETUP_RESP. Payload: %s", ranName, responsePayload)
262 responsePayload = utils.ReplaceEmptyTagsWithSelfClosing(responsePayload, emptyTagsToReplaceToSelfClosingTags)
264 h.logger.Infof("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - payload: %s", responsePayload)
265 msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_FAILURE, ranName, responsePayload, req.TransactionId, req.GetMsgSrc())
266 h.logger.Infof("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - RAN name: %s - RIC_E2_SETUP_RESP message has been built successfully. Message: %x", ranName, msg)
267 _ = h.rmrSender.WhSend(msg)
271 func (h *E2SetupRequestNotificationHandler) handleSuccessfulResponse(ranName string, req *models.NotificationRequest, setupRequest *models.E2SetupRequestMessage) {
273 plmnId := buildPlmnId(h.config.GlobalRicId.Mcc, h.config.GlobalRicId.Mnc)
275 ricNearRtId, err := convertTo20BitString(h.config.GlobalRicId.RicId)
279 successResponse := models.NewE2SetupSuccessResponseMessage(plmnId, ricNearRtId, setupRequest)
280 h.logger.Debugf("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - E2_SETUP_RESPONSE has been built successfully %+v", successResponse)
282 responsePayload, err := xml.Marshal(&successResponse.E2APPDU)
284 h.logger.Warnf("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - RAN name: %s - Error marshalling RIC_E2_SETUP_RESP. Payload: %s", ranName, responsePayload)
287 responsePayload = utils.ReplaceEmptyTagsWithSelfClosing(responsePayload, emptyTagsToReplaceToSelfClosingTags)
289 h.logger.Infof("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - payload: %s", responsePayload)
291 msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_RESP, ranName, responsePayload, req.TransactionId, req.GetMsgSrc())
292 h.logger.Infof("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - RAN name: %s - RIC_E2_SETUP_RESP message has been built successfully. Message: %x", ranName, msg)
293 _ = h.rmrSender.Send(msg)
296 func buildPlmnId(mmc string, mnc string) string {
297 var b strings.Builder
313 func convertTo20BitString(ricNearRtId string) (string, error) {
314 r, err := strconv.ParseUint(ricNearRtId, 16, 32)
318 return fmt.Sprintf("%020b", r)[:20], nil
321 func (h *E2SetupRequestNotificationHandler) parseSetupRequest(payload []byte) (*models.E2SetupRequestMessage, string, error) {
323 pipInd := bytes.IndexByte(payload, '|')
325 return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Error parsing E2 Setup Request failed extract Payload: no | separator found")
328 e2tIpAddress := string(payload[:pipInd])
329 if len(e2tIpAddress) == 0 {
330 return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Empty E2T Address received")
333 h.logger.Infof("#E2SetupRequestNotificationHandler.parseSetupRequest - payload: %s", payload[pipInd+1:])
335 setupRequest := &models.E2SetupRequestMessage{}
336 err := xml.Unmarshal(utils.NormalizeXml(payload[pipInd+1:]), &setupRequest.E2APPDU)
338 return nil, "", errors.New(fmt.Sprintf("#E2SetupRequestNotificationHandler.parseSetupRequest - Error unmarshalling E2 Setup Request payload: %x", payload))
341 return setupRequest, e2tIpAddress, nil
344 func (h *E2SetupRequestNotificationHandler) buildNodebInfo(ranName string, e2tAddress string, request *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
345 nodebInfo := &entities.NodebInfo{
346 AssociatedE2TInstanceAddress: e2tAddress,
348 GlobalNbId: h.buildGlobalNbId(request),
349 SetupFromNetwork: true,
351 err := h.setNodeTypeAndConfiguration(nodebInfo)
356 e2NodeConfig := request.ExtractE2NodeConfigList()
357 if e2NodeConfig == nil {
358 return nil, errors.New("Empty E2nodeComponentConfigAddition-List")
361 if nodebInfo.NodeType == entities.Node_ENB {
362 if len(e2NodeConfig) == 0 && len(nodebInfo.GetEnb().GetNodeConfigs()) == 0 {
363 return nil, errors.New("Empty E2nodeComponentConfigAddition-List")
365 nodebInfo.GetEnb().NodeConfigs = e2NodeConfig
367 return nodebInfo, nil
370 if len(e2NodeConfig) == 0 && len(nodebInfo.GetGnb().GetNodeConfigs()) == 0 {
371 return nil, errors.New("Empty E2nodeComponentConfigAddition-List")
373 nodebInfo.GetGnb().NodeConfigs = e2NodeConfig
375 ranFuncs := request.ExtractRanFunctionsList()
378 nodebInfo.GetGnb().RanFunctions = ranFuncs
381 return nodebInfo, nil
384 func (h *E2SetupRequestNotificationHandler) setNodeTypeAndConfiguration(nodebInfo *entities.NodebInfo) error {
385 for k, v := range gnbTypesMap {
386 if strings.HasPrefix(nodebInfo.RanName, k) {
387 nodebInfo.NodeType = entities.Node_GNB
388 nodebInfo.Configuration = &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{GnbType: v}}
392 for k, v := range enbTypesMap {
393 if strings.HasPrefix(nodebInfo.RanName, k) {
394 nodebInfo.NodeType = entities.Node_ENB
395 nodebInfo.Configuration = &entities.NodebInfo_Enb{Enb: &entities.Enb{EnbType: v}}
400 return e2managererrors.NewUnknownSetupRequestRanNameError(nodebInfo.RanName)
403 func (h *E2SetupRequestNotificationHandler) buildGlobalNbId(setupRequest *models.E2SetupRequestMessage) *entities.GlobalNbId {
404 return &entities.GlobalNbId{
405 PlmnId: setupRequest.GetPlmnId(),
406 NbId: setupRequest.GetNbId(),
410 func (h *E2SetupRequestNotificationHandler) buildNbIdentity(ranName string, setupRequest *models.E2SetupRequestMessage) *entities.NbIdentity {
411 return &entities.NbIdentity{
412 InventoryName: ranName,
413 GlobalNbId: h.buildGlobalNbId(setupRequest),