19cf9c742c919cef404498e7c7b480a5e92082e0
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-common / src / main / java / org / onap / dcae / apod / analytics / cdap / common / persistance / tca / TCAAlertsAbatementPersister.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
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
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
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===========================================
19  */
20
21 package org.onap.dcae.apod.analytics.cdap.common.persistance.tca;
22
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;
40
41 import java.util.Date;
42 import java.util.List;
43
44 import static org.onap.dcae.apod.analytics.common.utils.PersistenceUtils.TABLE_ROW_KEY_COLUMN_NAME;
45
46 /**
47  * Utility methods to persist TCA Alerts Abatement information
48  *
49  * @author Rajiv Singla . Creation Date: 9/11/2017.
50  */
51 public abstract class TCAAlertsAbatementPersister {
52
53     private static final Logger LOG = LoggerFactory.getLogger(TCAAlertsAbatementPersister.class);
54
55     private static final Joiner KEY_JOINER = Joiner.on(PersistenceUtils.ROW_KEY_DELIMITER);
56
57     private TCAAlertsAbatementPersister() {
58         // private constructor
59     }
60
61     /**
62      * Creates {@link DatasetProperties} for Alerts Table
63      *
64      * @param timeToLiveSeconds alerts table Time to Live
65      *
66      * @return Alerts Abatement table properties
67      */
68     public static DatasetProperties getDatasetProperties(final int timeToLiveSeconds) {
69         try {
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)
76                     .build();
77         } catch (UnsupportedTypeException e) {
78             final String errorMessage = "Unable to convert TCAAlertsAbatementEntity class to Schema";
79             throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);
80         }
81     }
82
83
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);
90
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);
96
97         LOG.debug("Persisted AlertsAbatementEntity: {} with Key: {}", tcaAlertsAbatementEntity, abatementTableKey);
98
99     }
100
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);
107     }
108
109
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();
120
121         final List<String> abatementKeyList =
122                 ImmutableList.of(eventName, sourceName, reportingEntityName, closedLoopControlName, fieldPath);
123
124         return KEY_JOINER.join(abatementKeyList);
125     }
126
127 }