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,
46 // Update retries and connection status (connecting)
47 func (m *RanSetupManager) updateConnectionStatusConnecting(nodebInfo *entities.NodebInfo) error {
48 // Update retries and connection status (connecting)
49 nodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
50 nodebInfo.ConnectionAttempts++
51 err:= m.rnibWriterProvider().UpdateNodebInfo(nodebInfo)
53 m.logger.Errorf("#ran_setup_manager.updateConnectionStatusConnecting - failed to update RAN's connection status to CONNECTING: %s", err)
55 m.logger.Infof("#ran_setup_manager.updateConnectionStatusConnecting - successfully updated RAN's connection status to CONNECTING: %s", err)
60 // Decrement retries and connection status (disconnected)
61 func (m *RanSetupManager) updateConnectionStatusDisconnected(nodebInfo *entities.NodebInfo) error {
62 // Update retries and connection status (connecting)
63 nodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
64 nodebInfo.ConnectionAttempts--
65 err := m.rnibWriterProvider().UpdateNodebInfo(nodebInfo)
67 m.logger.Errorf("#ran_setup_manager.updateConnectionStatusDisconnected - failed to update RAN's connection status to DISCONNECTED : %s", err)
69 m.logger.Errorf("#ran_setup_manager.updateConnectionStatusDisconnected - successfully updated RAN's connection status to DISCONNECTED : %s", err)
74 func (m *RanSetupManager) prepareSetupRequest(nodebInfo *entities.NodebInfo) (int, *models.E2RequestMessage, error) {
75 // Build the endc/x2 setup request
76 switch nodebInfo.E2ApplicationProtocol {
77 case entities.E2ApplicationProtocol_X2_SETUP_REQUEST:
78 rmrMsgType := rmrCgo.RIC_X2_SETUP_REQ
79 request := models.NewE2RequestMessage(nodebInfo.RanName /*tid*/, nodebInfo.Ip, uint16(nodebInfo.Port), nodebInfo.RanName, e2pdus.PackedX2setupRequest)
80 return rmrMsgType, request, nil
81 case entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST:
82 rmrMsgType := rmrCgo.RIC_ENDC_X2_SETUP_REQ
83 request:= models.NewE2RequestMessage(nodebInfo.RanName /*tid*/, nodebInfo.Ip, uint16(nodebInfo.Port), nodebInfo.RanName, e2pdus.PackedEndcX2setupRequest)
84 return rmrMsgType, request, nil
87 m.logger.Errorf("#ran_setup_manager.ExecuteSetup - unsupported nodebInfo.E2ApplicationProtocol %d ", nodebInfo.E2ApplicationProtocol)
88 return 0, nil, e2managererrors.NewInternalError()
91 // 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
92 func (m *RanSetupManager) ExecuteSetup(nodebInfo *entities.NodebInfo) error {
94 // Update retries and connection status (connecting)
95 if err := m.updateConnectionStatusConnecting(nodebInfo); err != nil {
96 return e2managererrors.NewRnibDbError()
99 // Build the endc/x2 setup request
100 rmrMsgType,request, err := m.prepareSetupRequest(nodebInfo)
105 // Send the endc/x2 setup request
106 response := &models.NotificationResponse{MgsType: rmrMsgType, RanName: nodebInfo.RanName, Payload: request.GetMessageAsBytes(m.logger)}
107 if err := m.rmrService.SendRmrMessage(response); err != nil {
108 m.logger.Errorf("#ran_setup_manager.ExecuteSetup - failed to send setup request to RMR: %s", err)
110 // Decrement retries and connection status (disconnected)
111 if err := m.updateConnectionStatusDisconnected(nodebInfo); err != nil {
112 return e2managererrors.NewRnibDbError()
115 return e2managererrors.NewRmrError()