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