Add support for ABATED alerts within CDAP TCA
[dcaegen2/analytics/tca.git] / dcae-analytics-tca / src / test / java / org / openecomp / dcae / apod / analytics / tca / BaseAnalyticsTCAUnitTest.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.openecomp.dcae.apod.analytics.tca;
22
23 import com.fasterxml.jackson.core.type.TypeReference;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.google.common.base.Suppliers;
26 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener;
27 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.ControlLoopEventStatus;
28 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Direction;
29 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy;
30 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold;
31 import org.openecomp.dcae.apod.analytics.model.util.AnalyticsModelIOUtils;
32 import org.openecomp.dcae.apod.analytics.model.util.json.AnalyticsModelObjectMapperSupplier;
33 import org.openecomp.dcae.apod.analytics.test.BaseDCAEAnalyticsUnitTest;
34
35 import java.io.IOException;
36 import java.util.Arrays;
37 import java.util.LinkedHashMap;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Properties;
41
42 /**
43  * @author Rajiv Singla . Creation Date: 10/25/2016.
44  */
45 public abstract class BaseAnalyticsTCAUnitTest extends BaseDCAEAnalyticsUnitTest {
46
47     /**
48      * Object mapper to be used for all TCA Json Parsing
49      */
50     protected static final ObjectMapper ANALYTICS_MODEL_OBJECT_MAPPER =
51             Suppliers.memoize(new AnalyticsModelObjectMapperSupplier()).get();
52
53     protected static final String TCA_POLICY_JSON_FILE_LOCATION = "data/json/policy/tca_policy.json";
54     protected static final String CEF_MESSAGES_JSON_FILE_LOCATION = "data/json/cef/cef_messages.json";
55     protected static final String CEF_MESSAGE_JSON_FILE_LOCATION = "data/json/cef/cef_message.json";
56     protected static final String CEF_MESSAGE_WITH_THRESHOLD_VIOLATION_JSON_FILE_LOCATION =
57             "data/json/cef/cef_message_with_threshold_violation.json";
58
59     protected static final String TCA_CONTROLLER_POLICY_FILE_LOCATION =
60             "data/properties/tca_controller_policy.properties";
61
62     protected static final String TCA_TEST_APP_CONFIG_NAME = "testTCAAppName";
63     protected static final String TCA_TEST_APP_CONFIG_DESCRIPTION = "testTCAAppDescription";
64     protected static final String TCA_TEST_APP_CONFIG_SUBSCRIBER_OUTPUT_STREAM_NAME =
65             "testTcaSubscriberOutputStreamName";
66     protected static final String TCA_TEST_APP_CONFIG_VES_ALERT_TABLE_NAME = "testTcaVESAlertsTableName";
67     protected static final String TCA_TEST_APP_CONFIG_VES_MESSAGE_STATUS_TABLE_NAME =
68             "testTcaVESMessageStatusTableName";
69
70
71     /**
72      * Provides TCA Policy that can be used for testing
73      *
74      * @return test TCA Policy Object
75      */
76     protected TCAPolicy getSampleTCAPolicy() {
77         try {
78             return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(fromStream(TCA_POLICY_JSON_FILE_LOCATION), TCAPolicy.class);
79         } catch (IOException e) {
80             LOG.error("Error while parsing policy: {}", e);
81             throw new RuntimeException("Error while parsing policy", e);
82         }
83     }
84
85     /**
86      * Provides list containing 350 CEF messages
87      *
88      * @return CEF Test Message
89      * @throws Exception Exception
90      */
91     protected List<EventListener> getCEFMessages() throws Exception {
92         final String cefMessageAsString = fromStream(CEF_MESSAGES_JSON_FILE_LOCATION);
93         final TypeReference<List<EventListener>> eventListenerListTypeReference =
94                 new TypeReference<List<EventListener>>() {
95                 };
96         return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(cefMessageAsString, eventListenerListTypeReference);
97     }
98
99     /**
100      * Provides 1 valid CEF messages which does not violate Threshold as String
101      *
102      * @return CEF Test Message String
103      * @throws Exception Exception
104      */
105     protected String getValidCEFMessage() throws Exception {
106         return fromStream(CEF_MESSAGE_JSON_FILE_LOCATION);
107     }
108
109
110     /**
111      * Provides single CEF Test Message
112      *
113      * @return CEF Test Message
114      * @throws Exception Exception
115      */
116     protected EventListener getCEFEventListener() throws Exception {
117         final String cefMessageAsString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION);
118         return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(cefMessageAsString, EventListener.class);
119     }
120
121     protected static List<Threshold> getThresholds() {
122         Threshold majorThreshold = new Threshold();
123         majorThreshold.setClosedLoopControlName("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A");
124         majorThreshold.setFieldPath("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn");
125         majorThreshold.setVersion("Test Version");
126         majorThreshold.setThresholdValue(500L);
127         majorThreshold.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
128         majorThreshold.setDirection(Direction.LESS_OR_EQUAL);
129
130         Threshold criticalThreshold = new Threshold();
131         criticalThreshold.setClosedLoopControlName("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A");
132         criticalThreshold.setThresholdValue(5000L);
133         criticalThreshold.setFieldPath("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn");
134         criticalThreshold.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
135         criticalThreshold.setDirection(Direction.GREATER_OR_EQUAL);
136         return Arrays.asList(majorThreshold, criticalThreshold);
137     }
138
139     protected static Threshold getCriticalThreshold() {
140         Threshold criticalThreshold = new Threshold();
141         criticalThreshold.setClosedLoopControlName("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A");
142         criticalThreshold.setThresholdValue(5000L);
143         criticalThreshold.setFieldPath("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn");
144         criticalThreshold.setDirection(Direction.GREATER_OR_EQUAL);
145         return criticalThreshold;
146     }
147
148     protected static Map<String, String> getControllerRuntimeArguments() {
149         final Properties controllerProperties =
150                 AnalyticsModelIOUtils.loadPropertiesFile(TCA_CONTROLLER_POLICY_FILE_LOCATION, new Properties());
151
152         final Map<String, String> runtimeArgs = new LinkedHashMap<>();
153         for (Map.Entry<Object, Object> property : controllerProperties.entrySet()) {
154             runtimeArgs.put(property.getKey().toString(), property.getValue().toString());
155         }
156
157         return runtimeArgs;
158     }
159
160 }