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.tca.worker;
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.onap.dcae.apod.analytics.cdap.common.CDAPComponentsConstants;
27 import org.onap.dcae.apod.analytics.cdap.tca.settings.TCAAppPreferences;
28 import org.onap.dcae.apod.analytics.cdap.tca.utils.AppPreferencesToSubscriberConfigMapper;
29 import org.onap.dcae.apod.analytics.cdap.tca.utils.CDAPTCAUtils;
30 import org.onap.dcae.apod.analytics.common.AnalyticsConstants;
31 import org.onap.dcae.apod.analytics.dmaap.DMaaPMRFactory;
32 import org.onap.dcae.apod.analytics.dmaap.domain.config.DMaaPMRSubscriberConfig;
33 import org.onap.dcae.apod.analytics.dmaap.service.subscriber.DMaaPMRSubscriber;
34 import org.onap.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;
41 import java.util.concurrent.atomic.AtomicBoolean;
44 * TCA DMaaP Subscriber will read messages and post them to cdap stream at regular intervals
46 * @author Rajiv Singla . Creation Date: 10/14/2016.
48 public class TCADMaaPSubscriberWorker extends BaseTCADMaaPMRWorker {
50 private static final Logger LOG = LoggerFactory.getLogger(TCADMaaPSubscriberWorker.class);
52 private DMaaPMRSubscriber subscriber;
53 private Metrics metrics;
55 private final String tcaSubscriberOutputStreamName;
57 public TCADMaaPSubscriberWorker(final String tcaSubscriberOutputStreamName) {
58 this.tcaSubscriberOutputStreamName = tcaSubscriberOutputStreamName;
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);
71 public void initialize(WorkerContext context) throws Exception {
72 super.initialize(context);
74 // Parse runtime arguments
75 final TCAAppPreferences tcaAppPreferences = CDAPTCAUtils.getValidatedTCAAppPreferences(context);
77 LOG.info("Initializing TCA MR DMaaP Subscriber worker with preferences: {}", tcaAppPreferences);
79 // Map TCA App Preferences to DMaaP MR Subscriber Config
80 final DMaaPMRSubscriberConfig subscriberConfig = AppPreferencesToSubscriberConfigMapper.map(tcaAppPreferences);
82 LOG.info("TCA DMaaP MR Subscriber worker will be writing to CDAP Stream: {}", tcaSubscriberOutputStreamName);
84 // Create an instance of DMaaP MR Subscriber
85 LOG.debug("Creating an instance of DMaaP Subscriber");
86 subscriber = DMaaPMRFactory.create().createSubscriber(subscriberConfig);
88 // initialize a new Quartz scheduler
89 initializeScheduler(tcaAppPreferences, new StdSchedulerFactory());
91 // initialize scheduler state
92 isSchedulerShutdown = new AtomicBoolean(true);
96 * Initializes a scheduler instance for DMaaP MR Subscriber Job
98 * @throws SchedulerException SchedulerException
100 private void initializeScheduler(final TCAAppPreferences tcaAppPreferences,
101 final StdSchedulerFactory stdSchedulerFactory) throws SchedulerException {
103 // Get Subscriber polling interval
104 final Integer subscriberPollingInterval = tcaAppPreferences.getSubscriberPollingInterval();
106 // Subscriber Quartz Properties file
107 final String quartzSubscriberPropertiesFileName = AnalyticsConstants.TCA_QUARTZ_SUBSCRIBER_PROPERTIES_FILE_NAME;
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);
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);