Standalone TCA with EELF Logger
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-tca-core / src / main / java / org / onap / dcae / analytics / tca / core / util / function / calculation / TcaDomainFilter.java
1 /*
2  * ================================================================================
3  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  *
18  */
19
20 package org.onap.dcae.analytics.tca.core.util.function.calculation;
21
22 import java.util.Optional;
23
24 import org.onap.dcae.analytics.model.cef.CommonEventHeader;
25 import org.onap.dcae.analytics.model.cef.Domain;
26 import org.onap.dcae.analytics.model.cef.Event;
27 import org.onap.dcae.analytics.model.cef.EventListener;
28 import org.onap.dcae.analytics.tca.core.service.TcaExecutionContext;
29 import org.onap.dcae.analytics.tca.core.service.TcaProcessingContext;
30
31 /**
32  * @author Rajiv Singla
33  */
34 public class TcaDomainFilter implements TcaCalculationFunction {
35
36     @Override
37     public TcaExecutionContext calculate(final TcaExecutionContext tcaExecutionContext) {
38
39         final TcaProcessingContext tcaProcessingContext = tcaExecutionContext.getTcaProcessingContext();
40         final String cefMessage = tcaExecutionContext.getCefMessage();
41
42         // Extract CEF domain as it is must be present as per CEF Schema
43         final Optional<Domain> domainOptional = Optional.ofNullable(tcaProcessingContext.getEventListener())
44                 .map(EventListener::getEvent)
45                 .map(Event::getCommonEventHeader)
46                 .map(CommonEventHeader::getDomain);
47
48         // Check domain is present
49         if (!domainOptional.isPresent()) {
50             final String errorMessage =
51                     "Event -> Common Event Header -> Domain not present. Invalid CEF Message: " + cefMessage;
52             setTerminatingMessage(errorMessage, tcaExecutionContext, true);
53             return tcaExecutionContext;
54         }
55
56         // Get Policy and CEF Message Domain
57         final String policyDomain = tcaExecutionContext.getTcaPolicy().getDomain();
58         final String cefMessageDomain = domainOptional.get().name();
59
60         // Check Policy domain matches CEF message domain
61         if (!policyDomain.equalsIgnoreCase(cefMessageDomain)) {
62             final String earlyTerminationMessage = String.format(
63                     "Policy Domain does not match CEF Message Domain. Policy Domain: %s, CEF  Message Domain: %s",
64                     policyDomain, cefMessageDomain);
65             setTerminatingMessage(earlyTerminationMessage, tcaExecutionContext, false);
66             return tcaExecutionContext;
67         }
68
69
70         // Policy Domain and CEF Message Domain match successful
71         // do nothing
72
73         return tcaExecutionContext;
74     }
75 }