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"
41 type E2SetupRequestNotificationHandler struct {
43 config *configuration.Configuration
44 e2tInstancesManager managers.IE2TInstancesManager
45 rmrSender *rmrsender.RmrSender
46 rNibDataService services.RNibDataService
47 e2tAssociationManager *managers.E2TAssociationManager
50 func NewE2SetupRequestNotificationHandler(logger *logger.Logger, config *configuration.Configuration, e2tInstancesManager managers.IE2TInstancesManager, rmrSender *rmrsender.RmrSender, rNibDataService services.RNibDataService, e2tAssociationManager *managers.E2TAssociationManager) E2SetupRequestNotificationHandler {
51 return E2SetupRequestNotificationHandler{
54 e2tInstancesManager: e2tInstancesManager,
56 rNibDataService: rNibDataService,
57 e2tAssociationManager: e2tAssociationManager,
61 func (h E2SetupRequestNotificationHandler) Handle(request *models.NotificationRequest) {
62 ranName := request.RanName
63 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - received E2_SETUP_REQUEST. Payload: %x", ranName, request.Payload)
65 setupRequest, e2tIpAddress, err := h.parseSetupRequest(request.Payload)
67 h.logger.Errorf(err.Error())
71 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - E2T Address: %s - handling E2_SETUP_REQUEST", e2tIpAddress)
72 h.logger.Debugf("#E2SetupRequestNotificationHandler.Handle - E2_SETUP_REQUEST has been parsed successfully %+v", setupRequest)
74 _, err = h.e2tInstancesManager.GetE2TInstance(e2tIpAddress)
77 h.logger.Errorf("#E2TermInitNotificationHandler.Handle - Failed retrieving E2TInstance. error: %s", err)
81 nodebInfo, err := h.rNibDataService.GetNodeb(ranName)
85 if _, ok := err.(*common.ResourceNotFoundError); !ok {
86 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to retrieve nodebInfo entity. Error: %s", ranName, err)
91 if nodebInfo, err = h.handleNewRan(ranName, e2tIpAddress, setupRequest); err != nil {
96 if err = h.handleExistingRan(ranName, nodebInfo, setupRequest); err != nil {
101 err = h.e2tAssociationManager.AssociateRan(e2tIpAddress, nodebInfo)
105 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to associate E2T to nodeB entity. Error: %s", ranName, err)
106 if _, ok := err.(*e2managererrors.RoutingManagerError); ok {
107 h.handleUnsuccessfulResponse(nodebInfo, request)
112 h.handleSuccessfulResponse(ranName, request, setupRequest)
115 func (h E2SetupRequestNotificationHandler) handleNewRan(ranName string, e2tIpAddress string, setupRequest *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
117 nodebInfo, err := h.buildNodebInfo(ranName, e2tIpAddress, setupRequest)
120 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleNewRan - RAN name: %s - failed to build nodebInfo entity. Error: %s", ranName, err)
124 nbIdentity := h.buildNbIdentity(ranName, setupRequest)
125 err = h.rNibDataService.SaveNodeb(nbIdentity, nodebInfo)
128 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleNewRan - RAN name: %s - failed to save nodebInfo entity. Error: %s", ranName, err)
132 return nodebInfo, nil
135 func (h E2SetupRequestNotificationHandler) setGnbFunctions(nodebInfo *entities.NodebInfo, setupRequest *models.E2SetupRequestMessage) error {
136 ranFunctions, err := setupRequest.ExtractRanFunctionsList()
139 h.logger.Errorf("#E2SetupRequestNotificationHandler.setGnbFunctions - RAN name: %s - failed to update nodebInfo entity. Error: %s", nodebInfo.GetRanName(), err)
143 if ranFunctions != nil {
144 nodebInfo.GetGnb().RanFunctions = ranFunctions
150 func (h E2SetupRequestNotificationHandler) handleExistingRan(ranName string, nodebInfo *entities.NodebInfo, setupRequest *models.E2SetupRequestMessage) error {
151 if nodebInfo.GetConnectionStatus() == entities.ConnectionStatus_SHUTTING_DOWN {
152 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s, connection status: %s - nodeB entity in incorrect state", ranName, nodebInfo.ConnectionStatus)
153 return errors.New("nodeB entity in incorrect state")
156 err := h.setGnbFunctions(nodebInfo, setupRequest)
160 func (h E2SetupRequestNotificationHandler) handleUnsuccessfulResponse(nodebInfo *entities.NodebInfo, req *models.NotificationRequest) {
161 failureResponse := models.NewE2SetupFailureResponseMessage(models.TimeToWaitEnum.V60s)
162 h.logger.Debugf("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - E2_SETUP_RESPONSE has been built successfully %+v", failureResponse)
164 responsePayload, err := xml.Marshal(&failureResponse.E2APPDU)
166 h.logger.Warnf("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - RAN name: %s - Error marshalling RIC_E2_SETUP_RESP. Payload: %s", nodebInfo.RanName, responsePayload)
169 responsePayload = replaceEmptyTagsWithSelfClosing(responsePayload)
171 msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_FAILURE, nodebInfo.RanName, responsePayload, req.TransactionId, req.GetMsgSrc())
172 h.logger.Infof("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - RAN name: %s - RIC_E2_SETUP_RESP message has been built successfully. Message: %x", nodebInfo.RanName, msg)
173 _ = h.rmrSender.WhSend(msg)
177 func (h E2SetupRequestNotificationHandler) handleSuccessfulResponse(ranName string, req *models.NotificationRequest, setupRequest *models.E2SetupRequestMessage) {
179 ricNearRtId, err := convertTo20BitString(h.config.GlobalRicId.RicNearRtId)
181 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - RAN name: %s - failed to convert RicNearRtId value %s to 20 bit string . Error: %s", ranName, h.config.GlobalRicId.RicNearRtId, err)
184 successResponse := models.NewE2SetupSuccessResponseMessage(h.config.GlobalRicId.PlmnId, ricNearRtId, setupRequest)
185 h.logger.Debugf("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - E2_SETUP_RESPONSE has been built successfully %+v", successResponse)
187 responsePayload, err := xml.Marshal(&successResponse.E2APPDU)
189 h.logger.Warnf("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - RAN name: %s - Error marshalling RIC_E2_SETUP_RESP. Payload: %s", ranName, responsePayload)
192 responsePayload = replaceEmptyTagsWithSelfClosing(responsePayload)
194 msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_RESP, ranName, responsePayload, req.TransactionId, req.GetMsgSrc())
195 h.logger.Infof("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - RAN name: %s - RIC_E2_SETUP_RESP message has been built successfully. Message: %x", ranName, msg)
196 _ = h.rmrSender.Send(msg)
199 func replaceEmptyTagsWithSelfClosing(responsePayload []byte) []byte {
200 responseString := strings.NewReplacer(
201 "<reject></reject>", "<reject/>",
202 "<ignore></ignore>", "<ignore/>",
203 "<transport-resource-unavailable></transport-resource-unavailable>", "<transport-resource-unavailable/>",
204 "<v60s></v60s>", "<v60s/>",
205 "<v20s></v20s>", "<v20s/>",
206 "<v10s></v10s>", "<v10s/>",
207 "<v5s></v5s>", "<v5s/>",
208 "<v2s></v2s>", "<v2s/>",
209 "<v1s></v1s>", "<v1s/>",
210 ).Replace(string(responsePayload))
211 return []byte(responseString)
214 func convertTo20BitString(ricNearRtId string) (string, error) {
215 r, err := strconv.ParseUint(ricNearRtId, 16, 32)
219 return fmt.Sprintf("%020b", r)[:20], nil
222 func (h E2SetupRequestNotificationHandler) parseSetupRequest(payload []byte) (*models.E2SetupRequestMessage, string, error) {
224 pipInd := bytes.IndexByte(payload, '|')
226 return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Error parsing E2 Setup Request failed extract Payload: no | separator found")
229 e2tIpAddress := string(payload[:pipInd])
230 if len(e2tIpAddress) == 0 {
231 return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Empty E2T Address received")
234 h.logger.Infof("#E2SetupRequestNotificationHandler.parseSetupRequest - payload: %s", payload[pipInd+1:])
236 setupRequest := &models.E2SetupRequestMessage{}
237 err := xml.Unmarshal(payload[pipInd+1:], &setupRequest.E2APPDU)
239 return nil, "", errors.New(fmt.Sprintf("#E2SetupRequestNotificationHandler.parseSetupRequest - Error unmarshalling E2 Setup Request payload: %x", payload))
242 return setupRequest, e2tIpAddress, nil
245 func (h E2SetupRequestNotificationHandler) buildNodebInfo(ranName string, e2tAddress string, request *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
248 nodebInfo := &entities.NodebInfo{
249 AssociatedE2TInstanceAddress: e2tAddress,
250 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
252 NodeType: entities.Node_GNB,
253 Configuration: &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{}},
254 GlobalNbId: h.buildGlobalNbId(request),
257 err = h.setGnbFunctions(nodebInfo, request)
258 return nodebInfo, err
261 func (h E2SetupRequestNotificationHandler) buildGlobalNbId(setupRequest *models.E2SetupRequestMessage) *entities.GlobalNbId {
262 return &entities.GlobalNbId{
263 PlmnId: setupRequest.GetPlmnId(),
264 NbId: setupRequest.GetNbId(),
268 func (h E2SetupRequestNotificationHandler) buildNbIdentity(ranName string, setupRequest *models.E2SetupRequestMessage) *entities.NbIdentity {
269 return &entities.NbIdentity{
270 InventoryName: ranName,
271 GlobalNbId: h.buildGlobalNbId(setupRequest),