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