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.vesagent.async;
\r
21 import java.util.HashMap;
\r
22 import java.util.Map;
\r
23 import java.util.concurrent.Future;
\r
24 import java.util.concurrent.Semaphore;
\r
25 import java.util.concurrent.TimeUnit;
\r
27 import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;
\r
28 import org.commscope.tr069adapter.acs.common.OperationCode;
\r
29 import org.commscope.tr069adapter.acs.common.OperationResponse;
\r
30 import org.commscope.tr069adapter.acs.common.dto.CustomOperationCode;
\r
31 import org.commscope.tr069adapter.mapper.model.VESNotification;
\r
32 import org.commscope.tr069adapter.vesagent.util.VesAgentConstants;
\r
33 import org.commscope.tr069adapter.vesagent.util.VesAgentUtils;
\r
34 import org.slf4j.Logger;
\r
35 import org.slf4j.LoggerFactory;
\r
36 import org.springframework.stereotype.Component;
\r
39 public class WaitForNotifications {
\r
41 private static final Logger LOG = LoggerFactory.getLogger(WaitForNotifications.class);
\r
43 private static Map<String, Future<DeviceRPCResponse>> opFutureMap = new HashMap<>();
\r
44 private static Map<String, DeviceRPCResponse> opResultMap = new HashMap<>();
\r
45 private static Map<String, Semaphore> semaphoreMap = new HashMap<>();
\r
47 public void notifyDeviceNotification(VESNotification notification) {
\r
48 String deviceOperationKey = VesAgentUtils.getDeviceOperationKey(
\r
49 notification.getDevnotification().getDeviceDetails().getDeviceId(),
\r
50 CustomOperationCode.CONNECT);
\r
52 if (!semaphoreMap.containsKey(deviceOperationKey)) {
\r
56 DeviceRPCResponse response = new DeviceRPCResponse();
\r
57 response.setDeviceDetails(notification.getDevnotification().getDeviceDetails());
\r
59 OperationResponse operationResponse = new OperationResponse();
\r
60 operationResponse.setStatus(VesAgentConstants.DEVICE_IS_REACHABLE);
\r
61 operationResponse.setOperationCode(CustomOperationCode.CONNECT);
\r
63 response.setOperationResponse(operationResponse);
\r
65 opResultMap.put(deviceOperationKey, response);
\r
66 Semaphore mutex = semaphoreMap.remove(deviceOperationKey);
\r
71 public void notifyResult(DeviceRPCResponse opResult) {
\r
72 String deviceOperationKey =
\r
73 VesAgentUtils.getDeviceOperationKey(opResult.getDeviceDetails().getDeviceId(),
\r
74 opResult.getOperationResponse().getOperationCode());
\r
76 if (!semaphoreMap.containsKey(deviceOperationKey)) {
\r
80 opResultMap.put(deviceOperationKey, opResult);
\r
81 Semaphore mutex = semaphoreMap.remove(deviceOperationKey);
\r
85 public DeviceRPCResponse getOperationResult(String deviceId, OperationCode opCode) {
\r
86 return opResultMap.remove(VesAgentUtils.getDeviceOperationKey(deviceId, opCode));
\r
89 public boolean waitForResult(String deviceId, OperationCode opCode,
\r
90 Future<DeviceRPCResponse> futureResponse, long timeout) throws InterruptedException {
\r
91 LOG.debug("Waiting for operation result for device:{}, operation: {}", deviceId, opCode);
\r
93 String deviceOperationKey = VesAgentUtils.getDeviceOperationKey(deviceId, opCode);
\r
94 opFutureMap.put(deviceOperationKey, futureResponse);
\r
96 Semaphore semaphore = new Semaphore(0);
\r
97 semaphoreMap.put(deviceOperationKey, semaphore);
\r
99 LOG.debug("Semaphore MAP size = {}", semaphoreMap.size());
\r
100 LOG.debug("opResultMap MAP size = {}", opResultMap.size());
\r
101 LOG.debug("opFutureMap MAP size = {}", opFutureMap.size());
\r
103 return semaphore.tryAcquire(timeout, TimeUnit.SECONDS);
\r
106 public void stopOperation(String deviceId, OperationCode opCode) {
\r
107 LOG.debug("Stopping waiting for operation result thread for device:{}, operation: {}", deviceId,
\r
110 Future<DeviceRPCResponse> operationInstance =
\r
111 opFutureMap.remove(VesAgentUtils.getDeviceOperationKey(deviceId, opCode));
\r
113 if (null != operationInstance) {
\r
114 LOG.info("Stopping operation result waiting thread for operation : {}", operationInstance);
\r
115 operationInstance.cancel(true);
\r