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 com.google.common.base.Joiner;
30 import com.google.common.collect.ImmutableList;
31 import org.onap.dcae.apod.analytics.cdap.common.CDAPComponentsConstants;
32 import org.onap.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;
33 import org.onap.dcae.apod.analytics.common.utils.PersistenceUtils;
34 import org.onap.dcae.apod.analytics.model.domain.cef.EventListener;
35 import org.onap.dcae.apod.analytics.model.domain.policy.tca.MetricsPerEventName;
36 import org.onap.dcae.apod.analytics.model.domain.policy.tca.Threshold;
37 import org.onap.dcae.apod.analytics.model.facade.tca.TCAVESResponse;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import java.util.Date;
42 import java.util.List;
44 import static org.onap.dcae.apod.analytics.common.utils.PersistenceUtils.TABLE_ROW_KEY_COLUMN_NAME;
47 * Utility methods to persist TCA Alerts Abatement information
49 * @author Rajiv Singla . Creation Date: 9/11/2017.
51 public abstract class TCAAlertsAbatementPersister {
53 private static final Logger LOG = LoggerFactory.getLogger(TCAAlertsAbatementPersister.class);
55 private static final Joiner KEY_JOINER = Joiner.on(PersistenceUtils.ROW_KEY_DELIMITER);
57 private TCAAlertsAbatementPersister() {
58 // private constructor
62 * Creates {@link DatasetProperties} for Alerts Table
64 * @param timeToLiveSeconds alerts table Time to Live
66 * @return Alerts Abatement table properties
68 public static DatasetProperties getDatasetProperties(final int timeToLiveSeconds) {
70 return ObjectMappedTableProperties.builder()
71 .setType(TCAAlertsAbatementEntity.class)
72 .setRowKeyExploreName(TABLE_ROW_KEY_COLUMN_NAME)
73 .setRowKeyExploreType(Schema.Type.STRING)
74 .add(IndexedTable.PROPERTY_TTL, timeToLiveSeconds)
75 .setDescription(CDAPComponentsConstants.TCA_FIXED_ALERTS_ABATEMENT_DESCRIPTION_TABLE)
77 } catch (UnsupportedTypeException e) {
78 final String errorMessage = "Unable to convert TCAAlertsAbatementEntity class to Schema";
79 throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);
84 public static void persist(final EventListener eventListener,
85 final MetricsPerEventName violatedMetricsPerEventName,
86 final TCAVESResponse tcavesResponse,
87 final String abatementTS,
88 final ObjectMappedTable<TCAAlertsAbatementEntity> tcaAlertsAbatementTable) {
89 final String abatementTableKey = createKey(eventListener, violatedMetricsPerEventName);
91 final long currentTimestamp = new Date().getTime();
92 final String requestID = tcavesResponse.getRequestID();
93 final TCAAlertsAbatementEntity tcaAlertsAbatementEntity = new TCAAlertsAbatementEntity(currentTimestamp,
94 requestID, abatementTS);
95 tcaAlertsAbatementTable.write(abatementTableKey, tcaAlertsAbatementEntity);
97 LOG.debug("Persisted AlertsAbatementEntity: {} with Key: {}", tcaAlertsAbatementEntity, abatementTableKey);
101 public static TCAAlertsAbatementEntity lookUpByKey(final EventListener eventListener,
102 final MetricsPerEventName violatedMetricsPerEventName,
103 final ObjectMappedTable<TCAAlertsAbatementEntity>
104 tcaAlertsAbatementTable) {
105 final String abatementTableKey = createKey(eventListener, violatedMetricsPerEventName);
106 return tcaAlertsAbatementTable.read(abatementTableKey);
110 public static String createKey(final EventListener eventListener,
111 final MetricsPerEventName violatedMetricsPerEventName) {
112 // no null check required as all are required fields
113 final String eventName = violatedMetricsPerEventName.getEventName();
114 final String sourceName = eventListener.getEvent().getCommonEventHeader().getSourceName();
115 final String reportingEntityName = eventListener.getEvent().getCommonEventHeader().getReportingEntityName();
116 // violated threshold will always be present
117 final Threshold violatedThreshold = violatedMetricsPerEventName.getThresholds().get(0);
118 final String closedLoopControlName = violatedThreshold.getClosedLoopControlName();
119 final String fieldPath = violatedThreshold.getFieldPath();
121 final List<String> abatementKeyList =
122 ImmutableList.of(eventName, sourceName, reportingEntityName, closedLoopControlName, fieldPath);
124 return KEY_JOINER.join(abatementKeyList);