+/*\r
+ * ============LICENSE_START========================================================================\r
+ * ONAP : tr-069-adapter\r
+ * =================================================================================================\r
+ * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
+ * =================================================================================================\r
+ * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
+ * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
+ * may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
+ * either express or implied. See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ===============LICENSE_END=======================================================================\r
+ */\r
+\r
+package org.commscope.tr069adapter.mapper.sync;\r
+\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+import java.util.concurrent.Semaphore;\r
+import java.util.concurrent.TimeUnit;\r
+\r
+import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;\r
+import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;\r
+import org.commscope.tr069adapter.mapper.MapperConfigProperties;\r
+import org.commscope.tr069adapter.mapper.acs.ACSRequestSender;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.stereotype.Component;\r
+\r
+@Component\r
+public class SynchronizedRequestHandler {\r
+\r
+ private static final Logger LOG = LoggerFactory.getLogger(SynchronizedRequestHandler.class);\r
+\r
+ private static Map<Long, DeviceRPCResponse> opResultMap = new HashMap<>();\r
+ private static Map<Long, Semaphore> semaphoreMap = new HashMap<>();\r
+\r
+ @Autowired\r
+ ACSRequestSender tr069RequestSender;\r
+\r
+ @Autowired\r
+ MapperConfigProperties config;\r
+\r
+ public DeviceRPCResponse performDeviceOperation(DeviceRPCRequest deviceRPCRequest) {\r
+ Long opId = tr069RequestSender.sendRequest(deviceRPCRequest);\r
+ if (null == opId) {\r
+ LOG.error("Request could not be sent. opId is null");\r
+ return null;\r
+ }\r
+ boolean isSuccess = false;\r
+ try {\r
+ isSuccess = waitForResult(opId);\r
+ } catch (InterruptedException e) {\r
+ LOG.debug(\r
+ "InterruptedException while waiting for tr069 operation result for operation request {}",\r
+ deviceRPCRequest);\r
+ LOG.error("Exception : {}", e.getMessage());\r
+ Thread.currentThread().interrupt();\r
+ }\r
+ DeviceRPCResponse result = null;\r
+ if (!isSuccess) {\r
+ LOG.error("Request got timed out.");\r
+ } else {\r
+ result = getOperationResult(opId);\r
+ LOG.debug("Received operation result for opId = {} GET-CONFIG : {}", opId, result);\r
+\r
+ }\r
+ return result;\r
+\r
+ }\r
+\r
+ public void notifyResult(DeviceRPCResponse opResult) {\r
+ opResultMap.put(opResult.getOperationId(), opResult);\r
+ Semaphore mutex = semaphoreMap.remove(opResult.getOperationId());\r
+ mutex.release();\r
+ }\r
+\r
+ private DeviceRPCResponse getOperationResult(long opId) {\r
+ return opResultMap.remove(opId);\r
+ }\r
+\r
+ private boolean waitForResult(long opId) throws InterruptedException {\r
+ LOG.debug("Waiting for operation result for opId : {}", opId);\r
+ Semaphore semaphore = new Semaphore(0);\r
+ semaphoreMap.put(opId, semaphore);\r
+ LOG.debug("Semaphore MAP size = {}", semaphoreMap.size());\r
+ LOG.debug("opResultMap MAP size = {}", opResultMap.size());\r
+ Integer timeout = 0;\r
+ if (null != config.getRequesTimeout()) {\r
+ timeout = Integer.valueOf(config.getRequesTimeout());\r
+ }\r
+ return semaphore.tryAcquire(timeout, TimeUnit.SECONDS);\r
+ }\r
+\r
+}\r