Initial TCA commit into DCAEGEN2
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-plugins / src / main / java / org / openecomp / dcae / apod / analytics / cdap / plugins / streaming / dmaap / DMaaPMRSource.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
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
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
21 package org.openecomp.dcae.apod.analytics.cdap.plugins.streaming.dmaap;
22
23 import co.cask.cdap.api.annotation.Description;
24 import co.cask.cdap.api.annotation.Name;
25 import co.cask.cdap.api.annotation.Plugin;
26 import co.cask.cdap.api.data.format.StructuredRecord;
27 import co.cask.cdap.etl.api.PipelineConfigurer;
28 import co.cask.cdap.etl.api.streaming.StreamingContext;
29 import co.cask.cdap.etl.api.streaming.StreamingSource;
30 import org.apache.spark.storage.StorageLevel;
31 import org.apache.spark.streaming.api.java.JavaDStream;
32 import org.openecomp.dcae.apod.analytics.cdap.common.utils.ValidationUtils;
33 import org.openecomp.dcae.apod.analytics.cdap.plugins.domain.config.dmaap.DMaaPMRSourcePluginConfig;
34 import org.openecomp.dcae.apod.analytics.cdap.plugins.domain.schema.dmaap.DMaaPSourceOutputSchema;
35 import org.openecomp.dcae.apod.analytics.cdap.plugins.validator.DMaaPMRSourcePluginConfigValidator;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * DMaaP MR Source Plugin which polls DMaaP MR topic at frequent intervals
41  * <p>
42  * @author Rajiv Singla . Creation Date: 1/18/2017.
43  */
44 @Plugin(type = StreamingSource.PLUGIN_TYPE)
45 @Name("DMaaPMRSource")
46 @Description("Fetches DMaaP MR Messages at regular intervals")
47 public class DMaaPMRSource extends StreamingSource<StructuredRecord> {
48
49     private static final Logger LOG = LoggerFactory.getLogger(DMaaPMRSource.class);
50     private static final long serialVersionUID = 1L;
51
52     private final DMaaPMRSourcePluginConfig pluginConfig;
53
54     public DMaaPMRSource(final DMaaPMRSourcePluginConfig pluginConfig) {
55         LOG.debug("Creating DMaaP MR Source plugin with plugin Config: {}", pluginConfig);
56         this.pluginConfig = pluginConfig;
57     }
58
59     @Override
60     public void configurePipeline(PipelineConfigurer pipelineConfigurer) {
61         ValidationUtils.validateSettings(pluginConfig, new DMaaPMRSourcePluginConfigValidator());
62         pipelineConfigurer.getStageConfigurer().setOutputSchema(DMaaPSourceOutputSchema.getSchema());
63     }
64
65     @Override
66     public JavaDStream<StructuredRecord> getStream(final StreamingContext streamingContext) throws Exception {
67         return streamingContext.getSparkStreamingContext().receiverStream(
68                 new DMaaPMRReceiver(StorageLevel.MEMORY_ONLY(), pluginConfig, streamingContext.getMetrics()));
69     }
70 }