[RICPLT-2165] Add rnibDataService to support retries
[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/rmrCgo"
26         "e2mgr/services"
27         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
28 )
29
30 type RanSetupManager struct {
31         logger             *logger.Logger
32         rnibDataService services.RNibDataService
33         rmrService         *services.RmrService
34 }
35
36 func NewRanSetupManager(logger *logger.Logger, rmrService *services.RmrService, rnibDataService services.RNibDataService) *RanSetupManager {
37         return &RanSetupManager{
38                 logger:             logger,
39                 rnibDataService: rnibDataService,
40                 rmrService:         rmrService,
41         }
42 }
43
44 // Update retries and connection status 
45 func (m *RanSetupManager) updateConnectionStatus(nodebInfo *entities.NodebInfo, status entities.ConnectionStatus) error {
46         // Update retries and connection status
47         nodebInfo.ConnectionStatus = status
48         nodebInfo.ConnectionAttempts++
49         err := m.rnibDataService.UpdateNodebInfo(nodebInfo)
50         if err != nil {
51                 m.logger.Errorf("#RanSetupManager.updateConnectionStatus - Ran name: %s - Failed updating RAN's connection status to %v : %s", nodebInfo.RanName, status, err)
52         } else {
53                 m.logger.Infof("#RanSetupManager.updateConnectionStatus - Ran name: %s - Successfully updated rNib. RAN's current connection status: %v, RAN's current connection attempts: %d", nodebInfo.RanName, status, nodebInfo.ConnectionAttempts)
54         }
55         return err
56 }
57
58 // Decrement retries and connection status (disconnected)
59 func (m *RanSetupManager) updateConnectionStatusDisconnected(nodebInfo *entities.NodebInfo) error {
60         // Update retries and connection status
61         nodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
62         nodebInfo.ConnectionAttempts--
63         err := m.rnibDataService.UpdateNodebInfo(nodebInfo)
64         if err != nil {
65                 m.logger.Errorf("#RanSetupManager.updateConnectionStatusDisconnected - Ran name: %s - Failed updating RAN's connection status to DISCONNECTED : %s", nodebInfo.RanName, err)
66         } else {
67                 m.logger.Infof("#RanSetupManager.updateConnectionStatusDisconnected - Ran name: %s - Successfully updated rNib. RAN's current connection status: DISCONNECTED, RAN's current connection attempts: %d", nodebInfo.RanName, nodebInfo.ConnectionAttempts)
68         }
69         return err
70 }
71
72 func (m *RanSetupManager) prepareSetupRequest(nodebInfo *entities.NodebInfo) (int, *models.E2RequestMessage, error) {
73         // Build the endc/x2 setup request
74         switch nodebInfo.E2ApplicationProtocol {
75         case entities.E2ApplicationProtocol_X2_SETUP_REQUEST:
76                 rmrMsgType := rmrCgo.RIC_X2_SETUP_REQ
77                 request := models.NewE2RequestMessage(nodebInfo.RanName /*tid*/, nodebInfo.Ip, uint16(nodebInfo.Port), nodebInfo.RanName, e2pdus.PackedX2setupRequest)
78                 return rmrMsgType, request, nil
79         case entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST:
80                 rmrMsgType := rmrCgo.RIC_ENDC_X2_SETUP_REQ
81                 request := models.NewE2RequestMessage(nodebInfo.RanName /*tid*/, nodebInfo.Ip, uint16(nodebInfo.Port), nodebInfo.RanName, e2pdus.PackedEndcX2setupRequest)
82                 return rmrMsgType, request, nil
83         }
84
85         m.logger.Errorf("#RanSetupManager.prepareSetupRequest - Unsupported nodebInfo.E2ApplicationProtocol %d ", nodebInfo.E2ApplicationProtocol)
86         return 0, nil, e2managererrors.NewInternalError()
87 }
88
89 // 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
90 func (m *RanSetupManager) ExecuteSetup(nodebInfo *entities.NodebInfo, status entities.ConnectionStatus) error {
91
92         // Update retries and connection status
93         if err := m.updateConnectionStatus(nodebInfo, status); err != nil {
94                 return e2managererrors.NewRnibDbError()
95         }
96
97         // Build the endc/x2 setup request
98         rmrMsgType, request, err := m.prepareSetupRequest(nodebInfo)
99         if err != nil {
100                 return err
101         }
102
103         // Send the endc/x2 setup request
104         response := &models.NotificationResponse{MgsType: rmrMsgType, RanName: nodebInfo.RanName, Payload: request.GetMessageAsBytes(m.logger)}
105         if err := m.rmrService.SendRmrMessage(response); err != nil {
106                 m.logger.Errorf("#RanSetupManager.ExecuteSetup - failed sending setup request to RMR: %s", err)
107
108                 // Decrement retries and connection status (disconnected)
109                 if err := m.updateConnectionStatusDisconnected(nodebInfo); err != nil {
110                         return e2managererrors.NewRnibDbError()
111                 }
112
113                 return e2managererrors.NewRmrError()
114         }
115
116         return nil
117 }