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.AppPreferencesToPublisherConfigMapper;
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.common.exception.DCAEAnalyticsRuntimeException;
32 import org.onap.dcae.apod.analytics.dmaap.DMaaPMRFactory;
33 import org.onap.dcae.apod.analytics.dmaap.domain.config.DMaaPMRPublisherConfig;
34 import org.onap.dcae.apod.analytics.dmaap.service.publisher.DMaaPMRPublisher;
35 import org.onap.dcae.apod.analytics.tca.utils.TCAUtils;
36 import org.quartz.JobDataMap;
37 import org.quartz.SchedulerException;
38 import org.quartz.impl.StdSchedulerFactory;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import java.util.concurrent.atomic.AtomicBoolean;
44 import static java.lang.String.format;
47 * TCA DMaaP Publisher will monitor alerts table at regular intervals and publish any alerts to DMaaP MR Publishing
50 * @author Rajiv Singla . Creation Date: 11/16/2016.
52 public class TCADMaaPPublisherWorker extends BaseTCADMaaPMRWorker {
54 private static final Logger LOG = LoggerFactory.getLogger(TCADMaaPPublisherWorker.class);
56 private DMaaPMRPublisher publisher;
57 private Metrics metrics;
59 private final String tcaVESAlertsTableName;
61 public TCADMaaPPublisherWorker(final String tcaVESAlertsTableName) {
62 this.tcaVESAlertsTableName = tcaVESAlertsTableName;
66 public void configure() {
67 setName(CDAPComponentsConstants.TCA_FIXED_DMAAP_PUBLISHER_WORKER);
68 setDescription(CDAPComponentsConstants.TCA_FIXED_DMAAP_PUBLISHER_DESCRIPTION_WORKER);
69 LOG.debug("Configuring TCA MR DMaaP Publisher worker with name: {}",
70 CDAPComponentsConstants.TCA_FIXED_DMAAP_PUBLISHER_WORKER);
75 public void initialize(WorkerContext context) throws Exception {
76 super.initialize(context);
78 // Parse runtime arguments
79 final TCAAppPreferences tcaAppPreferences = CDAPTCAUtils.getValidatedTCAAppPreferences(context);
81 LOG.info("Initializing TCA MR DMaaP Publisher worker with preferences: {}", tcaAppPreferences);
83 // Map TCA App Preferences to DMaaP MR Publisher Config
84 final DMaaPMRPublisherConfig publisherConfig = AppPreferencesToPublisherConfigMapper.map(tcaAppPreferences);
86 LOG.info("TCA DMaaP MR Publisher worker will be polling TCA Alerts Table Name: {}", tcaVESAlertsTableName);
88 // Create an instance of DMaaP MR Publisher
89 LOG.debug("Creating an instance of DMaaP Publisher");
90 publisher = DMaaPMRFactory.create().createPublisher(publisherConfig);
92 // initialize a new Quartz scheduler
93 initializeScheduler(tcaAppPreferences, new StdSchedulerFactory());
95 // initialize scheduler state
96 isSchedulerShutdown = new AtomicBoolean(true);
101 * Stop DMaaP Publisher
105 // Close Publisher - which will flush any batch messages if present in batch queue
106 if (publisher != null) {
109 } catch (Exception e) {
110 final String errorMessage = format("Error while shutting down DMaaP MR Publisher: %s", e);
111 throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);
114 // Shut down scheduler
120 * Initializes a scheduler instance for DMaaP MR Publisher Job
122 * @throws SchedulerException SchedulerException
124 private void initializeScheduler(final TCAAppPreferences tcaAnalyticsAppConfig,
125 final StdSchedulerFactory stdSchedulerFactory) throws SchedulerException {
127 // Get Publisher polling interval
128 final Integer publisherPollingInterval = tcaAnalyticsAppConfig.getPublisherPollingInterval();
130 // Publisher Quartz Properties file
131 final String quartzPublisherPropertiesFileName = AnalyticsConstants.TCA_QUARTZ_PUBLISHER_PROPERTIES_FILE_NAME;
133 // Create a new JobDataMap containing information required by TCA DMaaP Publisher Job
134 final JobDataMap jobDataMap = new JobDataMap();
135 jobDataMap.put(AnalyticsConstants.CDAP_ALERTS_TABLE_VARIABLE_NAME, tcaVESAlertsTableName);
136 jobDataMap.put(AnalyticsConstants.WORKER_CONTEXT_VARIABLE_NAME, getContext());
137 jobDataMap.put(AnalyticsConstants.DMAAP_PUBLISHER_VARIABLE_NAME, publisher);
138 jobDataMap.put(AnalyticsConstants.DMAAP_METRICS_VARIABLE_NAME, metrics);
140 // Create new publisher scheduler
141 scheduler = TCAUtils.createQuartzScheduler(publisherPollingInterval, stdSchedulerFactory,
142 quartzPublisherPropertiesFileName, jobDataMap, TCADMaaPMRPublisherJob.class,
143 AnalyticsConstants.TCA_DMAAP_PUBLISHER_QUARTZ_JOB_NAME,
144 AnalyticsConstants.TCA_DMAAP_PUBLISHER_QUARTZ_TRIGGER_NAME);