2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright 2018 TechMahindra
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=========================================================
20 package org.onap.universalvesadapter.service;
22 import java.util.ArrayList;
23 import java.util.LinkedList;
24 import java.util.List;
26 import org.onap.universalvesadapter.adapter.UniversalEventAdapter;
27 import org.onap.universalvesadapter.dmaap.Creator;
28 import org.onap.universalvesadapter.dmaap.MRPublisher.DMaaPMRPublisher;
29 import org.onap.universalvesadapter.dmaap.MRSubcriber.DMaaPMRSubscriber;
30 import org.onap.universalvesadapter.exception.DMaapException;
31 import org.onap.universalvesadapter.exception.VesException;
32 import org.onap.universalvesadapter.utils.DmaapConfig;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.beans.factory.annotation.Value;
37 import org.springframework.stereotype.Component;
40 public class DMaapService {
42 private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
43 private static List<String> list = new LinkedList<String>();
45 private UniversalEventAdapter eventAdapter;
47 private DmaapConfig dmaapConfig;
50 * It fetches events from DMaap in JSON, transforms JSON to VES format and
51 * publishes it to outgoing DMaap MR Topic
53 * @param DMaaPMRSubscriber,DMaaPMRPublisher
56 public void fetchAndPublishInDMaaP(DMaaPMRSubscriber dMaaPMRSubscriber, DMaaPMRPublisher publisher, Creator creater)
57 throws InterruptedException {
58 LOGGER.info("fetch and publish from and to Dmaap started");
60 int pollingInternalInt=dmaapConfig.getPollingInterval();
61 LOGGER.info("The Polling Interval in Milli Second is :" +pollingInternalInt);
64 for (String incomingJsonString : dMaaPMRSubscriber.fetchMessages().getFetchedMessages()) {
65 list.add(incomingJsonString);
70 Thread.sleep(pollingInternalInt);
72 LOGGER.debug("number of messages to be converted :{}", list.size());
74 if (!list.isEmpty()) {
75 String val = ((LinkedList<String>) list).removeFirst();
76 List<String> messages = new ArrayList<>();
77 String vesEvent = processReceivedJson(val);
78 if (vesEvent!=null && (!(vesEvent.isEmpty() || vesEvent.equals("")))) {
79 messages.add(vesEvent);
80 publisher.publish(messages);
81 LOGGER.info("Message successfully published to DMaaP Topic");
91 * It finds mapping file for received json, transforms json to VES format
93 * @param incomingJsonString
96 private String processReceivedJson(String incomingJsonString) {
97 String outgoingJsonString = null;
98 if (!"".equals(incomingJsonString)) {
102 * For Future events String eventType =
103 * adapterService.identifyEventTypeFromIncomingJson(incomingJsonString);
105 * LOGGER.debug("Event identified as " + eventType);
108 outgoingJsonString = eventAdapter.transform(incomingJsonString, "snmp");
110 } catch (VesException exception) {
111 LOGGER.error("Received exception : " + exception.getMessage(), exception);
112 LOGGER.error("APPLICATION WILL BE SHUTDOWN UNTIL ABOVE ISSUE IS RESOLVED.");
113 } catch (DMaapException e) {
114 LOGGER.error("Received exception : ", e.getMessage());
117 return outgoingJsonString;