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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=======================================================================
19 package org.commscope.tr069adapter.mapper.sync;
21 import java.util.HashMap;
23 import java.util.concurrent.Semaphore;
24 import java.util.concurrent.TimeUnit;
26 import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;
27 import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;
28 import org.commscope.tr069adapter.mapper.MapperConfigProperties;
29 import org.commscope.tr069adapter.mapper.acs.ACSRequestSender;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Component;
36 public class SynchronizedRequestHandler {
38 private static final Logger LOG = LoggerFactory.getLogger(SynchronizedRequestHandler.class);
40 private static Map<String, DeviceRPCResponse> opResultMap = new HashMap<>();
41 private static Map<String, Semaphore> semaphoreMap = new HashMap<>();
44 ACSRequestSender tr069RequestSender;
47 MapperConfigProperties config;
49 public DeviceRPCResponse performDeviceOperation(DeviceRPCRequest deviceRPCRequest) {
50 Long acsOperationId = tr069RequestSender.sendRequest(deviceRPCRequest);
52 if (null == acsOperationId) {
53 LOG.error("Request could not be sent. opId is null");
57 String mapperUniqOperId =
58 deviceRPCRequest.getDeviceDetails().getDeviceId() + "_" + acsOperationId;
59 LOG.debug("Received operation mapperUniqOperId = {}", mapperUniqOperId);
61 boolean isSuccess = false;
63 isSuccess = waitForResult(mapperUniqOperId);
64 } catch (InterruptedException e) {
66 "InterruptedException while waiting for tr069 operation result for operation request {}",
68 LOG.error("Exception : {}", e.getMessage());
69 Thread.currentThread().interrupt();
71 DeviceRPCResponse result = null;
73 LOG.error("Request got timed out for operation {}", mapperUniqOperId);
74 semaphoreMap.remove(mapperUniqOperId);
76 result = getOperationResult(mapperUniqOperId);
77 LOG.debug("Received operation result for mapperUniqOperId = {} result : {}", mapperUniqOperId,
84 public void notifyResult(DeviceRPCResponse opResult) {
85 Semaphore mutex = semaphoreMap
86 .remove(opResult.getDeviceDetails().getDeviceId() + "_" + opResult.getOperationId());
88 opResultMap.put(opResult.getDeviceDetails().getDeviceId() + "_" + opResult.getOperationId(),
94 private DeviceRPCResponse getOperationResult(String mapperUniqOperId) {
95 return opResultMap.remove(mapperUniqOperId);
98 private boolean waitForResult(String mapperUniqOperId) throws InterruptedException {
99 LOG.debug("Waiting for operation result for mapperUniqOperId : {}", mapperUniqOperId);
100 Semaphore semaphore = new Semaphore(0);
101 semaphoreMap.put(mapperUniqOperId, semaphore);
102 LOG.debug("Semaphore MAP size = {}", semaphoreMap.size());
103 LOG.debug("opResultMap MAP size = {}", opResultMap.size());
105 if (null != config.getRequesTimeout()) {
106 timeout = Integer.valueOf(config.getRequesTimeout());
108 return semaphore.tryAcquire(timeout, TimeUnit.SECONDS);