dd71ad45bbd87f8410098e5a4b5a73a0a26182ba
[ric-plt/e2mgr.git] / E2Manager / managers / e2t_shutdown_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 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 //  platform project (RICP).
20
21 package managers
22
23 import (
24         "e2mgr/configuration"
25         "e2mgr/logger"
26         "e2mgr/services"
27         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
28         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
29         "time"
30 )
31
32 type IE2TShutdownManager interface {
33         Shutdown(e2tInstance *entities.E2TInstance) error
34 }
35
36 type E2TShutdownManager struct {
37         logger                *logger.Logger
38         config                *configuration.Configuration
39         rnibDataService       services.RNibDataService
40         e2TInstancesManager   IE2TInstancesManager
41         e2tAssociationManager *E2TAssociationManager
42         ranSetupManager       IRanSetupManager
43 }
44
45 func NewE2TShutdownManager(logger *logger.Logger, config *configuration.Configuration, rnibDataService services.RNibDataService, e2TInstancesManager IE2TInstancesManager, e2tAssociationManager *E2TAssociationManager, ranSetupManager IRanSetupManager) *E2TShutdownManager {
46         return &E2TShutdownManager{
47                 logger:                logger,
48                 config:                config,
49                 rnibDataService:       rnibDataService,
50                 e2TInstancesManager:   e2TInstancesManager,
51                 e2tAssociationManager: e2tAssociationManager,
52                 ranSetupManager:       ranSetupManager,
53         }
54 }
55
56 func (m E2TShutdownManager) Shutdown(e2tInstance *entities.E2TInstance) error {
57         m.logger.Infof("#E2TShutdownManager.Shutdown - E2T %s is Dead, RIP", e2tInstance.Address)
58
59         isE2tInstanceBeingDeleted := m.isE2tInstanceAlreadyBeingDeleted(e2tInstance)
60         if isE2tInstanceBeingDeleted {
61                 m.logger.Infof("#E2TShutdownManager.Shutdown - E2T %s is already being deleted", e2tInstance.Address)
62                 return nil
63         }
64
65         err := m.markE2tInstanceToBeDeleted(e2tInstance)
66         if err != nil {
67                 m.logger.Errorf("#E2TShutdownManager.Shutdown - Failed to mark E2T %s as 'ToBeDeleted'.", e2tInstance.Address)
68                 return err
69         }
70
71         ranNamesToBeDissociated := []string{}
72         ranNamesToBeAssociated := make(map[string][]string) // e2tAddress -> associatedRanList
73
74         for _, ranName := range e2tInstance.AssociatedRanList {
75                 ranNamesToBeDissociated, err = m.reAssociateRanInMemory(ranName, ranNamesToBeAssociated, ranNamesToBeDissociated)
76                 if err != nil {
77                         m.logger.Errorf("#E2TShutdownManager.Shutdown - Failed to re-associate nodeb %s.", ranName)
78                         return err
79                 }
80         }
81
82         err = m.e2tAssociationManager.RemoveE2tInstance(e2tInstance, ranNamesToBeDissociated, ranNamesToBeAssociated)
83         if err != nil {
84                 m.logger.Errorf("#E2TShutdownManager.Shutdown - Failed to remove E2T %s.", e2tInstance.Address)
85                 return err
86         }
87
88         err = m.clearNodebsAssociation(ranNamesToBeDissociated)
89         if err != nil {
90                 m.logger.Errorf("#E2TShutdownManager.Shutdown - Failed to clear nodebs association to E2T %s.", e2tInstance.Address)
91                 return err
92         }
93
94         err = m.reassociateNodebs(ranNamesToBeAssociated)
95         if err != nil {
96                 m.logger.Errorf("#E2TShutdownManager.Shutdown - Failed to re-associate nodebs after killing E2T %s.", e2tInstance.Address)
97                 return err
98         }
99
100         m.logger.Infof("#E2TShutdownManager.Shutdown - E2T %s was sutdown successfully.", e2tInstance.Address)
101         return nil
102 }
103
104 func (m E2TShutdownManager) reassociateNodebs(ranNamesToBeAssociated map[string][]string) error {
105         for e2tAddress, ranNames := range ranNamesToBeAssociated {
106
107                 err := m.associateAndSetupNodebs(ranNames, e2tAddress)
108                 if err != nil {
109                         return err
110                 }
111
112         }
113         return nil
114 }
115
116 func (m E2TShutdownManager) clearNodebsAssociation(ranNamesToBeDissociated []string) error {
117         return m.associateAndSetupNodebs(ranNamesToBeDissociated, "")
118 }
119
120 func (m E2TShutdownManager) associateAndSetupNodebs(ranNamesToBeUpdated []string, e2tAddress string) error {
121         isDissociatedRans := len(e2tAddress) == 0
122         for _, ranName := range ranNamesToBeUpdated {
123                 nodeb, err := m.rnibDataService.GetNodeb(ranName)
124                 if err != nil {
125                         m.logger.Warnf("#E2TShutdownManager.associateAndSetupNodebs - Failed to get nodeb %s from db.", ranName)
126                         _, ok := err.(*common.ResourceNotFoundError)
127                         if !ok {
128                                 continue
129                         }
130                         return err
131                 }
132                 nodeb.AssociatedE2TInstanceAddress = e2tAddress
133                 if isDissociatedRans{
134                         nodeb.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
135                 }
136
137                 err = m.rnibDataService.UpdateNodebInfo(nodeb)
138                 if err != nil {
139                         m.logger.Errorf("#E2TShutdownManager.associateAndSetupNodebs - Failed to save nodeb %s from db.", ranName)
140                         return err
141                 }
142
143                 if !isDissociatedRans {
144                         err = m.ranSetupManager.ExecuteSetup(nodeb, entities.ConnectionStatus_CONNECTING)
145                         if err != nil {
146                                 m.logger.Errorf("#E2TShutdownManager.associateAndSetupNodebs - Failed to execute Setup for nodeb %s.", ranName)
147                                 continue
148                         }
149                 }
150         }
151         return nil
152 }
153
154 func (m E2TShutdownManager) reAssociateRanInMemory(ranName string, ranNamesToBeAssociated map[string][]string, ranNamesToBeDissociated []string) ([]string, error) {
155         nodeb, err := m.rnibDataService.GetNodeb(ranName)
156         if err != nil {
157
158                 _, ok := err.(*common.ResourceNotFoundError)
159
160                 if !ok {
161                         m.logger.Errorf("#E2TShutdownManager.reAssociateRanInMemory - Failed to get nodeb %s from db.", ranName)
162                         return ranNamesToBeDissociated, err
163                 }
164
165                 m.logger.Errorf("#E2TShutdownManager.reAssociateRanInMemory - nodeb %s not found in db. Skipping it...", ranName)
166                 return ranNamesToBeDissociated, nil
167         }
168
169         if nodeb.ConnectionStatus == entities.ConnectionStatus_SHUTTING_DOWN || nodeb.ConnectionStatus == entities.ConnectionStatus_SHUT_DOWN {
170                 m.logger.Errorf("#E2TShutdownManager.reAssociateRanInMemory - nodeb %s status is %s. Skipping it...", ranName, nodeb.ConnectionStatus)
171                 return ranNamesToBeDissociated, nil
172         }
173
174         selectedE2tAddress, err := m.e2TInstancesManager.SelectE2TInstance()
175         if err != nil {
176                 m.logger.Infof("#E2TShutdownManager.reAssociateRanInMemory - No selected E2T instance for nodeb %s found.", ranName)
177                 ranNamesToBeDissociated = append(ranNamesToBeDissociated, ranName)
178                 return ranNamesToBeDissociated, nil
179         }
180
181         ranNamesToBeAssociated[selectedE2tAddress] = append(ranNamesToBeAssociated[selectedE2tAddress], ranName)
182         return ranNamesToBeDissociated, nil
183 }
184
185 func (m E2TShutdownManager) markE2tInstanceToBeDeleted(e2tInstance *entities.E2TInstance) error {
186         e2tInstance.State = entities.ToBeDeleted
187         e2tInstance.DeletionTimestamp = time.Now().UnixNano()
188
189         return m.rnibDataService.SaveE2TInstance(e2tInstance)
190 }
191
192 func (m E2TShutdownManager) isE2tInstanceAlreadyBeingDeleted(e2tInstance *entities.E2TInstance) bool {
193         delta := time.Now().UnixNano() - e2tInstance.DeletionTimestamp
194         timestampNanosec := int64(time.Duration(m.config.E2TInstanceDeletionTimeoutMs) * time.Millisecond)
195
196         return e2tInstance.State == entities.ToBeDeleted && delta <= timestampNanosec
197 }