f6fe1d202e33d611fc88e0208e652dc22424a89a
[ric-plt/e2mgr.git] / E2Manager / managers / ran_setup_manager.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 //
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
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 package managers
19
20 import (
21         "e2mgr/e2managererrors"
22         "e2mgr/e2pdus"
23         "e2mgr/logger"
24         "e2mgr/models"
25         "e2mgr/rNibWriter"
26         "e2mgr/rmrCgo"
27         "e2mgr/services"
28         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
29 )
30
31 type RanSetupManager struct {
32         logger             *logger.Logger
33         rnibWriterProvider func() rNibWriter.RNibWriter
34         rmrService         *services.RmrService
35 }
36
37 func NewRanSetupManager(logger *logger.Logger, rmrService *services.RmrService, rnibWriterProvider func() rNibWriter.RNibWriter) *RanSetupManager {
38         return &RanSetupManager{
39                 logger:             logger,
40                 rnibWriterProvider: rnibWriterProvider,
41                 rmrService:         rmrService,
42         }
43 }
44
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)
51         if err != nil {
52                 m.logger.Errorf("#RanSetupManager.updateConnectionStatusConnecting - failed to update RAN's connection status to CONNECTING: %s", err)
53         } else {
54                 m.logger.Infof("#RanSetupManager.updateConnectionStatusConnecting - successfully updated RAN's connection status to CONNECTING")
55         }
56         return err
57 }
58
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)
65         if err != nil {
66                 m.logger.Errorf("#RanSetupManager.updateConnectionStatusDisconnected - failed to update RAN's connection status to DISCONNECTED : %s", err)
67         } else {
68                 m.logger.Errorf("#RanSetupManager.updateConnectionStatusDisconnected - successfully updated RAN's connection status to DISCONNECTED")
69         }
70         return err
71 }
72
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
84         }
85
86         m.logger.Errorf("#RanSetupManager.ExecuteSetup - unsupported nodebInfo.E2ApplicationProtocol %d ", nodebInfo.E2ApplicationProtocol)
87         return 0, nil, e2managererrors.NewInternalError()
88 }
89
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 {
92
93         // Update retries and connection status (connecting)
94         if err := m.updateConnectionStatusConnecting(nodebInfo); err != nil {
95                 return e2managererrors.NewRnibDbError()
96         }
97
98         // Build the endc/x2 setup request
99         rmrMsgType, request, err := m.prepareSetupRequest(nodebInfo)
100         if err != nil {
101                 return err
102         }
103
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)
108
109                 // Decrement retries and connection status (disconnected)
110                 if err := m.updateConnectionStatusDisconnected(nodebInfo); err != nil {
111                         return e2managererrors.NewRnibDbError()
112                 }
113
114                 return e2managererrors.NewRmrError()
115         }
116
117         return nil
118 }