2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.infrastructure.pnf.dmaap;
23 import java.io.IOException;
25 import java.util.Collections;
26 import java.util.List;
28 import java.util.concurrent.ConcurrentHashMap;
29 import java.util.concurrent.ScheduledThreadPoolExecutor;
30 import java.util.concurrent.TimeUnit;
32 import javax.ws.rs.core.UriBuilder;
34 import org.apache.http.HttpResponse;
35 import org.apache.http.client.HttpClient;
36 import org.apache.http.client.methods.HttpGet;
37 import org.apache.http.impl.client.HttpClientBuilder;
38 import org.apache.http.util.EntityUtils;
39 import org.onap.so.logger.MsoLogger;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.context.annotation.Scope;
42 import org.springframework.core.env.Environment;
43 import org.springframework.stereotype.Component;
47 public class PnfEventReadyDmaapClient implements DmaapClient {
49 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA, PnfEventReadyDmaapClient.class);
51 private final Environment env;
52 private HttpClient httpClient;
53 private String dmaapHost;
54 private int dmaapPort;
55 private String dmaapProtocol;
56 private String dmaapUriPathPrefix;
57 private String dmaapTopicName;
58 private String consumerId;
59 private String consumerGroup;
60 private Map<String, Runnable> pnfCorrelationIdToThreadMap;
61 private HttpGet getRequest;
62 private int dmaapClientDelayInSeconds;
63 private volatile ScheduledThreadPoolExecutor executor;
64 private volatile boolean dmaapThreadListenerIsRunning;
67 public PnfEventReadyDmaapClient(Environment env) {
72 httpClient = HttpClientBuilder.create().build();
73 pnfCorrelationIdToThreadMap = new ConcurrentHashMap<>();
74 dmaapHost = env.getProperty("pnf.dmaap.host");
75 dmaapPort = env.getProperty("pnf.dmaap.port", Integer.class);
77 getRequest = new HttpGet(buildURI());
81 public synchronized void registerForUpdate(String correlationId, Runnable informConsumer) {
82 LOGGER.debug("registering for pnf ready dmaap event for correlation id: " + correlationId);
83 pnfCorrelationIdToThreadMap.put(correlationId, informConsumer);
84 if (!dmaapThreadListenerIsRunning) {
85 startDmaapThreadListener();
90 public synchronized Runnable unregister(String correlationId) {
91 LOGGER.debug("unregistering from pnf ready dmaap event for correlation id: " + correlationId);
92 Runnable runnable = pnfCorrelationIdToThreadMap.remove(correlationId);
93 if (pnfCorrelationIdToThreadMap.isEmpty()) {
94 stopDmaapThreadListener();
99 private synchronized void startDmaapThreadListener() {
100 if (!dmaapThreadListenerIsRunning) {
101 executor = new ScheduledThreadPoolExecutor(1);
102 executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
103 executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
104 executor.scheduleWithFixedDelay(new DmaapTopicListenerThread(), 0,
105 dmaapClientDelayInSeconds, TimeUnit.SECONDS);
106 dmaapThreadListenerIsRunning = true;
110 private synchronized void stopDmaapThreadListener() {
111 if (dmaapThreadListenerIsRunning) {
113 dmaapThreadListenerIsRunning = false;
118 private URI buildURI() {
119 return UriBuilder.fromUri(dmaapUriPathPrefix)
120 .scheme(dmaapProtocol)
122 .port(dmaapPort).path(dmaapTopicName)
123 .path(consumerGroup).path(consumerId).build();
126 public void setDmaapProtocol(String dmaapProtocol) {
127 this.dmaapProtocol = dmaapProtocol;
130 public void setDmaapUriPathPrefix(String dmaapUriPathPrefix) {
131 this.dmaapUriPathPrefix = dmaapUriPathPrefix;
134 public void setDmaapTopicName(String dmaapTopicName) {
135 this.dmaapTopicName = dmaapTopicName;
138 public void setConsumerId(String consumerId) {
139 this.consumerId = consumerId;
142 public void setConsumerGroup(String consumerGroup) {
143 this.consumerGroup = consumerGroup;
146 public void setDmaapClientDelayInSeconds(int dmaapClientDelayInSeconds) {
147 this.dmaapClientDelayInSeconds = dmaapClientDelayInSeconds;
150 class DmaapTopicListenerThread implements Runnable {
155 HttpResponse response = httpClient.execute(getRequest);
156 getCorrelationIdListFromResponse(response).forEach(this::informAboutPnfReadyIfCorrelationIdFound);
157 } catch (IOException e) {
158 LOGGER.error("Exception caught during sending rest request to dmaap for listening event topic", e);
162 private List<String> getCorrelationIdListFromResponse(HttpResponse response) throws IOException {
163 if (response.getStatusLine().getStatusCode() == 200) {
164 String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
165 if (responseString != null) {
166 return JsonUtilForCorrelationId.parseJsonToGelAllCorrelationId(responseString);
169 return Collections.emptyList();
172 private void informAboutPnfReadyIfCorrelationIdFound(String correlationId) {
173 Runnable runnable = unregister(correlationId);
174 if (runnable != null) {
175 LOGGER.debug("pnf ready event got from dmaap for correlationId: " + correlationId);