Fix: Update Sonar needed parameters
[oam/tr069-adapter.git] / common / src / main / java / org / commscope / tr069adapter / common / timer / impl / TimerServiceManagerAPIImpl.java
1 /*\r
2  * ============LICENSE_START========================================================================\r
3  * ONAP : tr-069-adapter\r
4  * =================================================================================================\r
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
6  * =================================================================================================\r
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
9  * may obtain a copy of the License at\r
10  *\r
11  * http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
14  * either express or implied. See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  * ===============LICENSE_END=======================================================================\r
17  */\r
18 \r
19 package org.commscope.tr069adapter.common.timer.impl;\r
20 \r
21 import static org.commscope.tr069adapter.common.scheduler.impl.TimerAPIConstants.DM_SUB_SYSTEM;\r
22 import static org.commscope.tr069adapter.common.scheduler.impl.TimerAPIConstants.TIMEOUT_HANDLER_JNDI_NAME;\r
23 import static org.commscope.tr069adapter.common.scheduler.impl.TimerAPIConstants.TIMER_DATA_KEY;\r
24 import static org.commscope.tr069adapter.common.scheduler.impl.TimerAPIConstants.TIMER_ID_KEY;\r
25 import static org.commscope.tr069adapter.common.scheduler.impl.TimerAPIConstants.TIMER_JOB_GROUP;\r
26 import static org.commscope.tr069adapter.common.scheduler.impl.TimerAPIConstants.TIMER_LISTENER_KEY;\r
27 \r
28 import java.io.Serializable;\r
29 import java.util.Date;\r
30 import java.util.Map;\r
31 \r
32 import org.apache.commons.logging.Log;\r
33 import org.apache.commons.logging.LogFactory;\r
34 import org.commscope.tr069adapter.common.scheduler.ExecutionContext;\r
35 import org.commscope.tr069adapter.common.scheduler.SchedulerException;\r
36 import org.commscope.tr069adapter.common.scheduler.SchedulerManager;\r
37 import org.commscope.tr069adapter.common.scheduler.TriggerInfo;\r
38 import org.commscope.tr069adapter.common.timer.TimerException;\r
39 import org.commscope.tr069adapter.common.timer.TimerServiceManagerAPI;\r
40 import org.springframework.beans.factory.annotation.Autowired;\r
41 import org.springframework.stereotype.Component;\r
42 \r
43 @Component\r
44 public class TimerServiceManagerAPIImpl implements TimerServiceManagerAPI {\r
45 \r
46   @Autowired\r
47   SchedulerManager quartzScheduleManager;\r
48 \r
49   private static Log logger = LogFactory.getLog(TimerServiceManagerAPIImpl.class);\r
50 \r
51   @Override\r
52   public void startTimer(String timerId, String listenerBean, long timeout, Serializable data)\r
53       throws TimerException {\r
54     try {\r
55       logger.debug("Requested to start timer for " + timerId + ", listener bean=" + listenerBean);\r
56       TriggerInfo triggerInfo = new TriggerInfo();\r
57       triggerInfo.setStartDate(new Date(new Date().getTime() + timeout));\r
58       ExecutionContext executionContext =\r
59           new ExecutionContext(DM_SUB_SYSTEM, TIMEOUT_HANDLER_JNDI_NAME);\r
60       executionContext.addJobData(TIMER_ID_KEY, timerId);\r
61       executionContext.addJobData(TIMER_LISTENER_KEY, listenerBean);\r
62       executionContext.addJobData(TIMER_DATA_KEY, data);\r
63       quartzScheduleManager.scheduleJob(timerId, TIMER_JOB_GROUP, triggerInfo, executionContext);\r
64       logger.debug("Successfully started timer for " + timerId);\r
65     } catch (Exception e) {\r
66       logger.error("Failed to start timer for " + timerId + ", Error Details :" + e.getMessage(),\r
67           e);\r
68       throw new TimerException("Failed to start timer for " + timerId, e);\r
69     }\r
70 \r
71   }\r
72 \r
73   @Override\r
74   public void modifyTimer(String timerId, long newTimeout, Serializable data)\r
75       throws TimerException {\r
76     try {\r
77       logger.debug("Requested to modify timer for " + timerId);\r
78 \r
79       if (quartzScheduleManager.isJobScheduleExist(timerId, TIMER_JOB_GROUP)) {\r
80         Map<String, Object> jobDataMap = quartzScheduleManager.getJobData(timerId, TIMER_JOB_GROUP);\r
81         TriggerInfo triggerInfo = new TriggerInfo();\r
82         triggerInfo.setStartDate(new Date(new Date().getTime() + newTimeout));\r
83         ExecutionContext executionContext =\r
84             new ExecutionContext(DM_SUB_SYSTEM, TIMEOUT_HANDLER_JNDI_NAME);\r
85         executionContext.addJobData(TIMER_ID_KEY, timerId);\r
86         executionContext.addJobData(TIMER_LISTENER_KEY, jobDataMap.get(TIMER_LISTENER_KEY));\r
87         executionContext.addJobData(TIMER_DATA_KEY, data);\r
88         quartzScheduleManager.modifySchedule(timerId, TIMER_JOB_GROUP, triggerInfo);\r
89         logger.debug("Successfully modified timer for " + timerId);\r
90       } else {\r
91         logger.error("Timer with timerId " + timerId + " does not exist.");\r
92         throw new TimerException("Timer with timerId " + timerId + " does not exist.");\r
93       }\r
94 \r
95     } catch (SchedulerException e) {\r
96       logger.error("Failed to modify timer with timerId " + timerId);\r
97       throw new TimerException("Failed to modify timer with timerId " + timerId);\r
98     }\r
99   }\r
100 \r
101   @Override\r
102   public void stopTimer(String timerId) throws TimerException {\r
103     try {\r
104       logger.debug("Requested to stop the timer." + timerId);\r
105       if (quartzScheduleManager.isJobExist(timerId, TIMER_JOB_GROUP))\r
106         quartzScheduleManager.deleteSchedule(timerId, TIMER_JOB_GROUP);\r
107       logger.debug("Successfully stopped the timer." + timerId);\r
108     } catch (Exception e) {\r
109       logger.error("Failed to stop timer for " + timerId + ", Error Details :" + e.getMessage(), e);\r
110       throw new TimerException("Failed to stop the timer for " + timerId, e);\r
111     }\r
112   }\r
113 \r
114 }\r