Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / mapper / src / main / java / org / commscope / tr069adapter / mapper / sync / SynchronizedRequestHandler.java
1 /*
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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=======================================================================
17  */
18
19 package org.commscope.tr069adapter.mapper.sync;
20
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.concurrent.Semaphore;
24 import java.util.concurrent.TimeUnit;
25
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;
34
35 @Component
36 public class SynchronizedRequestHandler {
37
38   private static final Logger LOG = LoggerFactory.getLogger(SynchronizedRequestHandler.class);
39
40   private static Map<String, DeviceRPCResponse> opResultMap = new HashMap<>();
41   private static Map<String, Semaphore> semaphoreMap = new HashMap<>();
42
43   @Autowired
44   ACSRequestSender tr069RequestSender;
45
46   @Autowired
47   MapperConfigProperties config;
48
49   public DeviceRPCResponse performDeviceOperation(DeviceRPCRequest deviceRPCRequest) {
50     Long acsOperationId = tr069RequestSender.sendRequest(deviceRPCRequest);
51
52     if (null == acsOperationId) {
53       LOG.error("Request could not be sent. opId is null");
54       return null;
55     }
56
57     String mapperUniqOperId =
58         deviceRPCRequest.getDeviceDetails().getDeviceId() + "_" + acsOperationId;
59     LOG.debug("Received operation mapperUniqOperId = {}", mapperUniqOperId);
60
61     boolean isSuccess = false;
62     try {
63       isSuccess = waitForResult(mapperUniqOperId);
64     } catch (InterruptedException e) {
65       LOG.debug(
66           "InterruptedException while waiting for tr069 operation result for operation request {}",
67           deviceRPCRequest);
68       LOG.error("Exception : {}", e.getMessage());
69       Thread.currentThread().interrupt();
70     }
71     DeviceRPCResponse result = null;
72     if (!isSuccess) {
73       LOG.error("Request got timed out for operation {}", mapperUniqOperId);
74       semaphoreMap.remove(mapperUniqOperId);
75     } else {
76       result = getOperationResult(mapperUniqOperId);
77       LOG.debug("Received operation result for mapperUniqOperId = {} result : {}", mapperUniqOperId,
78           result);
79     }
80     return result;
81
82   }
83
84   public void notifyResult(DeviceRPCResponse opResult) {
85     Semaphore mutex = semaphoreMap
86         .remove(opResult.getDeviceDetails().getDeviceId() + "_" + opResult.getOperationId());
87     if (mutex != null) {
88       opResultMap.put(opResult.getDeviceDetails().getDeviceId() + "_" + opResult.getOperationId(),
89           opResult);
90       mutex.release();
91     }
92   }
93
94   private DeviceRPCResponse getOperationResult(String mapperUniqOperId) {
95     return opResultMap.remove(mapperUniqOperId);
96   }
97
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());
104     Integer timeout = 0;
105     if (null != config.getRequesTimeout()) {
106       timeout = Integer.valueOf(config.getRequesTimeout());
107     }
108     return semaphore.tryAcquire(timeout, TimeUnit.SECONDS);
109   }
110 }