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 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;
 
  32  * @author Rajiv Singla
 
  34 public class TcaDomainFilter implements TcaCalculationFunction {
 
  37     public TcaExecutionContext calculate(final TcaExecutionContext tcaExecutionContext) {
 
  39         final TcaProcessingContext tcaProcessingContext = tcaExecutionContext.getTcaProcessingContext();
 
  40         final String cefMessage = tcaExecutionContext.getCefMessage();
 
  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);
 
  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;
 
  56         // Get Policy and CEF Message Domain
 
  57         final String policyDomain = tcaExecutionContext.getTcaPolicy().getDomain();
 
  58         final String cefMessageDomain = domainOptional.get().name();
 
  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;
 
  70         // Policy Domain and CEF Message Domain match successful
 
  73         return tcaExecutionContext;