Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / acs / cpe / src / main / java / org / commscope / tr069adapter / acs / cpe / processor / ConnectionReqEventProcessor.java
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : tr-069-adapter
4  * =================================================================================================
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.
6  * =================================================================================================
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
9  * may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ===============LICENSE_END=======================================================================
17  */
18
19 package org.commscope.tr069adapter.acs.cpe.processor;
20
21 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.CONNECTION_REQUEST;
22 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.CR_TIMEOUT;
23 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.CR_TIMEOUT_CALLBACK;
24 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.HTTP_OP_FAILED;
25 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.SEPERATOR;
26
27 import java.io.IOException;
28
29 import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;
30 import org.commscope.tr069adapter.acs.common.dto.TR069DeviceDetails;
31 import org.commscope.tr069adapter.acs.common.exception.SessionManagerException;
32 import org.commscope.tr069adapter.acs.common.requestprocessor.service.TR069DeviceEventHandler;
33 import org.commscope.tr069adapter.acs.common.utils.ErrorCode;
34 import org.commscope.tr069adapter.acs.cpe.utils.DeviceConnector;
35 import org.commscope.tr069adapter.common.timer.TimerException;
36 import org.commscope.tr069adapter.common.timer.TimerServiceManagerAPI;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
41
42 @Component
43 public class ConnectionReqEventProcessor {
44
45   private static final Logger logger = LoggerFactory.getLogger(ConnectionReqEventProcessor.class);
46
47   @Autowired
48   private DeviceConnector deviceConnector;
49
50   @Autowired
51   private TimerServiceManagerAPI timerServiceManagerAPI;
52
53   @Autowired
54   TR069DeviceEventHandler tr069EventHandler;
55
56   public void initiateConnectionRequest(TR069DeviceDetails tr069DeviceDetails, Boolean isRetry)
57       throws SessionManagerException, IOException {
58     DeviceRPCResponse deviceRPCResponse = null;
59
60     logger.info("Initiating connection request on the device. Connection request URL is : {}",
61         tr069DeviceDetails.getConnectionRequestURL());
62     boolean isConnectionFailed = true;
63     try {
64       deviceRPCResponse = deviceConnector.requestConnectionHttp(tr069DeviceDetails);
65     } catch (Exception e) {
66       logger.error("Connection Failed with the Device: {}", e.getMessage());
67       isConnectionFailed = false;
68     }
69
70     if (!isConnectionFailed
71         || deviceRPCResponse.getOperationResponse().getStatus() == HTTP_OP_FAILED) {
72       tr069DeviceDetails.setCrRetryCount(tr069DeviceDetails.getCrRetryCount() + 1);
73       String faultStr = "";
74       if (deviceRPCResponse != null) {
75         faultStr = deviceRPCResponse.getFaultString();
76       }
77       logger.warn("Connection request failed with device, Error: {}, on attempt- {}", faultStr,
78           tr069DeviceDetails.getCrRetryCount());
79       if (tr069DeviceDetails.getCrRetryCount() == 1) {
80         logger.error("CONNECT request is failed, not retrying the CR to device");
81       } else {
82         SessionManagerException e = new SessionManagerException(ErrorCode.SESSION_INITIATION_FAILED,
83             tr069DeviceDetails.getDeviceId());
84         logger.error(e.getMessage());
85         throw e;
86       }
87     } else {
88       onSuccessHTTPGetOperation(tr069DeviceDetails, isRetry);
89     }
90   }
91
92   private void onSuccessHTTPGetOperation(TR069DeviceDetails tr069DeviceDetails, Boolean isRetry) {
93     try {
94       String timerId = tr069DeviceDetails.getDeviceId() + SEPERATOR + CONNECTION_REQUEST;
95       if (isRetry.booleanValue()) {
96         timerServiceManagerAPI.modifyTimer(timerId, CR_TIMEOUT, tr069DeviceDetails);
97       } else {
98         timerServiceManagerAPI.startTimer(timerId, CR_TIMEOUT_CALLBACK, CR_TIMEOUT,
99             tr069DeviceDetails);
100       }
101       logger.debug(
102           "Successfully started the timer task for connection request initiation on device : {}",
103           tr069DeviceDetails.getDeviceId());
104     } catch (TimerException e) {
105       logger.error("Couldn't start the timer task for connection request initiation on device : {}",
106           tr069DeviceDetails.getDeviceId());
107     }
108   }
109
110   public DeviceConnector getDeviceConnector() {
111     return deviceConnector;
112   }
113
114   public void setDeviceConnector(DeviceConnector deviceConnector) {
115     this.deviceConnector = deviceConnector;
116   }
117
118   public TimerServiceManagerAPI getTimerServiceManagerAPI() {
119     return timerServiceManagerAPI;
120   }
121
122   public void setTimerServiceManagerAPI(TimerServiceManagerAPI timerServiceManagerAPI) {
123     this.timerServiceManagerAPI = timerServiceManagerAPI;
124   }
125
126 }