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
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.async;
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;
33 * Record filter strategies, which helps to filter the consumer records based on some conditions.
38 public class RecordFilterStrategies {
40 private static final boolean EXCLUDE_EVENT = true;
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.
49 public RecordFilterStrategy<String, CloudEvent> includeDataOperationEventsOnly() {
50 return consumerRecord ->
51 isNotCloudEventOfType(consumerRecord.headers(), "DataOperationEvent");
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.
61 public RecordFilterStrategy<String, Serializable> includeNonCloudEventsOnly() {
62 return consumerRecord -> isCloudEvent(consumerRecord.headers());
65 private boolean isCloudEvent(final Headers headers) {
66 return headers.lastHeader("ce_type") != null;
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)");
75 return !(eventTypeHeaderValue.contains(requiredEventType));