3521e9f555a51b7452f038612815c1f286e69eab
[policy/drools-applications.git] / controlloop / common / feature-controlloop-trans / src / main / java / org / onap / policy / drools / server / restful / RestTransactionTracker.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.drools.server.restful;
22
23 import io.swagger.annotations.Api;
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiParam;
26 import java.util.UUID;
27 import javax.ws.rs.Consumes;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.PUT;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.PathParam;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35 import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
36 import org.onap.policy.controlloop.VirtualControlLoopNotification;
37 import org.onap.policy.drools.apps.controlloop.feature.trans.ControlLoopMetricsManager;
38
39 /**
40  * REST Transaction Tracker.
41  */
42
43 @Path("/policy/pdp/engine/controllers/transactions")
44 @Produces({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML})
45 @Consumes({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML})
46 @Api
47 public class RestTransactionTracker {
48
49     /**
50      * GET transactions.
51      */
52
53     @GET
54     @Path("inprogress")
55     @ApiOperation(value = "Retrieve in-progress transactions", responseContainer = "List")
56     public Response transactions() {
57         return Response.status(Response.Status.OK)
58                        .entity(ControlLoopMetricsManager.getManager().getTransactions()).build();
59     }
60
61     /**
62      * GET one transaction.
63      */
64
65     @GET
66     @Path("inprogress/{transactionId}")
67     @ApiOperation(value = "Retrieve an in-progress transaction", response = VirtualControlLoopNotification.class)
68     public Response transactionId(
69           @ApiParam(value = "UUID", required = true) @PathParam("transactionId") String transactionId) {
70         VirtualControlLoopNotification notification =
71                 ControlLoopMetricsManager.getManager().getTransaction(UUID.fromString(transactionId));
72         return Response.status((notification != null) ? Response.Status.OK : Response.Status.NOT_FOUND)
73                        .entity(notification).build();
74     }
75
76     /**
77      * Resets the cache with a new cache size.
78      */
79
80     @PUT
81     @Path("cacheSize/{cacheSize}")
82     @ApiOperation(value = "Sets the cache size", response = Integer.class)
83     public Response cacheSize(
84             @ApiParam(value = "cache size", required = true) @PathParam("cacheSize") int cacheSize) {
85         ControlLoopMetricsManager.getManager().resetCache(cacheSize,
86                 ControlLoopMetricsManager.getManager().getTransactionTimeout());
87         return Response.status(Response.Status.OK)
88                        .entity(ControlLoopMetricsManager.getManager().getCacheSize()).build();
89     }
90
91     /**
92      * GET the cache size.
93      */
94
95     @GET
96     @Path("cacheSize")
97     @ApiOperation(value = "Gets the cache size", response = Integer.class)
98     public Response cacheSize() {
99         return Response.status(Response.Status.OK)
100                        .entity(ControlLoopMetricsManager.getManager().getCacheSize()).build();
101     }
102
103     /**
104      * Resets the cache with a new transaction timeout in seconds.
105      */
106
107     @PUT
108     @Path("timeout/{timeoutSecs}")
109     @ApiOperation(value = "Sets the timeout in seconds", response = Integer.class)
110     public Response timeout(
111             @ApiParam(value = "timeout", required = true) @PathParam("timeoutSecs") long timeoutSecs) {
112         ControlLoopMetricsManager.getManager().resetCache(
113                 ControlLoopMetricsManager.getManager().getCacheSize(), timeoutSecs);
114         return Response.status(Response.Status.OK)
115                        .entity(ControlLoopMetricsManager.getManager().getTransactionTimeout()).build();
116     }
117
118     /**
119      * GET the cache timeout.
120      */
121
122     @GET
123     @Path("timeout")
124     @ApiOperation(value = "Gets the cache timeout", response = Long.class)
125     public Response timeout() {
126         return Response.status(Response.Status.OK)
127                        .entity(ControlLoopMetricsManager.getManager().getTransactionTimeout()).build();
128     }
129 }