Updating the Kafka listener compliance to could events and legacy
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / async / RecordFilterStrategies.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2023 Nordix Foundation
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
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
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  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.async;
22
23 import io.cloudevents.CloudEvent;
24 import io.cloudevents.kafka.impl.KafkaHeaders;
25 import java.io.Serializable;
26 import lombok.extern.slf4j.Slf4j;
27 import org.apache.kafka.common.header.Headers;
28 import org.springframework.context.annotation.Bean;
29 import org.springframework.context.annotation.Configuration;
30 import org.springframework.kafka.listener.adapter.RecordFilterStrategy;
31
32 /**
33  * Record filter strategies, which helps to filter the consumer records based on some conditions.
34  *
35  */
36 @Configuration
37 @Slf4j
38 public class RecordFilterStrategies {
39
40     private static final boolean EXCLUDE_EVENT = true;
41
42     /**
43      *  Include only DataOperation events based on the cloud event type header, It
44      *  returns boolean, true means exclude the consumer record and false
45      *  means include the consumer record.
46      * @return boolean value.
47      */
48     @Bean
49     public RecordFilterStrategy<String, CloudEvent> includeDataOperationEventsOnly() {
50         return consumerRecord ->
51                 isNotCloudEventOfType(consumerRecord.headers(), "DataOperationEvent");
52     }
53
54     /**
55      *  Includes the consumer records based on the cloud event type header, It  returns boolean,
56      *  true means exclude the consumer record and false means include the consumer record.
57      *  It includes only the legacy events i.e. non-cloud events
58      * @return boolean value.
59      */
60     @Bean
61     public RecordFilterStrategy<String, Serializable> includeNonCloudEventsOnly() {
62         return consumerRecord -> isCloudEvent(consumerRecord.headers());
63     }
64
65     private boolean isCloudEvent(final Headers headers) {
66         return headers.lastHeader("ce_type") != null;
67     }
68
69     private boolean isNotCloudEventOfType(final Headers headers, final String requiredEventType) {
70         final String eventTypeHeaderValue = KafkaHeaders.getParsedKafkaHeader(headers, "ce_type");
71         if (eventTypeHeaderValue == null) {
72             log.trace("No ce_type header found, possibly a legacy event (ignored)");
73             return EXCLUDE_EVENT;
74         }
75         return !(eventTypeHeaderValue.contains(requiredEventType));
76     }
77 }