RIC-997: ErrorIndication handling in e2mgr
[ric-plt/e2mgr.git] / E2Manager / managers / ric_service_update_manager.go
1 //
2 // Copyright 2023 Nokia
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 //  platform project (RICP).
18
19 package managers
20
21
22 import(
23         "e2mgr/logger"
24         "e2mgr/services"
25         "errors"
26         "e2mgr/models"
27 )
28
29
30 type IRicServiceUpdateManager interface {
31         RevertRanFunctions(ranName string) error
32         StoreExistingRanFunctions(ranName string) error
33 }
34
35
36 type RicServiceUpdateManager struct {
37         logger          *logger.Logger
38         rNibDataService services.RNibDataService
39 }
40
41 func NewRicServiceUpdateManager(logger *logger.Logger, rNibDataService services.RNibDataService) *RicServiceUpdateManager {
42         return &RicServiceUpdateManager{
43                 logger:          logger,
44                 rNibDataService: rNibDataService,
45         }
46 }
47
48
49 func (h *RicServiceUpdateManager) StoreExistingRanFunctions(ranName string) error {
50         nodebInfo, err := h.rNibDataService.GetNodeb(ranName)
51         if err != nil {
52                 h.logger.Errorf("#RicServiceUpdateManager.revertRanFunctions - failed to get nodeB entity for ran name: %v due to RNIB Error: %s", ranName, err)
53         }
54         if nodebInfo.GetGnb() == nil {
55         h.logger.Errorf("#RicServiceUpdateManager.revertRanFunctions - GNB is nil for RAN name: %s", ranName)
56         return errors.New("There is empty gnb nodebInfo")
57     }
58         models.ExistingRanFunctiuonsMap[ranName] =  nodebInfo.GetGnb().RanFunctions
59         h.logger.Errorf("#RicServiceUpdateManager.revertRanFunctions - Updated ranFunctions for reverting the changes are %v:", models.ExistingRanFunctiuonsMap[ranName])
60         return nil
61 }
62
63 func (h *RicServiceUpdateManager) RevertRanFunctions(ranName string) error {
64         nodebInfo, err := h.rNibDataService.GetNodeb(ranName)
65         if err != nil {
66                 h.logger.Errorf("#RicServiceUpdateManager.revertRanFunctions - failed to get nodeB entity for ran name: %v due to RNIB Error: %s", ranName, err)
67         }
68
69         if nodebInfo.GetGnb() != nil && nodebInfo.GetGnb().RanFunctions != nil {
70                 nodebInfo.GetGnb().RanFunctions = models.ExistingRanFunctiuonsMap[ranName]
71         } else {
72                 h.logger.Errorf("#RicServiceUpdateManager.revertRanFunctions returned nil")
73         }
74         err = h.rNibDataService.UpdateNodebInfoAndPublish(nodebInfo)
75         if err != nil {
76                 h.logger.Errorf("#RicServiceUpdateManager.revertRanFunctions - RAN name: %s - Failed at UpdateNodebInfoAndPublish. error: %s", nodebInfo.RanName, err)
77                 return err
78         }
79
80         h.logger.Infof("#RicServiceUpdateManager.revertRanFunctions - Revert ranFunctions for RAN name: %s", ranName)
81         return nil
82 }