cb24472d8ae5f32e57b7367de99b656b00858a9b
[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
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)
52         if err != nil {
53                 m.logger.Errorf("#ran_setup_manager.updateConnectionStatusConnecting - failed to update RAN's connection status to CONNECTING: %s", err)
54         } else {
55                 m.logger.Infof("#ran_setup_manager.updateConnectionStatusConnecting - successfully updated RAN's connection status to CONNECTING: %s", err)
56         }
57         return err
58 }
59
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)
66         if err != nil {
67                 m.logger.Errorf("#ran_setup_manager.updateConnectionStatusDisconnected - failed to update RAN's connection status to DISCONNECTED : %s", err)
68         } else {
69                 m.logger.Errorf("#ran_setup_manager.updateConnectionStatusDisconnected - successfully updated RAN's connection status to DISCONNECTED : %s", err)
70         }
71         return err
72 }
73
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
85         }
86
87         m.logger.Errorf("#ran_setup_manager.ExecuteSetup - unsupported nodebInfo.E2ApplicationProtocol %d ", nodebInfo.E2ApplicationProtocol)
88         return 0, nil, e2managererrors.NewInternalError()
89 }
90
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 {
93
94         // Update retries and connection status (connecting)
95         if err := m.updateConnectionStatusConnecting(nodebInfo); err != nil {
96                 return e2managererrors.NewRnibDbError()
97         }
98
99         // Build the endc/x2 setup request
100         rmrMsgType,request, err := m.prepareSetupRequest(nodebInfo)
101         if err != nil {
102                 return err
103         }
104         
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)
109
110                 // Decrement retries and connection status (disconnected)
111                 if err := m.updateConnectionStatusDisconnected(nodebInfo); err != nil {
112                         return e2managererrors.NewRnibDbError()
113                 }
114
115                 return e2managererrors.NewRmrError()
116         }
117         return nil
118 }