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=======================================================================
19 package org.commscope.tr069adapter.vesagent.service;
21 import com.google.gson.Gson;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.concurrent.TimeUnit;
28 import java.util.function.Function;
30 import org.commscope.tr069adapter.acs.common.DeviceDetails;
31 import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;
32 import org.commscope.tr069adapter.acs.common.ParameterDTO;
33 import org.commscope.tr069adapter.mapper.model.VESNotification;
34 import org.commscope.tr069adapter.vesagent.async.WaitForNotifications;
35 import org.commscope.tr069adapter.vesagent.entity.DeviceDataEntity;
36 import org.commscope.tr069adapter.vesagent.exception.VesAgentException;
37 import org.commscope.tr069adapter.vesagent.repository.VesDataRepository;
38 import org.commscope.tr069adapter.vesagent.timer.HeartBeatTimeoutTask;
39 import org.commscope.tr069adapter.vesagent.timer.ScheduleInfo;
40 import org.commscope.tr069adapter.vesagent.timer.ScheduleTaskService;
41 import org.commscope.tr069adapter.vesagent.util.VesAgentConstants;
42 import org.commscope.tr069adapter.vesagent.util.VesAgentUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.stereotype.Component;
49 public class VesAgentServiceHelper {
50 private final Logger logger = LoggerFactory.getLogger(this.getClass());
53 private Function<String, HeartBeatTimeoutTask> beanFactory;
55 public HeartBeatTimeoutTask getBeanInstance(String name) {
56 return beanFactory.apply(name);
60 VesDataRepository vesDataRepository;
63 WaitForNotifications waitForNotifications;
66 ScheduleTaskService timerService;
68 private boolean saveDeviceDataEntity(DeviceDetails deviceDetails, String eNodeBName,
69 String heartBeatPeriod, String countDownTimer) throws VesAgentException {
71 List<DeviceDataEntity> deviceDataEntityList = vesDataRepository
72 .findByDeviceIdAndAttrGroup(deviceDetails.getDeviceId(), VesAgentConstants.HEART_BEAT);
74 DeviceDataEntity deviceDataEntity = null;
75 Map<String, String> attrJsonMap = null;
77 if (null == deviceDataEntityList || deviceDataEntityList.isEmpty()) {
78 deviceDataEntity = new DeviceDataEntity();
80 deviceDataEntity.setDeviceId(deviceDetails.getDeviceId());
81 deviceDataEntity.seteNodeBName(eNodeBName);
82 deviceDataEntity.setOui(deviceDetails.getOui());
83 deviceDataEntity.setProductClass(deviceDetails.getProductClass());
84 deviceDataEntity.setAttrGroup(VesAgentConstants.HEART_BEAT);
86 attrJsonMap = new HashMap<>();
88 deviceDataEntity = deviceDataEntityList.get(0);
89 attrJsonMap = deviceDataEntity.getAttributesMap();
92 String existingHeartBeatPeriod = attrJsonMap.get(VesAgentConstants.HEART_BEAT_PERIOD);
94 if (null == heartBeatPeriod
95 && (Boolean.TRUE.equals(VesAgentUtils.isNullOrEmpty(existingHeartBeatPeriod))
96 || existingHeartBeatPeriod
97 .equalsIgnoreCase(VesAgentConstants.REMOVE_HEART_BEAT_TIMER_VAL))) {
100 if (null != countDownTimer
101 && !countDownTimer.equalsIgnoreCase(VesAgentConstants.COUNT_DOWN_TIMER_ZERO)) {
102 if (null == heartBeatPeriod || heartBeatPeriod.equalsIgnoreCase(existingHeartBeatPeriod)) {
103 String exceptionReason = "Can't change timer value if heartbeat value is same";
104 throw new VesAgentException(VesAgentConstants.INVALID_PARAMETER_VALUE, exceptionReason);
109 if (!VesAgentUtils.isNullOrEmpty(heartBeatPeriod)) {
110 attrJsonMap.put(VesAgentConstants.HEART_BEAT_PERIOD, heartBeatPeriod);
113 deviceDataEntity.setAttributesMap(attrJsonMap);
115 vesDataRepository.save(deviceDataEntity);
120 public void processHeartBeatSetRequest(DeviceRPCRequest deviceRPCRequest, String heartBeatPeriod,
121 String countDownTimer) throws VesAgentException {
123 String deviceId = deviceRPCRequest.getDeviceDetails().getDeviceId();
125 VesAgentUtils.validateDeviceId(deviceId);
127 if (VesAgentUtils.isNullOrEmpty(heartBeatPeriod)
128 && VesAgentUtils.isNullOrEmpty(countDownTimer)) {
130 "Invalid input: HeartBeatPeriod and countDownTimer both are null for device " + deviceId;
131 errorMsg = errorMsg.replaceAll("[\n|\r|\t]", "_");
132 logger.error(errorMsg);
133 throw new VesAgentException(VesAgentConstants.INVALID_PARAMETER_VALUE, errorMsg);
136 Object eNodeBNameObj = deviceRPCRequest.getContext().get(VesAgentConstants.ENODEB_NAME);
138 String eNodeBName = null;
139 if (null != eNodeBNameObj) {
140 eNodeBName = (String) eNodeBNameObj;
143 boolean resetTimerJob = saveDeviceDataEntity(deviceRPCRequest.getDeviceDetails(), eNodeBName,
144 heartBeatPeriod, countDownTimer);
147 resetTimerJob(deviceId, heartBeatPeriod, countDownTimer);
148 abortRunningDeviceConnectivityCheck(deviceRPCRequest);
153 public void processHeartBeatGetRequest(DeviceRPCRequest deviceRPCRequest) {
155 String deviceId = deviceRPCRequest.getDeviceDetails().getDeviceId();
156 List<DeviceDataEntity> deviceDataEntityList =
157 vesDataRepository.findByDeviceIdAndAttrGroup(deviceId, VesAgentConstants.HEART_BEAT);
159 if (VesAgentUtils.isNullOrEmpty(deviceDataEntityList)
160 || VesAgentUtils.isNullOrEmpty(deviceDataEntityList.get(0).getAttributesMap())) {
164 DeviceDataEntity deviceDataEntity = deviceDataEntityList.get(0);
166 List<ParameterDTO> resultparamDTOList = null;
167 List<ParameterDTO> paramDTOList = deviceRPCRequest.getOpDetails().getParmeters();
169 for (ParameterDTO paramDTO : paramDTOList) {
170 resultparamDTOList = ifDataTypeObject(paramDTO, deviceDataEntity);
172 if (!resultparamDTOList.isEmpty()) {
176 if (paramDTO.getParamName().equalsIgnoreCase(VesAgentConstants.COUNT_DOWN_TIMER)) {
177 paramDTO.setParamValue(getCountDownTimerParam(deviceDataEntity).getParamValue());
179 paramDTO.setParamValue(deviceDataEntity.getAttributesMap().get(paramDTO.getParamName()));
183 if (null != resultparamDTOList && !resultparamDTOList.isEmpty()) {
184 deviceRPCRequest.getOpDetails().setParmeters(resultparamDTOList);
188 public void processHeartBeatDeleteRequest(VESNotification vesNotification) {
189 List<ParameterDTO> paramDTOList = vesNotification.getOperationDetails().getParmeters();
191 for (ParameterDTO paramDTO : paramDTOList) {
192 if (Boolean.TRUE.equals(VesAgentUtils.isVesNotificationRequest(paramDTO))) {
193 List<DeviceDataEntity> deviceDataEntityList = vesDataRepository.findByDeviceIdAndAttrGroup(
194 vesNotification.geteNodeBName(), VesAgentConstants.HEART_BEAT);
196 if (Boolean.TRUE.equals(VesAgentUtils.isNullOrEmpty(deviceDataEntityList))) {
199 vesDataRepository.delete(deviceDataEntityList.get(0));
200 timerService.cancelSchedule(vesNotification.geteNodeBName());
206 private List<ParameterDTO> ifDataTypeObject(ParameterDTO paramDTO,
207 DeviceDataEntity deviceDataEntity) {
208 List<ParameterDTO> paramDTOList = new ArrayList<>();
210 if (null != paramDTO.getDataType()
211 && paramDTO.getDataType().equalsIgnoreCase(VesAgentConstants.OBJECT_DATA_TYPE.toLowerCase())
212 && paramDTO.getParamName().toLowerCase()
213 .contains(VesAgentConstants.HEART_BEAT.toLowerCase())) {
215 Map<String, String> attrMap = deviceDataEntity.getAttributesMap();
217 for (Map.Entry<String, String> entry : attrMap.entrySet()) {
218 ParameterDTO param = new ParameterDTO();
219 param.setParamName(entry.getKey());
220 param.setParamValue(entry.getValue());
222 paramDTOList.add(param);
225 ParameterDTO countDownParam = getCountDownTimerParam(deviceDataEntity);
226 paramDTOList.add(countDownParam);
232 private ParameterDTO getCountDownTimerParam(DeviceDataEntity deviceDataEntity) {
233 Long countDownTimerVal = timerService
234 .getTimeRemainingTillNextExecution(deviceDataEntity.getDeviceId(), TimeUnit.MINUTES);
236 ParameterDTO param = new ParameterDTO();
237 param.setParamName(VesAgentConstants.COUNT_DOWN_TIMER);
239 if (null != countDownTimerVal) {
240 param.setParamValue(countDownTimerVal.toString());
247 public void processHeartBeatGetRequest(String deviceId, Integer heartBeatPeriod,
248 Integer countDownTimer) throws VesAgentException {
249 VesAgentUtils.validateDeviceId(deviceId);
252 if (null == heartBeatPeriod && null == countDownTimer) {// this should just check if heartbeat
255 "Invalid input: HeartBeatPeriod and countDownTimer both are null for device " + deviceId;
256 logger.error(errorMsg);
257 throw new VesAgentException(errorMsg);
260 List<DeviceDataEntity> deviceDataEntityList =
261 vesDataRepository.findByDeviceIdAndAttrGroup(deviceId, VesAgentConstants.HEART_BEAT);
263 DeviceDataEntity deviceDataEntity = null;
264 Map<String, String> attrJsonMap = null;
266 if (null == deviceDataEntityList || deviceDataEntityList.isEmpty()) {
267 deviceDataEntity = new DeviceDataEntity();
268 deviceDataEntity.setDeviceId(deviceId);
269 deviceDataEntity.setAttrGroup(VesAgentConstants.HEART_BEAT);
271 attrJsonMap = new HashMap<>();
273 deviceDataEntity = deviceDataEntityList.get(0);
274 attrJsonMap = new Gson().fromJson(deviceDataEntity.getAttrJson(), Map.class);
278 if (null != heartBeatPeriod) {
279 attrJsonMap.put(VesAgentConstants.HEART_BEAT_PERIOD, heartBeatPeriod.toString());
282 if (null != countDownTimer) {
283 attrJsonMap.put(VesAgentConstants.COUNT_DOWN_TIMER, countDownTimer.toString());
286 String attrJson = new Gson().toJson(attrJsonMap);
287 deviceDataEntity.setAttrJson(attrJson);
289 vesDataRepository.save(deviceDataEntity);
292 private void resetTimerJob(String deviceId, String heartBeatPeriod, String countDownTimer) {
293 if (null == heartBeatPeriod || heartBeatPeriod.isEmpty()) {
294 scheduleTimerJob(deviceId, Integer.parseInt(countDownTimer));
295 } else if (heartBeatPeriod.equals(VesAgentConstants.REMOVE_HEART_BEAT_TIMER_VAL)) {
296 timerService.cancelSchedule(deviceId);
298 if (Boolean.FALSE.equals(VesAgentUtils.isNullOrEmpty(countDownTimer))) {
299 scheduleTimerJob(deviceId, Integer.parseInt(countDownTimer));
301 scheduleTimerJob(deviceId, Integer.parseInt(heartBeatPeriod));
306 private void scheduleTimerJob(String deviceId, Integer timeoutInterval) {
307 ScheduleInfo scheduleInfo = new ScheduleInfo();
308 scheduleInfo.setInterval(timeoutInterval);
309 scheduleInfo.setTimeUnit(TimeUnit.MINUTES);
311 HeartBeatTimeoutTask callbackTask = getBeanInstance(deviceId);
313 timerService.schedule(deviceId, scheduleInfo, callbackTask);
316 private void abortRunningDeviceConnectivityCheck(DeviceRPCRequest deviceRPCRequest) {
317 waitForNotifications.notifyResult(VesAgentUtils.getErrorResponse(deviceRPCRequest, null, null));
320 public List<DeviceDataEntity> getAllDeviceDataEntity() {
321 return (List<DeviceDataEntity>) vesDataRepository.findAll();
324 public List<DeviceDataEntity> findByDeviceIdAndGroup(String deviceId, String attrGroup) {
325 return vesDataRepository.findByDeviceIdAndAttrGroup(deviceId, attrGroup);