900d62e516d44b55a85d21bc75453a2920d5568a
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / main / java / org / openecomp / dcae / apod / analytics / cdap / tca / worker / TCADMaaPSubscriberWorker.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.tca.worker;
22
23 import co.cask.cdap.api.annotation.Property;
24 import co.cask.cdap.api.metrics.Metrics;
25 import co.cask.cdap.api.worker.WorkerContext;
26 import org.openecomp.dcae.apod.analytics.cdap.common.CDAPComponentsConstants;
27 import org.openecomp.dcae.apod.analytics.cdap.tca.settings.TCAAppPreferences;
28 import org.openecomp.dcae.apod.analytics.cdap.tca.utils.AppPreferencesToSubscriberConfigMapper;
29 import org.openecomp.dcae.apod.analytics.cdap.tca.utils.CDAPTCAUtils;
30 import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants;
31 import org.openecomp.dcae.apod.analytics.dmaap.DMaaPMRFactory;
32 import org.openecomp.dcae.apod.analytics.dmaap.domain.config.DMaaPMRSubscriberConfig;
33 import org.openecomp.dcae.apod.analytics.dmaap.service.subscriber.DMaaPMRSubscriber;
34 import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils;
35 import org.quartz.JobDataMap;
36 import org.quartz.SchedulerException;
37 import org.quartz.impl.StdSchedulerFactory;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import java.util.concurrent.atomic.AtomicBoolean;
42
43 /**
44  * TCA DMaaP Subscriber will read messages and post them to cdap stream at regular intervals
45  * <p>
46  * @author Rajiv Singla . Creation Date: 10/14/2016.
47  */
48 public class TCADMaaPSubscriberWorker extends BaseTCADMaaPMRWorker {
49
50     private static final Logger LOG = LoggerFactory.getLogger(TCADMaaPSubscriberWorker.class);
51
52     private DMaaPMRSubscriber subscriber;
53     private Metrics metrics;
54     @Property
55     private final String tcaSubscriberOutputStreamName;
56
57     public TCADMaaPSubscriberWorker(final String tcaSubscriberOutputStreamName) {
58         this.tcaSubscriberOutputStreamName = tcaSubscriberOutputStreamName;
59     }
60
61
62     @Override
63     public void configure() {
64         setName(CDAPComponentsConstants.TCA_FIXED_DMAAP_SUBSCRIBER_WORKER);
65         setDescription(CDAPComponentsConstants.TCA_FIXED_DMAAP_SUBSCRIBER_DESCRIPTION_WORKER);
66         LOG.debug("Configuring TCA MR DMaaP Subscriber worker with name: {}",
67                 CDAPComponentsConstants.TCA_FIXED_DMAAP_SUBSCRIBER_WORKER);
68     }
69
70     @Override
71     public void initialize(WorkerContext context) throws Exception {
72         super.initialize(context);
73
74         // Parse runtime arguments
75         final TCAAppPreferences tcaAppPreferences = CDAPTCAUtils.getValidatedTCAAppPreferences(context);
76
77         LOG.info("Initializing TCA MR DMaaP Subscriber worker with preferences: {}", tcaAppPreferences);
78
79         // Map TCA App Preferences to DMaaP MR Subscriber Config
80         final DMaaPMRSubscriberConfig subscriberConfig = AppPreferencesToSubscriberConfigMapper.map(tcaAppPreferences);
81
82         LOG.info("TCA DMaaP MR Subscriber worker will be writing to CDAP Stream: {}", tcaSubscriberOutputStreamName);
83
84         // Create an instance of DMaaP MR Subscriber
85         LOG.debug("Creating an instance of DMaaP Subscriber");
86         subscriber = DMaaPMRFactory.create().createSubscriber(subscriberConfig);
87
88         // initialize a new Quartz scheduler
89         initializeScheduler(tcaAppPreferences, new StdSchedulerFactory());
90
91         // initialize scheduler state
92         isSchedulerShutdown = new AtomicBoolean(true);
93     }
94
95     /**
96      * Initializes a scheduler instance for DMaaP MR Subscriber Job
97      *
98      * @throws SchedulerException SchedulerException
99      */
100     private void initializeScheduler(final TCAAppPreferences tcaAppPreferences,
101                                      final StdSchedulerFactory stdSchedulerFactory) throws SchedulerException {
102
103         // Get Subscriber polling interval
104         final Integer subscriberPollingInterval = tcaAppPreferences.getSubscriberPollingInterval();
105
106         // Subscriber Quartz Properties file
107         final String quartzSubscriberPropertiesFileName = AnalyticsConstants.TCA_QUARTZ_SUBSCRIBER_PROPERTIES_FILE_NAME;
108
109         // Create a new JobDataMap containing information required by TCA DMaaP Subscriber Job
110         final JobDataMap jobDataMap = new JobDataMap();
111         jobDataMap.put(AnalyticsConstants.CDAP_STREAM_VARIABLE_NAME, tcaSubscriberOutputStreamName);
112         jobDataMap.put(AnalyticsConstants.WORKER_CONTEXT_VARIABLE_NAME, getContext());
113         jobDataMap.put(AnalyticsConstants.DMAAP_SUBSCRIBER_VARIABLE_NAME, subscriber);
114         jobDataMap.put(AnalyticsConstants.DMAAP_METRICS_VARIABLE_NAME, metrics);
115
116         // Create new publisher scheduler
117         scheduler = TCAUtils.createQuartzScheduler(subscriberPollingInterval, stdSchedulerFactory,
118                 quartzSubscriberPropertiesFileName, jobDataMap, TCADMaaPMRSubscriberJob.class,
119                 AnalyticsConstants.TCA_DMAAP_SUBSCRIBER_QUARTZ_JOB_NAME,
120                 AnalyticsConstants.TCA_DMAAP_SUBSCRIBER_QUARTZ_TRIGGER_NAME);
121     }
122
123
124 }