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",
43 "v60s", "v20s", "v10s", "v5s", "v2s", "v1s"}
46 type E2SetupRequestNotificationHandler struct {
48 config *configuration.Configuration
49 e2tInstancesManager managers.IE2TInstancesManager
50 rmrSender *rmrsender.RmrSender
51 rNibDataService services.RNibDataService
52 e2tAssociationManager *managers.E2TAssociationManager
53 ranConnectStatusChangeManager managers.IRanConnectStatusChangeManager
56 func NewE2SetupRequestNotificationHandler(logger *logger.Logger, config *configuration.Configuration, e2tInstancesManager managers.IE2TInstancesManager, rmrSender *rmrsender.RmrSender, rNibDataService services.RNibDataService, e2tAssociationManager *managers.E2TAssociationManager, ranConnectStatusChangeManager managers.IRanConnectStatusChangeManager) *E2SetupRequestNotificationHandler {
57 return &E2SetupRequestNotificationHandler{
60 e2tInstancesManager: e2tInstancesManager,
62 rNibDataService: rNibDataService,
63 e2tAssociationManager: e2tAssociationManager,
64 ranConnectStatusChangeManager: ranConnectStatusChangeManager,
68 func (h *E2SetupRequestNotificationHandler) Handle(request *models.NotificationRequest) {
69 ranName := request.RanName
70 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - received E2_SETUP_REQUEST. Payload: %x", ranName, request.Payload)
72 generalConfiguration, err := h.rNibDataService.GetGeneralConfiguration()
75 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - Failed retrieving e2m general configuration - quitting e2 setup flow. error: %s", err)
79 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - got general configuration from rnib - enableRic: %t", generalConfiguration.EnableRic)
81 if !generalConfiguration.EnableRic {
82 cause := models.Cause{Misc: &models.CauseMisc{OmIntervention: &struct{}{}}}
83 h.handleUnsuccessfulResponse(ranName, request, cause)
87 setupRequest, e2tIpAddress, err := h.parseSetupRequest(request.Payload)
89 h.logger.Errorf(err.Error())
93 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - E2T Address: %s - handling E2_SETUP_REQUEST", e2tIpAddress)
94 h.logger.Debugf("#E2SetupRequestNotificationHandler.Handle - E2_SETUP_REQUEST has been parsed successfully %+v", setupRequest)
96 _, err = h.e2tInstancesManager.GetE2TInstance(e2tIpAddress)
99 h.logger.Errorf("#E2TermInitNotificationHandler.Handle - Failed retrieving E2TInstance. error: %s", err)
103 nodebInfo, err := h.rNibDataService.GetNodeb(ranName)
107 if _, ok := err.(*common.ResourceNotFoundError); !ok {
108 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to retrieve nodebInfo entity. Error: %s", ranName, err)
113 if nodebInfo, err = h.handleNewRan(ranName, e2tIpAddress, setupRequest); err != nil {
118 if err = h.handleExistingRan(ranName, nodebInfo, setupRequest); err != nil {
123 err = h.e2tAssociationManager.AssociateRan(e2tIpAddress, nodebInfo)
127 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to associate E2T to nodeB entity. Error: %s", ranName, err)
128 if _, ok := err.(*e2managererrors.RoutingManagerError); ok {
129 cause := models.Cause{Transport: &models.CauseTransport{TransportResourceUnavailable: &struct{}{}}}
130 h.handleUnsuccessfulResponse(nodebInfo.RanName, request, cause)
135 h.handleSuccessfulResponse(ranName, request, setupRequest)
138 func (h *E2SetupRequestNotificationHandler) handleNewRan(ranName string, e2tIpAddress string, setupRequest *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
140 nodebInfo, err := h.buildNodebInfo(ranName, e2tIpAddress, setupRequest)
143 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleNewRan - RAN name: %s - failed to build nodebInfo entity. Error: %s", ranName, err)
147 nbIdentity := h.buildNbIdentity(ranName, setupRequest)
148 err = h.rNibDataService.SaveNodeb(nbIdentity, nodebInfo)
151 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleNewRan - RAN name: %s - failed to save nodebInfo entity. Error: %s", ranName, err)
155 return nodebInfo, nil
158 func (h *E2SetupRequestNotificationHandler) setGnbFunctions(nodebInfo *entities.NodebInfo, setupRequest *models.E2SetupRequestMessage) {
159 ranFunctions := setupRequest.ExtractRanFunctionsList()
161 if ranFunctions != nil {
162 nodebInfo.GetGnb().RanFunctions = ranFunctions
166 func (h *E2SetupRequestNotificationHandler) handleExistingRan(ranName string, nodebInfo *entities.NodebInfo, setupRequest *models.E2SetupRequestMessage) error {
167 if nodebInfo.GetConnectionStatus() == entities.ConnectionStatus_SHUTTING_DOWN {
168 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s, connection status: %s - nodeB entity in incorrect state", ranName, nodebInfo.ConnectionStatus)
169 return errors.New("nodeB entity in incorrect state")
172 h.setGnbFunctions(nodebInfo, setupRequest)
174 return h.rNibDataService.UpdateNodebInfo(nodebInfo)
177 func (h *E2SetupRequestNotificationHandler) handleUnsuccessfulResponse(ranName string, req *models.NotificationRequest, cause models.Cause) {
178 failureResponse := models.NewE2SetupFailureResponseMessage(models.TimeToWaitEnum.V60s, cause)
179 h.logger.Debugf("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - E2_SETUP_RESPONSE has been built successfully %+v", failureResponse)
181 responsePayload, err := xml.Marshal(&failureResponse.E2APPDU)
183 h.logger.Warnf("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - RAN name: %s - Error marshalling RIC_E2_SETUP_RESP. Payload: %s", ranName, responsePayload)
186 responsePayload = replaceEmptyTagsWithSelfClosing(responsePayload)
188 h.logger.Infof("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - payload: %s", responsePayload)
189 msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_FAILURE, ranName, responsePayload, req.TransactionId, req.GetMsgSrc())
190 h.logger.Infof("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - RAN name: %s - RIC_E2_SETUP_RESP message has been built successfully. Message: %x", ranName, msg)
191 _ = h.rmrSender.WhSend(msg)
195 func (h *E2SetupRequestNotificationHandler) handleSuccessfulResponse(ranName string, req *models.NotificationRequest, setupRequest *models.E2SetupRequestMessage) {
197 plmnId := buildPlmnId(h.config.GlobalRicId.Mcc, h.config.GlobalRicId.Mnc)
199 ricNearRtId, err := convertTo20BitString(h.config.GlobalRicId.RicId)
203 successResponse := models.NewE2SetupSuccessResponseMessage(plmnId, ricNearRtId, setupRequest)
204 h.logger.Debugf("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - E2_SETUP_RESPONSE has been built successfully %+v", successResponse)
206 responsePayload, err := xml.Marshal(&successResponse.E2APPDU)
208 h.logger.Warnf("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - RAN name: %s - Error marshalling RIC_E2_SETUP_RESP. Payload: %s", ranName, responsePayload)
211 responsePayload = replaceEmptyTagsWithSelfClosing(responsePayload)
213 h.logger.Infof("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - payload: %s", responsePayload)
215 msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_RESP, ranName, responsePayload, req.TransactionId, req.GetMsgSrc())
216 h.logger.Infof("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - RAN name: %s - RIC_E2_SETUP_RESP message has been built successfully. Message: %x", ranName, msg)
217 _ = h.rmrSender.Send(msg)
220 func buildPlmnId(mmc string, mnc string) string {
221 var b strings.Builder
237 func replaceEmptyTagsWithSelfClosing(responsePayload []byte) []byte {
239 emptyTagVsSelfClosingTagPairs := make([]string, len(emptyTagsToReplaceToSelfClosingTags)*2)
243 for i := 0; i < len(emptyTagsToReplaceToSelfClosingTags); i++ {
244 emptyTagVsSelfClosingTagPairs[j] = fmt.Sprintf("<%[1]s></%[1]s>", emptyTagsToReplaceToSelfClosingTags[i])
245 emptyTagVsSelfClosingTagPairs[j+1] = fmt.Sprintf("<%s/>", emptyTagsToReplaceToSelfClosingTags[i])
248 responseString := strings.NewReplacer(emptyTagVsSelfClosingTagPairs...).Replace(string(responsePayload))
249 return []byte(responseString)
252 func convertTo20BitString(ricNearRtId string) (string, error) {
253 r, err := strconv.ParseUint(ricNearRtId, 16, 32)
257 return fmt.Sprintf("%020b", r)[:20], nil
260 func (h *E2SetupRequestNotificationHandler) parseSetupRequest(payload []byte) (*models.E2SetupRequestMessage, string, error) {
262 pipInd := bytes.IndexByte(payload, '|')
264 return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Error parsing E2 Setup Request failed extract Payload: no | separator found")
267 e2tIpAddress := string(payload[:pipInd])
268 if len(e2tIpAddress) == 0 {
269 return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Empty E2T Address received")
272 h.logger.Infof("#E2SetupRequestNotificationHandler.parseSetupRequest - payload: %s", payload[pipInd+1:])
274 setupRequest := &models.E2SetupRequestMessage{}
275 err := xml.Unmarshal(normalizeXml(payload[pipInd+1:]), &setupRequest.E2APPDU)
277 return nil, "", errors.New(fmt.Sprintf("#E2SetupRequestNotificationHandler.parseSetupRequest - Error unmarshalling E2 Setup Request payload: %x", payload))
280 return setupRequest, e2tIpAddress, nil
283 func normalizeXml(payload []byte) []byte {
284 xmlStr := string(payload)
285 normalized := strings.NewReplacer("<", "<", ">", ">").Replace(xmlStr)
286 return []byte(normalized)
289 func (h *E2SetupRequestNotificationHandler) buildNodebInfo(ranName string, e2tAddress string, request *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
292 nodebInfo := &entities.NodebInfo{
293 AssociatedE2TInstanceAddress: e2tAddress,
295 NodeType: entities.Node_GNB,
296 Configuration: &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{}},
297 GlobalNbId: h.buildGlobalNbId(request),
300 h.setGnbFunctions(nodebInfo, request)
301 return nodebInfo, err
304 func (h *E2SetupRequestNotificationHandler) buildGlobalNbId(setupRequest *models.E2SetupRequestMessage) *entities.GlobalNbId {
305 return &entities.GlobalNbId{
306 PlmnId: setupRequest.GetPlmnId(),
307 NbId: setupRequest.GetNbId(),
311 func (h *E2SetupRequestNotificationHandler) buildNbIdentity(ranName string, setupRequest *models.E2SetupRequestMessage) *entities.NbIdentity {
312 return &entities.NbIdentity{
313 InventoryName: ranName,
314 GlobalNbId: h.buildGlobalNbId(setupRequest),