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