Merge "[RIC-435] DevOps: Create Simulator on K8S"
[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         kubernetesManager             *KubernetesManager
43         ranConnectStatusChangeManager IRanConnectStatusChangeManager
44 }
45
46 func NewE2TShutdownManager(logger *logger.Logger, config *configuration.Configuration, rnibDataService services.RNibDataService, e2TInstancesManager IE2TInstancesManager, e2tAssociationManager *E2TAssociationManager, kubernetes *KubernetesManager, ranConnectStatusChangeManager IRanConnectStatusChangeManager) *E2TShutdownManager {
47         return &E2TShutdownManager{
48                 logger:                        logger,
49                 config:                        config,
50                 rnibDataService:               rnibDataService,
51                 e2TInstancesManager:           e2TInstancesManager,
52                 e2tAssociationManager:         e2tAssociationManager,
53                 kubernetesManager:             kubernetes,
54                 ranConnectStatusChangeManager: ranConnectStatusChangeManager,
55         }
56 }
57
58 func (m E2TShutdownManager) Shutdown(e2tInstance *entities.E2TInstance) error {
59         m.logger.Infof("#E2TShutdownManager.Shutdown - E2T %s is Dead, RIP", e2tInstance.Address)
60
61         isE2tInstanceBeingDeleted := m.isE2tInstanceAlreadyBeingDeleted(e2tInstance)
62         if isE2tInstanceBeingDeleted {
63                 m.logger.Infof("#E2TShutdownManager.Shutdown - E2T %s is already being deleted", e2tInstance.Address)
64                 return nil
65         }
66
67         //go m.kubernetesManager.DeletePod(e2tInstance.PodName)
68
69         err := m.markE2tInstanceToBeDeleted(e2tInstance)
70         if err != nil {
71                 m.logger.Errorf("#E2TShutdownManager.Shutdown - Failed to mark E2T %s as 'ToBeDeleted'.", e2tInstance.Address)
72                 return err
73         }
74
75         err = m.clearNodebsAssociation(e2tInstance.AssociatedRanList)
76         if err != nil {
77                 m.logger.Errorf("#E2TShutdownManager.Shutdown - Failed to clear nodebs association to E2T %s.", e2tInstance.Address)
78                 return err
79         }
80
81         err = m.e2tAssociationManager.RemoveE2tInstance(e2tInstance)
82         if err != nil {
83                 m.logger.Errorf("#E2TShutdownManager.Shutdown - Failed to remove E2T %s.", e2tInstance.Address)
84                 return err
85         }
86
87         m.logger.Infof("#E2TShutdownManager.Shutdown - E2T %s was shutdown successfully.", e2tInstance.Address)
88         return nil
89 }
90
91 func (m E2TShutdownManager) clearNodebsAssociation(ranNamesToBeDissociated []string) error {
92         for _, ranName := range ranNamesToBeDissociated {
93                 nodeb, err := m.rnibDataService.GetNodeb(ranName)
94                 if err != nil {
95                         m.logger.Warnf("#E2TShutdownManager.associateAndSetupNodebs - Failed to get nodeb %s from db.", ranName)
96                         _, ok := err.(*common.ResourceNotFoundError)
97                         if !ok {
98                                 continue
99                         }
100                         return err
101                 }
102
103                 err = m.ranConnectStatusChangeManager.ChangeStatus(nodeb, entities.ConnectionStatus_DISCONNECTED)
104                 if err != nil {
105                         return err
106                 }
107
108                 nodeb.AssociatedE2TInstanceAddress = ""
109                 err = m.rnibDataService.UpdateNodebInfo(nodeb)
110                 if err != nil {
111                         m.logger.Errorf("#E2TShutdownManager.clearNodebsAssociation - Failed to save nodeb %s to db.", ranName)
112                         return err
113                 }
114         }
115         return nil
116 }
117
118 func (m E2TShutdownManager) markE2tInstanceToBeDeleted(e2tInstance *entities.E2TInstance) error {
119         e2tInstance.State = entities.ToBeDeleted
120         e2tInstance.DeletionTimestamp = time.Now().UnixNano()
121
122         return m.rnibDataService.SaveE2TInstance(e2tInstance)
123 }
124
125 func (m E2TShutdownManager) isE2tInstanceAlreadyBeingDeleted(e2tInstance *entities.E2TInstance) bool {
126         delta := time.Now().UnixNano() - e2tInstance.DeletionTimestamp
127         timestampNanosec := int64(time.Duration(m.config.E2TInstanceDeletionTimeoutMs) * time.Millisecond)
128
129         return e2tInstance.State == entities.ToBeDeleted && delta <= timestampNanosec
130 }