Fix sonar issues in dcaegen2/services/mapper
[dcaegen2/services/mapper.git] / UniversalVesAdapter / src / main / java / org / onap / universalvesadapter / service / DMaapService.java
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : DCAE
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
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
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=========================================================
19 */
20 package org.onap.universalvesadapter.service;
21
22 import java.util.ArrayList;
23 import java.util.LinkedList;
24 import java.util.List;
25
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;
38
39 @Component
40 public class DMaapService {
41
42         private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
43         private static List<String> list = new LinkedList<String>();
44         @Autowired
45         private UniversalEventAdapter eventAdapter;
46         @Autowired
47         private DmaapConfig dmaapConfig;
48
49         /**
50          * It fetches events from DMaap in JSON, transforms JSON to VES format and
51          * publishes it to outgoing DMaap MR Topic
52          * 
53          * @param DMaaPMRSubscriber,DMaaPMRPublisher
54          * @return
55          */
56         public void fetchAndPublishInDMaaP(DMaaPMRSubscriber dMaaPMRSubscriber, DMaaPMRPublisher publisher, Creator creater)
57                         throws InterruptedException {
58                 LOGGER.info("fetch and publish from and to Dmaap started");
59
60                 int pollingInternalInt=dmaapConfig.getPollingInterval();
61                 LOGGER.info("The Polling Interval in Milli Second is :" +pollingInternalInt);
62                 while (true) {
63                         synchronized (this) {
64                                 for (String incomingJsonString : dMaaPMRSubscriber.fetchMessages().getFetchedMessages()) {
65                                         list.add(incomingJsonString);
66
67                                 }
68
69                                 if (list.isEmpty()) {
70                                         Thread.sleep(pollingInternalInt); 
71                                 }
72                                 LOGGER.debug("number of messages to be converted :{}", list.size());
73
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");
82                                         }
83
84                                 }
85
86                         }
87                 }
88         }
89
90         /**
91          * It finds mapping file for received json, transforms json to VES format
92          * 
93          * @param incomingJsonString
94          * @return
95          */
96         private String processReceivedJson(String incomingJsonString) {
97                 String outgoingJsonString = null;
98                 if (!"".equals(incomingJsonString)) {
99                 
100                         try {
101                                 /*
102                                  * For Future events  String eventType =
103                                  * adapterService.identifyEventTypeFromIncomingJson(incomingJsonString);
104                                  * 
105                                  * LOGGER.debug("Event identified as " + eventType);
106                                  */
107
108                                 outgoingJsonString = eventAdapter.transform(incomingJsonString, "snmp");
109
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());
115                         }
116                 }
117                 return outgoingJsonString;
118         }
119
120 }