Copy latest code to master
[ric-plt/resource-status-manager.git] / RSM / handlers / rmrmsghandlers / resource_status_initiate_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 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20 package rmrmsghandlers
21
22 import (
23         "encoding/json"
24         "fmt"
25         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
26         "rsm/enums"
27         "rsm/logger"
28         "rsm/models"
29         "rsm/services"
30 )
31
32 type ResourceStatusInitiateNotificationHandler struct {
33         logger                *logger.Logger
34         rnibDataService       services.RNibDataService
35         resourceStatusService services.IResourceStatusService
36         requestName           string
37 }
38
39 func NewResourceStatusInitiateNotificationHandler(logger *logger.Logger, rnibDataService services.RNibDataService, resourceStatusService services.IResourceStatusService, requestName string) ResourceStatusInitiateNotificationHandler {
40         return ResourceStatusInitiateNotificationHandler{
41                 logger:                logger,
42                 rnibDataService:       rnibDataService,
43                 resourceStatusService: resourceStatusService,
44                 requestName:           requestName,
45         }
46 }
47
48 func (h ResourceStatusInitiateNotificationHandler) UnmarshalResourceStatusPayload(inventoryName string, payload []byte) (*models.ResourceStatusPayload, error) {
49         unmarshalledPayload := models.ResourceStatusPayload{}
50         err := json.Unmarshal(payload, &unmarshalledPayload)
51
52         if err != nil {
53                 h.logger.Errorf("#ResourceStatusInitiateNotificationHandler.UnmarshalResourceStatusPayload - RAN name: %s - Error unmarshaling RMR request payload: %v", inventoryName, err)
54                 return nil, err
55         }
56
57         if unmarshalledPayload.NodeType == entities.Node_UNKNOWN {
58                 h.logger.Errorf("#ResourceStatusInitiateNotificationHandler.UnmarshalResourceStatusPayload - RAN name: %s - Unknown Node Type", inventoryName)
59                 return nil, fmt.Errorf("unknown node type for RAN %s", inventoryName)
60         }
61
62         h.logger.Infof("#ResourceStatusInitiateNotificationHandler.UnmarshalResourceStatusPayload - Unmarshaled payload successfully: %+v", unmarshalledPayload)
63         return &unmarshalledPayload, nil
64
65 }
66
67 func (h ResourceStatusInitiateNotificationHandler) SaveRsmRanInfoStopTrue(inventoryName string) {
68         rsmRanInfo := models.NewRsmRanInfo(inventoryName, 0, 0, enums.Stop, true)
69         _ = h.rnibDataService.SaveRsmRanInfo(rsmRanInfo)
70 }
71
72 func (h ResourceStatusInitiateNotificationHandler) Handle(request *models.RmrRequest) {
73         inventoryName := request.RanName
74         h.logger.Infof("#ResourceStatusInitiateNotificationHandler.Handle - RAN name: %s - Received %s notification", inventoryName, h.requestName)
75
76         payload, err := h.UnmarshalResourceStatusPayload(inventoryName, request.Payload)
77
78         if err != nil {
79                 return
80         }
81
82         if payload.NodeType != entities.Node_ENB {
83                 h.logger.Debugf("#ResourceStatusInitiateNotificationHandler.Handle - RAN name: %s, Node type isn't ENB", inventoryName)
84                 return
85         }
86
87         config, err := h.rnibDataService.GetRsmGeneralConfiguration()
88
89         if err != nil {
90                 return
91         }
92
93         if !config.EnableResourceStatus {
94                 h.SaveRsmRanInfoStopTrue(inventoryName)
95                 return
96         }
97
98         nodeb, err := h.rnibDataService.GetNodeb(inventoryName)
99
100         if err != nil {
101                 return
102         }
103
104         nodebConnectionStatus := nodeb.GetConnectionStatus()
105
106         h.logger.Infof("#ResourceStatusInitiateNotificationHandler.Handle - RAN name: %s - successfully fetched RAN from db. RAN's connection status: %s", inventoryName, nodebConnectionStatus)
107
108         if nodebConnectionStatus != entities.ConnectionStatus_CONNECTED {
109                 h.logger.Errorf("#ResourceStatusInitiateNotificationHandler.Handle - RAN name: %s - RAN's connection status isn't CONNECTED", inventoryName)
110                 h.SaveRsmRanInfoStopTrue(inventoryName)
111                 return
112         }
113
114         rsmRanInfo := models.NewRsmRanInfo(inventoryName, enums.Enb1MeasurementId, 0, enums.Start, false)
115         err = h.rnibDataService.SaveRsmRanInfo(rsmRanInfo)
116
117         if err != nil {
118                 return
119         }
120
121         err = h.resourceStatusService.BuildAndSendInitiateRequest(nodeb, config, rsmRanInfo.Enb1MeasurementId)
122
123         if err != nil {
124                 h.SaveRsmRanInfoStopTrue(inventoryName)
125                 return
126         }
127 }