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;
21 import java.util.concurrent.Future;
23 import org.commscope.tr069adapter.acs.common.DeviceDetails;
24 import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;
25 import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;
26 import org.commscope.tr069adapter.acs.common.OperationCode;
27 import org.commscope.tr069adapter.acs.common.OperationOptions;
28 import org.commscope.tr069adapter.acs.common.dto.CustomOperationCode;
29 import org.commscope.tr069adapter.acs.common.dto.TR069OperationDetails;
30 import org.commscope.tr069adapter.vesagent.VesConfiguration;
31 import org.commscope.tr069adapter.vesagent.controller.HeartBeatMessageHandler;
32 import org.commscope.tr069adapter.vesagent.entity.DeviceDataEntity;
33 import org.commscope.tr069adapter.vesagent.mapper.MapperRequestSender;
34 import org.commscope.tr069adapter.vesagent.util.VesAgentConstants;
35 import org.commscope.tr069adapter.vesagent.util.VesAgentUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.scheduling.annotation.Async;
40 import org.springframework.stereotype.Component;
45 * @since June 12, 2020
46 * @author Prashant Kumar
49 public class AsyncRequestHandler {
51 private static final Logger LOG = LoggerFactory.getLogger(AsyncRequestHandler.class);
54 MapperRequestSender mapperRequestSender;
57 WaitForNotifications waitForNotifications;
60 HeartBeatMessageHandler heartBeatMessageHandler;
63 VesConfiguration config;
65 public DeviceRPCResponse performDeviceOperation(DeviceRPCRequest deviceRPCRequest) {
66 LOG.info("Initiating device connectivity request to ACS for device {}",
67 deviceRPCRequest.getDeviceDetails().getDeviceId());
69 Future<DeviceRPCResponse> futureResponse = mapperRequestSender.sendRequest(deviceRPCRequest);
70 if (null == futureResponse) {
71 LOG.error("Request could not be sent. response is null");
75 DeviceRPCResponse response = null;
77 OperationCode opCode = deviceRPCRequest.getOpDetails().getOpCode();
78 String deviceId = deviceRPCRequest.getDeviceDetails().getDeviceId();
79 long timeOut = getOperationTimeOut(deviceRPCRequest.getOptions().getExecutionTimeout());
82 waitForNotifications.waitForResult(deviceId, opCode, futureResponse, timeOut);
83 response = waitForNotifications.getOperationResult(deviceId, opCode);
85 if (null == response) {
86 LOG.error("Request got timed out.");
88 LOG.debug("Received operation result for device : {}, operation = {} as {}", deviceId,
91 waitForNotifications.stopOperation(deviceId, opCode);
93 } catch (InterruptedException e) {
95 "InterruptedException while waiting for mapper operation result for device : {}, operation : {} request.",
97 Thread.currentThread().interrupt();
103 private long getOperationTimeOut(long timeOut) {
108 if (null != config.getRequestTimeout()) {
109 timeOut = Long.valueOf(config.getRequestTimeout());
115 @Async("threadPoolTaskExecutor1")
116 public void initiateDeviceReachabilityCheck(DeviceDataEntity deviceDataEntity) {
117 deviceDataEntity.setStartEpochMicrosec(VesAgentUtils.getStartEpochTime() * 1000);
118 DeviceDetails deviceDetails = new DeviceDetails();
119 deviceDetails.setDeviceId(deviceDataEntity.getDeviceId());
120 deviceDetails.setOui(deviceDataEntity.getOui());
121 deviceDetails.setProductClass(deviceDataEntity.getProductClass());
123 TR069OperationDetails operationDetails = new TR069OperationDetails();
124 operationDetails.setOpCode(CustomOperationCode.CONNECT);
126 DeviceRPCRequest deviceRPCRequest = new DeviceRPCRequest();
128 deviceRPCRequest.setDeviceDetails(deviceDetails);
129 deviceRPCRequest.setOpDetails(operationDetails);
131 OperationOptions options = new OperationOptions();
132 if (null != config.getRequestTimeout()) {
133 options.setExecutionTimeout(Integer.valueOf(config.getRequestTimeout()));
136 deviceRPCRequest.setOptions(options);
138 DeviceRPCResponse deviceRPCResponse = performDeviceOperation(deviceRPCRequest);
140 if (VesAgentUtils.isDeviceReachable(deviceRPCResponse)) {
141 LOG.debug("Device {} is reachable.", deviceDataEntity.getDeviceId());
143 LOG.debug("Sending heatbeat event for device {}.", deviceDataEntity.getDeviceId());
144 heartBeatMessageHandler.sendHeartBeatEvent(deviceDataEntity, Integer.parseInt(
145 deviceDataEntity.getAttributesMap().get(VesAgentConstants.HEART_BEAT_PERIOD)));
146 } catch (NumberFormatException e) {
147 LOG.error("heartBeatPeriod doesn't have numeric value. ErrorMsg: {}", e.getMessage());
148 } catch (Exception e) {
149 LOG.error("Error while sending heart beat ves event. ErrorMsg: {}", e.getMessage());