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