532443a0787c302569ef905ca4bfd6432b0a1074
[oam/tr069-adapter.git] / mapper / src / main / java / org / commscope / tr069adapter / mapper / sync / SynchronizedRequestHandler.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.mapper.sync;\r
20 \r
21 import java.util.HashMap;\r
22 import java.util.Map;\r
23 import java.util.concurrent.Semaphore;\r
24 import java.util.concurrent.TimeUnit;\r
25 import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;\r
26 import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;\r
27 import org.commscope.tr069adapter.mapper.MapperConfigProperties;\r
28 import org.commscope.tr069adapter.mapper.acs.ACSRequestSender;\r
29 import org.slf4j.Logger;\r
30 import org.slf4j.LoggerFactory;\r
31 import org.springframework.beans.factory.annotation.Autowired;\r
32 import org.springframework.stereotype.Component;\r
33 \r
34 @Component\r
35 public class SynchronizedRequestHandler {\r
36 \r
37   private static final Logger LOG = LoggerFactory.getLogger(SynchronizedRequestHandler.class);\r
38 \r
39   private static Map<String, DeviceRPCResponse> opResultMap = new HashMap<>();\r
40   private static Map<String, Semaphore> semaphoreMap = new HashMap<>();\r
41 \r
42   @Autowired\r
43   ACSRequestSender tr069RequestSender;\r
44 \r
45   @Autowired\r
46   MapperConfigProperties config;\r
47 \r
48   public DeviceRPCResponse performDeviceOperation(DeviceRPCRequest deviceRPCRequest) {\r
49     Long acsOperationId = tr069RequestSender.sendRequest(deviceRPCRequest);\r
50 \r
51     if (null == acsOperationId) {\r
52       LOG.error("Request could not be sent. opId is null");\r
53       return null;\r
54     }\r
55 \r
56     String mapperUniqOperId =\r
57         deviceRPCRequest.getDeviceDetails().getDeviceId() + "_" + acsOperationId;\r
58     LOG.debug("Received operation mapperUniqOperId = {}", mapperUniqOperId);\r
59 \r
60     boolean isSuccess = false;\r
61     try {\r
62       isSuccess = waitForResult(mapperUniqOperId);\r
63     } catch (InterruptedException e) {\r
64       LOG.debug(\r
65           "InterruptedException while waiting for tr069 operation result for operation request {}",\r
66           deviceRPCRequest);\r
67       LOG.error("Exception : {}", e.getMessage());\r
68       Thread.currentThread().interrupt();\r
69     }\r
70     DeviceRPCResponse result = null;\r
71     if (!isSuccess) {\r
72       LOG.error("Request got timed out for operation {}", mapperUniqOperId);\r
73       semaphoreMap.remove(mapperUniqOperId);\r
74     } else {\r
75       result = getOperationResult(mapperUniqOperId);\r
76       LOG.debug("Received operation result for mapperUniqOperId = {} result : {}", mapperUniqOperId,\r
77           result);\r
78     }\r
79     return result;\r
80 \r
81   }\r
82 \r
83   public void notifyResult(DeviceRPCResponse opResult) {\r
84     Semaphore mutex = semaphoreMap\r
85         .remove(opResult.getDeviceDetails().getDeviceId() + "_" + opResult.getOperationId());\r
86     if (mutex != null) {\r
87       opResultMap.put(opResult.getDeviceDetails().getDeviceId() + "_" + opResult.getOperationId(),\r
88           opResult);\r
89       mutex.release();\r
90     }\r
91   }\r
92 \r
93   private DeviceRPCResponse getOperationResult(String mapperUniqOperId) {\r
94     return opResultMap.remove(mapperUniqOperId);\r
95   }\r
96 \r
97   private boolean waitForResult(String mapperUniqOperId) throws InterruptedException {\r
98     LOG.debug("Waiting for operation result for mapperUniqOperId : {}", mapperUniqOperId);\r
99     Semaphore semaphore = new Semaphore(0);\r
100     semaphoreMap.put(mapperUniqOperId, semaphore);\r
101     LOG.debug("Semaphore MAP size = {}", semaphoreMap.size());\r
102     LOG.debug("opResultMap MAP size = {}", opResultMap.size());\r
103     Integer timeout = 0;\r
104     if (null != config.getRequesTimeout()) {\r
105       timeout = Integer.valueOf(config.getRequesTimeout());\r
106     }\r
107     return semaphore.tryAcquire(timeout, TimeUnit.SECONDS);\r
108   }\r
109 }\r