2 * ===============================LICENSE_START======================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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===========================================
21 package org.onap.dcae.apod.analytics.cdap.plugins.streaming.dmaap;
23 import co.cask.cdap.api.data.format.StructuredRecord;
24 import co.cask.cdap.api.metrics.Metrics;
26 import com.google.common.base.Optional;
27 import org.apache.spark.storage.StorageLevel;
28 import org.apache.spark.streaming.receiver.Receiver;
29 import org.onap.dcae.apod.analytics.cdap.common.utils.DMaaPMRUtils;
30 import org.onap.dcae.apod.analytics.cdap.plugins.domain.config.dmaap.DMaaPMRSourcePluginConfig;
31 import org.onap.dcae.apod.analytics.cdap.plugins.utils.CDAPPluginUtils;
32 import org.onap.dcae.apod.analytics.cdap.plugins.utils.DMaaPSourceConfigMapper;
33 import org.onap.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;
34 import org.onap.dcae.apod.analytics.dmaap.DMaaPMRFactory;
35 import org.onap.dcae.apod.analytics.dmaap.service.subscriber.DMaaPMRSubscriber;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import java.util.List;
40 import java.util.concurrent.TimeUnit;
43 * DMaaP MR Receiver which calls DMaaP MR Topic and stores structured records
45 * @author Rajiv Singla . Creation Date: 1/19/2017.
47 public class DMaaPMRReceiver extends Receiver<StructuredRecord> {
49 private static final Logger LOG = LoggerFactory.getLogger(DMaaPMRReceiver.class);
50 private static final long serialVersionUID = 1L;
52 private final DMaaPMRSourcePluginConfig pluginConfig;
53 private final transient Metrics metrics;
55 public DMaaPMRReceiver(final StorageLevel storageLevel, final DMaaPMRSourcePluginConfig pluginConfig,
56 final Metrics metrics) {
58 this.pluginConfig = pluginConfig;
59 this.metrics = metrics;
60 LOG.debug("Created DMaaP MR Receiver instance with plugin Config: {}", pluginConfig);
64 public void onStart() {
66 // create DMaaP MR Subscriber
67 try(final DMaaPMRSubscriber subscriber =
68 DMaaPMRFactory.create().createSubscriber(DMaaPSourceConfigMapper.map(pluginConfig))){
70 // Start a new thread with indefinite loop until receiver is stopped
74 while (!isStopped()) {
75 storeStructuredRecords(subscriber);
77 final Integer pollingInterval = pluginConfig.getPollingInterval();
78 LOG.debug("DMaaP MR Receiver sleeping for polling interval: {}", pollingInterval);
79 TimeUnit.MILLISECONDS.sleep(pollingInterval);
80 } catch (InterruptedException e) {
81 final String errorMessage = String.format(
82 "Interrupted Exception while DMaaP MR Receiver sleeping polling interval: %s", e);
83 Thread.currentThread().interrupt();
84 throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);
89 } catch (Exception e) {
90 LOG.error("Exception in DMaaPMRReceiver onStart",e);
95 public void onStop() {
96 LOG.debug("Stopping DMaaP MR Receiver with plugin config: {}", pluginConfig);
100 * Fetches records from DMaaP MR Subscriber and store them as structured records
102 * @param subscriber DMaaP MR Subscriber Instance
104 public void storeStructuredRecords(final DMaaPMRSubscriber subscriber) {
106 LOG.debug("DMaaP MR Receiver start fetching messages from DMaaP MR Topic");
108 // Fetch messages from DMaaP MR Topic
109 final Optional<List<String>> subscriberMessagesOptional =
110 DMaaPMRUtils.getSubscriberMessages(subscriber, metrics);
113 if (subscriberMessagesOptional.isPresent()) {
114 final List<String> messages = subscriberMessagesOptional.get();
115 for (final String message : messages) {
116 store(CDAPPluginUtils.createDMaaPMRResponseStructuredRecord(message));
118 LOG.debug("Stored DMaaP Subscriber messages as Structured Records. Message count {}", messages.size());