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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=======================================================================
19 package org.commscope.tr069adapter.vesagent.async;
\r
21 import java.util.concurrent.Future;
\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
45 * @since June 12, 2020
\r
46 * @author Prashant Kumar
\r
49 public class AsyncRequestHandler {
\r
51 private static final Logger LOG = LoggerFactory.getLogger(AsyncRequestHandler.class);
\r
54 MapperRequestSender mapperRequestSender;
\r
57 WaitForNotifications waitForNotifications;
\r
60 HeartBeatMessageHandler heartBeatMessageHandler;
\r
63 VesConfiguration config;
\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
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
75 boolean isSuccess = false;
\r
76 DeviceRPCResponse response = null;
\r
78 OperationCode opCode = deviceRPCRequest.getOpDetails().getOpCode();
\r
79 String deviceId = deviceRPCRequest.getDeviceDetails().getDeviceId();
\r
80 long timeOut = getOperationTimeOut(deviceRPCRequest.getOptions().getExecutionTimeout());
\r
83 waitForNotifications.waitForResult(deviceId, opCode, futureResponse, timeOut);
\r
84 response = waitForNotifications.getOperationResult(deviceId, opCode);
\r
86 if (null == response) {
\r
87 LOG.error("Request got timed out.");
\r
89 LOG.debug("Received operation result for device : {}, operation = {} as {}", deviceId,
\r
92 waitForNotifications.stopOperation(deviceId, opCode);
\r
95 // response = waitForNotifications.getOperationResult(deviceId, opCode);
\r
96 // LOG.debug("Received operation result for device : {}, operation = {} as {}",deviceId,
\r
97 // opCode,response);
\r
99 // waitForNotifications.stopOperation(deviceId, opCode);
\r
101 // LOG.error("Request got timed out.");
\r
103 } catch (InterruptedException e) {
\r
105 "InterruptedException while waiting for mapper operation result for device : {}, operation : {} request.",
\r
112 private long getOperationTimeOut(long timeOut) {
\r
117 if (null != config.getRequestTimeout()) {
\r
118 timeOut = Long.valueOf(config.getRequestTimeout());
\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
132 TR069OperationDetails operationDetails = new TR069OperationDetails();
\r
133 operationDetails.setOpCode(CustomOperationCode.CONNECT);
\r
135 DeviceRPCRequest deviceRPCRequest = new DeviceRPCRequest();
\r
137 deviceRPCRequest.setDeviceDetails(deviceDetails);
\r
138 deviceRPCRequest.setOpDetails(operationDetails);
\r
140 OperationOptions options = new OperationOptions();
\r
141 if (null != config.getRequestTimeout()) {
\r
142 options.setExecutionTimeout(Integer.valueOf(config.getRequestTimeout()));
\r
145 deviceRPCRequest.setOptions(options);
\r
147 DeviceRPCResponse deviceRPCResponse = performDeviceOperation(deviceRPCRequest);
\r
149 if (VesAgentUtils.isDeviceReachable(deviceRPCResponse)) {
\r
150 LOG.debug("Device {} is reachable.", deviceDataEntity.getDeviceId());
\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