ceee190827583d31be1b61407a6779de607b4a9f
[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/rNibWriter"
10         "e2mgr/sessions"
11         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
12         "time"
13 )
14
15 type EnbLoadInformationNotificationHandler struct {
16         rnibWriterProvider func() rNibWriter.RNibWriter
17 }
18
19 func NewEnbLoadInformationNotificationHandler(rnibWriterProvider func() rNibWriter.RNibWriter) EnbLoadInformationNotificationHandler {
20         return EnbLoadInformationNotificationHandler{
21                 rnibWriterProvider: rnibWriterProvider,
22         }
23 }
24
25 func elapsed(startTime time.Time) float64 {
26         return float64(time.Since(startTime)) / float64(time.Millisecond)
27 }
28
29 func (src EnbLoadInformationNotificationHandler) Handle(logger *logger.Logger, e2Sessions sessions.E2Sessions, request *models.NotificationRequest, messageChannel chan<- *models.NotificationResponse) {
30
31         pdu, err := converters.UnpackX2apPdu(logger, e2pdus.MaxAsn1CodecAllocationBufferSize, request.Len, request.Payload, e2pdus.MaxAsn1CodecMessageBufferSize)
32
33         if err != nil {
34                 logger.Errorf("#EnbLoadInformationNotificationHandler.Handle - RAN name: %s - Unpack failed. Error: %v", request.RanName, err)
35                 return
36         }
37
38         logger.Debugf("#EnbLoadInformationNotificationHandler.Handle - RAN name: %s - Unpacked message successfully", request.RanName)
39
40         ranLoadInformation := &entities.RanLoadInformation{LoadTimestamp: uint64(request.StartTime.UnixNano())}
41
42         err = converters.ExtractAndBuildRanLoadInformation(pdu, ranLoadInformation)
43
44         if (err != nil) {
45                 logger.Errorf("#EnbLoadInformationNotificationHandler.Handle - RAN name: %s - Failed at ExtractAndBuildRanLoadInformation. Error: %v", request.RanName, err)
46                 return
47         }
48
49         logger.Debugf("#EnbLoadInformationNotificationHandler.Handle - RAN name: %s - Successfully done with extracting and building RAN load information. elapsed: %f ms", request.RanName, elapsed(request.StartTime))
50
51         rnibErr := src.rnibWriterProvider().SaveRanLoadInformation(request.RanName, ranLoadInformation)
52
53         if rnibErr != nil {
54                 logger.Errorf("#EnbLoadInformationNotificationHandler.Handle - RAN name: %s - Failed saving RAN load information. Error: %v", request.RanName, rnibErr)
55                 return
56         }
57
58         logger.Infof("#EnbLoadInformationNotificationHandler.Handle - RAN name: %s - Successfully saved RAN load information to RNIB. elapsed: %f ms", request.RanName, elapsed(request.StartTime))
59 }