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