2 * ===============================LICENSE_START======================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===========================================
21 package org.openecomp.dcae.apod.analytics.tca.utils;
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;
51 import java.util.Arrays;
52 import java.util.HashMap;
53 import java.util.List;
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;
72 * @author Rajiv Singla . Creation Date: 11/9/2016.
74 public class TCAUtilsTest extends BaseAnalyticsTCAUnitTest {
77 public void testGetPolicyFunctionalRoles() throws Exception {
79 final TCAPolicy sampleTCAPolicy = getSampleTCAPolicy();
80 final List<String> policyFunctionalRoles = TCAUtils.getPolicyFunctionalRoles(sampleTCAPolicy);
82 assertThat("Policy Functional Roles must contain vFirewall and vLoadBalancer", policyFunctionalRoles,
83 containsInAnyOrder("vFirewall", "vLoadBalancer"));
87 public void testGetPolicyFunctionalRoleSupplier() throws Exception {
88 final TCAPolicy sampleTCAPolicy = getSampleTCAPolicy();
89 final Supplier<List<String>> policyFunctionalRoleSupplier = TCAUtils.getPolicyFunctionalRoleSupplier
91 final List<String> policyFunctionalRoles = policyFunctionalRoleSupplier.get();
92 assertThat("Policy Functional Roles must contain vFirewall and vLoadBalancer", policyFunctionalRoles,
93 containsInAnyOrder("vFirewall", "vLoadBalancer"));
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));
106 public void testGetPolicyFRThresholdsTableSupplier() throws Exception {
107 final Table<String, String, List<Threshold>> policyFRThresholdPathTable = TCAUtils
108 .getPolicyFRThresholdsTableSupplier(getSampleTCAPolicy()).get();
110 final Map<String, List<Threshold>> vFirewall = policyFRThresholdPathTable.row("vFirewall");
111 final Map<String, List<Threshold>> vLoadBalancer = policyFRThresholdPathTable.row("vLoadBalancer");
113 final Set<String> vFirewallThresholdPaths = vFirewall.keySet();
114 final Set<String> vLoadBalancerPaths = vLoadBalancer.keySet();
116 assertThat("vFirewall threshold field path size must be " +
117 "\"$.event.measurementsForVfScalingFields.vNicUsageArray[*].bytesIn\"",
118 vFirewallThresholdPaths.iterator().next(),
119 is("$.event.measurementsForVfScalingFields.vNicUsageArray[*].bytesIn"));
121 assertThat("vLoadBalancer threshold field path size must be " +
122 "\"\"$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn\"",
123 vLoadBalancerPaths.iterator().next(),
124 is("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn"));
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");
131 assertThat("vFirewall Threshold size must be 2", firewallThresholds.size(), is(2));
132 assertThat("vLoadBalancer Threshold size must be 2", vLoadBalancerThresholds.size(), is(2));
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));
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));
157 public void testCreateNewTCAVESResponseWithFunctionalRolevLoadBalancer() throws Exception {
158 TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class);
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");
166 when(tcacefProcessorContext.getCEFEventListener()).thenReturn(getCEFEventListener());
167 TCAVESResponse tcaVESResponse = TCAUtils.createNewTCAVESResponse(tcacefProcessorContext, "TCA_APP_NAME");
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());
179 public void testCreateNewTCAVESResponseWithFunctionalRolevFirewall() throws Exception {
180 TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class);
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");
188 when(tcacefProcessorContext.getCEFEventListener()).thenReturn(getCEFEventListener());
189 TCAVESResponse tcaVESResponse = TCAUtils.createNewTCAVESResponse(tcacefProcessorContext, "TCA_APP_NAME");
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());
201 public ExpectedException expectedIllegalArgumentException = ExpectedException.none();
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");
209 TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class);
210 TCAVESResponse tcaVESResponse = TCAUtils.createNewTCAVESResponse(tcacefProcessorContext, "TCA_APP_NAME");
211 assertNotNull(tcaVESResponse.getClosedLoopControlName());
215 public void testPrioritizeThresholdViolations() throws Exception {
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);
222 Threshold result1 = TCAUtils.prioritizeThresholdViolations(thresholdMap);
223 assertEquals(result1.getSeverity(), EventSeverity.MAJOR);
225 Threshold criticalThreshold = mock(Threshold.class);
226 when(criticalThreshold.getSeverity()).thenReturn(EventSeverity.CRITICAL);
227 thresholdMap.put("CRITICAL", criticalThreshold);
229 Threshold result2 = TCAUtils.prioritizeThresholdViolations(thresholdMap);
230 assertEquals(result2.getSeverity(), EventSeverity.CRITICAL);
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"));
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");
249 TCAPolicy tcaPolicy = getSampleTCAPolicy();
250 Threshold violatedThreshold = getCriticalThreshold();
251 String functionalRole = "badFunctionRoleName";
252 MetricsPerFunctionalRole result = TCAUtils.createViolatedMetrics(tcaPolicy, violatedThreshold, functionalRole);
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);
262 Pair<String, String> result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext);
263 assertNull(result.getLeft());
264 assertNull(result.getRight());
266 when(tcacefProcessorContext.getCEFEventListener()).thenReturn(eventListener);
267 result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext);
268 assertNull(result.getLeft());
269 assertNull(result.getRight());
271 when(eventListener.getEvent()).thenReturn(event);
272 result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext);
273 assertNull(result.getLeft());
274 assertNull(result.getRight());
276 when(event.getCommonEventHeader()).thenReturn(commonEventHeader);
277 result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext);
278 assertNull(result.getLeft());
279 assertNull(result.getRight());
281 when(commonEventHeader.getDomain()).thenReturn("testDomain");
282 when(commonEventHeader.getFunctionalRole()).thenReturn("functionalRole");
284 result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext);
285 assertEquals(result.getLeft(), "testDomain");
286 assertEquals(result.getRight(), "functionalRole");
291 public void testComputeThresholdViolationsNotPresent() throws Exception {
292 TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class);
293 when(tcacefProcessorContext.canProcessingContinue()).thenReturn(true);
294 when(tcacefProcessorContext.getMessage()).thenReturn(getValidCEFMessage());
296 when(tcacefProcessorContext.getTCAPolicy()).thenReturn(getSampleTCAPolicy());
297 when(tcacefProcessorContext.getCEFEventListener()).thenReturn(getCEFEventListener());
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());
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);
312 when(tcacefProcessorContext.getTCAPolicy()).thenReturn(getSampleTCAPolicy());
313 when(tcacefProcessorContext.getCEFEventListener()).thenReturn(getCEFEventListener());
315 TCACEFProcessorContext result = TCAUtils.computeThresholdViolations(tcacefProcessorContext);
316 verify(result, times(1)).setMetricsPerFunctionalRole(Mockito.any(MetricsPerFunctionalRole.class));
318 assertEquals("Policy must not change", getSampleTCAPolicy(), result.getTCAPolicy());
323 public void testCreateTCAPolicyMetricsPerFunctionalRoleList() throws Exception {
325 final Map<String, String> tcaPolicyMap = TCAUtils.filterMapByKeyNamePrefix(getControllerRuntimeArguments(),
326 AnalyticsConstants.TCA_POLICY_METRICS_PER_FUNCTIONAL_ROLE_PATH);
328 // determine functional Roles
329 final Map<String, Map<String, String>> functionalRolesMap =
330 TCAUtils.extractSubTree(tcaPolicyMap, 2, 3, AnalyticsConstants.TCA_POLICY_DELIMITER);
332 final List<MetricsPerFunctionalRole> tcaPolicyMetricsPerFunctionalRoleList =
333 TCAUtils.createTCAPolicyMetricsPerFunctionalRoleList(functionalRolesMap);
335 assertThat("There are two Metrics per function role", 2,
336 is(tcaPolicyMetricsPerFunctionalRoleList.size()));
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));
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"));
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);
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"));
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);
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);
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;