--- /dev/null
+/*\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.requestprocessor;\r
+\r
+import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.*;\r
+\r
+import org.commscope.tr069adapter.acs.common.DeviceInform;\r
+import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;\r
+import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;\r
+import org.commscope.tr069adapter.acs.common.dto.DeviceOperationRequestDetails;\r
+import org.commscope.tr069adapter.acs.common.dto.TR069DeviceDetails;\r
+import org.commscope.tr069adapter.acs.common.exception.DeviceOperationException;\r
+import org.commscope.tr069adapter.acs.common.exception.SessionConcurrentAccessException;\r
+import org.commscope.tr069adapter.acs.common.exception.SessionManagerException;\r
+import org.commscope.tr069adapter.acs.common.requestprocessor.service.TR069DeviceEventHandler;\r
+import org.commscope.tr069adapter.acs.common.response.DeviceInformResponse;\r
+import org.commscope.tr069adapter.acs.requestprocessor.impl.TR069RequestProcessEngine;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.slf4j.MDC;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.stereotype.Component;\r
+import org.springframework.transaction.annotation.Isolation;\r
+import org.springframework.transaction.annotation.Propagation;\r
+import org.springframework.transaction.annotation.Transactional;\r
+\r
+@Component\r
+public class TR069DeviceEventHandlerImpl implements TR069DeviceEventHandler {\r
+\r
+ private static final String RETRY_LIMIT_REACHED_AND_FAILING_THE_DEVICE_UNREGISTER_REQUEST =\r
+ "Retry limit reached and failing the device unregister request";\r
+\r
+ private static final Logger logger = LoggerFactory.getLogger(TR069DeviceEventHandlerImpl.class);\r
+\r
+ private static final String CLIENT_STR = "client";\r
+\r
+ @Autowired\r
+ private TR069RequestProcessEngine tr069RequestProcessEngine;\r
+\r
+ public TR069RequestProcessEngine getProcessEngine() {\r
+ return tr069RequestProcessEngine;\r
+ }\r
+\r
+ public void setProcessEngine(TR069RequestProcessEngine processEngine) {\r
+ this.tr069RequestProcessEngine = processEngine;\r
+ }\r
+\r
+ @Override\r
+ @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, timeout = 300,\r
+ rollbackFor = RuntimeException.class)\r
+ public DeviceInformResponse processDeviceInform(final DeviceInform deviceNotification)\r
+ throws Exception {\r
+ DeviceInformResponse deviceNotificationResponse = null;\r
+ try {\r
+ String deviceId = deviceNotification.getDeviceDetails().getDeviceId();\r
+ MDC.put(CLIENT_STR, deviceId);\r
+ deviceNotificationResponse =\r
+ processDeviceInformWithRetryOnConcurrentAccess(deviceNotification);\r
+ } finally {\r
+ MDC.remove(CLIENT_STR);\r
+ }\r
+\r
+ return deviceNotificationResponse;\r
+ }\r
+\r
+ @Override\r
+ @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, timeout = 300,\r
+ rollbackFor = RuntimeException.class)\r
+ public DeviceRPCRequest processDeviceRPCResponse(DeviceRPCResponse operationResult)\r
+ throws Exception {\r
+ DeviceRPCRequest deviceRPCRequest = null;\r
+ try {\r
+ String deviceId = operationResult.getDeviceDetails().getDeviceId();\r
+ MDC.put(CLIENT_STR, deviceId);\r
+ deviceRPCRequest = processDeviceRPCResponseWithRetryOnConcurrentAccess(operationResult);\r
+ } finally {\r
+ MDC.remove(CLIENT_STR);\r
+ }\r
+ return deviceRPCRequest;\r
+ }\r
+\r
+ @Override\r
+ @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, timeout = 300,\r
+ rollbackFor = RuntimeException.class)\r
+ public DeviceRPCRequest processEmptyDeviceRequest(TR069DeviceDetails deviceDetails)\r
+ throws Exception {\r
+ DeviceRPCRequest deviceRPCRequest = null;\r
+ try {\r
+ String deviceId = deviceDetails.getDeviceId();\r
+ MDC.put(CLIENT_STR, deviceId);\r
+ deviceRPCRequest = processEmptyDeviceRequestWithRetryOnConcurrentAccess(deviceDetails);\r
+ } finally {\r
+ MDC.remove(CLIENT_STR);\r
+ }\r
+ return deviceRPCRequest;\r
+ }\r
+\r
+ @Override\r
+ @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, timeout = 300,\r
+ rollbackFor = RuntimeException.class)\r
+ public DeviceOperationRequestDetails getOpRequestDetailsBySessionId(String sessionId)\r
+ throws SessionManagerException {\r
+ return tr069RequestProcessEngine.getOpRequestDetailsBySessionId(sessionId);\r
+ }\r
+\r
+ @Override\r
+ @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, timeout = 300,\r
+ rollbackFor = RuntimeException.class)\r
+ public TR069DeviceDetails getDeviceDetails(String deviceId) throws DeviceOperationException {\r
+ return tr069RequestProcessEngine.getDeviceDetails(deviceId);\r
+ }\r
+\r
+ /**\r
+ * @param deviceNotification\r
+ * @return\r
+ * @throws InterruptedException\r
+ */\r
+ private DeviceInformResponse processDeviceInformWithRetryOnConcurrentAccess(\r
+ DeviceInform deviceNotification)\r
+ throws SessionConcurrentAccessException, InterruptedException {\r
+ logger.debug("Processing Device Inform Event");\r
+ DeviceInformResponse deviceNotificationResponse = null;\r
+ for (int i = 0; i < MAX_RETRY_LIMIT; i++) {\r
+ try {\r
+ deviceNotificationResponse =\r
+ tr069RequestProcessEngine.processDeviceInform(deviceNotification);\r
+ logger.debug("Successfully processed Device Inform Event");\r
+ break;\r
+ } catch (SessionConcurrentAccessException scae) {\r
+ if ((i + 1) == MAX_RETRY_LIMIT) {\r
+ logger.error(RETRY_LIMIT_REACHED_AND_FAILING_THE_DEVICE_UNREGISTER_REQUEST);\r
+ throw scae;\r
+ }\r
+ Long delay = (i == 0) ? DELAY : DELAY * i;\r
+ Thread.sleep(delay);\r
+ }\r
+ }\r
+ return deviceNotificationResponse;\r
+ }\r
+\r
+ /**\r
+ * @param operationResult\r
+ * @return\r
+ * @throws InterruptedException\r
+ * @throws Exception\r
+ */\r
+ private DeviceRPCRequest processDeviceRPCResponseWithRetryOnConcurrentAccess(\r
+ DeviceRPCResponse operationResult)\r
+ throws SessionConcurrentAccessException, InterruptedException {\r
+ DeviceRPCRequest deviceRPCRequest = null;\r
+ logger.debug("Processing Device operation response");\r
+ for (int i = 0; i < MAX_RETRY_LIMIT; i++) {\r
+ try {\r
+ deviceRPCRequest = tr069RequestProcessEngine.processDeviceRPCResponse(operationResult);\r
+ logger.debug("Successfully processed Device operation response");\r
+ break;\r
+ } catch (SessionConcurrentAccessException scae) {\r
+ if ((i + 1) == MAX_RETRY_LIMIT) {\r
+ logger.error(RETRY_LIMIT_REACHED_AND_FAILING_THE_DEVICE_UNREGISTER_REQUEST);\r
+ throw scae;\r
+ }\r
+ Long delay = (i == 0) ? DELAY : DELAY * i;\r
+ Thread.sleep(delay);\r
+ }\r
+ }\r
+ return deviceRPCRequest;\r
+ }\r
+\r
+ /**\r
+ * @param deviceDetails\r
+ * @return\r
+ * @throws InterruptedException\r
+ * @throws Exception\r
+ */\r
+ private DeviceRPCRequest processEmptyDeviceRequestWithRetryOnConcurrentAccess(\r
+ TR069DeviceDetails deviceDetails)\r
+ throws SessionConcurrentAccessException, InterruptedException {\r
+ DeviceRPCRequest deviceRPCRequest = null;\r
+ logger.debug("Processing Empty request");\r
+ for (int i = 0; i < MAX_RETRY_LIMIT; i++) {\r
+ try {\r
+ deviceRPCRequest = tr069RequestProcessEngine.processEmptyDeviceRequest(deviceDetails);\r
+ logger.debug("Successfully processed Empty request");\r
+ break;\r
+ } catch (SessionConcurrentAccessException scae) {\r
+ if ((i + 1) == MAX_RETRY_LIMIT) {\r
+ logger.error(RETRY_LIMIT_REACHED_AND_FAILING_THE_DEVICE_UNREGISTER_REQUEST);\r
+ throw scae;\r
+ }\r
+ Long delay = (i == 0) ? DELAY : DELAY * i;\r
+ Thread.sleep(delay);\r
+ }\r
+ }\r
+ return deviceRPCRequest;\r
+ }\r
+}\r