Added timer & change status to connected state.
[ric-plt/e2mgr.git] / E2Manager / handlers / rmrmsghandlers / e2_reset_request_handler.go
1 //\r
2 // Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved.\r
3 //\r
4 // Licensed under the Apache License, Version 2.0 (the "License");\r
5 // you may not use this file except in compliance with the License.\r
6 // You may obtain a copy of the License at\r
7 //\r
8 //      http://www.apache.org/licenses/LICENSE-2.0\r
9 //\r
10 // Unless required by applicable law or agreed to in writing, software\r
11 // distributed under the License is distributed on an "AS IS" BASIS,\r
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 // See the License for the specific language governing permissions and\r
14 // limitations under the License.\r
15 \r
16 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)\r
17 //  platform project (RICP).\r
18 \r
19 package rmrmsghandlers\r
20 \r
21 import (\r
22         "e2mgr/configuration"\r
23         "e2mgr/logger"\r
24         "e2mgr/models"\r
25         "e2mgr/services"\r
26         "e2mgr/utils"\r
27         "time"\r
28 \r
29         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"\r
30 )\r
31 \r
32 const E2ResetRequestLogInfoElapsedTime = "#E2ResetRequestNotificationHandler.Handle - Summary: elapsed time for receiving and handling reset request message from E2 terminator: %f ms"\r
33 \r
34 type E2ResetRequestNotificationHandler struct {\r
35         logger          *logger.Logger\r
36         rnibDataService services.RNibDataService\r
37         config          *configuration.Configuration\r
38 }\r
39 \r
40 func NewE2ResetRequestNotificationHandler(logger *logger.Logger, rnibDataService services.RNibDataService, config *configuration.Configuration) *E2ResetRequestNotificationHandler {\r
41         return &E2ResetRequestNotificationHandler{\r
42                 logger:          logger,\r
43                 rnibDataService: rnibDataService,\r
44                 config:          config,\r
45         }\r
46 }\r
47 \r
48 func (e *E2ResetRequestNotificationHandler) Handle(request *models.NotificationRequest) {\r
49 \r
50         e.logger.Infof("#E2ResetRequestNotificationHandler.Handle - RAN name: %s - received E2_Reset. Payload: %x", request.RanName, request.Payload)\r
51 \r
52         e.logger.Debugf("#E2ResetRequestNotificationHandler.Handle - RIC_E2_Node_Reset parsed successfully ")\r
53 \r
54         nodebInfo, err := e.getNodebInfo(request.RanName)\r
55         if err != nil {\r
56                 e.logger.Errorf("#E2ResetRequestNotificationHandler.Handle - failed to retrieve nodeB entity. RanName: %s. Error: %s", request.RanName, err.Error())\r
57                 e.logger.Infof(E2ResetRequestLogInfoElapsedTime, utils.ElapsedTime(request.StartTime))\r
58                 return\r
59         }\r
60 \r
61         e.logger.Debugf("#E2ResetRequestNotificationHandler.Handle - nodeB entity retrieved. RanName %s, ConnectionStatus %s", nodebInfo.RanName, nodebInfo.ConnectionStatus)\r
62 \r
63         nodebInfo.ConnectionStatus = entities.ConnectionStatus_UNDER_RESET\r
64 \r
65         err = e.rnibDataService.UpdateNodebInfoAndPublish(nodebInfo)\r
66 \r
67         if err != nil {\r
68                 e.logger.Errorf("#E2ResetRequestNotificationHandler.Handle - failed to update connection status of nodeB entity. RanName: %s. Error: %s", request.RanName, err.Error())\r
69         }\r
70 \r
71         e.logger.Debugf("#E2ResetRequestNotificationHandler.Handle - nodeB entity under reset state. RanName %s, ConnectionStatus %s", nodebInfo.RanName, nodebInfo.ConnectionStatus)\r
72 \r
73         e.logger.Infof(E2ResetRequestLogInfoElapsedTime, utils.ElapsedTime(request.StartTime))\r
74 \r
75         e.waitfortimertimeout(request)\r
76 \r
77         nodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTED\r
78 \r
79         err = e.rnibDataService.UpdateNodebInfoAndPublish(nodebInfo)\r
80 \r
81         if err != nil {\r
82                 e.logger.Errorf("#E2ResetRequestNotificationHandler.Handle - failed to update connection status of nodeB entity. RanName: %s. Error: %s", request.RanName, err.Error())\r
83         }\r
84 \r
85         e.logger.Debugf("#E2ResetRequestNotificationHandler.Handle - nodeB entity connected state. RanName %s, ConnectionStatus %s", nodebInfo.RanName, nodebInfo.ConnectionStatus)\r
86 \r
87 }\r
88 \r
89 func (e *E2ResetRequestNotificationHandler) getNodebInfo(ranName string) (*entities.NodebInfo, error) {\r
90 \r
91         nodebInfo, err := e.rnibDataService.GetNodeb(ranName)\r
92         if err != nil {\r
93                 e.logger.Errorf("#E2ResetRequestNotificationHandler.Handle - failed to retrieve nodeB entity. RanName: %s. Error: %s", ranName, err.Error())\r
94                 return nil, err\r
95         }\r
96         return nodebInfo, err\r
97 }\r
98 \r
99 func (e *E2ResetRequestNotificationHandler) waitfortimertimeout(request *models.NotificationRequest) {\r
100         timeout := e.config.E2ResetTimeOutSec\r
101         for {\r
102                 timeElapsed := utils.ElapsedTime(request.StartTime)\r
103                 e.logger.Infof(E2ResetRequestLogInfoElapsedTime, utils.ElapsedTime(request.StartTime))\r
104                 if int(timeElapsed) > timeout {\r
105                         break\r
106                 }\r
107                 time.Sleep(time.Duration(timeout/100) * time.Millisecond)\r
108         }\r
109 }\r