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
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
36 public class SynchronizedRequestHandler {
\r
38 private static final Logger LOG = LoggerFactory.getLogger(SynchronizedRequestHandler.class);
\r
40 private static Map<Long, DeviceRPCResponse> opResultMap = new HashMap<>();
\r
41 private static Map<Long, Semaphore> semaphoreMap = new HashMap<>();
\r
44 ACSRequestSender tr069RequestSender;
\r
47 MapperConfigProperties config;
\r
49 public DeviceRPCResponse performDeviceOperation(DeviceRPCRequest deviceRPCRequest) {
\r
50 Long opId = tr069RequestSender.sendRequest(deviceRPCRequest);
\r
52 LOG.error("Request could not be sent. opId is null");
\r
55 boolean isSuccess = false;
\r
57 isSuccess = waitForResult(opId);
\r
58 } catch (InterruptedException e) {
\r
60 "InterruptedException while waiting for tr069 operation result for operation request {}",
\r
62 LOG.error("Exception : {}", e.getMessage());
\r
63 Thread.currentThread().interrupt();
\r
65 DeviceRPCResponse result = null;
\r
67 LOG.error("Request got timed out.");
\r
69 result = getOperationResult(opId);
\r
70 LOG.debug("Received operation result for opId = {} GET-CONFIG : {}", opId, result);
\r
77 public void notifyResult(DeviceRPCResponse opResult) {
\r
78 opResultMap.put(opResult.getOperationId(), opResult);
\r
79 Semaphore mutex = semaphoreMap.remove(opResult.getOperationId());
\r
83 private DeviceRPCResponse getOperationResult(long opId) {
\r
84 return opResultMap.remove(opId);
\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
97 return semaphore.tryAcquire(timeout, TimeUnit.SECONDS);
\r