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
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  20 package org.onap.dcae.analytics.tca.core.util.function.calculation;
 
  22 import java.util.Optional;
 
  24 import java.util.stream.Collectors;
 
  26 import org.onap.dcae.analytics.model.cef.CommonEventHeader;
 
  27 import org.onap.dcae.analytics.model.cef.Event;
 
  28 import org.onap.dcae.analytics.model.cef.EventListener;
 
  29 import org.onap.dcae.analytics.tca.core.service.TcaExecutionContext;
 
  30 import org.onap.dcae.analytics.tca.model.policy.MetricsPerEventName;
 
  31 import org.onap.dcae.analytics.tca.model.policy.TcaPolicy;
 
  34  * @author Rajiv Singla
 
  36 public class TcaEventNameFilter implements TcaCalculationFunction {
 
  39     public TcaExecutionContext calculate(final TcaExecutionContext tcaExecutionContext) {
 
  41         final String cefMessage = tcaExecutionContext.getCefMessage();
 
  42         final EventListener eventListener = tcaExecutionContext.getTcaProcessingContext().getEventListener();
 
  44         final Optional<String> eventNameOptional = Optional.ofNullable(eventListener)
 
  45                 .map(EventListener::getEvent)
 
  46                 .map(Event::getCommonEventHeader)
 
  47                 .map(CommonEventHeader::getEventName);
 
  49         // Check Event name is present
 
  50         if (!eventNameOptional.isPresent()) {
 
  51             final String errorMessage =
 
  52                     "Event -> Common Event Header -> Event Name not present. Invalid CEF Message: " + cefMessage;
 
  53             setTerminatingMessage(errorMessage, tcaExecutionContext, true);
 
  54             return tcaExecutionContext;
 
  57         // Get CEF Message Event name and Event names in tca policy
 
  58         final String cefMessageEventName = eventNameOptional.get();
 
  59         final TcaPolicy tcaPolicy = tcaExecutionContext.getTcaPolicy();
 
  60         final Set<String> policyEventNames = tcaPolicy.getMetricsPerEventName().stream()
 
  61                 .map(MetricsPerEventName::getEventName).collect(Collectors.toSet());
 
  63         // Check CEF Message Event name matches any Policy Event names
 
  64         if (!policyEventNames.contains(cefMessageEventName)) {
 
  65             final String earlyTerminationMessage = String.format(
 
  66                     "CEF Message Event name does not match any Policy Event Names. " +
 
  67                             "Message EventName: %s, Policy Event Names: %s", cefMessageEventName, policyEventNames);
 
  68             setTerminatingMessage(earlyTerminationMessage, tcaExecutionContext, false);
 
  69             return tcaExecutionContext;
 
  72         // CEF Messages one of the the Policy Event names
 
  75         return tcaExecutionContext;