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.util.HashMap;
\r
36 import java.util.List;
\r
37 import java.util.Map;
\r
38 import java.util.Set;
\r
40 import javax.annotation.Nonnull;
\r
44 * TCA CEF Policy Threshold processor
\r
46 * Pre Conditions: Domain and Functional Role must be present in CEF Event Listener Object
\r
49 * @author Rajiv Singla . Creation Date: 11/9/2016.
\r
51 public class TCACEFPolicyThresholdsProcessor extends AbstractTCAECEFPolicyProcessor {
\r
53 private static final long serialVersionUID = 1L;
\r
55 private static final Logger LOG = LoggerFactory.getLogger(TCACEFPolicyThresholdsProcessor.class);
\r
58 public TCACEFProcessorContext preProcessor(@Nonnull TCACEFProcessorContext processorContext) {
\r
59 // validates Domain and Functional Role are present
\r
60 final EventListener eventListener = processorContext.getCEFEventListener();
\r
61 final Domain domain = eventListener.getEvent().getCommonEventHeader().getDomain();
\r
62 final String eventName = eventListener.getEvent().getCommonEventHeader().getEventName();
\r
63 if (domain == null || eventName == null) {
\r
64 final String errorMessage = "CEF Event Listener domain or eventName not Present. " +
\r
65 "Invalid use of this Processor";
\r
66 throw new MessageProcessingException(errorMessage, LOG, new IllegalArgumentException(errorMessage));
\r
68 return super.preProcessor(processorContext);
\r
72 public String getProcessorDescription() {
\r
73 return "Applies TCA Policy rules to incoming CEF message. If any thresholds are violated attaches max " +
\r
74 "Severity violated threshold to TCA Processor Context";
\r
78 public TCACEFProcessorContext processMessage(TCACEFProcessorContext processorContext) {
\r
80 final String cefMessage = processorContext.getMessage();
\r
82 // Determine domain and eventName
\r
83 final EventListener eventListener = processorContext.getCEFEventListener();
\r
84 final String eventName = eventListener.getEvent().getCommonEventHeader().getEventName();
\r
86 // Get Table containing event Name and Thresholds Field Path
\r
87 final TCAPolicy tcaPolicy = processorContext.getTCAPolicy();
\r
88 final Table<String, String, List<Threshold>> eventNameFieldPathsTable =
\r
89 TCAUtils.getPolicyEventNameThresholdsTableSupplier(tcaPolicy).get();
\r
91 // Get Policy Field Paths for that event Name
\r
92 final Map<String, List<Threshold>> policyFieldPathsMap = eventNameFieldPathsTable.row(eventName);
\r
93 final Set<String> policyFieldPaths = policyFieldPathsMap.keySet();
\r
95 // Get Json Values for Policy Fields
\r
96 final Map<String, List<Long>> messageFieldValuesMap = TCAUtils.getJsonPathValue(cefMessage, policyFieldPaths);
\r
98 // Determine all violated thresholds per message field Path
\r
99 final Map<String, Threshold> violatedThresholdsMap = new HashMap<>();
\r
100 for (Map.Entry<String, List<Long>> messageFieldValuesMapEntry : messageFieldValuesMap.entrySet()) {
\r
101 final String messageFieldPath = messageFieldValuesMapEntry.getKey();
\r
102 final List<Threshold> messageFieldAssociatedPolicyThresholds = policyFieldPathsMap.get(messageFieldPath);
\r
103 if (messageFieldAssociatedPolicyThresholds != null) {
\r
104 final Optional<Threshold> thresholdOptional = TCAUtils.thresholdCalculator(
\r
105 messageFieldValuesMapEntry.getValue(), messageFieldAssociatedPolicyThresholds);
\r
106 if (thresholdOptional.isPresent()) {
\r
107 violatedThresholdsMap.put(messageFieldPath, thresholdOptional.get());
\r
112 // No threshold were violated
\r
113 if (violatedThresholdsMap.isEmpty()) {
\r
115 final String terminationMessage = "No Policy Threshold violated by the VES CEF Message.";
\r
116 setTerminatingProcessingMessage(terminationMessage, processorContext);
\r
120 // If there are policy violations then determine max priority violation
\r
121 final Threshold maxSeverityThresholdViolation =
\r
122 TCAUtils.prioritizeThresholdViolations(violatedThresholdsMap);
\r
123 final MetricsPerEventName violatedMetrics = TCAUtils.createViolatedMetrics(tcaPolicy,
\r
124 maxSeverityThresholdViolation, eventName);
\r
125 // attach policy violation to processor Context
\r
126 processorContext.setMetricsPerEventName(violatedMetrics);
\r
128 final String finishMessage = String.format("Policy Threshold violation detected for threshold: %s",
\r
129 maxSeverityThresholdViolation);
\r
130 setFinishedProcessingMessage(finishMessage, processorContext);
\r
134 return processorContext;
\r