ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 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 | // |
ss412g | 697bea2 | 2019-12-24 22:38:19 +0200 | [diff] [blame] | 17 | |
| 18 | // This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 19 | // platform project (RICP). |
| 20 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 21 | package managers |
| 22 | |
| 23 | import ( |
Amichai | f846c59 | 2020-01-08 16:45:07 +0200 | [diff] [blame] | 24 | "e2mgr/configuration" |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 25 | "e2mgr/logger" |
| 26 | "e2mgr/services" |
Amichai | f846c59 | 2020-01-08 16:45:07 +0200 | [diff] [blame] | 27 | "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common" |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 28 | "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities" |
Amichai | f846c59 | 2020-01-08 16:45:07 +0200 | [diff] [blame] | 29 | "time" |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 30 | ) |
| 31 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 32 | type IE2TShutdownManager interface { |
| 33 | Shutdown(e2tInstance *entities.E2TInstance) error |
| 34 | } |
| 35 | |
| 36 | type E2TShutdownManager struct { |
Irina | db03807 | 2020-06-21 13:09:56 +0300 | [diff] [blame] | 37 | logger *logger.Logger |
| 38 | config *configuration.Configuration |
| 39 | rnibDataService services.RNibDataService |
| 40 | e2TInstancesManager IE2TInstancesManager |
| 41 | e2tAssociationManager *E2TAssociationManager |
Irina | db03807 | 2020-06-21 13:09:56 +0300 | [diff] [blame] | 42 | ranConnectStatusChangeManager IRanConnectStatusChangeManager |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 43 | } |
| 44 | |
Irina | b2f2743 | 2020-07-07 11:17:10 +0000 | [diff] [blame] | 45 | func NewE2TShutdownManager(logger *logger.Logger, config *configuration.Configuration, rnibDataService services.RNibDataService, e2TInstancesManager IE2TInstancesManager, e2tAssociationManager *E2TAssociationManager, ranConnectStatusChangeManager IRanConnectStatusChangeManager) *E2TShutdownManager { |
Amichai | 380b7a2 | 2020-01-14 16:38:06 +0200 | [diff] [blame] | 46 | return &E2TShutdownManager{ |
Irina | db03807 | 2020-06-21 13:09:56 +0300 | [diff] [blame] | 47 | logger: logger, |
| 48 | config: config, |
| 49 | rnibDataService: rnibDataService, |
| 50 | e2TInstancesManager: e2TInstancesManager, |
| 51 | e2tAssociationManager: e2tAssociationManager, |
Irina | db03807 | 2020-06-21 13:09:56 +0300 | [diff] [blame] | 52 | ranConnectStatusChangeManager: ranConnectStatusChangeManager, |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
Amichai | f846c59 | 2020-01-08 16:45:07 +0200 | [diff] [blame] | 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 | |
ss412g | 011bb91 | 2020-03-17 18:34:42 +0200 | [diff] [blame] | 71 | err = m.clearNodebsAssociation(e2tInstance.AssociatedRanList) |
Amichai | f846c59 | 2020-01-08 16:45:07 +0200 | [diff] [blame] | 72 | if err != nil { |
| 73 | m.logger.Errorf("#E2TShutdownManager.Shutdown - Failed to clear nodebs association to E2T %s.", e2tInstance.Address) |
| 74 | return err |
| 75 | } |
| 76 | |
ss412g | 011bb91 | 2020-03-17 18:34:42 +0200 | [diff] [blame] | 77 | err = m.e2tAssociationManager.RemoveE2tInstance(e2tInstance) |
Amichai | f846c59 | 2020-01-08 16:45:07 +0200 | [diff] [blame] | 78 | if err != nil { |
ss412g | 011bb91 | 2020-03-17 18:34:42 +0200 | [diff] [blame] | 79 | m.logger.Errorf("#E2TShutdownManager.Shutdown - Failed to remove E2T %s.", e2tInstance.Address) |
Amichai | f846c59 | 2020-01-08 16:45:07 +0200 | [diff] [blame] | 80 | return err |
| 81 | } |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 82 | |
ss412g | 011bb91 | 2020-03-17 18:34:42 +0200 | [diff] [blame] | 83 | m.logger.Infof("#E2TShutdownManager.Shutdown - E2T %s was shutdown successfully.", e2tInstance.Address) |
Amichai | f846c59 | 2020-01-08 16:45:07 +0200 | [diff] [blame] | 84 | return nil |
| 85 | } |
| 86 | |
| 87 | func (m E2TShutdownManager) clearNodebsAssociation(ranNamesToBeDissociated []string) error { |
ss412g | 011bb91 | 2020-03-17 18:34:42 +0200 | [diff] [blame] | 88 | for _, ranName := range ranNamesToBeDissociated { |
Amichai | f846c59 | 2020-01-08 16:45:07 +0200 | [diff] [blame] | 89 | nodeb, err := m.rnibDataService.GetNodeb(ranName) |
| 90 | if err != nil { |
| 91 | m.logger.Warnf("#E2TShutdownManager.associateAndSetupNodebs - Failed to get nodeb %s from db.", ranName) |
| 92 | _, ok := err.(*common.ResourceNotFoundError) |
| 93 | if !ok { |
| 94 | continue |
| 95 | } |
| 96 | return err |
| 97 | } |
Amichai | 380b7a2 | 2020-01-14 16:38:06 +0200 | [diff] [blame] | 98 | |
Irina | db03807 | 2020-06-21 13:09:56 +0300 | [diff] [blame] | 99 | err = m.ranConnectStatusChangeManager.ChangeStatus(nodeb, entities.ConnectionStatus_DISCONNECTED) |
| 100 | if err != nil { |
| 101 | return err |
| 102 | } |
| 103 | |
| 104 | nodeb.AssociatedE2TInstanceAddress = "" |
Amichai | f846c59 | 2020-01-08 16:45:07 +0200 | [diff] [blame] | 105 | err = m.rnibDataService.UpdateNodebInfo(nodeb) |
| 106 | if err != nil { |
Irina | db03807 | 2020-06-21 13:09:56 +0300 | [diff] [blame] | 107 | m.logger.Errorf("#E2TShutdownManager.clearNodebsAssociation - Failed to save nodeb %s to db.", ranName) |
Amichai | f846c59 | 2020-01-08 16:45:07 +0200 | [diff] [blame] | 108 | return err |
| 109 | } |
Amichai | f846c59 | 2020-01-08 16:45:07 +0200 | [diff] [blame] | 110 | } |
| 111 | return nil |
| 112 | } |
| 113 | |
Amichai | f846c59 | 2020-01-08 16:45:07 +0200 | [diff] [blame] | 114 | func (m E2TShutdownManager) markE2tInstanceToBeDeleted(e2tInstance *entities.E2TInstance) error { |
| 115 | e2tInstance.State = entities.ToBeDeleted |
| 116 | e2tInstance.DeletionTimestamp = time.Now().UnixNano() |
| 117 | |
| 118 | return m.rnibDataService.SaveE2TInstance(e2tInstance) |
| 119 | } |
| 120 | |
| 121 | func (m E2TShutdownManager) isE2tInstanceAlreadyBeingDeleted(e2tInstance *entities.E2TInstance) bool { |
| 122 | delta := time.Now().UnixNano() - e2tInstance.DeletionTimestamp |
| 123 | timestampNanosec := int64(time.Duration(m.config.E2TInstanceDeletionTimeoutMs) * time.Millisecond) |
| 124 | |
Amichai | 380b7a2 | 2020-01-14 16:38:06 +0200 | [diff] [blame] | 125 | return e2tInstance.State == entities.ToBeDeleted && delta <= timestampNanosec |
Amichai | f846c59 | 2020-01-08 16:45:07 +0200 | [diff] [blame] | 126 | } |