b189cfd6d4e86423122bdb2e07d33562d73acb83
[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     String topic = endpoint.getEventTopic();
73
74     logger.debug("Checking for event on topic: " + topic);
75
76     int processCount = 0;
77
78     try {
79       Iterable<String> messages = consumer.consumeAndCommit();
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     } catch (Exception e) {
88       logger.error(RouterCoreMsgs.EVENT_PROCESSING_EXCEPTION, e, e.getLocalizedMessage());
89     }
90
91     logger.debug(RouterCoreMsgs.PROCESS_EVENT, topic, Integer.toString(processCount));
92
93     return processCount;
94   }
95   @Override
96   protected void doStop() throws Exception {
97     super.doStop();
98     if (endpoint != null) {
99       endpoint.close();
100     }
101   }
102   @Override
103   protected void doShutdown() throws Exception {
104     super.doShutdown();
105     if (endpoint != null) {
106       endpoint.close();
107     }
108   }
109
110   /**
111    * Class responsible for processing messages pulled off of the event bus.
112    */
113   private class EventProcessor implements Runnable {
114
115     private Exchange message;
116
117     private String topic;
118
119     EventProcessor(Exchange message, String topic) {
120       this.message = message;
121       this.topic = topic;
122     }
123         @Override
124     public void run() {
125       try {
126
127         MdcContext.initialize(UUID.randomUUID().toString(), "DataRouter", "", "Event-Bus", "");
128
129         // Sends the message to the next processor in the defined Camel route
130         getProcessor().process(message);
131
132         Message response = message.getOut();
133         if (response != null) {
134           logger.debug("Routing response: " + response.getBody());
135         }
136
137       } catch (Exception e) {
138         logger.error(RouterCoreMsgs.EVENT_PROCESSING_EXCEPTION,e,e.getLocalizedMessage());
139       } finally {
140         // log exception if an exception occurred and was not handled
141         if (message.getException() != null) {
142           logger.info(RouterCoreMsgs.PROCESS_EVENT, topic, "FAILURE");
143           auditLogger.info(RouterCoreMsgs.PROCESS_EVENT, topic, "FAILURE");
144         } else {
145           logger.info(RouterCoreMsgs.PROCESS_EVENT, topic, "SUCCESS");
146           auditLogger.info(RouterCoreMsgs.PROCESS_EVENT, topic, "SUCCESS");
147         }
148       }
149     }
150   }
151 }