f43e938220ab1e5929b1fe53ae1e23715d438d4d
[oam/tr069-adapter.git] / acs / cpe / src / main / java / org / commscope / tr069adapter / acs / cpe / processor / ConnectionReqEventProcessor.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.acs.cpe.processor;\r
20 \r
21 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.CONNECTION_REQUEST;\r
22 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.CONNECTION_RETRY_SLEEP_TIME;\r
23 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.CR_TIMEOUT;\r
24 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.CR_TIMEOUT_CALLBACK;\r
25 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.HTTP_OP_FAILED;\r
26 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.MAX_CONNECT_RETRY_COUNT;\r
27 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.SEPERATOR;\r
28 \r
29 import java.io.IOException;\r
30 \r
31 import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;\r
32 import org.commscope.tr069adapter.acs.common.dto.TR069DeviceDetails;\r
33 import org.commscope.tr069adapter.acs.common.exception.SessionManagerException;\r
34 import org.commscope.tr069adapter.acs.common.utils.ErrorCode;\r
35 import org.commscope.tr069adapter.acs.cpe.utils.DeviceConnector;\r
36 import org.commscope.tr069adapter.common.timer.TimerException;\r
37 import org.commscope.tr069adapter.common.timer.TimerServiceManagerAPI;\r
38 import org.slf4j.Logger;\r
39 import org.slf4j.LoggerFactory;\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 ConnectionReqEventProcessor {\r
45 \r
46   private static final Logger logger = LoggerFactory.getLogger(ConnectionReqEventProcessor.class);\r
47 \r
48   @Autowired\r
49   private DeviceConnector deviceConnector;\r
50 \r
51   @Autowired\r
52   private TimerServiceManagerAPI timerServiceManagerAPI;\r
53 \r
54   public void initiateConnectionRequest(TR069DeviceDetails tr069DeviceDetails, Boolean isRetry)\r
55       throws SessionManagerException, IOException {\r
56     DeviceRPCResponse deviceRPCResponse = null;\r
57 \r
58     for (int retryCount = 0; retryCount < MAX_CONNECT_RETRY_COUNT; retryCount++) {\r
59       logger.info("Initiating connection request on the device. Connection request URL is : {}",\r
60           tr069DeviceDetails.getConnectionRequestURL());\r
61       deviceRPCResponse = deviceConnector.requestConnectionHttp(tr069DeviceDetails);\r
62 \r
63       if (deviceRPCResponse.getOperationResponse().getStatus() == HTTP_OP_FAILED) {\r
64         onFailedHTTPGetOperation(deviceRPCResponse);\r
65         logger.debug("Connection Request Retry attempt - {}", retryCount + 1);\r
66 \r
67         if ((retryCount + 1) == MAX_CONNECT_RETRY_COUNT) {\r
68           SessionManagerException e = new SessionManagerException(\r
69               ErrorCode.SESSION_INITIATION_FAILED, tr069DeviceDetails.getDeviceId());\r
70           logger.error(e.getMessage());\r
71           throw e;\r
72         }\r
73       } else {\r
74         onSuccessHTTPGetOperation(tr069DeviceDetails, isRetry);\r
75         break;\r
76       }\r
77     }\r
78   }\r
79 \r
80   private void onFailedHTTPGetOperation(DeviceRPCResponse deviceRPCResponse) {\r
81     logger.warn("Connection request failed with device, Error: {}",\r
82         deviceRPCResponse.getFaultString());\r
83     logger.debug("Waiting for " + CONNECTION_RETRY_SLEEP_TIME + " millisec before retry");\r
84     try {\r
85       Thread.sleep(CONNECTION_RETRY_SLEEP_TIME);\r
86     } catch (InterruptedException e1) {\r
87       logger.error("Interrupted exception while waiting for CR retry");\r
88       Thread.currentThread().interrupt();\r
89     }\r
90   }\r
91 \r
92   private void onSuccessHTTPGetOperation(TR069DeviceDetails tr069DeviceDetails, Boolean isRetry) {\r
93     try {\r
94       String timerId = tr069DeviceDetails.getDeviceId() + SEPERATOR + CONNECTION_REQUEST;\r
95       if (isRetry.booleanValue()) {\r
96         timerServiceManagerAPI.modifyTimer(timerId, CR_TIMEOUT, tr069DeviceDetails);\r
97       } else {\r
98         timerServiceManagerAPI.startTimer(timerId, CR_TIMEOUT_CALLBACK, CR_TIMEOUT,\r
99             tr069DeviceDetails);\r
100       }\r
101       logger.debug(\r
102           "Successfully started the timer task for connection request initiation on device : {}",\r
103           tr069DeviceDetails.getDeviceId());\r
104     } catch (TimerException e) {\r
105       logger.error("Couldn't start the timer task for connection request initiation on device : {}",\r
106           tr069DeviceDetails.getDeviceId());\r
107     }\r
108   }\r
109 \r
110   public DeviceConnector getDeviceConnector() {\r
111     return deviceConnector;\r
112   }\r
113 \r
114   public void setDeviceConnector(DeviceConnector deviceConnector) {\r
115     this.deviceConnector = deviceConnector;\r
116   }\r
117 \r
118   public TimerServiceManagerAPI getTimerServiceManagerAPI() {\r
119     return timerServiceManagerAPI;\r
120   }\r
121 \r
122   public void setTimerServiceManagerAPI(TimerServiceManagerAPI timerServiceManagerAPI) {\r
123     this.timerServiceManagerAPI = timerServiceManagerAPI;\r
124   }\r
125 \r
126 }\r