aboutsummaryrefslogtreecommitdiffstats
path: root/dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/persistance/TCAVESAlertsPersister.java
blob: b9f636d2db22fd34caacfb5d4704a062670859f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * ============LICENSE_START=========================================================
 * dcae-analytics
 * ================================================================================
 *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.dcae.apod.analytics.tca.persistance;

import co.cask.cdap.api.data.schema.Schema;
import co.cask.cdap.api.data.schema.UnsupportedTypeException;
import co.cask.cdap.api.dataset.DatasetProperties;
import co.cask.cdap.api.dataset.lib.IndexedTable;
import co.cask.cdap.api.dataset.lib.ObjectMappedTable;
import co.cask.cdap.api.dataset.lib.ObjectMappedTableProperties;
import org.apache.commons.lang3.StringEscapeUtils;
import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

import static org.openecomp.dcae.apod.analytics.common.CDAPComponentsConstants.TCA_DEFAULT_VES_ALERTS_DESCRIPTION_TABLE;
import static org.openecomp.dcae.apod.analytics.common.utils.PersistenceUtils.TABLE_ROW_KEY_COLUMN_NAME;

/**
 * @author Rajiv Singla. Creation Date: 11/16/2016.
 */
public abstract class TCAVESAlertsPersister {

  private static final Logger LOG = LoggerFactory.getLogger(TCAVESAlertsPersister.class);

  /**
   * Persists Alert Message to Alerts Table
   *
   * @param alertMessage alert Message
   * @param tcaVESAlertTable alert Table Name
   */
  public static void persist(final String alertMessage,
      final ObjectMappedTable<TCAVESAlertEntity> tcaVESAlertTable) {
    final Date currentDate = new Date();
    final TCAVESAlertEntity alertEntity = new TCAVESAlertEntity(currentDate.getTime(),
        StringEscapeUtils.unescapeJson(alertMessage));
    // row key is same as current timestamp
    final String rowKey = createRowKey(currentDate);
    tcaVESAlertTable.write(rowKey, alertEntity);

    LOG.debug("Finished persisting VES Alert message ID: {} in VES Alerts table.", rowKey);
  }


  /**
   * Creates {@link DatasetProperties} for Alerts Table
   *
   * @param timeToLiveSeconds alerts table Time to Live
   * @return Alerts table properties
   */
  public static DatasetProperties getDatasetProperties(final int timeToLiveSeconds) {
    try {
      return ObjectMappedTableProperties.builder()
          .setType(TCAVESAlertEntity.class)
          .setRowKeyExploreName(TABLE_ROW_KEY_COLUMN_NAME)
          .setRowKeyExploreType(Schema.Type.STRING)
          .add(IndexedTable.PROPERTY_TTL, timeToLiveSeconds)
          .setDescription(TCA_DEFAULT_VES_ALERTS_DESCRIPTION_TABLE)
          .build();
    } catch (UnsupportedTypeException e) {
      final String errorMessage = "Unable to convert TCAVESAlertEntity class to Schema";
      throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, new
          IllegalArgumentException(errorMessage, e));
    }

  }

  /**
   * Creates Row Key for Alerts Table
   *
   * @param date current Date
   * @return row key
   */
  public static String createRowKey(final Date date) {
    return String.format("%025d", date.getTime());
  }

}