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