Add support for ABATED alerts within CDAP TCA
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-common / src / main / java / org / openecomp / dcae / apod / analytics / cdap / common / persistance / tca / TCAVESAlertsPersister.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.common.persistance.tca;
22
23 import co.cask.cdap.api.data.schema.Schema;
24 import co.cask.cdap.api.data.schema.UnsupportedTypeException;
25 import co.cask.cdap.api.dataset.DatasetProperties;
26 import co.cask.cdap.api.dataset.lib.IndexedTable;
27 import co.cask.cdap.api.dataset.lib.ObjectMappedTable;
28 import co.cask.cdap.api.dataset.lib.ObjectMappedTableProperties;
29 import org.apache.commons.lang3.StringEscapeUtils;
30 import org.openecomp.dcae.apod.analytics.cdap.common.CDAPComponentsConstants;
31 import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import java.util.Date;
36
37 import static org.openecomp.dcae.apod.analytics.common.utils.PersistenceUtils.TABLE_ROW_KEY_COLUMN_NAME;
38
39 /**
40  *
41  * @author Rajiv Singla . Creation Date: 11/16/2016.
42  */
43 public abstract class TCAVESAlertsPersister {
44
45     private static final Logger LOG = LoggerFactory.getLogger(TCAVESAlertsPersister.class);
46
47     private TCAVESAlertsPersister() {
48
49     }
50
51     /**
52      * Persists Alert Message to Alerts Table
53      *
54      * @param alertMessage alert Message
55      * @param tcaVESAlertTable alert Table Name
56      */
57     public static void persist(final String alertMessage, final ObjectMappedTable<TCAVESAlertEntity> tcaVESAlertTable) {
58         final Date currentDate = new Date();
59         final TCAVESAlertEntity alertEntity = new TCAVESAlertEntity(currentDate.getTime(),
60                 StringEscapeUtils.unescapeJson(alertMessage));
61         // row key is same as current timestamp
62         final String rowKey = createRowKey(currentDate);
63         tcaVESAlertTable.write(rowKey, alertEntity);
64
65         LOG.debug("Finished persisting VES Alert message ID: {} in VES Alerts table.", rowKey);
66     }
67
68
69     /**
70      * Creates {@link DatasetProperties} for Alerts Table
71      *
72      * @param timeToLiveSeconds alerts table Time to Live
73      * @return Alerts table properties
74      */
75     public static DatasetProperties getDatasetProperties(final int timeToLiveSeconds) {
76         try {
77             return ObjectMappedTableProperties.builder()
78                     .setType(TCAVESAlertEntity.class)
79                     .setRowKeyExploreName(TABLE_ROW_KEY_COLUMN_NAME)
80                     .setRowKeyExploreType(Schema.Type.STRING)
81                     .add(IndexedTable.PROPERTY_TTL, timeToLiveSeconds)
82                     .setDescription(CDAPComponentsConstants.TCA_FIXED_VES_ALERTS_DESCRIPTION_TABLE)
83                     .build();
84         } catch (UnsupportedTypeException e) {
85             final String errorMessage = "Unable to convert TCAVESAlertEntity class to Schema";
86             throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);
87         }
88
89     }
90
91     /**
92      * Creates Row Key for Alerts Table
93      *
94      * @param date current Date
95      *
96      * @return row key
97      */
98     public static String createRowKey(final Date date) {
99        return String.format("%025d", date.getTime());
100     }
101
102 }