Remove cambria client dependency
[aai/router-core.git] / src / main / java / org / onap / aai / event / EventBusConsumer.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.event;
22
23 import org.onap.aai.event.api.EventConsumer;
24
25 import org.apache.camel.Exchange;
26 import org.apache.camel.Message;
27 import org.apache.camel.Processor;
28 import org.apache.camel.impl.ScheduledPollConsumer;
29 import org.onap.aai.logging.RouterCoreMsgs;
30 import org.onap.aai.cl.api.Logger;
31 import org.onap.aai.cl.eelf.LoggerFactory;
32 import org.onap.aai.cl.mdc.MdcContext;
33
34 import java.util.UUID;
35 import java.util.concurrent.ScheduledThreadPoolExecutor;
36
37 /**
38  * The consumer component which is used to pull messages off of the event bus and send them to the
39  * next processor in the route chain. This type of consumer is based off of a scheduled poller so
40  * that events are pulled on a regular basis.
41  */
42 public class EventBusConsumer extends ScheduledPollConsumer {
43
44   private Logger logger = LoggerFactory.getInstance().getLogger(EventBusConsumer.class);
45   private Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(EventBusConsumer.class);
46   private final AbstractEventBusEndpoint endpoint;
47
48   private EventConsumer consumer;
49
50   /**
51    * EventBusConsumer Constructor.
52    */
53   public EventBusConsumer(AbstractEventBusEndpoint endpoint, Processor processor, EventConsumer consumer) {
54     super(endpoint, processor);
55     super.setDelay(endpoint.getPollingDelay());
56     this.endpoint = endpoint;
57
58     setScheduledExecutorService(new ScheduledThreadPoolExecutor(endpoint.getPoolSize()));
59
60     this.consumer = consumer;
61   }
62
63   /**
64    * Method which is called by the Camel process on a scheduled basis. This specific implementation
65    * reads messages off of the configured topic and schedules tasks to process them .
66    * 
67    * @return the number of messages that were processed off the event queue
68    */
69   @Override
70   protected int poll() throws Exception {
71
72     logger.debug("Checking for event on topic: " + endpoint.getEventTopic());
73
74     int processCount = 0;
75
76     //Iterable<String> messages = consumer.fetch();
77     Iterable<String> messages = consumer.consumeAndCommit();
78
79     String topic = endpoint.getEventTopic();
80
81     for (String message : messages) {
82       Exchange exchange = endpoint.createExchange();
83       exchange.getIn().setBody(message);
84       getScheduledExecutorService().submit(new EventProcessor(exchange, topic));
85       ++processCount;
86     }
87     return processCount;
88   }
89   @Override
90   protected void doStop() throws Exception {
91     super.doStop();
92     if (endpoint != null) {
93       endpoint.close();
94     }
95   }
96   @Override
97   protected void doShutdown() throws Exception {
98     super.doShutdown();
99     if (endpoint != null) {
100       endpoint.close();
101     }
102   }
103
104   /**
105    * Class responsible for processing messages pulled off of the event bus.
106    */
107   private class EventProcessor implements Runnable {
108
109     private Exchange message;
110
111     private String topic;
112
113     EventProcessor(Exchange message, String topic) {
114       this.message = message;
115       this.topic = topic;
116     }
117         @Override
118     public void run() {
119       try {
120
121         MdcContext.initialize(UUID.randomUUID().toString(), "DataRouter", "", "Event-Bus", "");
122
123         // Sends the message to the next processor in the defined Camel route
124         getProcessor().process(message);
125
126         Message response = message.getOut();
127         if (response != null) {
128           logger.debug("Routing response: " + response.getBody());
129         }
130
131       } catch (Exception e) {
132         logger.error(RouterCoreMsgs.EVENT_PROCESSING_EXCEPTION,e,e.getLocalizedMessage());
133       } finally {
134         // log exception if an exception occurred and was not handled
135         if (message.getException() != null) {
136           logger.info(RouterCoreMsgs.PROCESS_EVENT, topic, "FAILURE");
137           auditLogger.info(RouterCoreMsgs.PROCESS_EVENT, topic, "FAILURE");
138         } else {
139           logger.info(RouterCoreMsgs.PROCESS_EVENT, topic, "SUCCESS");
140           auditLogger.info(RouterCoreMsgs.PROCESS_EVENT, topic, "SUCCESS");
141         }
142       }
143     }
144   }
145 }