1 package org.commscope.tr069adapter.vesagent.service;
\r
3 import com.google.gson.Gson;
\r
5 import java.util.ArrayList;
\r
6 import java.util.HashMap;
\r
7 import java.util.List;
\r
8 import java.util.Map;
\r
9 import java.util.concurrent.TimeUnit;
\r
10 import java.util.function.Function;
\r
12 import org.commscope.tr069adapter.acs.common.DeviceDetails;
\r
13 import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;
\r
14 import org.commscope.tr069adapter.acs.common.ParameterDTO;
\r
15 import org.commscope.tr069adapter.mapper.model.VESNotification;
\r
16 import org.commscope.tr069adapter.vesagent.async.WaitForNotifications;
\r
17 import org.commscope.tr069adapter.vesagent.entity.DeviceDataEntity;
\r
18 import org.commscope.tr069adapter.vesagent.exception.VesAgentException;
\r
19 import org.commscope.tr069adapter.vesagent.repository.VesDataRepository;
\r
20 import org.commscope.tr069adapter.vesagent.timer.HeartBeatTimeoutTask;
\r
21 import org.commscope.tr069adapter.vesagent.timer.ScheduleInfo;
\r
22 import org.commscope.tr069adapter.vesagent.timer.ScheduleTaskService;
\r
23 import org.commscope.tr069adapter.vesagent.util.VesAgentConstants;
\r
24 import org.commscope.tr069adapter.vesagent.util.VesAgentUtils;
\r
25 import org.slf4j.Logger;
\r
26 import org.slf4j.LoggerFactory;
\r
27 import org.springframework.beans.factory.annotation.Autowired;
\r
28 import org.springframework.stereotype.Component;
\r
31 public class VesAgentServiceHelper {
\r
32 private final Logger logger = LoggerFactory.getLogger(this.getClass());
\r
35 private Function<String, HeartBeatTimeoutTask> beanFactory;
\r
37 public HeartBeatTimeoutTask getBeanInstance(String name) {
\r
38 return beanFactory.apply(name);
\r
42 VesDataRepository vesDataRepository;
\r
45 WaitForNotifications waitForNotifications;
\r
48 ScheduleTaskService timerService;
\r
50 private boolean saveDeviceDataEntity(DeviceDetails deviceDetails, String eNodeBName,
\r
51 String heartBeatPeriod) throws VesAgentException {
\r
53 List<DeviceDataEntity> deviceDataEntityList = vesDataRepository
\r
54 .findByDeviceIdAndAttrGroup(deviceDetails.getDeviceId(), VesAgentConstants.HEART_BEAT);
\r
56 DeviceDataEntity deviceDataEntity = null;
\r
57 Map<String, String> attrJsonMap = null;
\r
59 if (null == deviceDataEntityList || deviceDataEntityList.isEmpty()) {
\r
60 deviceDataEntity = new DeviceDataEntity();
\r
62 deviceDataEntity.setDeviceId(deviceDetails.getDeviceId());
\r
63 deviceDataEntity.seteNodeBName(eNodeBName);
\r
64 deviceDataEntity.setOui(deviceDetails.getOui());
\r
65 deviceDataEntity.setProductClass(deviceDetails.getProductClass());
\r
66 deviceDataEntity.setAttrGroup(VesAgentConstants.HEART_BEAT);
\r
68 attrJsonMap = new HashMap<>();
\r
70 deviceDataEntity = deviceDataEntityList.get(0);
\r
71 attrJsonMap = deviceDataEntity.getAttributesMap();
\r
74 String existingHeartBeatPeriod = attrJsonMap.get(VesAgentConstants.HEART_BEAT_PERIOD);
\r
76 if (null == heartBeatPeriod
\r
77 && (Boolean.TRUE.equals(VesAgentUtils.isNullOrEmpty(existingHeartBeatPeriod))
\r
78 || existingHeartBeatPeriod
\r
79 .equalsIgnoreCase(VesAgentConstants.REMOVE_HEART_BEAT_TIMER_VAL))) {
\r
83 if (!VesAgentUtils.isNullOrEmpty(heartBeatPeriod)) {
\r
84 attrJsonMap.put(VesAgentConstants.HEART_BEAT_PERIOD, heartBeatPeriod);
\r
87 deviceDataEntity.setAttributesMap(attrJsonMap);
\r
89 vesDataRepository.save(deviceDataEntity);
\r
94 public void processHeartBeatSetRequest(DeviceRPCRequest deviceRPCRequest, String heartBeatPeriod,
\r
95 String countDownTimer) throws VesAgentException {
\r
97 String deviceId = deviceRPCRequest.getDeviceDetails().getDeviceId();
\r
99 VesAgentUtils.validateDeviceId(deviceId);
\r
101 if (VesAgentUtils.isNullOrEmpty(heartBeatPeriod)
\r
102 && VesAgentUtils.isNullOrEmpty(countDownTimer)) {
\r
104 "Invalid input: HeartBeatPeriod and countDownTimer both are null for device " + deviceId;
\r
105 logger.error(errorMsg);
\r
106 throw new VesAgentException(VesAgentConstants.INVALID_PARAMETER_VALUE, errorMsg);
\r
109 Object eNodeBNameObj = deviceRPCRequest.getContext().get(VesAgentConstants.ENODEB_NAME);
\r
111 String eNodeBName = null;
\r
112 if (null != eNodeBNameObj) {
\r
113 eNodeBName = (String) eNodeBNameObj;
\r
116 boolean resetTimerJob =
\r
117 saveDeviceDataEntity(deviceRPCRequest.getDeviceDetails(), eNodeBName, heartBeatPeriod);
\r
119 if (resetTimerJob) {
\r
120 resetTimerJob(deviceId, heartBeatPeriod, countDownTimer);
\r
121 abortRunningDeviceConnectivityCheck(deviceRPCRequest);
\r
126 public void processHeartBeatGetRequest(DeviceRPCRequest deviceRPCRequest) {
\r
128 String deviceId = deviceRPCRequest.getDeviceDetails().getDeviceId();
\r
129 List<DeviceDataEntity> deviceDataEntityList =
\r
130 vesDataRepository.findByDeviceIdAndAttrGroup(deviceId, VesAgentConstants.HEART_BEAT);
\r
132 if (VesAgentUtils.isNullOrEmpty(deviceDataEntityList)
\r
133 || VesAgentUtils.isNullOrEmpty(deviceDataEntityList.get(0).getAttributesMap())) {
\r
137 DeviceDataEntity deviceDataEntity = deviceDataEntityList.get(0);
\r
139 List<ParameterDTO> resultparamDTOList = null;
\r
140 List<ParameterDTO> paramDTOList = deviceRPCRequest.getOpDetails().getParmeters();
\r
142 for (ParameterDTO paramDTO : paramDTOList) {
\r
143 resultparamDTOList = ifDataTypeObject(paramDTO, deviceDataEntity);
\r
145 if (!resultparamDTOList.isEmpty()) {
\r
149 if (paramDTO.getParamName().equalsIgnoreCase(VesAgentConstants.COUNT_DOWN_TIMER)) {
\r
150 paramDTO.setParamValue(getCountDownTimerParam(deviceDataEntity).getParamValue());
\r
152 paramDTO.setParamValue(deviceDataEntity.getAttributesMap().get(paramDTO.getParamName()));
\r
156 if (null != resultparamDTOList && !resultparamDTOList.isEmpty()) {
\r
157 deviceRPCRequest.getOpDetails().setParmeters(resultparamDTOList);
\r
161 public void processHeartBeatDeleteRequest(VESNotification vesNotification) {
\r
162 List<ParameterDTO> paramDTOList = vesNotification.getOperationDetails().getParmeters();
\r
164 for (ParameterDTO paramDTO : paramDTOList) {
\r
165 if (Boolean.TRUE.equals(VesAgentUtils.isVesNotificationRequest(paramDTO))) {
\r
166 List<DeviceDataEntity> deviceDataEntityList = vesDataRepository.findByDeviceIdAndAttrGroup(
\r
167 vesNotification.geteNodeBName(), VesAgentConstants.HEART_BEAT);
\r
169 if (Boolean.TRUE.equals(VesAgentUtils.isNullOrEmpty(deviceDataEntityList))) {
\r
172 vesDataRepository.delete(deviceDataEntityList.get(0));
\r
173 timerService.cancelSchedule(vesNotification.geteNodeBName());
\r
179 private List<ParameterDTO> ifDataTypeObject(ParameterDTO paramDTO,
\r
180 DeviceDataEntity deviceDataEntity) {
\r
181 List<ParameterDTO> paramDTOList = new ArrayList<>();
\r
183 if (null != paramDTO.getDataType()
\r
184 && paramDTO.getDataType().equalsIgnoreCase(VesAgentConstants.OBJECT_DATA_TYPE.toLowerCase())
\r
185 && paramDTO.getParamName().toLowerCase()
\r
186 .contains(VesAgentConstants.HEART_BEAT.toLowerCase())) {
\r
188 Map<String, String> attrMap = deviceDataEntity.getAttributesMap();
\r
190 for (Map.Entry<String, String> entry : attrMap.entrySet()) {
\r
191 ParameterDTO param = new ParameterDTO();
\r
192 param.setParamName(entry.getKey());
\r
193 param.setParamValue(entry.getValue());
\r
195 paramDTOList.add(param);
\r
198 ParameterDTO countDownParam = getCountDownTimerParam(deviceDataEntity);
\r
199 paramDTOList.add(countDownParam);
\r
202 return paramDTOList;
\r
205 private ParameterDTO getCountDownTimerParam(DeviceDataEntity deviceDataEntity) {
\r
206 Long countDownTimerVal = timerService
\r
207 .getTimeRemainingTillNextExecution(deviceDataEntity.getDeviceId(), TimeUnit.MINUTES);
\r
209 ParameterDTO param = new ParameterDTO();
\r
210 param.setParamName(VesAgentConstants.COUNT_DOWN_TIMER);
\r
212 if (null != countDownTimerVal) {
\r
213 param.setParamValue(countDownTimerVal.toString());
\r
220 public void processHeartBeatGetRequest(String deviceId, Integer HeartBeatPeriod,
\r
221 Integer countDownTimer) throws VesAgentException {
\r
222 VesAgentUtils.validateDeviceId(deviceId);
\r
225 if (null == HeartBeatPeriod && null == countDownTimer) {// this should just check if heartbeat
\r
228 "Invalid input: HeartBeatPeriod and countDownTimer both are null for device " + deviceId;
\r
229 logger.error(errorMsg);
\r
230 throw new VesAgentException(errorMsg);
\r
233 List<DeviceDataEntity> deviceDataEntityList =
\r
234 vesDataRepository.findByDeviceIdAndAttrGroup(deviceId, VesAgentConstants.HEART_BEAT);
\r
236 DeviceDataEntity deviceDataEntity = null;
\r
237 Map<String, String> attrJsonMap = null;
\r
239 if (null == deviceDataEntityList || deviceDataEntityList.isEmpty()) {
\r
240 deviceDataEntity = new DeviceDataEntity();
\r
241 deviceDataEntity.setDeviceId(deviceId);
\r
242 deviceDataEntity.setAttrGroup(VesAgentConstants.HEART_BEAT);
\r
244 attrJsonMap = new HashMap<String, String>();
\r
246 deviceDataEntity = deviceDataEntityList.get(0);
\r
247 attrJsonMap = new Gson().fromJson(deviceDataEntity.getAttrJson(), Map.class);
\r
251 if (null != HeartBeatPeriod) {
\r
252 attrJsonMap.put(VesAgentConstants.HEART_BEAT_PERIOD, HeartBeatPeriod.toString());
\r
255 if (null != countDownTimer) {
\r
256 attrJsonMap.put(VesAgentConstants.COUNT_DOWN_TIMER, countDownTimer.toString());
\r
259 String attrJson = new Gson().toJson(attrJsonMap);
\r
260 deviceDataEntity.setAttrJson(attrJson);
\r
262 vesDataRepository.save(deviceDataEntity);
\r
265 private void resetTimerJob(String deviceId, String heartBeatPeriod, String countDownTimer) {
\r
266 if (null == heartBeatPeriod || heartBeatPeriod.isEmpty()) {
\r
267 scheduleTimerJob(deviceId, Integer.parseInt(countDownTimer));
\r
268 } else if (heartBeatPeriod.equals(VesAgentConstants.REMOVE_HEART_BEAT_TIMER_VAL)) {
\r
269 timerService.cancelSchedule(deviceId);
\r
271 if (Boolean.FALSE.equals(VesAgentUtils.isNullOrEmpty(countDownTimer))) {
\r
272 scheduleTimerJob(deviceId, Integer.parseInt(countDownTimer));
\r
274 scheduleTimerJob(deviceId, Integer.parseInt(heartBeatPeriod));
\r
279 private void scheduleTimerJob(String deviceId, Integer timeoutInterval) {
\r
280 ScheduleInfo scheduleInfo = new ScheduleInfo();
\r
281 scheduleInfo.setInterval(timeoutInterval);
\r
282 scheduleInfo.setTimeUnit(TimeUnit.MINUTES);
\r
284 HeartBeatTimeoutTask callbackTask = getBeanInstance(deviceId);
\r
286 timerService.schedule(deviceId, scheduleInfo, callbackTask);
\r
289 private void abortRunningDeviceConnectivityCheck(DeviceRPCRequest deviceRPCRequest) {
\r
290 waitForNotifications.notifyResult(VesAgentUtils.getErrorResponse(deviceRPCRequest, null, null));
\r
293 public List<DeviceDataEntity> getAllDeviceDataEntity() {
\r
294 return (List<DeviceDataEntity>) vesDataRepository.findAll();
\r
297 public List<DeviceDataEntity> findByDeviceIdAndGroup(String deviceId, String attrGroup) {
\r
298 return vesDataRepository.findByDeviceIdAndAttrGroup(deviceId, attrGroup);
\r