2 * ===============================LICENSE_START======================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============================LICENSE_END===========================================
\r
21 package org.openecomp.dcae.apod.analytics.tca.processor;
\r
23 import com.google.common.base.Optional;
\r
24 import com.google.common.collect.Table;
\r
25 import org.openecomp.dcae.apod.analytics.common.exception.MessageProcessingException;
\r
26 import org.openecomp.dcae.apod.analytics.model.domain.cef.Domain;
\r
27 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener;
\r
28 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.MetricsPerEventName;
\r
29 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy;
\r
30 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold;
\r
31 import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils;
\r
32 import org.slf4j.Logger;
\r
33 import org.slf4j.LoggerFactory;
\r
35 import java.math.BigDecimal;
\r
36 import java.util.HashMap;
\r
37 import java.util.List;
\r
38 import java.util.Map;
\r
39 import java.util.Set;
\r
41 import javax.annotation.Nonnull;
\r
45 * TCA CEF Policy Threshold processor
\r
47 * Pre Conditions: Domain and Functional Role must be present in CEF Event Listener Object
\r
50 * @author Rajiv Singla . Creation Date: 11/9/2016.
\r
52 public class TCACEFPolicyThresholdsProcessor extends AbstractTCAECEFPolicyProcessor {
\r
54 private static final long serialVersionUID = 1L;
\r
56 private static final Logger LOG = LoggerFactory.getLogger(TCACEFPolicyThresholdsProcessor.class);
\r
59 public TCACEFProcessorContext preProcessor(@Nonnull TCACEFProcessorContext processorContext) {
\r
60 // validates Domain and Functional Role are present
\r
61 final EventListener eventListener = processorContext.getCEFEventListener();
\r
62 final Domain domain = eventListener.getEvent().getCommonEventHeader().getDomain();
\r
63 final String eventName = eventListener.getEvent().getCommonEventHeader().getEventName();
\r
64 if (domain == null || eventName == null) {
\r
65 final String errorMessage = "CEF Event Listener domain or eventName not Present. " +
\r
66 "Invalid use of this Processor";
\r
67 throw new MessageProcessingException(errorMessage, LOG, new IllegalArgumentException(errorMessage));
\r
69 return super.preProcessor(processorContext);
\r
73 public String getProcessorDescription() {
\r
74 return "Applies TCA Policy rules to incoming CEF message. If any thresholds are violated attaches max " +
\r
75 "Severity violated threshold to TCA Processor Context";
\r
79 public TCACEFProcessorContext processMessage(TCACEFProcessorContext processorContext) {
\r
81 final String cefMessage = processorContext.getMessage();
\r
83 // Determine domain and eventName
\r
84 final EventListener eventListener = processorContext.getCEFEventListener();
\r
85 final String eventName = eventListener.getEvent().getCommonEventHeader().getEventName();
\r
87 // Get Table containing event Name and Thresholds Field Path
\r
88 final TCAPolicy tcaPolicy = processorContext.getTCAPolicy();
\r
89 final Table<String, String, List<Threshold>> eventNameFieldPathsTable =
\r
90 TCAUtils.getPolicyEventNameThresholdsTableSupplier(tcaPolicy).get();
\r
92 // Get Policy Field Paths for that event Name
\r
93 final Map<String, List<Threshold>> policyFieldPathsMap = eventNameFieldPathsTable.row(eventName);
\r
94 final Set<String> policyFieldPaths = policyFieldPathsMap.keySet();
\r
96 // Get Json Values for Policy Fields
\r
97 final Map<String, List<BigDecimal>> messageFieldValuesMap =
\r
98 TCAUtils.getJsonPathValue(cefMessage, policyFieldPaths);
\r
100 // Determine all violated thresholds per message field Path
\r
101 final Map<String, Threshold> violatedThresholdsMap = new HashMap<>();
\r
102 for (Map.Entry<String, List<BigDecimal>> messageFieldValuesMapEntry : messageFieldValuesMap.entrySet()) {
\r
103 final String messageFieldPath = messageFieldValuesMapEntry.getKey();
\r
104 final List<Threshold> messageFieldAssociatedPolicyThresholds = policyFieldPathsMap.get(messageFieldPath);
\r
105 if (messageFieldAssociatedPolicyThresholds != null) {
\r
106 final Optional<Threshold> thresholdOptional = TCAUtils.thresholdCalculator(
\r
107 messageFieldValuesMapEntry.getValue(), messageFieldAssociatedPolicyThresholds);
\r
108 if (thresholdOptional.isPresent()) {
\r
109 violatedThresholdsMap.put(messageFieldPath, thresholdOptional.get());
\r
114 // No threshold were violated
\r
115 if (violatedThresholdsMap.isEmpty()) {
\r
117 final String terminationMessage = "No Policy Threshold violated by the VES CEF Message.";
\r
118 setTerminatingProcessingMessage(terminationMessage, processorContext);
\r
122 // If there are policy violations then determine max priority violation
\r
123 final Threshold maxSeverityThresholdViolation =
\r
124 TCAUtils.prioritizeThresholdViolations(violatedThresholdsMap);
\r
125 final MetricsPerEventName violatedMetrics = TCAUtils.createViolatedMetrics(tcaPolicy,
\r
126 maxSeverityThresholdViolation, eventName);
\r
127 // attach policy violation to processor Context
\r
128 processorContext.setMetricsPerEventName(violatedMetrics);
\r
130 final String finishMessage = String.format("Policy Threshold violation detected for threshold: %s",
\r
131 maxSeverityThresholdViolation);
\r
132 setFinishedProcessingMessage(finishMessage, processorContext);
\r
136 return processorContext;
\r