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.common.persistance.tca;
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.onap.dcae.apod.analytics.cdap.common.CDAPComponentsConstants;
31 import org.onap.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import java.util.Date;
37 import static org.onap.dcae.apod.analytics.common.utils.PersistenceUtils.TABLE_ROW_KEY_COLUMN_NAME;
41 * @author Rajiv Singla . Creation Date: 11/16/2016.
43 public abstract class TCAVESAlertsPersister {
45 private static final Logger LOG = LoggerFactory.getLogger(TCAVESAlertsPersister.class);
47 private TCAVESAlertsPersister() {
52 * Persists Alert Message to Alerts Table
54 * @param alertMessage alert Message
55 * @param tcaVESAlertTable alert Table Name
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);
65 LOG.debug("Finished persisting VES Alert message ID: {} in VES Alerts table.", rowKey);
70 * Creates {@link DatasetProperties} for Alerts Table
72 * @param timeToLiveSeconds alerts table Time to Live
73 * @return Alerts table properties
75 public static DatasetProperties getDatasetProperties(final int timeToLiveSeconds) {
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)
84 } catch (UnsupportedTypeException e) {
85 final String errorMessage = "Unable to convert TCAVESAlertEntity class to Schema";
86 throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);
92 * Creates Row Key for Alerts Table
94 * @param date current Date
98 public static String createRowKey(final Date date) {
99 return String.format("%025d", date.getTime());