db0ee1b9a972b0b97b8353f8c1b4e3e9ac351e10
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * ECOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalsdk.core.onboarding.ueb;
21
22 import java.util.Map;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.LinkedBlockingQueue;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 /**
30  * A thin wrapper around ConcurrentHashMap that stores a queue for each
31  * Requester that is waiting for a Reply. When a reply is received that has a
32  * matching msgId, that requesters queue is populated with the reply message.
33  * 
34  * Primarily for the UebManager to track requests while it waits for responses.
35  */
36 public class WaitingRequestersQueueList {
37         private final Log logger = LogFactory.getLog(getClass());
38
39         private final Map<String, LinkedBlockingQueue<UebMsg>> map;
40
41         public WaitingRequestersQueueList() {
42                 map = new ConcurrentHashMap<>();
43         }
44         
45         public void addQueueToMap(String msgId, LinkedBlockingQueue<UebMsg> queue) {
46                 this.map.put(msgId, queue);
47         }
48
49         public void addMsg(String msgId, UebMsg message) {
50                 LinkedBlockingQueue<UebMsg> queue = this.map.get(msgId);
51                 if (queue != null) {
52                         queue.add(message);
53                 } else {
54                         logger.warn("Did not find entry in WaitingRequestersQueueList for msgId " + msgId);
55                 }
56         }
57
58         public void removeQueueFromMap(String msgId) {
59                 this.map.remove(msgId);
60         }
61
62         public String toString() {
63                 StringBuffer sb = new StringBuffer();
64                 sb.append("Map contains " + this.map.size() + " Publishers.");
65                 for (Map.Entry<String, LinkedBlockingQueue<UebMsg>> entry : this.map.entrySet()) {
66                         String key = entry.getKey().toString();
67                         LinkedBlockingQueue<UebMsg> queue = entry.getValue();
68                         sb.append("Entry msgId, " + key + " queue " + queue);
69                 }
70                 return sb.toString();
71         }
72
73 }