Initial TCA commit into DCAEGEN2
[dcaegen2/analytics/tca.git] / dcae-analytics-tca / src / test / java / org / openecomp / dcae / apod / analytics / tca / utils / TCAUtilsTest.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.utils;
22
23 import com.google.common.base.Supplier;
24 import com.google.common.collect.ImmutableSet;
25 import com.google.common.collect.Table;
26 import org.apache.commons.lang3.tuple.Pair;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.mockito.Mockito;
31 import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants;
32 import org.openecomp.dcae.apod.analytics.common.exception.MessageProcessingException;
33 import org.openecomp.dcae.apod.analytics.model.domain.cef.CommonEventHeader;
34 import org.openecomp.dcae.apod.analytics.model.domain.cef.Event;
35 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener;
36 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventSeverity;
37 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Direction;
38 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.MetricsPerFunctionalRole;
39 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy;
40 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold;
41 import org.openecomp.dcae.apod.analytics.model.facade.tca.TCAVESResponse;
42 import org.openecomp.dcae.apod.analytics.tca.BaseAnalyticsTCAUnitTest;
43 import org.openecomp.dcae.apod.analytics.tca.processor.TCACEFProcessorContext;
44 import org.quartz.Job;
45 import org.quartz.JobDataMap;
46 import org.quartz.JobDetail;
47 import org.quartz.Scheduler;
48 import org.quartz.SimpleTrigger;
49 import org.quartz.impl.StdSchedulerFactory;
50
51 import java.util.Arrays;
52 import java.util.HashMap;
53 import java.util.List;
54 import java.util.Map;
55 import java.util.Set;
56
57 import static org.hamcrest.CoreMatchers.is;
58 import static org.hamcrest.CoreMatchers.isA;
59 import static org.hamcrest.Matchers.containsInAnyOrder;
60 import static org.junit.Assert.assertEquals;
61 import static org.junit.Assert.assertFalse;
62 import static org.junit.Assert.assertNotNull;
63 import static org.junit.Assert.assertNull;
64 import static org.junit.Assert.assertThat;
65 import static org.junit.Assert.assertTrue;
66 import static org.mockito.Mockito.mock;
67 import static org.mockito.Mockito.times;
68 import static org.mockito.Mockito.verify;
69 import static org.mockito.Mockito.when;
70
71 /**
72  * @author Rajiv Singla . Creation Date: 11/9/2016.
73  */
74 public class TCAUtilsTest extends BaseAnalyticsTCAUnitTest {
75
76     @Test
77     public void testGetPolicyFunctionalRoles() throws Exception {
78
79         final TCAPolicy sampleTCAPolicy = getSampleTCAPolicy();
80         final List<String> policyFunctionalRoles = TCAUtils.getPolicyFunctionalRoles(sampleTCAPolicy);
81
82         assertThat("Policy Functional Roles must contain vFirewall and vLoadBalancer", policyFunctionalRoles,
83                 containsInAnyOrder("vFirewall", "vLoadBalancer"));
84     }
85
86     @Test
87     public void testGetPolicyFunctionalRoleSupplier() throws Exception {
88         final TCAPolicy sampleTCAPolicy = getSampleTCAPolicy();
89         final Supplier<List<String>> policyFunctionalRoleSupplier = TCAUtils.getPolicyFunctionalRoleSupplier
90                 (sampleTCAPolicy);
91         final List<String> policyFunctionalRoles = policyFunctionalRoleSupplier.get();
92         assertThat("Policy Functional Roles must contain vFirewall and vLoadBalancer", policyFunctionalRoles,
93                 containsInAnyOrder("vFirewall", "vLoadBalancer"));
94     }
95
96     @Test
97     public void testProcessCEFMessage() throws Exception {
98         final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION);
99         final TCACEFProcessorContext tcacefProcessorContext = TCAUtils.filterCEFMessage(cefMessageString,
100                 getSampleTCAPolicy());
101         assertThat("TCAECEFProcessor Processor Context can continue flag is true", tcacefProcessorContext
102                 .canProcessingContinue(), is(true));
103     }
104
105     @Test
106     public void testGetPolicyFRThresholdsTableSupplier() throws Exception {
107         final Table<String, String, List<Threshold>> policyFRThresholdPathTable = TCAUtils
108                 .getPolicyFRThresholdsTableSupplier(getSampleTCAPolicy()).get();
109
110         final Map<String, List<Threshold>> vFirewall = policyFRThresholdPathTable.row("vFirewall");
111         final Map<String, List<Threshold>> vLoadBalancer = policyFRThresholdPathTable.row("vLoadBalancer");
112
113         final Set<String> vFirewallThresholdPaths = vFirewall.keySet();
114         final Set<String> vLoadBalancerPaths = vLoadBalancer.keySet();
115
116         assertThat("vFirewall threshold field path size must be " +
117                         "\"$.event.measurementsForVfScalingFields.vNicUsageArray[*].bytesIn\"",
118                 vFirewallThresholdPaths.iterator().next(),
119                 is("$.event.measurementsForVfScalingFields.vNicUsageArray[*].bytesIn"));
120
121         assertThat("vLoadBalancer threshold field path size must be " +
122                         "\"\"$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn\"",
123                 vLoadBalancerPaths.iterator().next(),
124                 is("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn"));
125
126         final List<Threshold> firewallThresholds = policyFRThresholdPathTable.get("vFirewall",
127                 "$.event.measurementsForVfScalingFields.vNicUsageArray[*].bytesIn");
128         final List<Threshold> vLoadBalancerThresholds = policyFRThresholdPathTable.get("vLoadBalancer",
129                 "$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn");
130
131         assertThat("vFirewall Threshold size must be 2", firewallThresholds.size(), is(2));
132         assertThat("vLoadBalancer Threshold size must be 2", vLoadBalancerThresholds.size(), is(2));
133     }
134
135     @Test
136     public void testGetJsonPathValueWithValidMessageAndPolicy() throws Exception {
137         final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION);
138         final String jsonPath = "$.event.measurementsForVfScalingFields.vNicUsageArray[*].bytesIn";
139         final ImmutableSet<String> fieldPaths = ImmutableSet.of(jsonPath);
140         final Map<String, List<Long>> jsonPathValueMap = TCAUtils.getJsonPathValue(cefMessageString, fieldPaths);
141         assertThat("Json Path value must match", jsonPathValueMap.get(jsonPath).get(0), is(6086L));
142
143     }
144
145     @Test
146     public void testGetJsonPathValueWithValidPath() throws Exception {
147         final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION);
148         final String jsonPath = "$.event.measurementsForVfScalingFields.vNicUsageArray[*].invalid";
149         final ImmutableSet<String> fieldPaths = ImmutableSet.of(jsonPath);
150         final Map<String, List<Long>> jsonPathValueMap = TCAUtils.getJsonPathValue(cefMessageString, fieldPaths);
151         assertThat("Json path value must be empty", jsonPathValueMap.size(), is(0));
152
153     }
154
155
156     @Test
157     public void testCreateNewTCAVESResponseWithFunctionalRolevLoadBalancer() throws Exception {
158         TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class);
159
160         MetricsPerFunctionalRole metricsPerFunctionalRole = mock(MetricsPerFunctionalRole.class);
161         when(metricsPerFunctionalRole.getThresholds()).thenReturn(getThresholds());
162         when(metricsPerFunctionalRole.getPolicyScope()).thenReturn("Test Policy scope");
163         when(tcacefProcessorContext.getMetricsPerFunctionalRole()).thenReturn(metricsPerFunctionalRole);
164         when(metricsPerFunctionalRole.getFunctionalRole()).thenReturn("vLoadBalancer");
165
166         when(tcacefProcessorContext.getCEFEventListener()).thenReturn(getCEFEventListener());
167         TCAVESResponse tcaVESResponse = TCAUtils.createNewTCAVESResponse(tcacefProcessorContext, "TCA_APP_NAME");
168
169         //TODO :  Add proper assertions, as the usage is not clearly understood
170         assertThat(tcaVESResponse.getClosedLoopControlName(),
171                 is("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A"));
172         assertThat(tcaVESResponse.getVersion(), is("Test Version"));
173         assertThat(tcaVESResponse.getPolicyScope(), is("Test Policy scope"));
174         assertNull(tcaVESResponse.getAai().getGenericVNFId());
175         assertNotNull(tcaVESResponse.getAai().getGenericServerId());
176     }
177
178     @Test
179     public void testCreateNewTCAVESResponseWithFunctionalRolevFirewall() throws Exception {
180         TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class);
181
182         MetricsPerFunctionalRole metricsPerFunctionalRole = mock(MetricsPerFunctionalRole.class);
183         when(metricsPerFunctionalRole.getThresholds()).thenReturn(getThresholds());
184         when(metricsPerFunctionalRole.getPolicyScope()).thenReturn("Test Policy scope");
185         when(tcacefProcessorContext.getMetricsPerFunctionalRole()).thenReturn(metricsPerFunctionalRole);
186         when(metricsPerFunctionalRole.getFunctionalRole()).thenReturn("vFirewall");
187
188         when(tcacefProcessorContext.getCEFEventListener()).thenReturn(getCEFEventListener());
189         TCAVESResponse tcaVESResponse = TCAUtils.createNewTCAVESResponse(tcacefProcessorContext, "TCA_APP_NAME");
190
191         //TODO :  Add proper assertions, as the usage is not clearly understood
192         assertThat(tcaVESResponse.getClosedLoopControlName(),
193                 is("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A"));
194         assertThat(tcaVESResponse.getVersion(), is("Test Version"));
195         assertThat(tcaVESResponse.getPolicyScope(), is("Test Policy scope"));
196         assertNotNull(tcaVESResponse.getAai().getGenericVNFId());
197         assertNull(tcaVESResponse.getAai().getGenericServerId());
198     }
199
200     @Rule
201     public ExpectedException expectedIllegalArgumentException = ExpectedException.none();
202
203     @Test
204     public void testCreateNewTCAVESResponseNullFunctionalRole() throws Exception {
205         expectedIllegalArgumentException.expect(MessageProcessingException.class);
206         expectedIllegalArgumentException.expectCause(isA(IllegalArgumentException.class));
207         expectedIllegalArgumentException.expectMessage("No violations metrics. Unable to create VES Response");
208
209         TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class);
210         TCAVESResponse tcaVESResponse = TCAUtils.createNewTCAVESResponse(tcacefProcessorContext, "TCA_APP_NAME");
211         assertNotNull(tcaVESResponse.getClosedLoopControlName());
212     }
213
214     @Test
215     public void testPrioritizeThresholdViolations() throws Exception {
216
217         Map<String, Threshold> thresholdMap = new HashMap<>();
218         Threshold majorThreshold = mock(Threshold.class);
219         when(majorThreshold.getSeverity()).thenReturn(EventSeverity.MAJOR);
220         thresholdMap.put("MAJOR", majorThreshold);
221
222         Threshold result1 = TCAUtils.prioritizeThresholdViolations(thresholdMap);
223         assertEquals(result1.getSeverity(), EventSeverity.MAJOR);
224
225         Threshold criticalThreshold = mock(Threshold.class);
226         when(criticalThreshold.getSeverity()).thenReturn(EventSeverity.CRITICAL);
227         thresholdMap.put("CRITICAL", criticalThreshold);
228
229         Threshold result2 = TCAUtils.prioritizeThresholdViolations(thresholdMap);
230         assertEquals(result2.getSeverity(), EventSeverity.CRITICAL);
231     }
232
233     @Test
234     public void testCreateViolatedMetrics() throws Exception {
235         TCAPolicy tcaPolicy = getSampleTCAPolicy();
236         Threshold violatedThreshold = getCriticalThreshold();
237         String functionalRole = "vFirewall";
238         MetricsPerFunctionalRole result = TCAUtils.createViolatedMetrics(tcaPolicy, violatedThreshold, functionalRole);
239         assertThat(result.getPolicyScope(), is("resource=vFirewall;type=configuration"));
240         assertThat(result.getPolicyName(), is("configuration.dcae.microservice.tca.xml"));
241     }
242
243     @Test
244     public void testCreateViolatedMetricsWrongFunctionalRole() throws Exception {
245         expectedIllegalArgumentException.expect(MessageProcessingException.class);
246         expectedIllegalArgumentException.expectCause(isA(IllegalStateException.class));
247         expectedIllegalArgumentException.expectMessage("TCA Policy must contain functional Role: badFunctionRoleName");
248
249         TCAPolicy tcaPolicy = getSampleTCAPolicy();
250         Threshold violatedThreshold = getCriticalThreshold();
251         String functionalRole = "badFunctionRoleName";
252         MetricsPerFunctionalRole result = TCAUtils.createViolatedMetrics(tcaPolicy, violatedThreshold, functionalRole);
253     }
254
255     @Test
256     public void testGetDomainAndFunctionalRole() {
257         TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class);
258         EventListener eventListener = mock(EventListener.class);
259         Event event = mock(Event.class);
260         CommonEventHeader commonEventHeader = mock(CommonEventHeader.class);
261
262         Pair<String, String> result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext);
263         assertNull(result.getLeft());
264         assertNull(result.getRight());
265
266         when(tcacefProcessorContext.getCEFEventListener()).thenReturn(eventListener);
267         result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext);
268         assertNull(result.getLeft());
269         assertNull(result.getRight());
270
271         when(eventListener.getEvent()).thenReturn(event);
272         result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext);
273         assertNull(result.getLeft());
274         assertNull(result.getRight());
275
276         when(event.getCommonEventHeader()).thenReturn(commonEventHeader);
277         result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext);
278         assertNull(result.getLeft());
279         assertNull(result.getRight());
280
281         when(commonEventHeader.getDomain()).thenReturn("testDomain");
282         when(commonEventHeader.getFunctionalRole()).thenReturn("functionalRole");
283
284         result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext);
285         assertEquals(result.getLeft(), "testDomain");
286         assertEquals(result.getRight(), "functionalRole");
287
288     }
289
290     @Test
291     public void testComputeThresholdViolationsNotPresent() throws Exception {
292         TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class);
293         when(tcacefProcessorContext.canProcessingContinue()).thenReturn(true);
294         when(tcacefProcessorContext.getMessage()).thenReturn(getValidCEFMessage());
295
296         when(tcacefProcessorContext.getTCAPolicy()).thenReturn(getSampleTCAPolicy());
297         when(tcacefProcessorContext.getCEFEventListener()).thenReturn(getCEFEventListener());
298
299         TCACEFProcessorContext result = TCAUtils.computeThresholdViolations(tcacefProcessorContext);
300         assertNotNull(result);
301         verify(result, times(0)).setMetricsPerFunctionalRole(Mockito.any(MetricsPerFunctionalRole.class));
302         assertEquals("Policy must not change", getSampleTCAPolicy(), result.getTCAPolicy());
303     }
304
305     @Test
306     public void testComputeThresholdViolationsPresent() throws Exception {
307         TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class);
308         when(tcacefProcessorContext.canProcessingContinue()).thenReturn(true);
309         final String cefMessageString = fromStream(CEF_MESSAGE_WITH_THRESHOLD_VIOLATION_JSON_FILE_LOCATION);
310         when(tcacefProcessorContext.getMessage()).thenReturn(cefMessageString);
311
312         when(tcacefProcessorContext.getTCAPolicy()).thenReturn(getSampleTCAPolicy());
313         when(tcacefProcessorContext.getCEFEventListener()).thenReturn(getCEFEventListener());
314
315         TCACEFProcessorContext result = TCAUtils.computeThresholdViolations(tcacefProcessorContext);
316         verify(result, times(1)).setMetricsPerFunctionalRole(Mockito.any(MetricsPerFunctionalRole.class));
317
318         assertEquals("Policy must not change", getSampleTCAPolicy(), result.getTCAPolicy());
319     }
320
321
322     @Test
323     public void testCreateTCAPolicyMetricsPerFunctionalRoleList() throws Exception {
324
325         final Map<String, String> tcaPolicyMap = TCAUtils.filterMapByKeyNamePrefix(getControllerRuntimeArguments(),
326                 AnalyticsConstants.TCA_POLICY_METRICS_PER_FUNCTIONAL_ROLE_PATH);
327
328         // determine functional Roles
329         final Map<String, Map<String, String>> functionalRolesMap =
330                 TCAUtils.extractSubTree(tcaPolicyMap, 2, 3, AnalyticsConstants.TCA_POLICY_DELIMITER);
331
332         final List<MetricsPerFunctionalRole> tcaPolicyMetricsPerFunctionalRoleList =
333                 TCAUtils.createTCAPolicyMetricsPerFunctionalRoleList(functionalRolesMap);
334
335         assertThat("There are two Metrics per function role", 2,
336                 is(tcaPolicyMetricsPerFunctionalRoleList.size()));
337     }
338
339
340     @Test
341     public void testCreateQuartzScheduler() throws Exception {
342         final Scheduler scheduler = Mockito.mock(Scheduler.class);
343         final StdSchedulerFactory stdSchedulerFactory = Mockito.mock(StdSchedulerFactory.class);
344         when(stdSchedulerFactory.getScheduler()).thenReturn(scheduler);
345         final JobDataMap jobDataMap = Mockito.mock(JobDataMap.class);
346         TCAUtils.createQuartzScheduler(1000, stdSchedulerFactory,
347                 "data/properties/quartz-test.properties", jobDataMap, Job.class,
348                 "testJob", "testTigger");
349         verify(scheduler, times(1))
350                 .scheduleJob(Mockito.any(JobDetail.class), Mockito.any(SimpleTrigger.class));
351     }
352
353
354     @Test
355     public void testCreateTCAAlertStringWhenCEFIsEnabled() throws Exception {
356         final MetricsPerFunctionalRole violatedMetrics = createViolatedMetricsPerFunctionalRole(EventSeverity.CRITICAL);
357         TCACEFProcessorContext processorContext = mock(TCACEFProcessorContext.class);
358         when(processorContext.getMetricsPerFunctionalRole()).thenReturn(violatedMetrics);
359         when(processorContext.getCEFEventListener()).thenReturn(getCEFEventListener());
360         final String alertString = TCAUtils.createTCAAlertString(processorContext, "testApp", true);
361         assertTrue(alertString.contains("thresholdCrossingAlertFields"));
362     }
363
364     @Test(expected = MessageProcessingException.class)
365     public void testCreateTCAAlertStringWhenViolatedMetricsNotPresentAndCEFIsEnabled() throws Exception {
366         TCACEFProcessorContext processorContext = mock(TCACEFProcessorContext.class);
367         when(processorContext.getMetricsPerFunctionalRole()).thenReturn(null);
368         TCAUtils.createTCAAlertString(processorContext, "testApp", true);
369     }
370
371     @Test
372     public void testCreateTCAAlertStringWhenCEFIsDisabled() throws Exception {
373         final MetricsPerFunctionalRole violatedMetrics = createViolatedMetricsPerFunctionalRole(EventSeverity.MAJOR);
374         TCACEFProcessorContext processorContext = mock(TCACEFProcessorContext.class);
375         when(processorContext.getMetricsPerFunctionalRole()).thenReturn(violatedMetrics);
376         when(processorContext.getCEFEventListener()).thenReturn(getCEFEventListener());
377         final String alertString = TCAUtils.createTCAAlertString(processorContext, "testApp", false);
378         assertFalse(alertString.contains("thresholdCrossingAlertFields"));
379     }
380
381     @Test(expected = MessageProcessingException.class)
382     public void testCreateTCAAlertStringWhenViolatedMetricsNotPresentAndCEFIsDisabled() throws Exception {
383         TCACEFProcessorContext processorContext = mock(TCACEFProcessorContext.class);
384         when(processorContext.getMetricsPerFunctionalRole()).thenReturn(null);
385         TCAUtils.createTCAAlertString(processorContext, "testApp", false);
386     }
387
388     private static MetricsPerFunctionalRole createViolatedMetricsPerFunctionalRole(EventSeverity severity) {
389         final Threshold violatedThreshold = new Threshold();
390         violatedThreshold.setSeverity(severity);
391         violatedThreshold.setDirection(Direction.GREATER);
392         violatedThreshold.setClosedLoopControlName("violatedThresholdClosedLoopName");
393         violatedThreshold.setActualFieldValue(100L);
394         violatedThreshold.setFieldPath("violatedThresholdFieldPath");
395         violatedThreshold.setVersion("violatedThresholdVersion");
396         violatedThreshold.setThresholdValue(50L);
397
398         final MetricsPerFunctionalRole violatedMetrics = new MetricsPerFunctionalRole();
399         violatedMetrics.setPolicyName("violatePolicyName");
400         violatedMetrics.setPolicyVersion("violatedPolicyVersion");
401         violatedMetrics.setPolicyScope("violatedPolicyScope");
402         violatedMetrics.setFunctionalRole("violatedFunctionalRole");
403         violatedMetrics.setThresholds(Arrays.asList(violatedThreshold));
404         return violatedMetrics;
405     }
406 }