RIC-194 Setup from RAN: On Routing Manager Failure, return Setup Failure
[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 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20
21 package managers
22
23 import (
24         "e2mgr/e2managererrors"
25         "e2mgr/e2pdus"
26         "e2mgr/logger"
27         "e2mgr/models"
28         "e2mgr/rmrCgo"
29         "e2mgr/services"
30         "e2mgr/services/rmrsender"
31         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
32         "unsafe"
33 )
34
35 type RanSetupManager struct {
36         logger          *logger.Logger
37         rnibDataService services.RNibDataService
38         rmrSender       *rmrsender.RmrSender
39 }
40
41 type IRanSetupManager interface {
42         ExecuteSetup(nodebInfo *entities.NodebInfo, status entities.ConnectionStatus) error
43 }
44
45 func NewRanSetupManager(logger *logger.Logger, rmrSender *rmrsender.RmrSender, rnibDataService services.RNibDataService) *RanSetupManager {
46         return &RanSetupManager{
47                 logger:          logger,
48                 rnibDataService: rnibDataService,
49                 rmrSender:       rmrSender,
50         }
51 }
52
53 // Update retries and connection status 
54 func (m *RanSetupManager) updateConnectionStatus(nodebInfo *entities.NodebInfo, status entities.ConnectionStatus) error {
55         // Update retries and connection status
56         nodebInfo.ConnectionStatus = status
57         nodebInfo.ConnectionAttempts++
58         err := m.rnibDataService.UpdateNodebInfo(nodebInfo)
59         if err != nil {
60                 m.logger.Errorf("#RanSetupManager.updateConnectionStatus - Ran name: %s - Failed updating RAN's connection status to %v : %s", nodebInfo.RanName, status, err)
61         } else {
62                 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)
63         }
64         return err
65 }
66
67 // Decrement retries and connection status (disconnected)
68 func (m *RanSetupManager) updateConnectionStatusDisconnected(nodebInfo *entities.NodebInfo) error {
69         // Update retries and connection status
70         nodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
71         nodebInfo.ConnectionAttempts--
72         err := m.rnibDataService.UpdateNodebInfo(nodebInfo)
73         if err != nil {
74                 m.logger.Errorf("#RanSetupManager.updateConnectionStatusDisconnected - Ran name: %s - Failed updating RAN's connection status to DISCONNECTED : %s", nodebInfo.RanName, err)
75         } else {
76                 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)
77         }
78         return err
79 }
80
81 func (m *RanSetupManager) prepareSetupRequest(nodebInfo *entities.NodebInfo) (int, *models.E2RequestMessage, error) {
82         // Build the endc/x2 setup request
83         switch nodebInfo.E2ApplicationProtocol {
84         case entities.E2ApplicationProtocol_X2_SETUP_REQUEST:
85                 rmrMsgType := rmrCgo.RIC_X2_SETUP_REQ
86                 request := models.NewE2RequestMessage(nodebInfo.RanName /*tid*/, nodebInfo.Ip, uint16(nodebInfo.Port), nodebInfo.RanName, e2pdus.PackedX2setupRequest)
87                 return rmrMsgType, request, nil
88         case entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST:
89                 rmrMsgType := rmrCgo.RIC_ENDC_X2_SETUP_REQ
90                 request := models.NewE2RequestMessage(nodebInfo.RanName /*tid*/, nodebInfo.Ip, uint16(nodebInfo.Port), nodebInfo.RanName, e2pdus.PackedEndcX2setupRequest)
91                 return rmrMsgType, request, nil
92         }
93
94         m.logger.Errorf("#RanSetupManager.prepareSetupRequest - Unsupported nodebInfo.E2ApplicationProtocol %d ", nodebInfo.E2ApplicationProtocol)
95         return 0, nil, e2managererrors.NewInternalError()
96 }
97
98 // 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
99 func (m *RanSetupManager) ExecuteSetup(nodebInfo *entities.NodebInfo, status entities.ConnectionStatus) error {
100
101         // Update retries and connection status
102         if err := m.updateConnectionStatus(nodebInfo, status); err != nil {
103                 return e2managererrors.NewRnibDbError()
104         }
105
106         // Build the endc/x2 setup request
107         rmrMsgType, request, err := m.prepareSetupRequest(nodebInfo)
108         if err != nil {
109                 return err
110         }
111
112         // Send the endc/x2 setup request
113         var xAction []byte
114         var msgSrc unsafe.Pointer
115         msg := models.NewRmrMessage(rmrMsgType, nodebInfo.RanName, request.GetMessageAsBytes(m.logger), xAction, msgSrc)
116
117         err = m.rmrSender.Send(msg)
118
119         if err != nil {
120                 m.logger.Errorf("#RanSetupManager.ExecuteSetup - failed sending setup request to RMR: %s", err)
121
122                 err := m.updateConnectionStatusDisconnected(nodebInfo)
123
124                 // Decrement retries and connection status (disconnected)
125                 if err != nil {
126                         return e2managererrors.NewRnibDbError()
127                 }
128
129                 return e2managererrors.NewRmrError()
130         }
131
132         return nil
133 }