TCA: Replace any openecomp reference by onap
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / main / java / org / onap / dcae / apod / analytics / cdap / tca / worker / TCADMaaPPublisherWorker.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.onap.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.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;
41
42 import java.util.concurrent.atomic.AtomicBoolean;
43
44 import static java.lang.String.format;
45
46 /**
47  * TCA DMaaP Publisher will monitor alerts table at regular intervals and publish any alerts to DMaaP MR Publishing
48  * Topic
49  * <p>
50  * @author Rajiv Singla . Creation Date: 11/16/2016.
51  */
52 public class TCADMaaPPublisherWorker extends BaseTCADMaaPMRWorker {
53
54     private static final Logger LOG = LoggerFactory.getLogger(TCADMaaPPublisherWorker.class);
55
56     private DMaaPMRPublisher publisher;
57     private Metrics metrics;
58     @Property
59     private final String tcaVESAlertsTableName;
60
61     public TCADMaaPPublisherWorker(final String tcaVESAlertsTableName) {
62         this.tcaVESAlertsTableName = tcaVESAlertsTableName;
63     }
64
65     @Override
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);
71     }
72
73
74     @Override
75     public void initialize(WorkerContext context) throws Exception {
76         super.initialize(context);
77
78         // Parse runtime arguments
79         final TCAAppPreferences tcaAppPreferences = CDAPTCAUtils.getValidatedTCAAppPreferences(context);
80
81         LOG.info("Initializing TCA MR DMaaP Publisher worker with preferences: {}", tcaAppPreferences);
82
83         //  Map TCA App Preferences to DMaaP MR Publisher Config
84         final DMaaPMRPublisherConfig publisherConfig = AppPreferencesToPublisherConfigMapper.map(tcaAppPreferences);
85
86         LOG.info("TCA DMaaP MR Publisher worker will be polling TCA Alerts Table Name: {}", tcaVESAlertsTableName);
87
88         // Create an instance of DMaaP MR Publisher
89         LOG.debug("Creating an instance of DMaaP Publisher");
90         publisher = DMaaPMRFactory.create().createPublisher(publisherConfig);
91
92         // initialize a new Quartz scheduler
93         initializeScheduler(tcaAppPreferences, new StdSchedulerFactory());
94
95         // initialize scheduler state
96         isSchedulerShutdown = new AtomicBoolean(true);
97     }
98
99
100     /**
101      * Stop DMaaP Publisher
102      */
103     @Override
104     public void stop() {
105         // Close Publisher - which will flush any batch messages if present in batch queue
106         if (publisher != null) {
107             try {
108                 publisher.close();
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);
112             }
113         }
114         // Shut down scheduler
115         super.stop();
116     }
117
118
119     /**
120      * Initializes a scheduler instance for DMaaP MR Publisher Job
121      *
122      * @throws SchedulerException SchedulerException
123      */
124     private void initializeScheduler(final TCAAppPreferences tcaAnalyticsAppConfig,
125                                      final StdSchedulerFactory stdSchedulerFactory) throws SchedulerException {
126
127         // Get Publisher polling interval
128         final Integer publisherPollingInterval = tcaAnalyticsAppConfig.getPublisherPollingInterval();
129
130         // Publisher Quartz Properties file
131         final String quartzPublisherPropertiesFileName = AnalyticsConstants.TCA_QUARTZ_PUBLISHER_PROPERTIES_FILE_NAME;
132
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);
139
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);
145     }
146 }