2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
20 package httpmsghandlers
26 "e2mgr/e2managererrors"
32 "e2mgr/services/rmrsender"
33 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
34 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
38 type DeleteAllRequestHandler struct {
39 rnibDataService services.RNibDataService
40 rmrSender *rmrsender.RmrSender
41 config *configuration.Configuration
43 e2tInstancesManager managers.IE2TInstancesManager
44 rmClient clients.IRoutingManagerClient
45 ranConnectStatusChangeManager managers.IRanConnectStatusChangeManager
48 const PartialSuccessDueToRmErrorMessage = "Operation succeeded except for routing manager outbound call"
50 func NewDeleteAllRequestHandler(logger *logger.Logger, rmrSender *rmrsender.RmrSender, config *configuration.Configuration, rnibDataService services.RNibDataService, e2tInstancesManager managers.IE2TInstancesManager, rmClient clients.IRoutingManagerClient, ranConnectStatusChangeManager managers.IRanConnectStatusChangeManager) *DeleteAllRequestHandler {
51 return &DeleteAllRequestHandler{
53 rnibDataService: rnibDataService,
56 e2tInstancesManager: e2tInstancesManager,
58 ranConnectStatusChangeManager: ranConnectStatusChangeManager,
62 func (h *DeleteAllRequestHandler) Handle(request models.Request) (models.IResponse, error) {
63 h.logger.Infof("#DeleteAllRequestHandler.Handle - handling shutdown request")
65 e2tAddresses, err := h.e2tInstancesManager.GetE2TAddresses()
71 if len(e2tAddresses) == 0 {
72 err, _ = h.updateNodebs(h.updateNodebInfoForceShutdown)
76 dissocErr := h.rmClient.DissociateAllRans(e2tAddresses)
79 h.logger.Warnf("#DeleteAllRequestHandler.Handle - routing manager failure. continue flow.")
82 err, updatedAtLeastOnce := h.updateNodebs(h.updateNodebInfoShuttingDown)
88 err = h.e2tInstancesManager.ClearRansOfAllE2TInstances()
94 rmrMessage := models.RmrMessage{MsgType: rmrCgo.RIC_SCTP_CLEAR_ALL}
96 err = h.rmrSender.Send(&rmrMessage)
99 h.logger.Errorf("#DeleteAllRequestHandler.Handle - failed to send sctp clear all message to RMR: %s", err)
100 return nil, e2managererrors.NewRmrError()
103 if !updatedAtLeastOnce {
104 h.logger.Infof("#DeleteAllRequestHandler.Handle - DB wasn't updated, not activating timer")
106 if dissocErr != nil {
107 return models.NewRedButtonPartialSuccessResponseModel(PartialSuccessDueToRmErrorMessage), nil
113 time.Sleep(time.Duration(h.config.BigRedButtonTimeoutSec) * time.Second)
114 h.logger.Infof("#DeleteAllRequestHandler.Handle - timer expired")
116 err, _ = h.updateNodebs(h.updateNodebInfoShutDown)
122 if dissocErr != nil {
123 return models.NewRedButtonPartialSuccessResponseModel(PartialSuccessDueToRmErrorMessage), nil
129 func (h *DeleteAllRequestHandler) updateNodebs(updateCb func(node *entities.NodebInfo) (error, bool)) (error, bool) {
130 nbIdentityList, err := h.rnibDataService.GetListNodebIds()
133 h.logger.Errorf("#DeleteAllRequestHandler.updateNodebs - failed to get nodes list from rNib. Error: %s", err)
134 return e2managererrors.NewRnibDbError(), false
137 updatedAtLeastOnce := false
139 for _, nbIdentity := range nbIdentityList {
140 node, err := h.rnibDataService.GetNodeb(nbIdentity.InventoryName)
143 _, ok := err.(*common.ResourceNotFoundError)
146 h.logger.Errorf("#DeleteAllRequestHandler.updateNodebs - failed to get nodeB entity for ran name: %s from rNib. error: %s", nbIdentity.InventoryName, err)
147 return e2managererrors.NewRnibDbError(), false
152 err, updated := updateCb(node)
159 updatedAtLeastOnce = true
163 return nil, updatedAtLeastOnce
166 func (h *DeleteAllRequestHandler) updateNodebInfoForceShutdown(node *entities.NodebInfo) (error, bool) {
167 err := h.updateNodebInfo(node, entities.ConnectionStatus_SHUT_DOWN, true)
176 func (h *DeleteAllRequestHandler) updateNodebInfoShuttingDown(node *entities.NodebInfo) (error, bool) {
177 if node.ConnectionStatus == entities.ConnectionStatus_SHUT_DOWN {
181 err := h.updateNodebInfo(node, entities.ConnectionStatus_SHUTTING_DOWN, true)
190 func (h *DeleteAllRequestHandler) updateNodebInfoShutDown(node *entities.NodebInfo) (error, bool) {
191 if node.ConnectionStatus == entities.ConnectionStatus_SHUT_DOWN {
195 if node.ConnectionStatus != entities.ConnectionStatus_SHUTTING_DOWN {
196 h.logger.Warnf("#DeleteAllRequestHandler.updateNodebInfoShutDown - RAN name: %s - ignore, status is not Shutting Down", node.RanName)
200 err := h.updateNodebInfo(node, entities.ConnectionStatus_SHUT_DOWN, false)
209 func (h *DeleteAllRequestHandler) updateNodebInfo(node *entities.NodebInfo, connectionStatus entities.ConnectionStatus, resetAssociatedE2TAddress bool) error {
211 err := h.ranConnectStatusChangeManager.ChangeStatus(node, connectionStatus)
213 return e2managererrors.NewRnibDbError()
216 if resetAssociatedE2TAddress {
217 node.AssociatedE2TInstanceAddress = ""
219 err = h.rnibDataService.UpdateNodebInfo(node)
221 h.logger.Errorf("#DeleteAllRequestHandler.updateNodebInfo - RAN name: %s - failed updating nodeB entity in rNib. error: %s", node.RanName, err)
222 return e2managererrors.NewRnibDbError()
225 h.logger.Infof("#DeleteAllRequestHandler.updateNodebInfo - RAN name: %s, connection status: %s", node.RanName, connectionStatus)