2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2018 Nokia.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.bpmn.infrastructure.pnf.dmaap;
25 import java.io.IOException;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.ScheduledThreadPoolExecutor;
29 import java.util.concurrent.TimeUnit;
30 import javax.ws.rs.core.UriBuilder;
31 import org.apache.http.HttpResponse;
32 import org.apache.http.client.HttpClient;
33 import org.apache.http.client.methods.HttpGet;
34 import org.apache.http.impl.client.HttpClientBuilder;
35 import org.apache.http.util.EntityUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.core.env.Environment;
40 import org.springframework.stereotype.Component;
41 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
42 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
43 import org.onap.so.client.aai.AAIResourcesClient;
44 import org.onap.so.client.aai.AAIObjectType;
47 public class PnfEventReadyDmaapClient implements DmaapClient {
49 private static final Logger logger = LoggerFactory.getLogger(PnfEventReadyDmaapClient.class);
51 private HttpClient httpClient;
52 private Map<String, Runnable> pnfCorrelationIdToThreadMap;
53 private HttpGet getRequest;
54 private int topicListenerDelayInSeconds;
55 private volatile ScheduledThreadPoolExecutor executor;
56 private volatile boolean dmaapThreadListenerIsRunning;
58 public volatile List<HashMap<String, String>> updateInfoMap;
61 public PnfEventReadyDmaapClient(Environment env) {
62 httpClient = HttpClientBuilder.create().build();
63 pnfCorrelationIdToThreadMap = new ConcurrentHashMap<>();
64 topicListenerDelayInSeconds = env.getProperty("pnf.dmaap.topicListenerDelayInSeconds", Integer.class);
66 getRequest = new HttpGet(UriBuilder.fromUri(env.getProperty("pnf.dmaap.uriPathPrefix"))
67 .scheme(env.getProperty("pnf.dmaap.protocol")).host(env.getProperty("pnf.dmaap.host"))
68 .port(env.getProperty("pnf.dmaap.port", Integer.class)).path(env.getProperty("pnf.dmaap.topicName"))
69 .path(env.getProperty("pnf.dmaap.consumerGroup")).path(env.getProperty("pnf.dmaap.consumerId"))
71 updateInfoMap = new ArrayList<>();
75 public synchronized void registerForUpdate(String pnfCorrelationId, Runnable informConsumer,
76 Optional<HashMap<String, String>> updateInfo) {
77 logger.debug("registering for pnf ready dmaap event for pnf correlation id: {}", pnfCorrelationId);
78 HashMap<String, String> map = updateInfo.get();
79 if (map != null && map.size() > 0) {
80 synchronized (updateInfoMap) {
81 updateInfoMap.add(map);
84 pnfCorrelationIdToThreadMap.put(pnfCorrelationId, informConsumer);
85 if (!dmaapThreadListenerIsRunning) {
86 startDmaapThreadListener();
91 public synchronized Runnable unregister(String pnfCorrelationId) {
92 logger.debug("unregistering from pnf ready dmaap event for pnf correlation id: {}", pnfCorrelationId);
93 Runnable runnable = runnable = pnfCorrelationIdToThreadMap.remove(pnfCorrelationId);
94 synchronized (updateInfoMap) {
95 for (int i = updateInfoMap.size() - 1; i >= 0; i--) {
96 if (!updateInfoMap.get(i).containsKey("pnfCorrelationId"))
98 String id = updateInfoMap.get(i).get("pnfCorrelationId");
99 if (id != pnfCorrelationId)
101 updateInfoMap.remove(i);
104 if (pnfCorrelationIdToThreadMap.isEmpty()) {
105 stopDmaapThreadListener();
110 private synchronized void startDmaapThreadListener() {
111 if (!dmaapThreadListenerIsRunning) {
112 executor = new ScheduledThreadPoolExecutor(1);
113 executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
114 executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
115 executor.scheduleWithFixedDelay(new DmaapTopicListenerThread(), 0, topicListenerDelayInSeconds,
117 dmaapThreadListenerIsRunning = true;
121 private synchronized void stopDmaapThreadListener() {
122 if (dmaapThreadListenerIsRunning) {
124 dmaapThreadListenerIsRunning = false;
129 class DmaapTopicListenerThread implements Runnable {
134 logger.debug("dmaap listener starts listening pnf ready dmaap topic");
135 HttpResponse response = httpClient.execute(getRequest);
136 List<String> idList = getPnfCorrelationIdListFromResponse(response);
138 if (idList != null && idList.size() > 0) {
139 // send only body of response
140 registerClientResponse(idList.get(0), EntityUtils.toString(response.getEntity(), "UTF-8"));
143 idList.forEach(this::informAboutPnfReadyIfPnfCorrelationIdFound);
144 } catch (IOException e) {
145 logger.error("Exception caught during sending rest request to dmaap for listening event topic", e);
151 private List<String> getPnfCorrelationIdListFromResponse(HttpResponse response) throws IOException {
152 if (response.getStatusLine().getStatusCode() == 200) {
153 String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
154 if (responseString != null) {
155 return JsonUtilForPnfCorrelationId.parseJsonToGelAllPnfCorrelationId(responseString);
158 return Collections.emptyList();
161 private void informAboutPnfReadyIfPnfCorrelationIdFound(String pnfCorrelationId) {
162 Runnable runnable = unregister(pnfCorrelationId);
163 if (runnable != null) {
164 logger.debug("dmaap listener gets pnf ready event for pnfCorrelationId: {}", pnfCorrelationId);
169 private void registerClientResponse(String pnfCorrelationId, String response) {
171 String customerId = null;
172 String serviceType = null;
174 synchronized (updateInfoMap) {
175 for (HashMap<String, String> map : updateInfoMap) {
176 if (!map.containsKey("pnfCorrelationId"))
178 if (pnfCorrelationId != map.get("pnfCorrelationId"))
180 if (!map.containsKey("globalSubscriberID"))
182 if (!map.containsKey("serviceType"))
184 if (!map.containsKey("serviceInstanceId"))
186 customerId = map.get("pnfCorrelationId");
187 serviceType = map.get("serviceType");
188 serId = map.get("serviceInstanceId");
191 if (customerId == null || serviceType == null || serId == null)
193 AAIResourcesClient client = new AAIResourcesClient();
194 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE_METADATA, customerId,
196 client.update(uri, response);