2 * ===============================LICENSE_START======================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============================LICENSE_END===========================================
\r
21 package org.openecomp.dcae.apod.analytics.cdap.plugins.streaming.dmaap;
\r
23 import co.cask.cdap.api.data.format.StructuredRecord;
\r
24 import co.cask.cdap.api.metrics.Metrics;
\r
25 import com.google.common.base.Optional;
\r
26 import org.apache.spark.storage.StorageLevel;
\r
27 import org.apache.spark.streaming.receiver.Receiver;
\r
28 import org.openecomp.dcae.apod.analytics.cdap.common.utils.DMaaPMRUtils;
\r
29 import org.openecomp.dcae.apod.analytics.cdap.plugins.domain.config.dmaap.DMaaPMRSourcePluginConfig;
\r
30 import org.openecomp.dcae.apod.analytics.cdap.plugins.utils.CDAPPluginUtils;
\r
31 import org.openecomp.dcae.apod.analytics.cdap.plugins.utils.DMaaPSourceConfigMapper;
\r
32 import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;
\r
33 import org.openecomp.dcae.apod.analytics.dmaap.DMaaPMRFactory;
\r
34 import org.openecomp.dcae.apod.analytics.dmaap.service.subscriber.DMaaPMRSubscriber;
\r
35 import org.slf4j.Logger;
\r
36 import org.slf4j.LoggerFactory;
\r
38 import java.util.List;
\r
39 import java.util.concurrent.TimeUnit;
\r
42 * DMaaP MR Receiver which calls DMaaP MR Topic and stores structured records
\r
44 * @author Rajiv Singla . Creation Date: 1/19/2017.
\r
46 public class DMaaPMRReceiver extends Receiver<StructuredRecord> {
\r
48 private static final Logger LOG = LoggerFactory.getLogger(DMaaPMRReceiver.class);
\r
49 private static final long serialVersionUID = 1L;
\r
51 private final DMaaPMRSourcePluginConfig pluginConfig;
\r
52 private final transient Metrics metrics;
\r
54 public DMaaPMRReceiver(final StorageLevel storageLevel, final DMaaPMRSourcePluginConfig pluginConfig,
\r
55 final Metrics metrics) {
\r
56 super(storageLevel);
\r
57 this.pluginConfig = pluginConfig;
\r
58 this.metrics = metrics;
\r
59 LOG.debug("Created DMaaP MR Receiver instance with plugin Config: {}", pluginConfig);
\r
63 public void onStart() {
\r
65 // create DMaaP MR Subscriber
\r
66 final DMaaPMRSubscriber subscriber =
\r
67 DMaaPMRFactory.create().createSubscriber(DMaaPSourceConfigMapper.map(pluginConfig));
\r
69 // Start a new thread with indefinite loop until receiver is stopped
\r
73 while (!isStopped()) {
\r
74 storeStructuredRecords(subscriber);
\r
76 final Integer pollingInterval = pluginConfig.getPollingInterval();
\r
77 LOG.debug("DMaaP MR Receiver sleeping for polling interval: {}", pollingInterval);
\r
78 TimeUnit.MILLISECONDS.sleep(pollingInterval);
\r
79 } catch (InterruptedException e) {
\r
80 final String errorMessage = String.format(
\r
81 "Interrupted Exception while DMaaP MR Receiver sleeping polling interval: %s", e);
\r
82 throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);
\r
91 public void onStop() {
\r
92 LOG.debug("Stopping DMaaP MR Receiver with plugin config: {}", pluginConfig);
\r
96 * Fetches records from DMaaP MR Subscriber and store them as structured records
\r
98 * @param subscriber DMaaP MR Subscriber Instance
\r
100 public void storeStructuredRecords(final DMaaPMRSubscriber subscriber) {
\r
102 LOG.debug("DMaaP MR Receiver start fetching messages from DMaaP MR Topic");
\r
104 // Fetch messages from DMaaP MR Topic
\r
105 final Optional<List<String>> subscriberMessagesOptional =
\r
106 DMaaPMRUtils.getSubscriberMessages(subscriber, metrics);
\r
109 if (subscriberMessagesOptional.isPresent()) {
\r
110 final List<String> messages = subscriberMessagesOptional.get();
\r
111 for (final String message : messages) {
\r
112 store(CDAPPluginUtils.createDMaaPMRResponseStructuredRecord(message));
\r
114 LOG.debug("Stored DMaaP Subscriber messages as Structured Records. Message count {}", messages.size());
\r