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