sync from Azure to LF
[ric-plt/e2mgr.git] / E2Manager / handlers / rmrmsghandlers / enb_load_information_notification_handler.go
1 package rmrmsghandlers
2
3 import "C"
4 import (
5         "e2mgr/converters"
6         "e2mgr/e2pdus"
7         "e2mgr/logger"
8         "e2mgr/models"
9         "e2mgr/services"
10         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
11         "time"
12 )
13
14 type EnbLoadInformationNotificationHandler struct {
15         logger          *logger.Logger
16         rnibDataService services.RNibDataService
17         extractor       converters.IEnbLoadInformationExtractor
18 }
19
20 func NewEnbLoadInformationNotificationHandler(logger *logger.Logger, rnibDataService services.RNibDataService, extractor converters.IEnbLoadInformationExtractor) EnbLoadInformationNotificationHandler {
21         return EnbLoadInformationNotificationHandler{
22                 logger:          logger,
23                 rnibDataService: rnibDataService,
24                 extractor: extractor,
25         }
26 }
27
28 func elapsed(startTime time.Time) float64 {
29         return float64(time.Since(startTime)) / float64(time.Millisecond)
30 }
31
32 func (h EnbLoadInformationNotificationHandler) Handle(request *models.NotificationRequest) {
33
34         pdu, err := converters.UnpackX2apPdu(h.logger, e2pdus.MaxAsn1CodecAllocationBufferSize, request.Len, request.Payload, e2pdus.MaxAsn1CodecMessageBufferSize)
35
36         if err != nil {
37                 h.logger.Errorf("#EnbLoadInformationNotificationHandler.Handle - RAN name: %s - Unpack failed. Error: %v", request.RanName, err)
38                 return
39         }
40
41         h.logger.Debugf("#EnbLoadInformationNotificationHandler.Handle - RAN name: %s - Unpacked message successfully", request.RanName)
42
43         ranLoadInformation := &entities.RanLoadInformation{LoadTimestamp: uint64(request.StartTime.UnixNano())}
44
45         err = h.extractor.ExtractAndBuildRanLoadInformation(pdu, ranLoadInformation)
46
47         if (err != nil) {
48                 h.logger.Errorf("#EnbLoadInformationNotificationHandler.Handle - RAN name: %s - Failed at ExtractAndBuildRanLoadInformation. Error: %v", request.RanName, err)
49                 return
50         }
51
52         h.logger.Debugf("#EnbLoadInformationNotificationHandler.Handle - RAN name: %s - Successfully done with extracting and building RAN load information. elapsed: %f ms", request.RanName, elapsed(request.StartTime))
53
54         rnibErr := h.rnibDataService.SaveRanLoadInformation(request.RanName, ranLoadInformation)
55
56         if rnibErr != nil {
57                 h.logger.Errorf("#EnbLoadInformationNotificationHandler.Handle - RAN name: %s - Failed saving RAN load information. Error: %v", request.RanName, rnibErr)
58                 return
59         }
60
61         h.logger.Infof("#EnbLoadInformationNotificationHandler.Handle - RAN name: %s - Successfully saved RAN load information to RNIB. elapsed: %f ms", request.RanName, elapsed(request.StartTime))
62 }