Adding Copyright Missing files
[oam/tr069-adapter.git] / ves-agent / src / main / java / org / commscope / tr069adapter / vesagent / async / WaitForNotifications.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.vesagent.async;\r
20 \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
26 \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
37 \r
38 @Component\r
39 public class WaitForNotifications {\r
40 \r
41   private static final Logger LOG = LoggerFactory.getLogger(WaitForNotifications.class);\r
42 \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
46 \r
47   public void notifyDeviceNotification(VESNotification notification) {\r
48     String deviceOperationKey = VesAgentUtils.getDeviceOperationKey(\r
49         notification.getDevnotification().getDeviceDetails().getDeviceId(),\r
50         CustomOperationCode.CONNECT);\r
51 \r
52     if (!semaphoreMap.containsKey(deviceOperationKey)) {\r
53       return;\r
54     }\r
55 \r
56     DeviceRPCResponse response = new DeviceRPCResponse();\r
57     response.setDeviceDetails(notification.getDevnotification().getDeviceDetails());\r
58 \r
59     OperationResponse operationResponse = new OperationResponse();\r
60     operationResponse.setStatus(VesAgentConstants.DEVICE_IS_REACHABLE);\r
61     operationResponse.setOperationCode(CustomOperationCode.CONNECT);\r
62 \r
63     response.setOperationResponse(operationResponse);\r
64 \r
65     opResultMap.put(deviceOperationKey, response);\r
66     Semaphore mutex = semaphoreMap.remove(deviceOperationKey);\r
67     mutex.release();\r
68   }\r
69 \r
70 \r
71   public void notifyResult(DeviceRPCResponse opResult) {\r
72     String deviceOperationKey =\r
73         VesAgentUtils.getDeviceOperationKey(opResult.getDeviceDetails().getDeviceId(),\r
74             opResult.getOperationResponse().getOperationCode());\r
75 \r
76     if (!semaphoreMap.containsKey(deviceOperationKey)) {\r
77       return;\r
78     }\r
79 \r
80     opResultMap.put(deviceOperationKey, opResult);\r
81     Semaphore mutex = semaphoreMap.remove(deviceOperationKey);\r
82     mutex.release();\r
83   }\r
84 \r
85   public DeviceRPCResponse getOperationResult(String deviceId, OperationCode opCode) {\r
86     return opResultMap.remove(VesAgentUtils.getDeviceOperationKey(deviceId, opCode));\r
87   }\r
88 \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
92 \r
93     String deviceOperationKey = VesAgentUtils.getDeviceOperationKey(deviceId, opCode);\r
94     opFutureMap.put(deviceOperationKey, futureResponse);\r
95 \r
96     Semaphore semaphore = new Semaphore(0);\r
97     semaphoreMap.put(deviceOperationKey, semaphore);\r
98 \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
102 \r
103     return semaphore.tryAcquire(timeout, TimeUnit.SECONDS);\r
104   }\r
105 \r
106   public void stopOperation(String deviceId, OperationCode opCode) {\r
107     LOG.debug("Stopping waiting for operation result thread for device:{}, operation: {}", deviceId,\r
108         opCode);\r
109 \r
110     Future<DeviceRPCResponse> operationInstance =\r
111         opFutureMap.remove(VesAgentUtils.getDeviceOperationKey(deviceId, opCode));\r
112 \r
113     if (null != operationInstance) {\r
114       LOG.info("Stopping operation result waiting thread for operation : {}", operationInstance);\r
115       operationInstance.cancel(true);\r
116     }\r
117   }\r
118 }\r