Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / acs / requestprocessor / src / main / java / org / commscope / tr069adapter / acs / requestprocessor / TR069DeviceEventHandlerImpl.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.requestprocessor;
20
21 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.*;
22
23 import java.util.Date;
24
25 import org.commscope.tr069adapter.acs.common.DeviceInform;
26 import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;
27 import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;
28 import org.commscope.tr069adapter.acs.common.dto.DeviceOperationRequestDetails;
29 import org.commscope.tr069adapter.acs.common.dto.TR069DeviceDetails;
30 import org.commscope.tr069adapter.acs.common.exception.DeviceOperationException;
31 import org.commscope.tr069adapter.acs.common.exception.SessionConcurrentAccessException;
32 import org.commscope.tr069adapter.acs.common.exception.SessionManagerException;
33 import org.commscope.tr069adapter.acs.common.requestprocessor.service.TR069DeviceEventHandler;
34 import org.commscope.tr069adapter.acs.common.response.DeviceInformResponse;
35 import org.commscope.tr069adapter.acs.requestprocessor.dao.DeviceRepository;
36 import org.commscope.tr069adapter.acs.requestprocessor.entity.TR069DeviceEntity;
37 import org.commscope.tr069adapter.acs.requestprocessor.impl.TR069RequestProcessEngine;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.slf4j.MDC;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Component;
43 import org.springframework.transaction.annotation.Isolation;
44 import org.springframework.transaction.annotation.Propagation;
45 import org.springframework.transaction.annotation.Transactional;
46
47 @Component
48 public class TR069DeviceEventHandlerImpl implements TR069DeviceEventHandler {
49
50   private static final String RETRY_LIMIT_REACHED_AND_FAILING_THE_DEVICE_UNREGISTER_REQUEST =
51       "Retry limit reached and failing the device unregister request";
52
53   private static final Logger logger = LoggerFactory.getLogger(TR069DeviceEventHandlerImpl.class);
54
55   private static final String CLIENT_STR = "client";
56
57   @Autowired
58   private TR069RequestProcessEngine tr069RequestProcessEngine;
59
60   @Autowired
61   private DeviceRepository deviceRepository;
62
63   public TR069RequestProcessEngine getProcessEngine() {
64     return tr069RequestProcessEngine;
65   }
66
67   public void setProcessEngine(TR069RequestProcessEngine processEngine) {
68     this.tr069RequestProcessEngine = processEngine;
69   }
70
71   @Override
72   @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRES_NEW,
73       timeout = 300, rollbackFor = RuntimeException.class)
74   public void processConnectionRequest(String errorMsg, String deviceId, boolean isSuccess) {
75     TR069DeviceEntity tr069DeviceEntity = deviceRepository.findByDeviceId(deviceId);
76
77     if (isSuccess) {
78       logger.debug("processConnectionRequest success case");
79       tr069DeviceEntity.setConnStatus(true);
80       tr069DeviceEntity.setLastUpdatedTime(new Date());
81     } else {
82       logger.debug("processConnectionRequest failed case");
83       tr069DeviceEntity.setConnStatus(false);
84       tr069DeviceEntity.setLastFailedAttemptTime(new Date());
85     }
86     tr069DeviceEntity.setErrorMsg(errorMsg);
87     deviceRepository.save(tr069DeviceEntity);
88   }
89
90   @Override
91   @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, timeout = 300,
92       rollbackFor = RuntimeException.class)
93   public DeviceInformResponse processDeviceInform(final DeviceInform deviceNotification)
94       throws SessionConcurrentAccessException, InterruptedException {
95     DeviceInformResponse deviceNotificationResponse = null;
96     try {
97       String deviceId = deviceNotification.getDeviceDetails().getDeviceId();
98       MDC.put(CLIENT_STR, deviceId);
99       deviceNotificationResponse =
100           processDeviceInformWithRetryOnConcurrentAccess(deviceNotification);
101     } finally {
102       MDC.remove(CLIENT_STR);
103     }
104
105     return deviceNotificationResponse;
106   }
107
108   @Override
109   @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, timeout = 300,
110       rollbackFor = RuntimeException.class)
111   public DeviceRPCRequest processDeviceRPCResponse(DeviceRPCResponse operationResult)
112       throws SessionConcurrentAccessException, InterruptedException {
113     DeviceRPCRequest deviceRPCRequest = null;
114     try {
115       String deviceId = operationResult.getDeviceDetails().getDeviceId();
116       MDC.put(CLIENT_STR, deviceId);
117       deviceRPCRequest = processDeviceRPCResponseWithRetryOnConcurrentAccess(operationResult);
118     } finally {
119       MDC.remove(CLIENT_STR);
120     }
121     return deviceRPCRequest;
122   }
123
124   @Override
125   @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, timeout = 300,
126       rollbackFor = RuntimeException.class)
127   public DeviceRPCRequest processEmptyDeviceRequest(TR069DeviceDetails deviceDetails)
128       throws SessionConcurrentAccessException, InterruptedException {
129     DeviceRPCRequest deviceRPCRequest = null;
130     try {
131       String deviceId = deviceDetails.getDeviceId();
132       MDC.put(CLIENT_STR, deviceId);
133       deviceRPCRequest = processEmptyDeviceRequestWithRetryOnConcurrentAccess(deviceDetails);
134     } finally {
135       MDC.remove(CLIENT_STR);
136     }
137     return deviceRPCRequest;
138   }
139
140   @Override
141   @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, timeout = 300,
142       rollbackFor = RuntimeException.class)
143   public DeviceOperationRequestDetails getOpRequestDetailsBySessionId(String sessionId)
144       throws SessionManagerException {
145     return tr069RequestProcessEngine.getOpRequestDetailsBySessionId(sessionId);
146   }
147
148   @Override
149   @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, timeout = 300,
150       rollbackFor = RuntimeException.class)
151   public TR069DeviceDetails getDeviceDetails(String deviceId) throws DeviceOperationException {
152     return tr069RequestProcessEngine.getDeviceDetails(deviceId);
153   }
154
155   /**
156    * @param deviceNotification
157    * @return
158    * @throws InterruptedException
159    */
160   private DeviceInformResponse processDeviceInformWithRetryOnConcurrentAccess(
161       DeviceInform deviceNotification)
162       throws SessionConcurrentAccessException, InterruptedException {
163     logger.debug("Processing Device Inform Event");
164     DeviceInformResponse deviceNotificationResponse = null;
165     for (int i = 0; i < MAX_RETRY_LIMIT; i++) {
166       try {
167         deviceNotificationResponse =
168             tr069RequestProcessEngine.processDeviceInform(deviceNotification);
169         logger.debug("Successfully processed Device Inform Event");
170         break;
171       } catch (SessionConcurrentAccessException scae) {
172         if ((i + 1) == MAX_RETRY_LIMIT) {
173           logger.error(RETRY_LIMIT_REACHED_AND_FAILING_THE_DEVICE_UNREGISTER_REQUEST);
174           throw scae;
175         }
176         Long delay = (i == 0) ? DELAY : DELAY * i;
177         Thread.sleep(delay);
178       }
179     }
180     return deviceNotificationResponse;
181   }
182
183   /**
184    * @param operationResult
185    * @return
186    * @throws InterruptedException
187    * @throws Exception
188    */
189   private DeviceRPCRequest processDeviceRPCResponseWithRetryOnConcurrentAccess(
190       DeviceRPCResponse operationResult)
191       throws SessionConcurrentAccessException, InterruptedException {
192     DeviceRPCRequest deviceRPCRequest = null;
193     logger.debug("Processing Device operation response");
194     for (int i = 0; i < MAX_RETRY_LIMIT; i++) {
195       try {
196         deviceRPCRequest = tr069RequestProcessEngine.processDeviceRPCResponse(operationResult);
197         logger.debug("Successfully processed Device operation response");
198         break;
199       } catch (SessionConcurrentAccessException scae) {
200         if ((i + 1) == MAX_RETRY_LIMIT) {
201           logger.error(RETRY_LIMIT_REACHED_AND_FAILING_THE_DEVICE_UNREGISTER_REQUEST);
202           throw scae;
203         }
204         Long delay = (i == 0) ? DELAY : DELAY * i;
205         Thread.sleep(delay);
206       }
207     }
208     return deviceRPCRequest;
209   }
210
211   /**
212    * @param deviceDetails
213    * @return
214    * @throws InterruptedException
215    * @throws Exception
216    */
217   private DeviceRPCRequest processEmptyDeviceRequestWithRetryOnConcurrentAccess(
218       TR069DeviceDetails deviceDetails)
219       throws SessionConcurrentAccessException, InterruptedException {
220     DeviceRPCRequest deviceRPCRequest = null;
221     logger.debug("Processing Empty request");
222     for (int i = 0; i < MAX_RETRY_LIMIT; i++) {
223       try {
224         deviceRPCRequest = tr069RequestProcessEngine.processEmptyDeviceRequest(deviceDetails);
225         logger.debug("Successfully processed Empty request");
226         break;
227       } catch (SessionConcurrentAccessException scae) {
228         if ((i + 1) == MAX_RETRY_LIMIT) {
229           logger.error(RETRY_LIMIT_REACHED_AND_FAILING_THE_DEVICE_UNREGISTER_REQUEST);
230           throw scae;
231         }
232         Long delay = (i == 0) ? DELAY : DELAY * i;
233         Thread.sleep(delay);
234       }
235     }
236     return deviceRPCRequest;
237   }
238 }