cd29ed177822958670cbcb550191c6d29a50023a
[dcaegen2/analytics/tca.git] / dcae-analytics-tca / src / main / java / org / openecomp / dcae / apod / analytics / tca / processor / TCACEFPolicyDomainFilter.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.openecomp.dcae.apod.analytics.tca.processor;
22
23 import org.openecomp.dcae.apod.analytics.model.domain.cef.Domain;
24 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener;
25
26 /**
27  * <p>
28  *     TCA Processor which acts like a filter to filter out messages which does not belong to TCA Policy Domain
29  *     <br>
30  *     Pre Conditions: CEF Event Listener must be present
31  * </p>
32  *
33  * @author Rajiv Singla . Creation Date: 11/7/2016.
34  */
35 public class TCACEFPolicyDomainFilter extends AbstractTCAECEFPolicyProcessor {
36
37
38     private static final long serialVersionUID = 1L;
39
40     @Override
41     public String getProcessorDescription() {
42         return "Filters out CEF Messages which does not match TCAPolicy Domain";
43     }
44
45     @Override
46     public TCACEFProcessorContext processMessage(TCACEFProcessorContext processorContext) {
47
48         // Safe to get event Listener here without null check as pre processor will validate if
49         // event listener is indeed present
50         final EventListener eventListener = processorContext.getCEFEventListener();
51
52         Domain cefMessageDomain;
53
54         // Extract CEF domain as it is must be present as per CEF Schema
55         if (eventListener.getEvent() != null &&
56                 eventListener.getEvent().getCommonEventHeader() != null &&
57                 eventListener.getEvent().getCommonEventHeader().getDomain() != null) {
58             cefMessageDomain = eventListener.getEvent().getCommonEventHeader().getDomain();
59
60         } else {
61             final String terminatingMessage = "Invalid CEF Message.Common Event Header Domain not present.";
62             setTerminatingProcessingMessage(terminatingMessage, processorContext);
63             return processorContext;
64         }
65
66         // Get Policy Domain. TCA Policy Validation must ensure that Domain is indeed present
67         // no null check will be required here
68         final String policyDomain = processorContext.getTCAPolicy().getDomain();
69
70         // If Policy domain matches CEF message domain then continue processing
71         if (cefMessageDomain.toString().equalsIgnoreCase(policyDomain)) {
72             final String finishMessage = String.format("Policy Domain and CEF Message Domain match successful." +
73                     " Message Domain: %s, Policy Domain: %s", cefMessageDomain, policyDomain);
74             setFinishedProcessingMessage(finishMessage, processorContext);
75         } else {
76             // If policy domain does not match with CEF message terminate processing chain
77             final String terminatingMessage = String.format("Policy Domain and CEF Message Domain match unsuccessful." +
78                     " Message Domain: %s, Policy Domain: %s", cefMessageDomain, policyDomain);
79             setTerminatingProcessingMessage(terminatingMessage, processorContext);
80         }
81
82         return processorContext;
83     }
84 }