Use BidirectionalTopicClient from policy-common
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / topic / BidirectionalTopicHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actorserviceprovider.topic;
22
23 import java.util.List;
24 import org.onap.policy.common.endpoints.event.comm.client.BidirectionalTopicClient;
25 import org.onap.policy.common.endpoints.event.comm.client.BidirectionalTopicClientException;
26
27 /**
28  * Handler for a bidirectional topic, supporting both publishing and forwarding of
29  * incoming messages.
30  */
31 public class BidirectionalTopicHandler extends BidirectionalTopicClient {
32
33     /**
34      * Listener that will be attached to the topic to receive responses.
35      */
36     private final TopicListenerImpl listener = new TopicListenerImpl();
37
38
39     /**
40      * Constructs the object.
41      *
42      * @param sinkTopic sink topic name
43      * @param sourceTopic source topic name
44      * @throws BidirectionalTopicClientException if an error occurs
45      */
46     public BidirectionalTopicHandler(String sinkTopic, String sourceTopic) throws BidirectionalTopicClientException {
47         super(sinkTopic, sourceTopic);
48     }
49
50     /**
51      * Starts listening on the source topic(s).
52      */
53     public void start() {
54         getSource().register(listener);
55     }
56
57     /**
58      * Stops listening on the source topic(s).
59      */
60     public void stop() {
61         getSource().unregister(listener);
62     }
63
64     /**
65      * Stops listening on the source topic(s) and clears all of the forwarders.
66      */
67     public void shutdown() {
68         stop();
69         listener.shutdown();
70     }
71
72     public Forwarder addForwarder(SelectorKey... keys) {
73         return listener.addForwarder(keys);
74     }
75
76     public Forwarder addForwarder(List<SelectorKey> keys) {
77         return listener.addForwarder(keys);
78     }
79 }