Merge "Flesh out DMaaP simulator"
[policy/models.git] / models-sim / models-sim-dmaap / src / main / java / org / onap / policy / models / sim / dmaap / rest / DmaapSimRestControllerV1.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.sim.dmaap.rest;
23
24 import javax.ws.rs.Consumes;
25 import javax.ws.rs.DefaultValue;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.PathParam;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.QueryParam;
32 import javax.ws.rs.core.Response;
33 import org.onap.policy.models.sim.dmaap.provider.DmaapSimProvider;
34
35 /**
36  * Class to provide REST endpoints for DMaaP simulator component statistics.
37  */
38 @Path("/events")
39 @Produces(DmaapSimRestControllerV1.MEDIA_TYPE_APPLICATION_JSON)
40 public class DmaapSimRestControllerV1 extends BaseRestControllerV1 {
41     public static final String MEDIA_TYPE_APPLICATION_JSON = "application/json";
42
43     /**
44      * Get a DMaaP message.
45      *
46      * @param topicName topic to get message from
47      * @param consumerGroup consumer group that is getting the message
48      * @param consumerId consumer ID that is getting the message
49      * @param timeoutMs timeout for the message
50      * @return the message
51      */
52     @GET
53     @Path("{topicName}/{consumerGroup}/{consumerId}")
54     public Response getDmaapMessage(@PathParam("topicName") final String topicName,
55                     @PathParam("consumerGroup") final String consumerGroup,
56                     @PathParam("consumerId") final String consumerId,
57                     @QueryParam("limit") @DefaultValue("1") final int limit,
58                     @QueryParam("timeout") @DefaultValue("15000") final long timeoutMs) {
59
60         return DmaapSimProvider.getInstance().processDmaapMessageGet(topicName, consumerGroup, consumerId, limit,
61                         timeoutMs);
62     }
63
64     /**
65      * Post a DMaaP message.
66      *
67      * @param topicName topic to get message from
68      * @return the response to the post
69      */
70     @POST
71     @Path("{topicName}")
72     @Consumes(value = {CambriaMessageBodyHandler.MEDIA_TYPE_APPLICATION_CAMBRIA,
73                     TextMessageBodyHandler.MEDIA_TYPE_TEXT_PLAIN, MEDIA_TYPE_APPLICATION_JSON})
74     public Response postDmaapMessage(@PathParam("topicName") final String topicName, final Object dmaapMessage) {
75
76         return DmaapSimProvider.getInstance().processDmaapMessagePut(topicName, dmaapMessage);
77     }
78 }