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 / MockDMaaPMRSource.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.exception.CDAPSettingsException;
33 import org.openecomp.dcae.apod.analytics.cdap.plugins.domain.config.dmaap.DMaaPMRSourcePluginConfig;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * A mock implementation of DMaaP MR Receiver which sends mock ves messages
39  * <p>
40  * @author Rajiv Singla . Creation Date: 2/15/2017.
41  */
42 @Plugin(type = StreamingSource.PLUGIN_TYPE)
43 @Name("MockDMaaPMRSource")
44 @Description("Fetches DMaaP MR Messages at regular intervals")
45 public class MockDMaaPMRSource extends StreamingSource<StructuredRecord> {
46
47     private static final Logger LOG = LoggerFactory.getLogger(MockDMaaPMRSource.class);
48     private static final long serialVersionUID = 1L;
49
50     private final DMaaPMRSourcePluginConfig pluginConfig;
51
52     public MockDMaaPMRSource(final DMaaPMRSourcePluginConfig pluginConfig) {
53         LOG.debug("Creating DMaaP MR Source plugin with plugin Config: {}", pluginConfig);
54         this.pluginConfig = pluginConfig;
55     }
56
57     @Override
58     public void configurePipeline(PipelineConfigurer pipelineConfigurer) {
59         final Integer pollingInterval = pluginConfig.getPollingInterval();
60         if (pollingInterval == null) {
61             final String errorMessage = "Polling Interval field must be present";
62             throw new CDAPSettingsException(errorMessage, LOG, new IllegalArgumentException(errorMessage));
63         } else {
64             LOG.info("Mock Message will be send every ms: {}", pollingInterval);
65         }
66     }
67
68     @Override
69     public JavaDStream<StructuredRecord> getStream(final StreamingContext streamingContext) throws Exception {
70         return streamingContext.getSparkStreamingContext().receiverStream(
71                 new MockDMaaPMRReceiver(StorageLevel.MEMORY_ONLY(), pluginConfig));
72     }
73 }