Support 7.2.1 VES in TCAGEN2
[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  * ===========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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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=========================================================
18  *
19  */
20
21 package org.onap.dcae.analytics.tca.core.util.function.calculation;
22
23 import java.util.List;
24 import java.util.Optional;
25
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;
33
34 /**
35  * @author Rajiv Singla
36  */
37 public class TcaDomainFilter implements TcaCalculationFunction {
38
39     @Override
40     public TcaExecutionContext calculate(final TcaExecutionContext tcaExecutionContext) {
41
42         final TcaProcessingContext tcaProcessingContext = tcaExecutionContext.getTcaProcessingContext();
43         final String cefMessage = tcaExecutionContext.getCefMessage();
44
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);
50
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;
57         }
58
59         // Get Policy and CEF Message Domain
60         final List<TcaPolicy> tcaPolList = tcaExecutionContext.getTcaPolicy();
61         final String cefMessageDomain = domainOptional.get().name();
62          
63         for( TcaPolicy tcaPol : tcaPolList){    
64             String policyDomain = tcaPol.getDomain();
65             int size = tcaPolList.size();
66             int count = 0;
67             // Check Policy domain matches CEF message domain
68             if (!policyDomain.equalsIgnoreCase(cefMessageDomain)) {
69                 count++;
70                 if (count >= size){
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;
76                  }
77             }
78         }
79         // Policy Domain and CEF Message Domain match successful
80         // do nothing
81
82         return tcaExecutionContext;
83     }
84 }