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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
19 package org.commscope.tr069adapter.mapper.sync;
\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
35 public class SynchronizedRequestHandler {
\r
37 private static final Logger LOG = LoggerFactory.getLogger(SynchronizedRequestHandler.class);
\r
39 private static Map<String, DeviceRPCResponse> opResultMap = new HashMap<>();
\r
40 private static Map<String, Semaphore> semaphoreMap = new HashMap<>();
\r
43 ACSRequestSender tr069RequestSender;
\r
46 MapperConfigProperties config;
\r
48 public DeviceRPCResponse performDeviceOperation(DeviceRPCRequest deviceRPCRequest) {
\r
49 Long acsOperationId = tr069RequestSender.sendRequest(deviceRPCRequest);
\r
51 if (null == acsOperationId) {
\r
52 LOG.error("Request could not be sent. opId is null");
\r
56 String mapperUniqOperId =
\r
57 deviceRPCRequest.getDeviceDetails().getDeviceId() + "_" + acsOperationId;
\r
58 LOG.debug("Received operation mapperUniqOperId = {}", mapperUniqOperId);
\r
60 boolean isSuccess = false;
\r
62 isSuccess = waitForResult(mapperUniqOperId);
\r
63 } catch (InterruptedException e) {
\r
65 "InterruptedException while waiting for tr069 operation result for operation request {}",
\r
67 LOG.error("Exception : {}", e.getMessage());
\r
68 Thread.currentThread().interrupt();
\r
70 DeviceRPCResponse result = null;
\r
72 LOG.error("Request got timed out for operation {}", mapperUniqOperId);
\r
73 semaphoreMap.remove(mapperUniqOperId);
\r
75 result = getOperationResult(mapperUniqOperId);
\r
76 LOG.debug("Received operation result for mapperUniqOperId = {} result : {}", mapperUniqOperId,
\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
93 private DeviceRPCResponse getOperationResult(String mapperUniqOperId) {
\r
94 return opResultMap.remove(mapperUniqOperId);
\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
107 return semaphore.tryAcquire(timeout, TimeUnit.SECONDS);
\r