2 * ============LICENSE_START========================================================================
3 * ONAP : tr-069-adapter
4 * =================================================================================================
5 * Copyright (C) 2020 CommScope Inc Intellectual Property.
6 * =================================================================================================
7 * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
9 * may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14 * either express or implied. See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ===============LICENSE_END=======================================================================
18 package org.commscope.tr069adapter.vesagent.controller;
20 import com.fasterxml.jackson.databind.ObjectMapper;
22 import java.util.ArrayList;
23 import java.util.List;
25 import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;
26 import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;
27 import org.commscope.tr069adapter.acs.common.OperationDetails;
28 import org.commscope.tr069adapter.acs.common.ParameterDTO;
29 import org.commscope.tr069adapter.mapper.model.VESNotification;
30 import org.commscope.tr069adapter.mapper.model.VESNotificationResponse;
31 import org.commscope.tr069adapter.vesagent.VesConfiguration;
32 import org.commscope.tr069adapter.vesagent.entity.DeviceDataEntity;
33 import org.commscope.tr069adapter.vesagent.exception.VesAgentException;
34 import org.commscope.tr069adapter.vesagent.http.HttpRequestSender;
35 import org.commscope.tr069adapter.vesagent.model.CommonEventHeader;
36 import org.commscope.tr069adapter.vesagent.model.Event;
37 import org.commscope.tr069adapter.vesagent.model.EventMessage;
38 import org.commscope.tr069adapter.vesagent.model.HeartbeatFields;
39 import org.commscope.tr069adapter.vesagent.service.VesAgentServiceHelper;
40 import org.commscope.tr069adapter.vesagent.util.VesAgentConstants;
41 import org.commscope.tr069adapter.vesagent.util.VesAgentUtils;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.stereotype.Component;
48 public class HeartBeatMessageHandler {
50 private static final Logger logger = LoggerFactory.getLogger(HeartBeatMessageHandler.class);
53 VesAgentServiceHelper vesAgentServiceHelper;
56 VesConfiguration config;
59 HttpRequestSender sender;
61 public VESNotificationResponse sendHeartBeatEvent(DeviceDataEntity deviceDataEntity,
62 int heartBeatPeriod) throws Exception {
63 Event event = convertNotificationToVESEvent(deviceDataEntity, heartBeatPeriod);
65 EventMessage evMsg = new EventMessage();
66 evMsg.setEvent(event);
68 ObjectMapper mapper = new ObjectMapper();
70 String requestBody = mapper.writeValueAsString(evMsg);
71 if (requestBody.isEmpty()) {
72 logger.debug("VES Event body is empty");
75 String url = config.getPnfRegVesUrl();
76 return sender.postRequest(url, requestBody);
79 public void handleRegisterRequest(VESNotification vesNotification) {
81 VesAgentUtils.validateVESNotification(vesNotification);
82 } catch (VesAgentException e) {
84 "Failed to create heartbeat timers for device on recieving bootstrap. ErrorMsg: {}",
89 List<DeviceDataEntity> deviceDataEntityList = vesAgentServiceHelper.findByDeviceIdAndGroup(
90 vesNotification.getDevnotification().getDeviceDetails().getDeviceId(),
91 VesAgentConstants.HEART_BEAT);
93 if (Boolean.FALSE.equals(VesAgentUtils.isNullOrEmpty(deviceDataEntityList))) {
94 DeviceDataEntity deviceDataEntity = deviceDataEntityList.get(0);
95 String heartbeatPeriod =
96 deviceDataEntity.getAttributesMap().get(VesAgentConstants.HEART_BEAT_PERIOD);
98 if (!VesAgentUtils.isNullOrEmpty(heartbeatPeriod)
99 && !heartbeatPeriod.equalsIgnoreCase(VesAgentConstants.REMOVE_HEART_BEAT_TIMER_VAL)) {
104 logger.info("Creating heartbeat records and timer using default heartbeatPeriod {} minutes.",
105 VesAgentConstants.HEART_BEAT_PERIOD_DEFAULT_VAL);
107 DeviceRPCRequest deviceRPCRequest = new DeviceRPCRequest();
108 deviceRPCRequest.setDeviceDetails(vesNotification.getDevnotification().getDeviceDetails());
110 OperationDetails opDetails = new OperationDetails();
112 ParameterDTO heartBeatPeriodParam = new ParameterDTO();
113 heartBeatPeriodParam.setParamName(VesAgentConstants.HEART_BEAT_PERIOD);
114 heartBeatPeriodParam.setParamValue(VesAgentConstants.HEART_BEAT_PERIOD_DEFAULT_VAL);
116 List<ParameterDTO> paramList = new ArrayList<>();
117 paramList.add(heartBeatPeriodParam);
119 opDetails.setParmeters(paramList);
120 deviceRPCRequest.setOpDetails(opDetails);
122 deviceRPCRequest.addContextParam(VesAgentConstants.ENODEB_NAME,
123 vesNotification.geteNodeBName());
125 handleSetConfigRequest(deviceRPCRequest);
129 public DeviceRPCResponse handleSetConfigRequest(DeviceRPCRequest deviceRPCRequest) {
131 VesAgentUtils.validateDeviceRPCRequest(deviceRPCRequest);
132 } catch (VesAgentException e) {
133 return VesAgentUtils.getErrorResponse(deviceRPCRequest, e.getErrorCode(), e.getMessage());
136 OperationDetails operationDetails = deviceRPCRequest.getOpDetails();
138 String heartBeatPeriod = null;
139 String countDownTimer = null;
141 ParameterDTO countDownTimerDTO = null;
143 List<ParameterDTO> paramDTOList = operationDetails.getParmeters();
144 for (ParameterDTO paramDTO : paramDTOList) {
145 if (paramDTO.getParamName().equalsIgnoreCase(VesAgentConstants.HEART_BEAT_PERIOD)) {
146 heartBeatPeriod = paramDTO.getParamValue();
149 if (paramDTO.getParamName().equalsIgnoreCase(VesAgentConstants.COUNT_DOWN_TIMER)) {
150 countDownTimer = paramDTO.getParamValue();
151 countDownTimerDTO = paramDTO;
156 vesAgentServiceHelper.processHeartBeatSetRequest(deviceRPCRequest, heartBeatPeriod,
158 } catch (VesAgentException e) {
159 return VesAgentUtils.getErrorResponse(deviceRPCRequest, e.getErrorCode(), e.getMessage());
162 copyHeartBeatPeriodToTimerForResponse(countDownTimerDTO, heartBeatPeriod);
163 return VesAgentUtils.getSuccessResponse(deviceRPCRequest);
166 private void copyHeartBeatPeriodToTimerForResponse(ParameterDTO countDownParam,
167 String heartBeatPeriod) {
168 if (null != countDownParam && !VesAgentUtils.isNullOrEmpty(heartBeatPeriod)) {
169 countDownParam.setParamValue(heartBeatPeriod);
173 public DeviceRPCResponse handleGetConfigRequest(DeviceRPCRequest deviceRPCRequest) {
175 VesAgentUtils.validateDeviceRPCRequest(deviceRPCRequest);
176 } catch (VesAgentException e) {
177 return VesAgentUtils.getErrorResponse(deviceRPCRequest, e.getErrorCode(), e.getMessage());
180 vesAgentServiceHelper.processHeartBeatGetRequest(deviceRPCRequest);
181 return VesAgentUtils.getSuccessResponse(deviceRPCRequest);
184 public VESNotificationResponse handleDeleteConfigRequest(VESNotification vesNotification) {
186 VesAgentUtils.validateDelVESNotification(vesNotification);
187 vesAgentServiceHelper.processHeartBeatDeleteRequest(vesNotification);
188 } catch (VesAgentException e) {
189 return new VESNotificationResponse(Integer.parseInt(e.getErrorCode()), e.getMessage());
190 } catch (Exception e) {
191 return new VESNotificationResponse(VesAgentConstants.RPC_FAILED, e.getMessage());
194 return new VESNotificationResponse(VesAgentConstants.RPC_SUCCESS, "success");
197 Event convertNotificationToVESEvent(DeviceDataEntity deviceDataEntity, int heartBeatPeriod) {
198 Event hbEvent = new Event();
199 CommonEventHeader eventHeader = new CommonEventHeader();
201 eventHeader.setDomain("heartbeat");
203 .setEventId("heartbeat" + deviceDataEntity.getDeviceId() + System.currentTimeMillis());
204 eventHeader.setEventName(
205 "heartbeat_" + deviceDataEntity.getProductClass() + "-" + config.getVendorName());
206 eventHeader.setEventType("CommScope_RAN_heartbeat");
207 eventHeader.setLastEpochMicrosec(System.currentTimeMillis() * 1000);
209 eventHeader.setPriority("High");
210 eventHeader.setSequence(0);
212 if (deviceDataEntity.geteNodeBName() == null) {
213 eventHeader.setReportingEntityName(deviceDataEntity.getDeviceId());
214 eventHeader.setReportingEntityId(deviceDataEntity.getDeviceId());
215 eventHeader.setSourceId(deviceDataEntity.getDeviceId());
216 eventHeader.setSourceName(deviceDataEntity.getDeviceId());
218 eventHeader.setReportingEntityName(deviceDataEntity.geteNodeBName());
219 eventHeader.setSourceName(deviceDataEntity.geteNodeBName());
220 eventHeader.setReportingEntityId(deviceDataEntity.getDeviceId());
221 eventHeader.setSourceId(deviceDataEntity.getDeviceId());
224 eventHeader.setStartEpochMicrosec(deviceDataEntity.getStartEpochMicrosec());
225 eventHeader.setVersion(config.getEventVersion());
226 eventHeader.setNfNamingCode("");
227 eventHeader.setNfcNamingCode("");
228 eventHeader.setNfVendorName(config.getVendorName());
229 eventHeader.setVesEventListenerVersion(config.getVesVersion());
230 hbEvent.setCommonEventHeader(eventHeader);
232 HeartbeatFields heartbeatFields = new HeartbeatFields();
233 heartbeatFields.setHeartbeatFieldsVersion("3.0");
234 heartbeatFields.setHeartbeatInterval(heartBeatPeriod * 60);
235 hbEvent.setHeartbeatFields(heartbeatFields);