summaryrefslogtreecommitdiffstats
path: root/dcae-analytics/dcae-analytics-tca-core/src/main/java/org/onap/dcae/analytics/tca/core/util/function/calculation/TcaEventNameFilter.java
blob: e5f501e567b46720677b8211a41075cede833848 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * ==========LICENSE_START=========================================================
 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
 * Copyright (c) 2022 Wipro Limited Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 */

package org.onap.dcae.analytics.tca.core.util.function.calculation;

import java.util.Optional;
import java.util.Set;
import java.util.List;
import java.util.stream.Collectors;

import org.onap.dcae.analytics.model.cef.CommonEventHeader;
import org.onap.dcae.analytics.model.cef.Event;
import org.onap.dcae.analytics.model.cef.EventListener;
import org.onap.dcae.analytics.tca.core.service.TcaExecutionContext;
import org.onap.dcae.analytics.tca.model.policy.MetricsPerEventName;
import org.onap.dcae.analytics.tca.model.policy.TcaPolicy;

/**
 * @author Rajiv Singla
 */
public class TcaEventNameFilter implements TcaCalculationFunction {

    @Override
    public TcaExecutionContext calculate(final TcaExecutionContext tcaExecutionContext) {

        final String cefMessage = tcaExecutionContext.getCefMessage();
        final EventListener eventListener = tcaExecutionContext.getTcaProcessingContext().getEventListener();

        final Optional<String> eventNameOptional = Optional.ofNullable(eventListener)
                .map(EventListener::getEvent)
                .map(Event::getCommonEventHeader)
                .map(CommonEventHeader::getEventName);

        // Check Event name is present
        if (!eventNameOptional.isPresent()) {
            final String errorMessage =
                    "Event -> Common Event Header -> Event Name not present. Invalid CEF Message: " + cefMessage;
            setTerminatingMessage(errorMessage, tcaExecutionContext, true);
            return tcaExecutionContext;
        }

        // Get CEF Message Event name and Event names in tca policy
        final String cefMessageEventName = eventNameOptional.get();
        final List<TcaPolicy> tcaPolList = tcaExecutionContext.getTcaPolicy();
        int count = 0;
        int size = tcaPolList.size();
        for( TcaPolicy tcaPol : tcaPolList){
            final Set<String> policyEventNames = tcaPol.getMetricsPerEventName().stream()
                .map(MetricsPerEventName::getEventName).collect(Collectors.toSet());
            // Check CEF Message Event name matches any Policy Event names
            if (!policyEventNames.contains(cefMessageEventName)) {
                    count++;
                    if(count >= size) {
                         final String earlyTerminationMessage = String.format(
                            "CEF Message Event name does not match any Policy Event Names. " +
                            "Message EventName: %s, Policy Event Names: %s", cefMessageEventName, policyEventNames);
                         setTerminatingMessage(earlyTerminationMessage, tcaExecutionContext, false);
                         return tcaExecutionContext;
                    }
            }	    
        }

        // CEF Messages one of the the Policy Event names
        // do nothing

        return tcaExecutionContext;
    }
}