0404e74590c757fea2edd1cb74b67a29f4587910
[ric-plt/e2mgr.git] / E2Manager / handlers / ranLostConnectionHandler.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 package handlers
19
20 import (
21         "e2mgr/logger"
22         "e2mgr/models"
23         "e2mgr/rNibWriter"
24         "e2mgr/sessions"
25         "fmt"
26         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
27         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
28         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
29 )
30
31 type RanLostConnectionHandler struct{
32         rnibReaderProvider func() reader.RNibReader
33         rnibWriterProvider func() rNibWriter.RNibWriter
34 }
35
36 func NewRanLostConnectionHandler(rnibReaderProvider func() reader.RNibReader, rnibWriterProvider func() rNibWriter.RNibWriter) RanLostConnectionHandler {
37         return RanLostConnectionHandler{
38                 rnibReaderProvider: rnibReaderProvider,
39                 rnibWriterProvider: rnibWriterProvider,
40         }
41 }
42 func (src RanLostConnectionHandler) Handle(logger *logger.Logger, e2Sessions sessions.E2Sessions,
43         request *models.NotificationRequest, messageChannel chan<- *models.NotificationResponse) {
44
45         logger.Warnf("#ranLostConnectionHandler.Handle - Received lost connection (transaction id = %s): %s", request.TransactionId, request.Payload)
46
47         var nb *entities.NodebInfo
48         var rNibErr common.IRNibError
49         if nb, rNibErr = src.rnibReaderProvider().GetNodeb(request.RanName); rNibErr != nil {
50                 logger.Errorf("#ranLostConnectionHandler.Handle - transactionId %s: rNib reader failed to retrieve nb entity with RanName: %s. Error: %s", request.TransactionId, request.RanName, rNibErr.Error())
51         } else {
52                 logger.Debugf("#ranLostConnectionHandler.Handle - transactionId %s: nb entity has been retrieved. RanName %s, ConnectionStatus %s", request.TransactionId, nb.RanName, nb.ConnectionStatus)
53                 changeNodebState(logger, nb)
54                 nbIdentity := &entities.NbIdentity{InventoryName:nb.RanName, GlobalNbId:nb.GlobalNbId}
55                 if rNibErr = src.rnibWriterProvider().SaveNodeb(nbIdentity, nb); rNibErr != nil {
56                         logger.Errorf("#ranLostConnectionHandler.Handle - transactionId %s: rNibWriter failed to save nb entity %s. Error: %s", request.TransactionId, nb.RanName, rNibErr.Error())
57                 } else {
58                         logger.Infof("#ranLostConnectionHandler.Handle - transactionId %s: saved to rNib", request.TransactionId)
59                         logger.Debugf("#ranLostConnectionHandler.Handle - transactionId %s: saved to rNib. RanName %s, ConnectionStatus %v", request.TransactionId, nb.RanName, nb.ConnectionStatus)
60
61                 }
62         }
63         e2session, ok := e2Sessions[request.TransactionId]
64         printHandlingSetupResponseElapsedTimeInMs(logger, "#ranLostConnectionHandler.Handle - Summary: Elapsed time for receiving and handling sctp error response from E2 terminator", request.StartTime)
65         if ok {
66                 printHandlingSetupResponseElapsedTimeInMs(logger, fmt.Sprintf("#ranLostConnectionHandler.Handle- Summary: Total roundtrip elapsed time for transactionId %s", request.TransactionId), e2session.SessionStart)
67                 delete(e2Sessions, request.TransactionId) // Avoid pinning memory (help GC)
68         }
69
70 }
71
72 func changeNodebState(logger *logger.Logger, nb *entities.NodebInfo) {
73         switch nb.ConnectionStatus{
74         case entities.ConnectionStatus_CONNECTED, entities.ConnectionStatus_CONNECTING, entities.ConnectionStatus_CONNECTED_SETUP_FAILED:
75                 nb.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
76         case entities.ConnectionStatus_DISCONNECTED:
77                 logger.Infof("#ranLostConnectionHandler.changeNodebState - nb entity with ConnectionStatus %v occurred. RanName: %s", nb.ConnectionStatus, nb.RanName)
78         default:
79                 nb.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
80         }
81 }