Development of NETCONF RPCs for tr-069 adapter to
[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;
20
21 import java.util.concurrent.Future;
22
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;
41
42 /**
43  * 
44  * @version 1.0
45  * @since June 12, 2020
46  * @author Prashant Kumar
47  */
48 @Component
49 public class AsyncRequestHandler {
50
51   private static final Logger LOG = LoggerFactory.getLogger(AsyncRequestHandler.class);
52
53   @Autowired
54   MapperRequestSender mapperRequestSender;
55
56   @Autowired
57   WaitForNotifications waitForNotifications;
58
59   @Autowired
60   HeartBeatMessageHandler heartBeatMessageHandler;
61
62   @Autowired
63   VesConfiguration config;
64
65   public DeviceRPCResponse performDeviceOperation(DeviceRPCRequest deviceRPCRequest) {
66     LOG.info("Initiating device connectivity request to ACS for device {}",
67         deviceRPCRequest.getDeviceDetails().getDeviceId());
68
69     Future<DeviceRPCResponse> futureResponse = mapperRequestSender.sendRequest(deviceRPCRequest);
70     if (null == futureResponse) {
71       LOG.error("Request could not be sent. response is null");
72       return null;
73     }
74
75     DeviceRPCResponse response = null;
76
77     OperationCode opCode = deviceRPCRequest.getOpDetails().getOpCode();
78     String deviceId = deviceRPCRequest.getDeviceDetails().getDeviceId();
79     long timeOut = getOperationTimeOut(deviceRPCRequest.getOptions().getExecutionTimeout());
80
81     try {
82       waitForNotifications.waitForResult(deviceId, opCode, futureResponse, timeOut);
83       response = waitForNotifications.getOperationResult(deviceId, opCode);
84
85       if (null == response) {
86         LOG.error("Request got timed out.");
87       } else {
88         LOG.debug("Received operation result for device : {}, operation = {} as {}", deviceId,
89             opCode, response);
90       }
91       waitForNotifications.stopOperation(deviceId, opCode);
92
93     } catch (InterruptedException e) {
94       LOG.debug(
95           "InterruptedException while waiting for mapper operation result for device : {}, operation : {} request.",
96           deviceId, opCode);
97       Thread.currentThread().interrupt();
98     }
99
100     return response;
101   }
102
103   private long getOperationTimeOut(long timeOut) {
104     if (timeOut > 0) {
105       return timeOut;
106     }
107
108     if (null != config.getRequestTimeout()) {
109       timeOut = Long.valueOf(config.getRequestTimeout());
110     }
111
112     return timeOut;
113   }
114
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());
122
123     TR069OperationDetails operationDetails = new TR069OperationDetails();
124     operationDetails.setOpCode(CustomOperationCode.CONNECT);
125
126     DeviceRPCRequest deviceRPCRequest = new DeviceRPCRequest();
127
128     deviceRPCRequest.setDeviceDetails(deviceDetails);
129     deviceRPCRequest.setOpDetails(operationDetails);
130
131     OperationOptions options = new OperationOptions();
132     if (null != config.getRequestTimeout()) {
133       options.setExecutionTimeout(Integer.valueOf(config.getRequestTimeout()));
134     }
135
136     deviceRPCRequest.setOptions(options);
137
138     DeviceRPCResponse deviceRPCResponse = performDeviceOperation(deviceRPCRequest);
139
140     if (VesAgentUtils.isDeviceReachable(deviceRPCResponse)) {
141       LOG.debug("Device {} is reachable.", deviceDataEntity.getDeviceId());
142       try {
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());
150       }
151     }
152   }
153 }