[RICPLT-1852] - update rnib from the setup response handler
[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         "e2mgr/sessions"
29         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
30         "time"
31 )
32
33 type RanSetupManager struct {
34         logger             *logger.Logger
35         rnibWriterProvider func() rNibWriter.RNibWriter
36         rmrService         *services.RmrService
37 }
38
39 func NewRanSetupManager(logger *logger.Logger, rmrService *services.RmrService, rnibWriterProvider func() rNibWriter.RNibWriter) *RanSetupManager {
40         return &RanSetupManager{
41                 logger:             logger,
42                 rnibWriterProvider: rnibWriterProvider,
43                 rmrService:         rmrService,
44         }
45 }
46
47 // Update retries and connection status (connecting)
48 func (m *RanSetupManager) updateConnectionStatusConnecting(nodebInfo *entities.NodebInfo) error {
49         // Update retries and connection status (connecting)
50         nodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
51         nodebInfo.ConnectionAttempts++
52         err := m.rnibWriterProvider().UpdateNodebInfo(nodebInfo)
53         if err != nil {
54                 m.logger.Errorf("#RanSetupManager.updateConnectionStatusConnecting - failed to update RAN's connection status to CONNECTING: %s", err)
55         } else {
56                 m.logger.Infof("#RanSetupManager.updateConnectionStatusConnecting - successfully updated RAN's connection status to CONNECTING")
57         }
58         return err
59 }
60
61 // Decrement retries and connection status (disconnected)
62 func (m *RanSetupManager) updateConnectionStatusDisconnected(nodebInfo *entities.NodebInfo) error {
63         // Update retries and connection status (connecting)
64         nodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
65         nodebInfo.ConnectionAttempts--
66         err := m.rnibWriterProvider().UpdateNodebInfo(nodebInfo)
67         if err != nil {
68                 m.logger.Errorf("#RanSetupManager.updateConnectionStatusDisconnected - failed to update RAN's connection status to DISCONNECTED : %s", err)
69         } else {
70                 m.logger.Errorf("#RanSetupManager.updateConnectionStatusDisconnected - successfully updated RAN's connection status to DISCONNECTED")
71         }
72         return err
73 }
74
75 func (m *RanSetupManager) prepareSetupRequest(nodebInfo *entities.NodebInfo) (int, *models.E2RequestMessage, error) {
76         // Build the endc/x2 setup request
77         switch nodebInfo.E2ApplicationProtocol {
78         case entities.E2ApplicationProtocol_X2_SETUP_REQUEST:
79                 rmrMsgType := rmrCgo.RIC_X2_SETUP_REQ
80                 request := models.NewE2RequestMessage(nodebInfo.RanName /*tid*/, nodebInfo.Ip, uint16(nodebInfo.Port), nodebInfo.RanName, e2pdus.PackedX2setupRequest)
81                 return rmrMsgType, request, nil
82         case entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST:
83                 rmrMsgType := rmrCgo.RIC_ENDC_X2_SETUP_REQ
84                 request := models.NewE2RequestMessage(nodebInfo.RanName /*tid*/, nodebInfo.Ip, uint16(nodebInfo.Port), nodebInfo.RanName, e2pdus.PackedEndcX2setupRequest)
85                 return rmrMsgType, request, nil
86         }
87
88         m.logger.Errorf("#RanSetupManager.ExecuteSetup - unsupported nodebInfo.E2ApplicationProtocol %d ", nodebInfo.E2ApplicationProtocol)
89         return 0, nil, e2managererrors.NewInternalError()
90 }
91
92 // 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
93 func (m *RanSetupManager) ExecuteSetup(nodebInfo *entities.NodebInfo) error {
94
95         //Fill details for the response handler
96         requestDetails := models.RequestDetails{RanName: nodebInfo.RanName, RanPort: uint16(nodebInfo.Port), RanIp: nodebInfo.Ip}
97         m.rmrService.E2sessions[nodebInfo.RanName] = sessions.E2SessionDetails{SessionStart: time.Now(), Request: &requestDetails}
98
99         // Update retries and connection status (connecting)
100         if err := m.updateConnectionStatusConnecting(nodebInfo); err != nil {
101                 delete(m.rmrService.E2sessions,nodebInfo.RanName)
102                 return e2managererrors.NewRnibDbError()
103         }
104
105         // Build the endc/x2 setup request
106         rmrMsgType, request, err := m.prepareSetupRequest(nodebInfo)
107         if err != nil {
108                 delete(m.rmrService.E2sessions,nodebInfo.RanName)
109                 return err
110         }
111
112         // Send the endc/x2 setup request
113         response := &models.NotificationResponse{MgsType: rmrMsgType, RanName: nodebInfo.RanName, Payload: request.GetMessageAsBytes(m.logger)}
114         if err := m.rmrService.SendRmrMessage(response); err != nil {
115                 m.logger.Errorf("#RanSetupManager.ExecuteSetup - failed to send setup request to RMR: %s", err)
116
117                 delete(m.rmrService.E2sessions,nodebInfo.RanName)
118
119                 // Decrement retries and connection status (disconnected)
120                 if err := m.updateConnectionStatusDisconnected(nodebInfo); err != nil {
121                         return e2managererrors.NewRnibDbError()
122                 }
123
124                 return e2managererrors.NewRmrError()
125         }
126
127         return nil
128 }