sync R3 content from Azure
[ric-plt/e2mgr.git] / E2Manager / handlers / rmrmsghandlers / enb_load_information_notification_handler.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 package rmrmsghandlers
18
19 import (
20         "e2mgr/converters"
21         "e2mgr/e2pdus"
22         "e2mgr/logger"
23         "e2mgr/models"
24         "e2mgr/services"
25         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
26         "time"
27 )
28
29 type EnbLoadInformationNotificationHandler struct {
30         logger          *logger.Logger
31         rnibDataService services.RNibDataService
32         extractor       converters.IEnbLoadInformationExtractor
33 }
34
35 func NewEnbLoadInformationNotificationHandler(logger *logger.Logger, rnibDataService services.RNibDataService, extractor converters.IEnbLoadInformationExtractor) EnbLoadInformationNotificationHandler {
36         return EnbLoadInformationNotificationHandler{
37                 logger:          logger,
38                 rnibDataService: rnibDataService,
39                 extractor: extractor,
40         }
41 }
42
43 func elapsed(startTime time.Time) float64 {
44         return float64(time.Since(startTime)) / float64(time.Millisecond)
45 }
46
47 func (h EnbLoadInformationNotificationHandler) Handle(request *models.NotificationRequest) {
48
49         pdu, err := converters.UnpackX2apPdu(h.logger, e2pdus.MaxAsn1CodecAllocationBufferSize, request.Len, request.Payload, e2pdus.MaxAsn1CodecMessageBufferSize)
50
51         if err != nil {
52                 h.logger.Errorf("#EnbLoadInformationNotificationHandler.Handle - RAN name: %s - Unpack failed. Error: %v", request.RanName, err)
53                 return
54         }
55
56         h.logger.Debugf("#EnbLoadInformationNotificationHandler.Handle - RAN name: %s - Unpacked message successfully", request.RanName)
57
58         ranLoadInformation := &entities.RanLoadInformation{LoadTimestamp: uint64(request.StartTime.UnixNano())}
59
60         err = h.extractor.ExtractAndBuildRanLoadInformation(pdu, ranLoadInformation)
61
62         if err != nil {
63                 h.logger.Errorf("#EnbLoadInformationNotificationHandler.Handle - RAN name: %s - Failed at ExtractAndBuildRanLoadInformation. Error: %v", request.RanName, err)
64                 return
65         }
66
67         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))
68
69         rnibErr := h.rnibDataService.SaveRanLoadInformation(request.RanName, ranLoadInformation)
70
71         if rnibErr != nil {
72                 h.logger.Errorf("#EnbLoadInformationNotificationHandler.Handle - RAN name: %s - Failed saving RAN load information. Error: %v", request.RanName, rnibErr)
73                 return
74         }
75
76         h.logger.Infof("#EnbLoadInformationNotificationHandler.Handle - RAN name: %s - Successfully saved RAN load information to RNIB. elapsed: %f ms", request.RanName, elapsed(request.StartTime))
77 }