Adding Copyright Missing files
[oam/tr069-adapter.git] / ves-agent / src / main / java / org / commscope / tr069adapter / vesagent / timer / HeartBeatTimeoutTask.java
1 /*
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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=======================================================================
17  */
18
19 package org.commscope.tr069adapter.vesagent.timer;\r
20 \r
21 import java.util.List;\r
22 import java.util.concurrent.TimeUnit;\r
23 \r
24 import org.commscope.tr069adapter.vesagent.VesConfiguration;\r
25 import org.commscope.tr069adapter.vesagent.async.AsyncRequestHandler;\r
26 import org.commscope.tr069adapter.vesagent.controller.HeartBeatMessageHandler;\r
27 import org.commscope.tr069adapter.vesagent.entity.DeviceDataEntity;\r
28 import org.commscope.tr069adapter.vesagent.repository.VesDataRepository;\r
29 import org.commscope.tr069adapter.vesagent.util.VesAgentConstants;\r
30 import org.commscope.tr069adapter.vesagent.util.VesAgentUtils;\r
31 import org.slf4j.Logger;\r
32 import org.slf4j.LoggerFactory;\r
33 import org.springframework.beans.factory.annotation.Autowired;\r
34 \r
35 public class HeartBeatTimeoutTask implements Runnable {\r
36   private static final Logger logger = LoggerFactory.getLogger(HeartBeatTimeoutTask.class);\r
37 \r
38   @Autowired\r
39   VesDataRepository vesDataRepository;\r
40 \r
41   @Autowired\r
42   ScheduleTaskService timerService;\r
43 \r
44   @Autowired\r
45   AsyncRequestHandler asyncHandler;\r
46 \r
47   @Autowired\r
48   HeartBeatMessageHandler heartBeatMessageHandler;\r
49 \r
50   @Autowired\r
51   VesConfiguration config;\r
52 \r
53   private String deviceId;\r
54 \r
55   @Override\r
56   public void run() {\r
57     logger.debug("Timer task: checking device {} connectivity.", deviceId);\r
58     List<DeviceDataEntity> deviceDataEntityList =\r
59         vesDataRepository.findByDeviceIdAndAttrGroup(deviceId, VesAgentConstants.HEART_BEAT);\r
60 \r
61     if (VesAgentUtils.isNullOrEmpty(deviceDataEntityList)\r
62         || VesAgentUtils.isNullOrEmpty(deviceDataEntityList.get(0).getAttributesMap())) {\r
63       timerService.cancelSchedule(deviceId);\r
64       return;\r
65     }\r
66 \r
67     DeviceDataEntity deviceDataEntity = deviceDataEntityList.get(0);\r
68     String heartBeatPeriod =\r
69         deviceDataEntity.getAttributesMap().get(VesAgentConstants.HEART_BEAT_PERIOD);\r
70 \r
71     if (VesAgentUtils.isNullOrEmpty(heartBeatPeriod)\r
72         || heartBeatPeriod.equals(VesAgentConstants.REMOVE_HEART_BEAT_TIMER_VAL)) {\r
73       timerService.cancelSchedule(deviceId);\r
74       return;\r
75     }\r
76 \r
77     ScheduleInfo scheduleInfo = new ScheduleInfo();\r
78     scheduleInfo.setInterval(Integer.parseInt(heartBeatPeriod));\r
79     scheduleInfo.setTimeUnit(TimeUnit.MINUTES);\r
80 \r
81     timerService.schedule(deviceId, scheduleInfo, this);\r
82 \r
83     asyncHandler.initiateDeviceReachabilityCheck(deviceDataEntity);\r
84   }\r
85 \r
86   public HeartBeatTimeoutTask(String deviceId) {\r
87     super();\r
88     this.deviceId = deviceId;\r
89   }\r
90 \r
91   public String getDeviceId() {\r
92     return deviceId;\r
93   }\r
94 \r
95   public void setDeviceId(String deviceId) {\r
96     this.deviceId = deviceId;\r
97   }\r
98 }\r