Initial source code
[oam/tr069-adapter.git] / acs / cpe / src / main / java / org / commscope / tr069adapter / acs / cpe / processor / ConnectionReqEventProcessor.java
diff --git a/acs/cpe/src/main/java/org/commscope/tr069adapter/acs/cpe/processor/ConnectionReqEventProcessor.java b/acs/cpe/src/main/java/org/commscope/tr069adapter/acs/cpe/processor/ConnectionReqEventProcessor.java
new file mode 100644 (file)
index 0000000..f43e938
--- /dev/null
@@ -0,0 +1,126 @@
+/*\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.acs.cpe.processor;\r
+\r
+import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.CONNECTION_REQUEST;\r
+import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.CONNECTION_RETRY_SLEEP_TIME;\r
+import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.CR_TIMEOUT;\r
+import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.CR_TIMEOUT_CALLBACK;\r
+import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.HTTP_OP_FAILED;\r
+import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.MAX_CONNECT_RETRY_COUNT;\r
+import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.SEPERATOR;\r
+\r
+import java.io.IOException;\r
+\r
+import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;\r
+import org.commscope.tr069adapter.acs.common.dto.TR069DeviceDetails;\r
+import org.commscope.tr069adapter.acs.common.exception.SessionManagerException;\r
+import org.commscope.tr069adapter.acs.common.utils.ErrorCode;\r
+import org.commscope.tr069adapter.acs.cpe.utils.DeviceConnector;\r
+import org.commscope.tr069adapter.common.timer.TimerException;\r
+import org.commscope.tr069adapter.common.timer.TimerServiceManagerAPI;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.stereotype.Component;\r
+\r
+@Component\r
+public class ConnectionReqEventProcessor {\r
+\r
+  private static final Logger logger = LoggerFactory.getLogger(ConnectionReqEventProcessor.class);\r
+\r
+  @Autowired\r
+  private DeviceConnector deviceConnector;\r
+\r
+  @Autowired\r
+  private TimerServiceManagerAPI timerServiceManagerAPI;\r
+\r
+  public void initiateConnectionRequest(TR069DeviceDetails tr069DeviceDetails, Boolean isRetry)\r
+      throws SessionManagerException, IOException {\r
+    DeviceRPCResponse deviceRPCResponse = null;\r
+\r
+    for (int retryCount = 0; retryCount < MAX_CONNECT_RETRY_COUNT; retryCount++) {\r
+      logger.info("Initiating connection request on the device. Connection request URL is : {}",\r
+          tr069DeviceDetails.getConnectionRequestURL());\r
+      deviceRPCResponse = deviceConnector.requestConnectionHttp(tr069DeviceDetails);\r
+\r
+      if (deviceRPCResponse.getOperationResponse().getStatus() == HTTP_OP_FAILED) {\r
+        onFailedHTTPGetOperation(deviceRPCResponse);\r
+        logger.debug("Connection Request Retry attempt - {}", retryCount + 1);\r
+\r
+        if ((retryCount + 1) == MAX_CONNECT_RETRY_COUNT) {\r
+          SessionManagerException e = new SessionManagerException(\r
+              ErrorCode.SESSION_INITIATION_FAILED, tr069DeviceDetails.getDeviceId());\r
+          logger.error(e.getMessage());\r
+          throw e;\r
+        }\r
+      } else {\r
+        onSuccessHTTPGetOperation(tr069DeviceDetails, isRetry);\r
+        break;\r
+      }\r
+    }\r
+  }\r
+\r
+  private void onFailedHTTPGetOperation(DeviceRPCResponse deviceRPCResponse) {\r
+    logger.warn("Connection request failed with device, Error: {}",\r
+        deviceRPCResponse.getFaultString());\r
+    logger.debug("Waiting for " + CONNECTION_RETRY_SLEEP_TIME + " millisec before retry");\r
+    try {\r
+      Thread.sleep(CONNECTION_RETRY_SLEEP_TIME);\r
+    } catch (InterruptedException e1) {\r
+      logger.error("Interrupted exception while waiting for CR retry");\r
+      Thread.currentThread().interrupt();\r
+    }\r
+  }\r
+\r
+  private void onSuccessHTTPGetOperation(TR069DeviceDetails tr069DeviceDetails, Boolean isRetry) {\r
+    try {\r
+      String timerId = tr069DeviceDetails.getDeviceId() + SEPERATOR + CONNECTION_REQUEST;\r
+      if (isRetry.booleanValue()) {\r
+        timerServiceManagerAPI.modifyTimer(timerId, CR_TIMEOUT, tr069DeviceDetails);\r
+      } else {\r
+        timerServiceManagerAPI.startTimer(timerId, CR_TIMEOUT_CALLBACK, CR_TIMEOUT,\r
+            tr069DeviceDetails);\r
+      }\r
+      logger.debug(\r
+          "Successfully started the timer task for connection request initiation on device : {}",\r
+          tr069DeviceDetails.getDeviceId());\r
+    } catch (TimerException e) {\r
+      logger.error("Couldn't start the timer task for connection request initiation on device : {}",\r
+          tr069DeviceDetails.getDeviceId());\r
+    }\r
+  }\r
+\r
+  public DeviceConnector getDeviceConnector() {\r
+    return deviceConnector;\r
+  }\r
+\r
+  public void setDeviceConnector(DeviceConnector deviceConnector) {\r
+    this.deviceConnector = deviceConnector;\r
+  }\r
+\r
+  public TimerServiceManagerAPI getTimerServiceManagerAPI() {\r
+    return timerServiceManagerAPI;\r
+  }\r
+\r
+  public void setTimerServiceManagerAPI(TimerServiceManagerAPI timerServiceManagerAPI) {\r
+    this.timerServiceManagerAPI = timerServiceManagerAPI;\r
+  }\r
+\r
+}\r