465b6dc2815476162ab832187ce7e71eb1f68149
[oam/tr069-adapter.git] / ves-agent / src / main / java / org / commscope / tr069adapter / vesagent / timer / StartupTimerService.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 import java.util.function.Function;\r
24 \r
25 import javax.annotation.PostConstruct;\r
26 \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 import org.springframework.stereotype.Component;\r
35 \r
36 @Component\r
37 public class StartupTimerService {\r
38   private static final Logger logger = LoggerFactory.getLogger(StartupTimerService.class);\r
39 \r
40   @Autowired\r
41   private Function<String, HeartBeatTimeoutTask> beanFactory;\r
42 \r
43   public HeartBeatTimeoutTask getBeanInstance(String name) {\r
44     return beanFactory.apply(name);\r
45   }\r
46 \r
47   @Autowired\r
48   VesDataRepository vesDataRepository;\r
49 \r
50   @Autowired\r
51   ScheduleTaskService timerService;\r
52 \r
53   @PostConstruct\r
54   public void initializeDeviceReachabilityCheckTimers() {\r
55     logger.debug("Initializing all device connectivity check timer tasks.");\r
56     List<DeviceDataEntity> deviceDataEntityList =\r
57         vesDataRepository.findByAttrGroup(VesAgentConstants.HEART_BEAT);\r
58 \r
59     if (VesAgentUtils.isNullOrEmpty(deviceDataEntityList)) {\r
60       logger.debug("No device reachability check timer tasks exist.");\r
61       return;\r
62     }\r
63 \r
64     for (DeviceDataEntity deviceDataEntity : deviceDataEntityList) {\r
65       String heartBeatPeriod = null;\r
66 \r
67       if (null != deviceDataEntity.getAttributesMap()) {\r
68         heartBeatPeriod =\r
69             deviceDataEntity.getAttributesMap().get(VesAgentConstants.HEART_BEAT_PERIOD);\r
70       }\r
71 \r
72       if (!VesAgentUtils.isNullOrEmpty(heartBeatPeriod)\r
73           && !heartBeatPeriod.equals(VesAgentConstants.REMOVE_HEART_BEAT_TIMER_VAL)) {\r
74         logger.info("Creating device connectivity check timer tasks for device {}.",\r
75             deviceDataEntity.getDeviceId());\r
76         ScheduleInfo scheduleInfo = new ScheduleInfo();\r
77         scheduleInfo.setInterval(Integer.parseInt(heartBeatPeriod));\r
78         scheduleInfo.setTimeUnit(TimeUnit.SECONDS);\r
79 \r
80         HeartBeatTimeoutTask callbackTask = getBeanInstance(deviceDataEntity.getDeviceId());\r
81 \r
82         timerService.schedule(deviceDataEntity.getDeviceId(), scheduleInfo, callbackTask);\r
83       }\r
84     }\r
85   }\r
86 }\r