2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.simulators;
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;
31 * Server whose requests are received from a topic, and whose responses are sent to a
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;
41 * Constructs the object.
43 * @param sink sink to which responses should be published
44 * @param source source from which requests arrive
46 protected TopicServer(TopicSink sink, TopicSource source, Coder coder, Class<Q> reqClass) {
50 this.reqClass = reqClass;
52 source.register(this);
55 public void shutdown() {
56 source.unregister(this);
60 public void onTopicEvent(CommInfrastructure commType, String topic, String request) {
63 req = coder.decode(request, reqClass);
64 } catch (CoderException e) {
65 throw new IllegalArgumentException("cannot decode request from " + source.getTopic());
68 String resp = process(req);
75 * Processes a request.
77 * @param request request to be processed
78 * @return the response, or {@code null} if no response is to be sent
80 protected abstract String process(Q request);