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;
\r
21 import com.google.gson.Gson;
\r
23 import java.util.ArrayList;
\r
24 import java.util.HashMap;
\r
25 import java.util.List;
\r
26 import java.util.Map;
\r
27 import java.util.concurrent.TimeUnit;
\r
28 import java.util.function.Function;
\r
30 import org.commscope.tr069adapter.acs.common.DeviceDetails;
\r
31 import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;
\r
32 import org.commscope.tr069adapter.acs.common.ParameterDTO;
\r
33 import org.commscope.tr069adapter.mapper.model.VESNotification;
\r
34 import org.commscope.tr069adapter.vesagent.async.WaitForNotifications;
\r
35 import org.commscope.tr069adapter.vesagent.entity.DeviceDataEntity;
\r
36 import org.commscope.tr069adapter.vesagent.exception.VesAgentException;
\r
37 import org.commscope.tr069adapter.vesagent.repository.VesDataRepository;
\r
38 import org.commscope.tr069adapter.vesagent.timer.HeartBeatTimeoutTask;
\r
39 import org.commscope.tr069adapter.vesagent.timer.ScheduleInfo;
\r
40 import org.commscope.tr069adapter.vesagent.timer.ScheduleTaskService;
\r
41 import org.commscope.tr069adapter.vesagent.util.VesAgentConstants;
\r
42 import org.commscope.tr069adapter.vesagent.util.VesAgentUtils;
\r
43 import org.slf4j.Logger;
\r
44 import org.slf4j.LoggerFactory;
\r
45 import org.springframework.beans.factory.annotation.Autowired;
\r
46 import org.springframework.stereotype.Component;
\r
49 public class VesAgentServiceHelper {
\r
50 private final Logger logger = LoggerFactory.getLogger(this.getClass());
\r
53 private Function<String, HeartBeatTimeoutTask> beanFactory;
\r
55 public HeartBeatTimeoutTask getBeanInstance(String name) {
\r
56 return beanFactory.apply(name);
\r
60 VesDataRepository vesDataRepository;
\r
63 WaitForNotifications waitForNotifications;
\r
66 ScheduleTaskService timerService;
\r
68 private boolean saveDeviceDataEntity(DeviceDetails deviceDetails, String eNodeBName,
\r
69 String heartBeatPeriod) throws VesAgentException {
\r
71 List<DeviceDataEntity> deviceDataEntityList = vesDataRepository
\r
72 .findByDeviceIdAndAttrGroup(deviceDetails.getDeviceId(), VesAgentConstants.HEART_BEAT);
\r
74 DeviceDataEntity deviceDataEntity = null;
\r
75 Map<String, String> attrJsonMap = null;
\r
77 if (null == deviceDataEntityList || deviceDataEntityList.isEmpty()) {
\r
78 deviceDataEntity = new DeviceDataEntity();
\r
80 deviceDataEntity.setDeviceId(deviceDetails.getDeviceId());
\r
81 deviceDataEntity.seteNodeBName(eNodeBName);
\r
82 deviceDataEntity.setOui(deviceDetails.getOui());
\r
83 deviceDataEntity.setProductClass(deviceDetails.getProductClass());
\r
84 deviceDataEntity.setAttrGroup(VesAgentConstants.HEART_BEAT);
\r
86 attrJsonMap = new HashMap<>();
\r
88 deviceDataEntity = deviceDataEntityList.get(0);
\r
89 attrJsonMap = deviceDataEntity.getAttributesMap();
\r
92 String existingHeartBeatPeriod = attrJsonMap.get(VesAgentConstants.HEART_BEAT_PERIOD);
\r
94 if (null == heartBeatPeriod
\r
95 && (Boolean.TRUE.equals(VesAgentUtils.isNullOrEmpty(existingHeartBeatPeriod))
\r
96 || existingHeartBeatPeriod
\r
97 .equalsIgnoreCase(VesAgentConstants.REMOVE_HEART_BEAT_TIMER_VAL))) {
\r
101 if (!VesAgentUtils.isNullOrEmpty(heartBeatPeriod)) {
\r
102 attrJsonMap.put(VesAgentConstants.HEART_BEAT_PERIOD, heartBeatPeriod);
\r
105 deviceDataEntity.setAttributesMap(attrJsonMap);
\r
107 vesDataRepository.save(deviceDataEntity);
\r
112 public void processHeartBeatSetRequest(DeviceRPCRequest deviceRPCRequest, String heartBeatPeriod,
\r
113 String countDownTimer) throws VesAgentException {
\r
115 String deviceId = deviceRPCRequest.getDeviceDetails().getDeviceId();
\r
117 VesAgentUtils.validateDeviceId(deviceId);
\r
119 if (VesAgentUtils.isNullOrEmpty(heartBeatPeriod)
\r
120 && VesAgentUtils.isNullOrEmpty(countDownTimer)) {
\r
122 "Invalid input: HeartBeatPeriod and countDownTimer both are null for device " + deviceId;
\r
123 logger.error(errorMsg);
\r
124 throw new VesAgentException(VesAgentConstants.INVALID_PARAMETER_VALUE, errorMsg);
\r
127 Object eNodeBNameObj = deviceRPCRequest.getContext().get(VesAgentConstants.ENODEB_NAME);
\r
129 String eNodeBName = null;
\r
130 if (null != eNodeBNameObj) {
\r
131 eNodeBName = (String) eNodeBNameObj;
\r
134 boolean resetTimerJob =
\r
135 saveDeviceDataEntity(deviceRPCRequest.getDeviceDetails(), eNodeBName, heartBeatPeriod);
\r
137 if (resetTimerJob) {
\r
138 resetTimerJob(deviceId, heartBeatPeriod, countDownTimer);
\r
139 abortRunningDeviceConnectivityCheck(deviceRPCRequest);
\r
144 public void processHeartBeatGetRequest(DeviceRPCRequest deviceRPCRequest) {
\r
146 String deviceId = deviceRPCRequest.getDeviceDetails().getDeviceId();
\r
147 List<DeviceDataEntity> deviceDataEntityList =
\r
148 vesDataRepository.findByDeviceIdAndAttrGroup(deviceId, VesAgentConstants.HEART_BEAT);
\r
150 if (VesAgentUtils.isNullOrEmpty(deviceDataEntityList)
\r
151 || VesAgentUtils.isNullOrEmpty(deviceDataEntityList.get(0).getAttributesMap())) {
\r
155 DeviceDataEntity deviceDataEntity = deviceDataEntityList.get(0);
\r
157 List<ParameterDTO> resultparamDTOList = null;
\r
158 List<ParameterDTO> paramDTOList = deviceRPCRequest.getOpDetails().getParmeters();
\r
160 for (ParameterDTO paramDTO : paramDTOList) {
\r
161 resultparamDTOList = ifDataTypeObject(paramDTO, deviceDataEntity);
\r
163 if (!resultparamDTOList.isEmpty()) {
\r
167 if (paramDTO.getParamName().equalsIgnoreCase(VesAgentConstants.COUNT_DOWN_TIMER)) {
\r
168 paramDTO.setParamValue(getCountDownTimerParam(deviceDataEntity).getParamValue());
\r
170 paramDTO.setParamValue(deviceDataEntity.getAttributesMap().get(paramDTO.getParamName()));
\r
174 if (null != resultparamDTOList && !resultparamDTOList.isEmpty()) {
\r
175 deviceRPCRequest.getOpDetails().setParmeters(resultparamDTOList);
\r
179 public void processHeartBeatDeleteRequest(VESNotification vesNotification) {
\r
180 List<ParameterDTO> paramDTOList = vesNotification.getOperationDetails().getParmeters();
\r
182 for (ParameterDTO paramDTO : paramDTOList) {
\r
183 if (Boolean.TRUE.equals(VesAgentUtils.isVesNotificationRequest(paramDTO))) {
\r
184 List<DeviceDataEntity> deviceDataEntityList = vesDataRepository.findByDeviceIdAndAttrGroup(
\r
185 vesNotification.geteNodeBName(), VesAgentConstants.HEART_BEAT);
\r
187 if (Boolean.TRUE.equals(VesAgentUtils.isNullOrEmpty(deviceDataEntityList))) {
\r
190 vesDataRepository.delete(deviceDataEntityList.get(0));
\r
191 timerService.cancelSchedule(vesNotification.geteNodeBName());
\r
197 private List<ParameterDTO> ifDataTypeObject(ParameterDTO paramDTO,
\r
198 DeviceDataEntity deviceDataEntity) {
\r
199 List<ParameterDTO> paramDTOList = new ArrayList<>();
\r
201 if (null != paramDTO.getDataType()
\r
202 && paramDTO.getDataType().equalsIgnoreCase(VesAgentConstants.OBJECT_DATA_TYPE.toLowerCase())
\r
203 && paramDTO.getParamName().toLowerCase()
\r
204 .contains(VesAgentConstants.HEART_BEAT.toLowerCase())) {
\r
206 Map<String, String> attrMap = deviceDataEntity.getAttributesMap();
\r
208 for (Map.Entry<String, String> entry : attrMap.entrySet()) {
\r
209 ParameterDTO param = new ParameterDTO();
\r
210 param.setParamName(entry.getKey());
\r
211 param.setParamValue(entry.getValue());
\r
213 paramDTOList.add(param);
\r
216 ParameterDTO countDownParam = getCountDownTimerParam(deviceDataEntity);
\r
217 paramDTOList.add(countDownParam);
\r
220 return paramDTOList;
\r
223 private ParameterDTO getCountDownTimerParam(DeviceDataEntity deviceDataEntity) {
\r
224 Long countDownTimerVal = timerService
\r
225 .getTimeRemainingTillNextExecution(deviceDataEntity.getDeviceId(), TimeUnit.MINUTES);
\r
227 ParameterDTO param = new ParameterDTO();
\r
228 param.setParamName(VesAgentConstants.COUNT_DOWN_TIMER);
\r
230 if (null != countDownTimerVal) {
\r
231 param.setParamValue(countDownTimerVal.toString());
\r
238 public void processHeartBeatGetRequest(String deviceId, Integer HeartBeatPeriod,
\r
239 Integer countDownTimer) throws VesAgentException {
\r
240 VesAgentUtils.validateDeviceId(deviceId);
\r
243 if (null == HeartBeatPeriod && null == countDownTimer) {// this should just check if heartbeat
\r
246 "Invalid input: HeartBeatPeriod and countDownTimer both are null for device " + deviceId;
\r
247 logger.error(errorMsg);
\r
248 throw new VesAgentException(errorMsg);
\r
251 List<DeviceDataEntity> deviceDataEntityList =
\r
252 vesDataRepository.findByDeviceIdAndAttrGroup(deviceId, VesAgentConstants.HEART_BEAT);
\r
254 DeviceDataEntity deviceDataEntity = null;
\r
255 Map<String, String> attrJsonMap = null;
\r
257 if (null == deviceDataEntityList || deviceDataEntityList.isEmpty()) {
\r
258 deviceDataEntity = new DeviceDataEntity();
\r
259 deviceDataEntity.setDeviceId(deviceId);
\r
260 deviceDataEntity.setAttrGroup(VesAgentConstants.HEART_BEAT);
\r
262 attrJsonMap = new HashMap<String, String>();
\r
264 deviceDataEntity = deviceDataEntityList.get(0);
\r
265 attrJsonMap = new Gson().fromJson(deviceDataEntity.getAttrJson(), Map.class);
\r
269 if (null != HeartBeatPeriod) {
\r
270 attrJsonMap.put(VesAgentConstants.HEART_BEAT_PERIOD, HeartBeatPeriod.toString());
\r
273 if (null != countDownTimer) {
\r
274 attrJsonMap.put(VesAgentConstants.COUNT_DOWN_TIMER, countDownTimer.toString());
\r
277 String attrJson = new Gson().toJson(attrJsonMap);
\r
278 deviceDataEntity.setAttrJson(attrJson);
\r
280 vesDataRepository.save(deviceDataEntity);
\r
283 private void resetTimerJob(String deviceId, String heartBeatPeriod, String countDownTimer) {
\r
284 if (null == heartBeatPeriod || heartBeatPeriod.isEmpty()) {
\r
285 scheduleTimerJob(deviceId, Integer.parseInt(countDownTimer));
\r
286 } else if (heartBeatPeriod.equals(VesAgentConstants.REMOVE_HEART_BEAT_TIMER_VAL)) {
\r
287 timerService.cancelSchedule(deviceId);
\r
289 if (Boolean.FALSE.equals(VesAgentUtils.isNullOrEmpty(countDownTimer))) {
\r
290 scheduleTimerJob(deviceId, Integer.parseInt(countDownTimer));
\r
292 scheduleTimerJob(deviceId, Integer.parseInt(heartBeatPeriod));
\r
297 private void scheduleTimerJob(String deviceId, Integer timeoutInterval) {
\r
298 ScheduleInfo scheduleInfo = new ScheduleInfo();
\r
299 scheduleInfo.setInterval(timeoutInterval);
\r
300 scheduleInfo.setTimeUnit(TimeUnit.MINUTES);
\r
302 HeartBeatTimeoutTask callbackTask = getBeanInstance(deviceId);
\r
304 timerService.schedule(deviceId, scheduleInfo, callbackTask);
\r
307 private void abortRunningDeviceConnectivityCheck(DeviceRPCRequest deviceRPCRequest) {
\r
308 waitForNotifications.notifyResult(VesAgentUtils.getErrorResponse(deviceRPCRequest, null, null));
\r
311 public List<DeviceDataEntity> getAllDeviceDataEntity() {
\r
312 return (List<DeviceDataEntity>) vesDataRepository.findAll();
\r
315 public List<DeviceDataEntity> findByDeviceIdAndGroup(String deviceId, String attrGroup) {
\r
316 return vesDataRepository.findByDeviceIdAndAttrGroup(deviceId, attrGroup);
\r