aeb8c03cf09abafc64150acd5c4677a7283907e1
[oam/tr069-adapter.git] / ves-agent / src / main / java / org / commscope / tr069adapter / vesagent / async / AsyncRequestHandler.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.vesagent.async;\r
20 \r
21 import java.util.concurrent.Future;\r
22 \r
23 import org.commscope.tr069adapter.acs.common.DeviceDetails;\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.OperationCode;\r
27 import org.commscope.tr069adapter.acs.common.OperationOptions;\r
28 import org.commscope.tr069adapter.acs.common.dto.CustomOperationCode;\r
29 import org.commscope.tr069adapter.acs.common.dto.TR069OperationDetails;\r
30 import org.commscope.tr069adapter.vesagent.VesConfiguration;\r
31 import org.commscope.tr069adapter.vesagent.controller.HeartBeatMessageHandler;\r
32 import org.commscope.tr069adapter.vesagent.entity.DeviceDataEntity;\r
33 import org.commscope.tr069adapter.vesagent.mapper.MapperRequestSender;\r
34 import org.commscope.tr069adapter.vesagent.util.VesAgentConstants;\r
35 import org.commscope.tr069adapter.vesagent.util.VesAgentUtils;\r
36 import org.slf4j.Logger;\r
37 import org.slf4j.LoggerFactory;\r
38 import org.springframework.beans.factory.annotation.Autowired;\r
39 import org.springframework.scheduling.annotation.Async;\r
40 import org.springframework.stereotype.Component;\r
41 \r
42 /**\r
43  * \r
44  * @version 1.0\r
45  * @since June 12, 2020\r
46  * @author Prashant Kumar\r
47  */\r
48 @Component\r
49 public class AsyncRequestHandler {\r
50 \r
51   private static final Logger LOG = LoggerFactory.getLogger(AsyncRequestHandler.class);\r
52 \r
53   @Autowired\r
54   MapperRequestSender mapperRequestSender;\r
55 \r
56   @Autowired\r
57   WaitForNotifications waitForNotifications;\r
58 \r
59   @Autowired\r
60   HeartBeatMessageHandler heartBeatMessageHandler;\r
61 \r
62   @Autowired\r
63   VesConfiguration config;\r
64 \r
65   public DeviceRPCResponse performDeviceOperation(DeviceRPCRequest deviceRPCRequest) {\r
66     LOG.info("Initiating device connectivity request to ACS for device {}",\r
67         deviceRPCRequest.getDeviceDetails().getDeviceId());\r
68 \r
69     Future<DeviceRPCResponse> futureResponse = mapperRequestSender.sendRequest(deviceRPCRequest);\r
70     if (null == futureResponse) {\r
71       LOG.error("Request could not be sent. response is null");\r
72       return null;\r
73     }\r
74 \r
75     boolean isSuccess = false;\r
76     DeviceRPCResponse response = null;\r
77 \r
78     OperationCode opCode = deviceRPCRequest.getOpDetails().getOpCode();\r
79     String deviceId = deviceRPCRequest.getDeviceDetails().getDeviceId();\r
80     long timeOut = getOperationTimeOut(deviceRPCRequest.getOptions().getExecutionTimeout());\r
81 \r
82     try {\r
83       waitForNotifications.waitForResult(deviceId, opCode, futureResponse, timeOut);\r
84       response = waitForNotifications.getOperationResult(deviceId, opCode);\r
85 \r
86       if (null == response) {\r
87         LOG.error("Request got timed out.");\r
88       } else {\r
89         LOG.debug("Received operation result for device : {}, operation = {} as {}", deviceId,\r
90             opCode, response);\r
91       }\r
92       waitForNotifications.stopOperation(deviceId, opCode);\r
93 \r
94       // if(isSuccess) {\r
95       // response = waitForNotifications.getOperationResult(deviceId, opCode);\r
96       // LOG.debug("Received operation result for device : {}, operation = {} as {}",deviceId,\r
97       // opCode,response);\r
98       //\r
99       // waitForNotifications.stopOperation(deviceId, opCode);\r
100       // }else {\r
101       // LOG.error("Request got timed out.");\r
102       // }\r
103     } catch (InterruptedException e) {\r
104       LOG.debug(\r
105           "InterruptedException while waiting for mapper operation result for device : {}, operation : {} request.",\r
106           deviceId, opCode);\r
107     }\r
108 \r
109     return response;\r
110   }\r
111 \r
112   private long getOperationTimeOut(long timeOut) {\r
113     if (timeOut > 0) {\r
114       return timeOut;\r
115     }\r
116 \r
117     if (null != config.getRequestTimeout()) {\r
118       timeOut = Long.valueOf(config.getRequestTimeout());\r
119     }\r
120 \r
121     return timeOut;\r
122   }\r
123 \r
124   @Async("threadPoolTaskExecutor1")\r
125   public void initiateDeviceReachabilityCheck(DeviceDataEntity deviceDataEntity) {
126     deviceDataEntity.setStartEpochMicrosec(VesAgentUtils.getStartEpochTime()*1000);\r
127     DeviceDetails deviceDetails = new DeviceDetails();\r
128     deviceDetails.setDeviceId(deviceDataEntity.getDeviceId());\r
129     deviceDetails.setOui(deviceDataEntity.getOui());\r
130     deviceDetails.setProductClass(deviceDataEntity.getProductClass());\r
131 \r
132     TR069OperationDetails operationDetails = new TR069OperationDetails();\r
133     operationDetails.setOpCode(CustomOperationCode.CONNECT);\r
134 \r
135     DeviceRPCRequest deviceRPCRequest = new DeviceRPCRequest();\r
136 \r
137     deviceRPCRequest.setDeviceDetails(deviceDetails);\r
138     deviceRPCRequest.setOpDetails(operationDetails);\r
139 \r
140     OperationOptions options = new OperationOptions();\r
141     if (null != config.getRequestTimeout()) {\r
142       options.setExecutionTimeout(Integer.valueOf(config.getRequestTimeout()));\r
143     }\r
144 \r
145     deviceRPCRequest.setOptions(options);\r
146 \r
147     DeviceRPCResponse deviceRPCResponse = performDeviceOperation(deviceRPCRequest);\r
148 \r
149     if (VesAgentUtils.isDeviceReachable(deviceRPCResponse)) {\r
150       LOG.debug("Device {} is reachable.", deviceDataEntity.getDeviceId());\r
151       try {\r
152         LOG.debug("Sending heatbeat event for device {}.", deviceDataEntity.getDeviceId());\r
153         heartBeatMessageHandler.sendHeartBeatEvent(deviceDataEntity, Integer.parseInt(\r
154             deviceDataEntity.getAttributesMap().get(VesAgentConstants.HEART_BEAT_PERIOD)));\r
155       } catch (NumberFormatException e) {\r
156         LOG.error("heartBeatPeriod doesn't have numeric value. ErrorMsg: {}", e.getMessage());\r
157       } catch (Exception e) {\r
158         LOG.error("Error while sending heart beat ves event. ErrorMsg: {}", e.getMessage());\r
159       }\r
160     }\r
161   }\r
162 }\r