aboutsummaryrefslogtreecommitdiffstats
path: root/feature-simulators/src/main/java/org
diff options
context:
space:
mode:
authorCharles Cole <cc847m@att.com>2017-12-14 08:59:26 -0600
committerCharles Cole <cc847m@att.com>2017-12-14 14:58:16 -0600
commitc8e0ec698ab687e81654b7cde3ce9ae4118de87e (patch)
treefc4f437523ecefc8fe961328f978c102964f2867 /feature-simulators/src/main/java/org
parent4884099eff44975eee57e6748823ff73f965e332 (diff)
Add a DMaaP simulator
Added a DMaaP simulator for testing applications. One current limitation is that the simulator does not support multiple "subscribers" on the same topic; if someone gets a message, that message cannot be subsequently retrieved by anyone else. The simulator has also not been tested for concurrent getting and posting. Also added a way to set the response code DMaaP would return for a get to test that policy can gracefully handle errors. It may need some work to become truely its own "feature" Issue-ID: POLICY-489 Change-Id: I524981bdf5e4e825f13e6197dda11d9498e4f4bf Signed-off-by: Charles Cole <cc847m@att.com>
Diffstat (limited to 'feature-simulators/src/main/java/org')
-rw-r--r--feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java123
1 files changed, 123 insertions, 0 deletions
diff --git a/feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java b/feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java
new file mode 100644
index 00000000..bdabc6ee
--- /dev/null
+++ b/feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java
@@ -0,0 +1,123 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * feature-simulators
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.drools.simulators;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.BlockingQueue;
+
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DefaultValue;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Path("/events")
+public class DMaaPSimulatorJaxRs {
+
+ private static final Map<String, BlockingQueue<String>> queues = new ConcurrentHashMap<>();
+ private static final Logger logger = LoggerFactory.getLogger(DMaaPSimulatorJaxRs.class);
+ private static int responseCode = 200;
+
+ @GET
+ @Path("/{topicName}/{consumeGroup}/{consumerId}")
+ public String subscribe(@DefaultValue("0") @QueryParam("timeout") int timeout, @PathParam("topicName") String topicName,
+ @Context final HttpServletResponse httpResponse) {
+ int currentRespCode = responseCode;
+ httpResponse.setStatus(currentRespCode);
+ try {
+ httpResponse.flushBuffer();
+ } catch (IOException e) {
+ final Logger logger = LoggerFactory.getLogger(DMaaPSimulatorJaxRs.class);
+ logger.error("flushBuffer threw: ", e);
+ return "Got an error";
+ }
+
+ if (currentRespCode < 200 || currentRespCode >= 300)
+ {
+ return "You got response code: " + currentRespCode;
+ }
+ if (queues.containsKey(topicName)) {
+ BlockingQueue<String> queue = queues.get(topicName);
+ String response = "No Data";
+ try {
+ response = queue.poll(timeout, TimeUnit.MILLISECONDS);
+ } catch (InterruptedException e) {
+ logger.debug("error in DMaaP simulator", e);
+ }
+ if (response == null) {
+ response = "No Data";
+ }
+ return response;
+ }
+ else if (timeout > 0) {
+ try {
+ Thread.sleep(timeout);
+ if (queues.containsKey(topicName)) {
+ BlockingQueue<String> queue = queues.get(topicName);
+ String response = queue.poll();
+ if (response == null) {
+ response = "No Data";
+ }
+ return response;
+ }
+ } catch (InterruptedException e) {
+ logger.debug("error in DMaaP simulator", e);
+ }
+ }
+ return "No topic";
+ }
+
+ @POST
+ @Path("/{topicName}")
+ @Consumes(MediaType.TEXT_PLAIN)
+ public String publish(@PathParam("topicName") String topicName, String body) {
+ if (queues.containsKey(topicName)) {
+ BlockingQueue<String> queue = queues.get(topicName);
+ queue.offer(body);
+ }
+ else {
+ BlockingQueue<String> queue = new LinkedBlockingQueue<>();
+ queue.offer(body);
+ queues.put(topicName, queue);
+ }
+
+ return "";
+ }
+
+ @POST
+ @Path("/setStatus")
+ public String setStatus(@QueryParam("statusCode") int statusCode) {
+ responseCode = statusCode;
+ return "Status code set";
+ }
+}