2 * ===============================LICENSE_START======================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package org.openecomp.dcae.apod.analytics.cdap.common.persistance.tca;
\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
35 import java.util.Date;
\r
37 import static org.openecomp.dcae.apod.analytics.common.utils.PersistenceUtils.TABLE_ROW_KEY_COLUMN_NAME;
\r
41 * @author Rajiv Singla . Creation Date: 11/16/2016.
\r
43 public abstract class TCAVESAlertsPersister {
\r
45 private static final Logger LOG = LoggerFactory.getLogger(TCAVESAlertsPersister.class);
\r
47 private TCAVESAlertsPersister() {
\r
52 * Persists Alert Message to Alerts Table
\r
54 * @param alertMessage alert Message
\r
55 * @param tcaVESAlertTable alert Table Name
\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
65 LOG.debug("Finished persisting VES Alert message ID: {} in VES Alerts table.", rowKey);
\r
70 * Creates {@link DatasetProperties} for Alerts Table
\r
72 * @param timeToLiveSeconds alerts table Time to Live
\r
73 * @return Alerts table properties
\r
75 public static DatasetProperties getDatasetProperties(final int timeToLiveSeconds) {
\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
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
92 * Creates Row Key for Alerts Table
\r
94 * @param date current Date
\r
98 public static String createRowKey(final Date date) {
\r
99 return String.format("%025d", date.getTime());
\r