Initial source code
[oam/tr069-adapter.git] / common / src / main / java / org / commscope / tr069adapter / common / timer / impl / TimerServiceManagerAPIImpl.java
diff --git a/common/src/main/java/org/commscope/tr069adapter/common/timer/impl/TimerServiceManagerAPIImpl.java b/common/src/main/java/org/commscope/tr069adapter/common/timer/impl/TimerServiceManagerAPIImpl.java
new file mode 100644 (file)
index 0000000..684f991
--- /dev/null
@@ -0,0 +1,114 @@
+/*\r
+ * ============LICENSE_START========================================================================\r
+ * ONAP : tr-069-adapter\r
+ * =================================================================================================\r
+ * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
+ * =================================================================================================\r
+ * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
+ * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
+ * may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
+ * either express or implied. See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ===============LICENSE_END=======================================================================\r
+ */\r
+\r
+package org.commscope.tr069adapter.common.timer.impl;\r
+\r
+import static org.commscope.tr069adapter.common.scheduler.impl.TimerAPIConstants.DM_SUB_SYSTEM;\r
+import static org.commscope.tr069adapter.common.scheduler.impl.TimerAPIConstants.TIMEOUT_HANDLER_JNDI_NAME;\r
+import static org.commscope.tr069adapter.common.scheduler.impl.TimerAPIConstants.TIMER_DATA_KEY;\r
+import static org.commscope.tr069adapter.common.scheduler.impl.TimerAPIConstants.TIMER_ID_KEY;\r
+import static org.commscope.tr069adapter.common.scheduler.impl.TimerAPIConstants.TIMER_JOB_GROUP;\r
+import static org.commscope.tr069adapter.common.scheduler.impl.TimerAPIConstants.TIMER_LISTENER_KEY;\r
+\r
+import java.io.Serializable;\r
+import java.util.Date;\r
+import java.util.Map;\r
+\r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
+import org.commscope.tr069adapter.common.scheduler.ExecutionContext;\r
+import org.commscope.tr069adapter.common.scheduler.SchedulerException;\r
+import org.commscope.tr069adapter.common.scheduler.SchedulerManager;\r
+import org.commscope.tr069adapter.common.scheduler.TriggerInfo;\r
+import org.commscope.tr069adapter.common.timer.TimerException;\r
+import org.commscope.tr069adapter.common.timer.TimerServiceManagerAPI;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.stereotype.Component;\r
+\r
+@Component\r
+public class TimerServiceManagerAPIImpl implements TimerServiceManagerAPI {\r
+\r
+  @Autowired\r
+  SchedulerManager quartzScheduleManager;\r
+\r
+  private static Log logger = LogFactory.getLog(TimerServiceManagerAPIImpl.class);\r
+\r
+  @Override\r
+  public void startTimer(String timerId, String listenerBean, long timeout, Serializable data)\r
+      throws TimerException {\r
+    try {\r
+      logger.debug("Requested to start timer for " + timerId + ", listener bean=" + listenerBean);\r
+      TriggerInfo triggerInfo = new TriggerInfo();\r
+      triggerInfo.setStartDate(new Date(new Date().getTime() + timeout));\r
+      ExecutionContext executionContext =\r
+          new ExecutionContext(DM_SUB_SYSTEM, TIMEOUT_HANDLER_JNDI_NAME);\r
+      executionContext.addJobData(TIMER_ID_KEY, timerId);\r
+      executionContext.addJobData(TIMER_LISTENER_KEY, listenerBean);\r
+      executionContext.addJobData(TIMER_DATA_KEY, data);\r
+      quartzScheduleManager.scheduleJob(timerId, TIMER_JOB_GROUP, triggerInfo, executionContext);\r
+      logger.debug("Successfully started timer for " + timerId);\r
+    } catch (Exception e) {\r
+      logger.error("Failed to start timer for " + timerId + ", Error Details :" + e.getMessage(),\r
+          e);\r
+      throw new TimerException("Failed to start timer for " + timerId, e);\r
+    }\r
+\r
+  }\r
+\r
+  @Override\r
+  public void modifyTimer(String timerId, long newTimeout, Serializable data)\r
+      throws TimerException {\r
+    try {\r
+      logger.debug("Requested to modify timer for " + timerId);\r
+\r
+      if (quartzScheduleManager.isJobScheduleExist(timerId, TIMER_JOB_GROUP)) {\r
+        Map<String, Object> jobDataMap = quartzScheduleManager.getJobData(timerId, TIMER_JOB_GROUP);\r
+        TriggerInfo triggerInfo = new TriggerInfo();\r
+        triggerInfo.setStartDate(new Date(new Date().getTime() + newTimeout));\r
+        ExecutionContext executionContext =\r
+            new ExecutionContext(DM_SUB_SYSTEM, TIMEOUT_HANDLER_JNDI_NAME);\r
+        executionContext.addJobData(TIMER_ID_KEY, timerId);\r
+        executionContext.addJobData(TIMER_LISTENER_KEY, jobDataMap.get(TIMER_LISTENER_KEY));\r
+        executionContext.addJobData(TIMER_DATA_KEY, data);\r
+        quartzScheduleManager.modifySchedule(timerId, TIMER_JOB_GROUP, triggerInfo);\r
+        logger.debug("Successfully modified timer for " + timerId);\r
+      } else {\r
+        logger.error("Timer with timerId " + timerId + " does not exist.");\r
+        throw new TimerException("Timer with timerId " + timerId + " does not exist.");\r
+      }\r
+\r
+    } catch (SchedulerException e) {\r
+      logger.error("Failed to modify timer with timerId " + timerId);\r
+      throw new TimerException("Failed to modify timer with timerId " + timerId);\r
+    }\r
+  }\r
+\r
+  @Override\r
+  public void stopTimer(String timerId) throws TimerException {\r
+    try {\r
+      logger.debug("Requested to stop the timer." + timerId);\r
+      if (quartzScheduleManager.isJobExist(timerId, TIMER_JOB_GROUP))\r
+        quartzScheduleManager.deleteSchedule(timerId, TIMER_JOB_GROUP);\r
+      logger.debug("Successfully stopped the timer." + timerId);\r
+    } catch (Exception e) {\r
+      logger.error("Failed to stop timer for " + timerId + ", Error Details :" + e.getMessage(), e);\r
+      throw new TimerException("Failed to stop the timer for " + timerId, e);\r
+    }\r
+  }\r
+\r
+}\r