c5427db6bd9e9e96e70ed3faa7222c330759d321
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.clamp.acm.participant.intermediary.comm;
25
26 import java.util.function.Consumer;
27 import org.onap.policy.clamp.acm.participant.intermediary.handler.Listener;
28 import org.onap.policy.clamp.acm.participant.intermediary.handler.ParticipantHandler;
29 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantAckMessage;
30 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
31 import org.onap.policy.common.endpoints.listeners.ScoListener;
32 import org.onap.policy.common.utils.coder.StandardCoderObject;
33
34 /**
35  * Abstract Listener for Participant Ack messages sent by runtime.
36  */
37 public abstract class ParticipantAckListener<T extends ParticipantAckMessage> extends ScoListener<T>
38                 implements Listener<T> {
39
40     private final ParticipantHandler participantHandler;
41     private final Consumer<T> consumer;
42
43     /**
44      * Constructs the object.
45      *
46      * @param clazz class of message this handles
47      * @param participantHandler ParticipantHandler
48      * @param consumer function that handles the message
49      */
50     protected ParticipantAckListener(Class<T> clazz, ParticipantHandler participantHandler, Consumer<T> consumer) {
51         super(clazz);
52         this.participantHandler = participantHandler;
53         this.consumer = consumer;
54     }
55
56     @Override
57     public void onTopicEvent(CommInfrastructure infra, String topic, StandardCoderObject sco, T message) {
58         if (participantHandler.appliesTo(message)) {
59             consumer.accept(message);
60         }
61     }
62
63     @Override
64     public ScoListener<T> getScoListener() {
65         return this;
66     }
67 }