Update snapshot and/or references of policy/models to latest snapshots
[policy/models.git] / models-interactions / model-simulators / src / main / java / org / onap / policy / simulators / TopicServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 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.simulators;
22
23 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
24 import org.onap.policy.common.endpoints.event.comm.TopicListener;
25 import org.onap.policy.common.endpoints.event.comm.TopicSink;
26 import org.onap.policy.common.endpoints.event.comm.TopicSource;
27 import org.onap.policy.common.utils.coder.Coder;
28 import org.onap.policy.common.utils.coder.CoderException;
29
30 /**
31  * Server whose requests are received from a topic, and whose responses are sent to a
32  * topic.
33  */
34 public abstract class TopicServer<Q> implements TopicListener {
35     private final TopicSink sink;
36     private final TopicSource source;
37     private final Coder coder;
38     private final Class<Q> reqClass;
39
40     /**
41      * Constructs the object.
42      *
43      * @param sink sink to which responses should be published
44      * @param source source from which requests arrive
45      */
46     protected TopicServer(TopicSink sink, TopicSource source, Coder coder, Class<Q> reqClass) {
47         this.sink = sink;
48         this.source = source;
49         this.coder = coder;
50         this.reqClass = reqClass;
51
52         source.register(this);
53     }
54
55     public void shutdown() {
56         source.unregister(this);
57     }
58
59     @Override
60     public void onTopicEvent(CommInfrastructure commType, String topic, String request) {
61         Q req;
62         try {
63             req = coder.decode(request, reqClass);
64         } catch (CoderException e) {
65             throw new IllegalArgumentException("cannot decode request from " + source.getTopic());
66         }
67
68         String resp = process(req);
69         if (resp != null) {
70             sink.send(resp);
71         }
72     }
73
74     /**
75      * Processes a request.
76      *
77      * @param request request to be processed
78      * @return the response, or {@code null} if no response is to be sent
79      */
80     protected abstract String process(Q request);
81 }