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.openecomp.mso.bpmn.infrastructure.pnf.dmaap;
23 import java.io.IOException;
26 import java.util.Optional;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.Executors;
29 import java.util.concurrent.ScheduledExecutorService;
30 import java.util.concurrent.TimeUnit;
31 import javax.ws.rs.core.UriBuilder;
32 import org.apache.http.HttpResponse;
33 import org.apache.http.client.HttpClient;
34 import org.apache.http.client.methods.HttpGet;
35 import org.apache.http.impl.client.HttpClientBuilder;
36 import org.apache.http.util.EntityUtils;
37 import org.openecomp.mso.bpmn.infrastructure.pnf.implementation.DmaapClient;
38 import org.openecomp.mso.jsonpath.JsonPathUtil;
39 import org.openecomp.mso.logger.MsoLogger;
41 public class PnfEventReadyConsumer implements Runnable, DmaapClient {
43 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
45 private static final String JSON_PATH_CORRELATION_ID = "$.pnfRegistrationFields.correlationId";
46 private HttpClient httpClient;
47 private String dmaapHost;
48 private int dmaapPort;
49 private String dmaapProtocol;
50 private String dmaapUriPathPrefix;
51 private String dmaapTopicName;
52 private String consumerId;
53 private String consumerGroup;
54 private Map<String, Runnable> pnfCorrelationIdToThreadMap;
55 private HttpGet getRequest;
56 private ScheduledExecutorService executor;
57 private int dmaapClientInitialDelayInSeconds;
58 private int dmaapClientDelayInSeconds;
59 private boolean dmaapThreadListenerIsRunning;
61 public PnfEventReadyConsumer() {
62 httpClient = HttpClientBuilder.create().build();
63 pnfCorrelationIdToThreadMap = new ConcurrentHashMap<>();
68 getRequest = new HttpGet(buildURI());
74 HttpResponse response = httpClient.execute(getRequest);
75 getCorrelationIdFromResponse(response).ifPresent(this::informAboutPnfReadyIfCorrelationIdFound);
76 } catch (IOException e) {
77 LOGGER.error("Exception caught during sending rest request to dmaap for listening event topic", e);
82 public void registerForUpdate(String correlationId, Runnable informConsumer) {
83 pnfCorrelationIdToThreadMap.put(correlationId, informConsumer);
84 if (!dmaapThreadListenerIsRunning) {
85 startDmaapThreadListener();
89 private void startDmaapThreadListener() {
90 executor = Executors.newScheduledThreadPool(1);
91 executor.scheduleWithFixedDelay(this, dmaapClientInitialDelayInSeconds,
92 dmaapClientDelayInSeconds, TimeUnit.SECONDS);
93 dmaapThreadListenerIsRunning = true;
96 private void stopDmaapThreadListener() {
97 if (dmaapThreadListenerIsRunning) {
98 executor.shutdownNow();
99 dmaapThreadListenerIsRunning = false;
104 private URI buildURI() {
105 return UriBuilder.fromUri(dmaapUriPathPrefix)
106 .scheme(dmaapProtocol)
108 .port(dmaapPort).path(dmaapTopicName)
109 .path(consumerGroup).path(consumerId).build();
112 private Optional<String> getCorrelationIdFromResponse(HttpResponse response) throws IOException {
113 if (response.getStatusLine().getStatusCode() == 200) {
114 String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
115 if (responseString != null) {
116 return JsonPathUtil.getInstance().locateResult(responseString, JSON_PATH_CORRELATION_ID);
119 return Optional.empty();
123 private void informAboutPnfReadyIfCorrelationIdFound(String correlationId) {
124 pnfCorrelationIdToThreadMap.keySet().stream().filter(key -> key.equals(correlationId)).findAny()
125 .ifPresent(this::informAboutPnfReady);
128 private void informAboutPnfReady(String correlationId) {
129 pnfCorrelationIdToThreadMap.get(correlationId).run();
130 pnfCorrelationIdToThreadMap.remove(correlationId);
132 if (pnfCorrelationIdToThreadMap.isEmpty()) {
133 stopDmaapThreadListener();
137 public void setDmaapHost(String dmaapHost) {
138 this.dmaapHost = dmaapHost;
141 public void setDmaapPort(int dmaapPort) {
142 this.dmaapPort = dmaapPort;
145 public void setDmaapProtocol(String dmaapProtocol) {
146 this.dmaapProtocol = dmaapProtocol;
149 public void setDmaapUriPathPrefix(String dmaapUriPathPrefix) {
150 this.dmaapUriPathPrefix = dmaapUriPathPrefix;
153 public void setDmaapTopicName(String dmaapTopicName) {
154 this.dmaapTopicName = dmaapTopicName;
157 public void setConsumerId(String consumerId) {
158 this.consumerId = consumerId;
161 public void setConsumerGroup(String consumerGroup) {
162 this.consumerGroup = consumerGroup;
165 public void setDmaapClientInitialDelayInSeconds(int dmaapClientInitialDelayInSeconds) {
166 this.dmaapClientInitialDelayInSeconds = dmaapClientInitialDelayInSeconds;
169 public void setDmaapClientDelayInSeconds(int dmaapClientDelayInSeconds) {
170 this.dmaapClientDelayInSeconds = dmaapClientDelayInSeconds;