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.Optional;
25 import java.util.List;
26 import java.util.stream.Collectors;
28 import org.onap.dcae.analytics.model.cef.CommonEventHeader;
29 import org.onap.dcae.analytics.model.cef.Event;
30 import org.onap.dcae.analytics.model.cef.EventListener;
31 import org.onap.dcae.analytics.tca.core.service.TcaExecutionContext;
32 import org.onap.dcae.analytics.tca.model.policy.MetricsPerEventName;
33 import org.onap.dcae.analytics.tca.model.policy.TcaPolicy;
36 * @author Rajiv Singla
38 public class TcaEventNameFilter implements TcaCalculationFunction {
41 public TcaExecutionContext calculate(final TcaExecutionContext tcaExecutionContext) {
43 final String cefMessage = tcaExecutionContext.getCefMessage();
44 final EventListener eventListener = tcaExecutionContext.getTcaProcessingContext().getEventListener();
46 final Optional<String> eventNameOptional = Optional.ofNullable(eventListener)
47 .map(EventListener::getEvent)
48 .map(Event::getCommonEventHeader)
49 .map(CommonEventHeader::getEventName);
51 // Check Event name is present
52 if (!eventNameOptional.isPresent()) {
53 final String errorMessage =
54 "Event -> Common Event Header -> Event Name not present. Invalid CEF Message: " + cefMessage;
55 setTerminatingMessage(errorMessage, tcaExecutionContext, true);
56 return tcaExecutionContext;
59 // Get CEF Message Event name and Event names in tca policy
60 final String cefMessageEventName = eventNameOptional.get();
61 final List<TcaPolicy> tcaPolList = tcaExecutionContext.getTcaPolicy();
63 int size = tcaPolList.size();
64 for( TcaPolicy tcaPol : tcaPolList){
65 final Set<String> policyEventNames = tcaPol.getMetricsPerEventName().stream()
66 .map(MetricsPerEventName::getEventName).collect(Collectors.toSet());
67 // Check CEF Message Event name matches any Policy Event names
68 if (!policyEventNames.contains(cefMessageEventName)) {
71 final String earlyTerminationMessage = String.format(
72 "CEF Message Event name does not match any Policy Event Names. " +
73 "Message EventName: %s, Policy Event Names: %s", cefMessageEventName, policyEventNames);
74 setTerminatingMessage(earlyTerminationMessage, tcaExecutionContext, false);
75 return tcaExecutionContext;
80 // CEF Messages one of the the Policy Event names
83 return tcaExecutionContext;