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