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;
26 import java.nio.charset.StandardCharsets;
28 import java.util.concurrent.ConcurrentHashMap;
29 import java.util.concurrent.ScheduledThreadPoolExecutor;
30 import java.util.concurrent.TimeUnit;
31 import javax.ws.rs.core.UriBuilder;
32 import org.apache.http.HttpResponse;
33 import org.apache.http.HttpStatus;
34 import org.apache.http.client.HttpClient;
35 import org.apache.http.client.methods.HttpGet;
36 import org.apache.http.impl.client.HttpClientBuilder;
37 import org.apache.http.util.EntityUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.core.env.Environment;
42 import org.springframework.stereotype.Component;
43 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
44 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
45 import org.onap.so.client.aai.AAIResourcesClient;
46 import org.onap.so.client.aai.AAIObjectType;
47 import static org.onap.so.bpmn.infrastructure.pnf.dmaap.JsonUtilForPnfCorrelationId.*;
50 public class PnfEventReadyDmaapClient implements DmaapClient {
52 private static final Logger logger = LoggerFactory.getLogger(PnfEventReadyDmaapClient.class);
54 private HttpClient httpClient;
55 private Map<String, Runnable> pnfCorrelationIdToThreadMap;
56 private HttpGet getRequest;
57 private int topicListenerDelayInSeconds;
58 private volatile ScheduledThreadPoolExecutor executor;
59 private volatile boolean dmaapThreadListenerIsRunning;
60 private volatile List<Map<String, String>> listOfUpdateInfoMap;
63 public PnfEventReadyDmaapClient(Environment env) {
64 httpClient = HttpClientBuilder.create().build();
65 pnfCorrelationIdToThreadMap = new ConcurrentHashMap<>();
66 topicListenerDelayInSeconds = env.getProperty("pnf.dmaap.topicListenerDelayInSeconds", Integer.class);
68 getRequest = new HttpGet(UriBuilder.fromUri(env.getProperty("pnf.dmaap.uriPathPrefix"))
69 .scheme(env.getProperty("pnf.dmaap.protocol")).host(env.getProperty("pnf.dmaap.host"))
70 .port(env.getProperty("pnf.dmaap.port", Integer.class)).path(env.getProperty("pnf.dmaap.topicName"))
71 .path(env.getProperty("pnf.dmaap.consumerGroup")).path(env.getProperty("pnf.dmaap.consumerId"))
73 listOfUpdateInfoMap = new ArrayList<>();
77 public synchronized void registerForUpdate(String pnfCorrelationId, Runnable informConsumer,
78 Map<String, String> updateInfo) {
79 logger.debug("registering for pnf ready dmaap event for pnf correlation id: {}", pnfCorrelationId);
80 synchronized (listOfUpdateInfoMap) {
81 listOfUpdateInfoMap.add(updateInfo);
83 pnfCorrelationIdToThreadMap.put(pnfCorrelationId, informConsumer);
84 if (!dmaapThreadListenerIsRunning) {
85 startDmaapThreadListener();
90 public synchronized Runnable unregister(String pnfCorrelationId) {
91 logger.debug("unregistering from pnf ready dmaap event for pnf correlation id: {}", pnfCorrelationId);
92 Runnable runnable = pnfCorrelationIdToThreadMap.remove(pnfCorrelationId);
93 synchronized (listOfUpdateInfoMap) {
94 for (int i = listOfUpdateInfoMap.size() - 1; i >= 0; i--) {
95 if (!listOfUpdateInfoMap.get(i).containsKey("pnfCorrelationId"))
97 String id = listOfUpdateInfoMap.get(i).get("pnfCorrelationId");
98 if (id != pnfCorrelationId)
100 listOfUpdateInfoMap.remove(i);
103 if (pnfCorrelationIdToThreadMap.isEmpty()) {
104 stopDmaapThreadListener();
109 private synchronized void startDmaapThreadListener() {
110 if (!dmaapThreadListenerIsRunning) {
111 executor = new ScheduledThreadPoolExecutor(1);
112 executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
113 executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
114 executor.scheduleWithFixedDelay(new DmaapTopicListenerThread(), 0, topicListenerDelayInSeconds,
116 dmaapThreadListenerIsRunning = true;
120 private synchronized void stopDmaapThreadListener() {
121 if (dmaapThreadListenerIsRunning) {
123 dmaapThreadListenerIsRunning = false;
128 class DmaapTopicListenerThread implements Runnable {
133 logger.debug("dmaap listener starts listening pnf ready dmaap topic");
134 HttpResponse response = httpClient.execute(getRequest);
135 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
136 String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
137 List<String> idList = parseJsonToGelAllPnfCorrelationId(responseString);
138 idList.stream().findFirst().ifPresent(id -> registerClientResponse(id, responseString));
139 idList.forEach(this::informAboutPnfReadyIfPnfCorrelationIdFound);
141 } catch (IOException e) {
142 logger.error("Exception caught during sending rest request to dmaap for listening event topic", e);
148 private void informAboutPnfReadyIfPnfCorrelationIdFound(String pnfCorrelationId) {
149 Runnable runnable = unregister(pnfCorrelationId);
150 if (runnable != null) {
151 logger.debug("dmaap listener gets pnf ready event for pnfCorrelationId: {}", pnfCorrelationId);
156 private void registerClientResponse(String pnfCorrelationId, String response) {
158 String customerId = null;
159 String serviceType = null;
161 synchronized (listOfUpdateInfoMap) {
162 for (Map<String, String> map : listOfUpdateInfoMap) {
163 if (!map.containsKey("pnfCorrelationId"))
165 if (pnfCorrelationId != map.get("pnfCorrelationId"))
167 if (!map.containsKey("globalSubscriberID"))
169 if (!map.containsKey("serviceType"))
171 if (!map.containsKey("serviceInstanceId"))
173 customerId = map.get("pnfCorrelationId");
174 serviceType = map.get("serviceType");
175 serId = map.get("serviceInstanceId");
178 if (customerId == null || serviceType == null || serId == null)
180 AAIResourcesClient client = new AAIResourcesClient();
181 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE_METADATA, customerId,
183 client.update(uri, response);