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=========================================================
22 package org.onap.so.bpmn.infrastructure.pnf.dmaap;
24 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;
31 import org.onap.so.client.kafka.KafkaConsumerImpl;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.core.env.Environment;
36 import org.springframework.stereotype.Component;
39 public class PnfEventReadyDmaapClient implements DmaapClient {
40 private static final Logger logger = LoggerFactory.getLogger(PnfEventReadyDmaapClient.class);
41 private Map<String, Runnable> pnfCorrelationIdToThreadMap;
42 private int topicListenerDelayInSeconds;
43 private volatile ScheduledThreadPoolExecutor executor;
44 private volatile boolean dmaapThreadListenerIsRunning;
45 private KafkaConsumerImpl consumerForPnfReady;
46 private KafkaConsumerImpl consumerForPnfUpdate;
47 private String pnfReadyTopic;
48 private String pnfUpdateTopic;
49 private String consumerGroup;
50 private String consumerId;
51 private String consumerIdUpdate;
56 public PnfEventReadyDmaapClient(Environment env) throws IOException {
57 pnfCorrelationIdToThreadMap = new ConcurrentHashMap<>();
58 topicListenerDelayInSeconds = env.getProperty("pnf.dmaap.topicListenerDelayInSeconds", Integer.class);
61 consumerForPnfReady = new KafkaConsumerImpl(env.getProperty("pnf.kafka.kafkaBootstrapServers"));
62 consumerForPnfUpdate = new KafkaConsumerImpl(env.getProperty("pnf.kafka.kafkaBootstrapServers"));
63 } catch (Exception e) {
64 throw new RuntimeException(e);
66 pnfReadyTopic = env.getProperty("pnf.kafka.pnfReadyTopicName");
67 pnfUpdateTopic = env.getProperty("pnf.kafka.pnfUpdateTopicName");
68 consumerGroup = env.getProperty("pnf.kafka.consumerGroup");
69 consumerId = env.getProperty("pnf.kafka.consumerId");
70 consumerIdUpdate = env.getProperty("pnf.kafka.consumerIdUpdate");
75 public synchronized void registerForUpdate(String pnfCorrelationId, Runnable informConsumer) {
76 logger.debug("registering for pnf ready kafka event for pnf correlation id: {}", pnfCorrelationId);
77 pnfCorrelationIdToThreadMap.put(pnfCorrelationId, informConsumer);
78 if (!dmaapThreadListenerIsRunning) {
79 startDmaapThreadListener();
84 public synchronized Runnable unregister(String pnfCorrelationId) {
85 logger.debug("unregistering from pnf ready kafka event for pnf correlation id: {}", pnfCorrelationId);
86 Runnable runnable = pnfCorrelationIdToThreadMap.remove(pnfCorrelationId);
87 if (pnfCorrelationIdToThreadMap.isEmpty()) {
88 consumerForPnfUpdate.close();
89 consumerForPnfReady.close();
90 stopDmaapThreadListener();
95 private synchronized void startDmaapThreadListener() {
96 if (!dmaapThreadListenerIsRunning) {
97 executor = new ScheduledThreadPoolExecutor(1);
98 executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
99 executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
100 executor.scheduleWithFixedDelay(new DmaapTopicListenerThread(), 0, topicListenerDelayInSeconds,
102 dmaapThreadListenerIsRunning = true;
106 private synchronized void stopDmaapThreadListener() {
107 if (dmaapThreadListenerIsRunning) {
109 dmaapThreadListenerIsRunning = false;
114 class DmaapTopicListenerThread implements Runnable {
118 List<String> response;
119 System.out.println(pnfUpdateTopic + " " + consumerGroup);
120 response = consumerForPnfUpdate.get(pnfUpdateTopic, consumerGroup, consumerIdUpdate);
121 if (response.isEmpty()) {
122 response = consumerForPnfReady.get(pnfReadyTopic, consumerGroup, consumerId);
123 getPnfCorrelationIdListFromResponse(response)
124 .forEach(this::informAboutPnfReadyIfPnfCorrelationIdFound);
126 getPnfCorrelationIdListFromResponse(response)
127 .forEach(this::informAboutPnfReadyIfPnfCorrelationIdFound);
129 } catch (IOException e) {
130 logger.error("Exception caught during sending rest request to kafka for listening event topic", e);
134 private List<String> getPnfCorrelationIdListFromResponse(List<String> response) throws IOException {
135 if (response != null) {
136 return JsonUtilForPnfCorrelationId.parseJsonToGelAllPnfCorrelationId(response);
138 return Collections.emptyList();
141 private void informAboutPnfReadyIfPnfCorrelationIdFound(String pnfCorrelationId) {
142 Runnable runnable = unregister(pnfCorrelationId);
143 if (runnable != null) {
144 logger.debug("dmaap listener gets pnf ready event for pnfCorrelationId: {}", pnfCorrelationId);