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