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