Development of NETCONF RPCs for tr-069 adapter to
[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;
20
21 import java.util.List;
22 import java.util.concurrent.TimeUnit;
23 import java.util.function.Function;
24
25 import javax.annotation.PostConstruct;
26
27 import org.commscope.tr069adapter.vesagent.entity.DeviceDataEntity;
28 import org.commscope.tr069adapter.vesagent.repository.VesDataRepository;
29 import org.commscope.tr069adapter.vesagent.util.VesAgentConstants;
30 import org.commscope.tr069adapter.vesagent.util.VesAgentUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
35
36 @Component
37 public class StartupTimerService {
38   private static final Logger logger = LoggerFactory.getLogger(StartupTimerService.class);
39
40   @Autowired
41   private Function<String, HeartBeatTimeoutTask> beanFactory;
42
43   public HeartBeatTimeoutTask getBeanInstance(String name) {
44     return beanFactory.apply(name);
45   }
46
47   @Autowired
48   VesDataRepository vesDataRepository;
49
50   @Autowired
51   ScheduleTaskService timerService;
52
53   @PostConstruct
54   public void initializeDeviceReachabilityCheckTimers() {
55     logger.debug("Initializing all device connectivity check timer tasks.");
56     List<DeviceDataEntity> deviceDataEntityList =
57         vesDataRepository.findByAttrGroup(VesAgentConstants.HEART_BEAT);
58
59     if (VesAgentUtils.isNullOrEmpty(deviceDataEntityList)) {
60       logger.debug("No device reachability check timer tasks exist.");
61       return;
62     }
63
64     for (DeviceDataEntity deviceDataEntity : deviceDataEntityList) {
65       String heartBeatPeriod = null;
66
67       if (null != deviceDataEntity.getAttributesMap()) {
68         heartBeatPeriod =
69             deviceDataEntity.getAttributesMap().get(VesAgentConstants.HEART_BEAT_PERIOD);
70       }
71       if (heartBeatPeriod == null) {
72         logger.info("Heartbeat is null");
73         return;
74       }
75       if (!VesAgentUtils.isNullOrEmpty(heartBeatPeriod)
76           && !heartBeatPeriod.equals(VesAgentConstants.REMOVE_HEART_BEAT_TIMER_VAL)) {
77         logger.info("Creating device connectivity check timer tasks for device {}.",
78             deviceDataEntity.getDeviceId());
79         ScheduleInfo scheduleInfo = new ScheduleInfo();
80         scheduleInfo.setInterval(Integer.parseInt(heartBeatPeriod));
81         scheduleInfo.setTimeUnit(TimeUnit.SECONDS);
82
83         HeartBeatTimeoutTask callbackTask = getBeanInstance(deviceDataEntity.getDeviceId());
84
85         timerService.schedule(deviceDataEntity.getDeviceId(), scheduleInfo, callbackTask);
86       }
87     }
88   }
89 }