9b3762c97dd38d163de5a2c6f1d55cc79d955084
[dcaegen2/analytics/tca.git] / dcae-analytics-tca / src / test / java / org / onap / 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.onap.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.onap.dcae.apod.analytics.model.domain.cef.EventListener;
27 import org.onap.dcae.apod.analytics.model.domain.policy.tca.ClosedLoopEventStatus;
28 import org.onap.dcae.apod.analytics.model.domain.policy.tca.Direction;
29 import org.onap.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy;
30 import org.onap.dcae.apod.analytics.model.domain.policy.tca.Threshold;
31 import org.onap.dcae.apod.analytics.model.util.AnalyticsModelIOUtils;
32 import org.onap.dcae.apod.analytics.model.util.json.AnalyticsModelObjectMapperSupplier;
33 import org.onap.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_AAI_VNF_ENRICHMENT_FILE_LOCATION = "data/json/aai/aai_vnf_enrichment.json";
63
64     protected static final String TCA_TEST_APP_CONFIG_NAME = "testTCAAppName";
65     protected static final String TCA_TEST_APP_CONFIG_DESCRIPTION = "testTCAAppDescription";
66     protected static final String TCA_TEST_APP_CONFIG_SUBSCRIBER_OUTPUT_STREAM_NAME =
67             "testTcaSubscriberOutputStreamName";
68     protected static final String TCA_TEST_APP_CONFIG_VES_ALERT_TABLE_NAME = "testTcaVESAlertsTableName";
69     protected static final String TCA_TEST_APP_CONFIG_VES_MESSAGE_STATUS_TABLE_NAME =
70             "testTcaVESMessageStatusTableName";
71
72
73     /**
74      * Provides TCA Policy that can be used for testing
75      *
76      * @return test TCA Policy Object
77      */
78     protected TCAPolicy getSampleTCAPolicy() {
79         try {
80             return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(fromStream(TCA_POLICY_JSON_FILE_LOCATION), TCAPolicy.class);
81         } catch (IOException e) {
82             LOG.error("Error while parsing policy: {}", e);
83             throw new RuntimeException("Error while parsing policy", e);
84         }
85     }
86
87     /**
88      * Provides list containing 350 CEF messages
89      *
90      * @return CEF Test Message
91      * @throws Exception Exception
92      */
93     protected List<EventListener> getCEFMessages() throws Exception {
94         final String cefMessageAsString = fromStream(CEF_MESSAGES_JSON_FILE_LOCATION);
95         final TypeReference<List<EventListener>> eventListenerListTypeReference =
96                 new TypeReference<List<EventListener>>() {
97                 };
98         return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(cefMessageAsString, eventListenerListTypeReference);
99     }
100
101     /**
102      * Provides 1 valid CEF messages which does not violate Threshold as String
103      *
104      * @return CEF Test Message String
105      * @throws Exception Exception
106      */
107     protected String getValidCEFMessage() throws Exception {
108         return fromStream(CEF_MESSAGE_JSON_FILE_LOCATION);
109     }
110
111
112     /**
113      * Provides single CEF Test Message
114      *
115      * @return CEF Test Message
116      * @throws Exception Exception
117      */
118     protected EventListener getCEFEventListener() throws Exception {
119         final String cefMessageAsString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION);
120         return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(cefMessageAsString, EventListener.class);
121     }
122
123     protected static List<Threshold> getThresholds() {
124         Threshold majorThreshold = new Threshold();
125         majorThreshold.setClosedLoopControlName("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A");
126         majorThreshold.setFieldPath("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn");
127         majorThreshold.setVersion("Test Version");
128         majorThreshold.setThresholdValue(500L);
129         majorThreshold.setClosedLoopEventStatus(ClosedLoopEventStatus.ONSET);
130         majorThreshold.setDirection(Direction.LESS_OR_EQUAL);
131
132         Threshold criticalThreshold = new Threshold();
133         criticalThreshold.setClosedLoopControlName("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A");
134         criticalThreshold.setThresholdValue(5000L);
135         criticalThreshold.setFieldPath("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn");
136         criticalThreshold.setClosedLoopEventStatus(ClosedLoopEventStatus.ONSET);
137         criticalThreshold.setDirection(Direction.GREATER_OR_EQUAL);
138         return Arrays.asList(majorThreshold, criticalThreshold);
139     }
140
141     protected static Threshold getCriticalThreshold() {
142         Threshold criticalThreshold = new Threshold();
143         criticalThreshold.setClosedLoopControlName("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A");
144         criticalThreshold.setThresholdValue(5000L);
145         criticalThreshold.setFieldPath("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn");
146         criticalThreshold.setDirection(Direction.GREATER_OR_EQUAL);
147         return criticalThreshold;
148     }
149
150     protected static Map<String, String> getControllerRuntimeArguments() {
151         final Properties controllerProperties =
152                 AnalyticsModelIOUtils.loadPropertiesFile(TCA_CONTROLLER_POLICY_FILE_LOCATION, new Properties());
153
154         final Map<String, String> runtimeArgs = new LinkedHashMap<>();
155         for (Map.Entry<Object, Object> property : controllerProperties.entrySet()) {
156             runtimeArgs.put(property.getKey().toString(), property.getValue().toString());
157         }
158
159         return runtimeArgs;
160     }
161
162 }