704151d4afee1b5ad7b7e84a751b8998fa32af2c
[dcaegen2/analytics/tca.git] / dcae-analytics-tca / src / main / java / org / openecomp / dcae / apod / analytics / tca / processor / TCACEFPolicyThresholdsProcessor.java
1 /*\r
2  * ===============================LICENSE_START======================================\r
3  *  dcae-analytics\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
10  *\r
11  *          http://www.apache.org/licenses/LICENSE-2.0\r
12  *\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
19  */\r
20 \r
21 package org.openecomp.dcae.apod.analytics.tca.processor;\r
22 \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
34 \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
39 \r
40 import javax.annotation.Nonnull;\r
41 \r
42 /**\r
43  *<p>\r
44  *     TCA CEF Policy Threshold processor\r
45  *     <br>\r
46  *     Pre Conditions: Domain and Functional Role must be present in CEF Event Listener Object\r
47  *</p>\r
48  *\r
49  * @author Rajiv Singla . Creation Date: 11/9/2016.\r
50  */\r
51 public class TCACEFPolicyThresholdsProcessor extends AbstractTCAECEFPolicyProcessor {\r
52 \r
53     private static final long serialVersionUID = 1L;\r
54 \r
55     private static final Logger LOG = LoggerFactory.getLogger(TCACEFPolicyThresholdsProcessor.class);\r
56 \r
57     @Override\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
67         }\r
68         return super.preProcessor(processorContext);\r
69     }\r
70 \r
71     @Override\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
75     }\r
76 \r
77     @Override\r
78     public TCACEFProcessorContext processMessage(TCACEFProcessorContext processorContext) {\r
79 \r
80         final String cefMessage = processorContext.getMessage();\r
81 \r
82         // Determine domain and eventName\r
83         final EventListener eventListener = processorContext.getCEFEventListener();\r
84         final String eventName = eventListener.getEvent().getCommonEventHeader().getEventName();\r
85 \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
90 \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
94 \r
95         // Get Json Values for Policy Fields\r
96         final Map<String, List<Long>> messageFieldValuesMap = TCAUtils.getJsonPathValue(cefMessage, policyFieldPaths);\r
97 \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
108                 }\r
109             }\r
110         }\r
111 \r
112         // No threshold were violated\r
113         if (violatedThresholdsMap.isEmpty()) {\r
114 \r
115             final String terminationMessage = "No Policy Threshold violated by the VES CEF Message.";\r
116             setTerminatingProcessingMessage(terminationMessage, processorContext);\r
117 \r
118         } else {\r
119 \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
127 \r
128             final String finishMessage = String.format("Policy Threshold violation detected for threshold: %s",\r
129                     maxSeverityThresholdViolation);\r
130             setFinishedProcessingMessage(finishMessage, processorContext);\r
131 \r
132         }\r
133 \r
134         return processorContext;\r
135     }\r
136 }\r