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.
21 "e2mgr/e2managererrors"
28 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
31 type RanSetupManager struct {
33 rnibWriterProvider func() rNibWriter.RNibWriter
34 rmrService *services.RmrService
37 func NewRanSetupManager(logger *logger.Logger, rmrService *services.RmrService, rnibWriterProvider func() rNibWriter.RNibWriter) *RanSetupManager {
38 return &RanSetupManager{
40 rnibWriterProvider: rnibWriterProvider,
41 rmrService: rmrService,
45 // Update retries and connection status (connecting)
46 func (m *RanSetupManager) updateConnectionStatusConnecting(nodebInfo *entities.NodebInfo) error {
47 // Update retries and connection status (connecting)
48 nodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
49 nodebInfo.ConnectionAttempts++
50 err := m.rnibWriterProvider().UpdateNodebInfo(nodebInfo)
52 m.logger.Errorf("#RanSetupManager.updateConnectionStatusConnecting - failed to update RAN's connection status to CONNECTING: %s", err)
54 m.logger.Infof("#RanSetupManager.updateConnectionStatusConnecting - successfully updated RAN's connection status to CONNECTING")
59 // Decrement retries and connection status (disconnected)
60 func (m *RanSetupManager) updateConnectionStatusDisconnected(nodebInfo *entities.NodebInfo) error {
61 // Update retries and connection status (connecting)
62 nodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
63 nodebInfo.ConnectionAttempts--
64 err := m.rnibWriterProvider().UpdateNodebInfo(nodebInfo)
66 m.logger.Errorf("#RanSetupManager.updateConnectionStatusDisconnected - failed to update RAN's connection status to DISCONNECTED : %s", err)
68 m.logger.Errorf("#RanSetupManager.updateConnectionStatusDisconnected - successfully updated RAN's connection status to DISCONNECTED")
73 func (m *RanSetupManager) prepareSetupRequest(nodebInfo *entities.NodebInfo) (int, *models.E2RequestMessage, error) {
74 // Build the endc/x2 setup request
75 switch nodebInfo.E2ApplicationProtocol {
76 case entities.E2ApplicationProtocol_X2_SETUP_REQUEST:
77 rmrMsgType := rmrCgo.RIC_X2_SETUP_REQ
78 request := models.NewE2RequestMessage(nodebInfo.RanName /*tid*/, nodebInfo.Ip, uint16(nodebInfo.Port), nodebInfo.RanName, e2pdus.PackedX2setupRequest)
79 return rmrMsgType, request, nil
80 case entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST:
81 rmrMsgType := rmrCgo.RIC_ENDC_X2_SETUP_REQ
82 request := models.NewE2RequestMessage(nodebInfo.RanName /*tid*/, nodebInfo.Ip, uint16(nodebInfo.Port), nodebInfo.RanName, e2pdus.PackedEndcX2setupRequest)
83 return rmrMsgType, request, nil
86 m.logger.Errorf("#RanSetupManager.ExecuteSetup - unsupported nodebInfo.E2ApplicationProtocol %d ", nodebInfo.E2ApplicationProtocol)
87 return 0, nil, e2managererrors.NewInternalError()
90 // ExecuteSetup updates the connection status and number of attempts in the nodebInfo and send an endc/x2 setup request to establish a connection with the RAN
91 func (m *RanSetupManager) ExecuteSetup(nodebInfo *entities.NodebInfo) error {
93 // Update retries and connection status (connecting)
94 if err := m.updateConnectionStatusConnecting(nodebInfo); err != nil {
95 return e2managererrors.NewRnibDbError()
98 // Build the endc/x2 setup request
99 rmrMsgType, request, err := m.prepareSetupRequest(nodebInfo)
104 // Send the endc/x2 setup request
105 response := &models.NotificationResponse{MgsType: rmrMsgType, RanName: nodebInfo.RanName, Payload: request.GetMessageAsBytes(m.logger)}
106 if err := m.rmrService.SendRmrMessage(response); err != nil {
107 m.logger.Errorf("#RanSetupManager.ExecuteSetup - failed to send setup request to RMR: %s", err)
109 // Decrement retries and connection status (disconnected)
110 if err := m.updateConnectionStatusDisconnected(nodebInfo); err != nil {
111 return e2managererrors.NewRnibDbError()
114 return e2managererrors.NewRmrError()