Fix op policies distribution to controllers
[policy/drools-pdp.git] / feature-lifecycle / src / main / java / org / onap / policy / drools / server / restful / RestLifecycleManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.policy.drools.server.restful;
20
21 import io.swagger.annotations.Api;
22 import io.swagger.annotations.ApiOperation;
23 import io.swagger.annotations.ApiParam;
24 import java.util.Properties;
25 import javax.ws.rs.Consumes;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.PUT;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.PathParam;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.Response;
33 import org.onap.policy.common.endpoints.event.comm.TopicSink;
34 import org.onap.policy.common.endpoints.event.comm.TopicSource;
35 import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
36 import org.onap.policy.drools.lifecycle.LifecycleFeature;
37 import org.onap.policy.drools.lifecycle.PolicyTypeController;
38 import org.onap.policy.models.pdp.concepts.PdpStateChange;
39 import org.onap.policy.models.pdp.enums.PdpState;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
42
43 /**
44  * REST Lifecycle Manager.
45  */
46
47 @Path("/policy/pdp/engine/lifecycle")
48 @Produces({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML})
49 @Consumes({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML})
50 @Api
51 public class RestLifecycleManager {
52
53     /**
54      * GET group.
55      */
56
57     @GET
58     @Path("group")
59     @ApiOperation(value = "Retrieves the Lifecycle group",
60         notes = "Lifecycle Group", response = String.class)
61     public Response group() {
62         return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.getGroup()).build();
63     }
64
65     /**
66      * PUT group.
67      */
68
69     @PUT
70     @Path("group/{group}")
71     @ApiOperation(value = "Updates the Lifecycle group",
72             notes = "Lifecycle Group", response = String.class)
73     public Response updateGroup(
74         @ApiParam(value = "Group", required = true) @PathParam("group") String group) {
75         LifecycleFeature.fsm.setGroup(group);
76         return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.getGroup()).build();
77     }
78
79     /**
80      * GET subgroup.
81      */
82
83     @GET
84     @Path("subgroup")
85     @ApiOperation(value = "Retrieves the Lifecycle subgroup",
86             notes = "Lifecycle Subgroup", response = String.class)
87     public Response subgroup() {
88         return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.getSubgroup()).build();
89     }
90
91     /**
92      * PUT subgroup.
93      */
94
95     @PUT
96     @Path("subgroup/{subgroup}")
97     @ApiOperation(value = "Retrieves the Lifecycle subgroup",
98             notes = "Lifecycle Subgroup", response = String.class)
99     public Response subgroup(
100         @ApiParam(value = "Subgroup", required = true) @PathParam("subgroup") String subgroup) {
101         LifecycleFeature.fsm.setSubgroup(subgroup);
102         return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.getSubgroup()).build();
103     }
104
105     /**
106      * GET properties.
107      */
108
109     @GET
110     @Path("properties")
111     @ApiOperation(value = "Retrieves the Lifecycle properties",
112             notes = "Lifecycle Properties", response = Properties.class)
113     public Response properties() {
114         return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.getProperties()).build();
115     }
116
117     /**
118      * GET state.
119      */
120
121     @GET
122     @Path("state")
123     @ApiOperation(value = "Retrieves the Lifecycle state", notes = "Lifecycle State", response = PdpState.class)
124     public Response state() {
125         return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.state()).build();
126     }
127
128     /**
129      * PUT state.
130      */
131
132     @PUT
133     @Path("state/{state}")
134     @ApiOperation(value = "updates the Lifecycle state", notes = "Lifecycle State", response = Boolean.class)
135     public Response updateState(
136         @ApiParam(value = "state", required = true) @PathParam("state") String state) {
137
138         PdpStateChange change = new PdpStateChange();
139         change.setPdpGroup(LifecycleFeature.fsm.getGroup());
140         change.setPdpSubgroup(LifecycleFeature.fsm.getSubgroup());
141         change.setState(PdpState.valueOf(state));
142         change.setName(LifecycleFeature.fsm.getName());
143
144         return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.stateChange(change)).build();
145     }
146
147     /**
148      * GET topic source.
149      */
150
151     @GET
152     @Path("topic/source")
153     @ApiOperation(value = "Retrieves the Lifecycle topic source",
154             notes = "Lifecycle Topic Source", response = TopicSource.class)
155     public Response source() {
156         return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.getSource()).build();
157     }
158
159     /**
160      * GET topic sink.
161      */
162
163     @GET
164     @Path("topic/sink")
165     @ApiOperation(value = "Retrieves the Lifecycle topic sink",
166             notes = "Lifecycle Topic Sink", response = TopicSink.class)
167     public Response sink() {
168         return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.getClient()).build();
169     }
170
171     /**
172      * GET status interval.
173      */
174
175     @GET
176     @Path("status/interval")
177     @ApiOperation(value = "Retrieves the Lifecycle Status Timer Interval in seconds",
178             notes = "Lifecycle Status Timer Interval in seconds", response = Long.class)
179     public Response updateStatusTimer() {
180         return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.getStatusTimerSeconds()).build();
181     }
182
183     /**
184      * PUT timeout.
185      */
186
187     @PUT
188     @Path("status/interval/{timeout}")
189     @ApiOperation(value = "Updates the Lifecycle Status Timer Interval in seconds",
190             notes = "Lifecycle Status Timer Interval in seconds", response = Long.class)
191     public Response statusTimer(
192             @ApiParam(value = "timeout", required = true) @PathParam("timeout") Long timeout) {
193         LifecycleFeature.fsm.setStatusTimerSeconds(timeout);
194         return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.getStatusTimerSeconds()).build();
195     }
196
197     /**
198      * GET policy types.
199      */
200
201     @GET
202     @Path("policyTypes")
203     @ApiOperation(value = "List of supported policy types",
204             notes = "Lifecycle Policy Types", responseContainer = "List")
205     public Response policyTypes() {
206         return Response.status(Response.Status.OK)
207                        .entity(LifecycleFeature.fsm.getPolicyTypesMap().keySet())
208                        .build();
209     }
210
211     /**
212      * GET controllers.
213      */
214
215     @GET
216     @Path("policyTypes/{policyType}/{policyVersion}")
217     @ApiOperation(value = "Entities associated with a policy type",
218             notes = "Lifecycle policy Types", response = PolicyTypeController.class)
219     public Response policyType(
220         @ApiParam(value = "Policy Type", required = true) @PathParam("policyType") String policyType,
221         @ApiParam(value = "Policy Type Version", required = true) @PathParam("policyVersion") String policyVersion) {
222         return Response.status(Response.Status.OK)
223                        .entity(LifecycleFeature.fsm
224                                 .getPolicyTypesMap()
225                                 .get(new ToscaPolicyTypeIdentifier(policyType, policyVersion)))
226                        .build();
227     }
228
229     /**
230      * GET policies.
231      */
232
233     @GET
234     @Path("policies")
235     @ApiOperation(value = "List of tracked policies",
236             notes = "Lifecycle Policies", responseContainer = "List")
237     public Response policies() {
238         return Response.status(Response.Status.OK)
239                        .entity(LifecycleFeature.fsm.getPoliciesMap().keySet())
240                        .build();
241
242     }
243
244     /**
245      * GET a policy.
246      */
247
248     @GET
249     @Path("policies/{policy}/{policyVersion}")
250     @ApiOperation(value = "Lifecycle tracked policy",
251             notes = "Lifecycle Tracked Policy", response = ToscaPolicy.class)
252     public Response policy(
253             @ApiParam(value = "Policy", required = true) @PathParam("policyName") String policyName,
254             @ApiParam(value = "Policy Version", required = true) @PathParam("policyVersion") String policyVersion) {
255
256         ToscaPolicy policy = LifecycleFeature.fsm
257                                      .getPoliciesMap()
258                                      .get(new ToscaPolicyTypeIdentifier(policyName, policyVersion));
259         if (policy != null) {
260             return
261                 Response.status(Response.Status.OK)
262                         .entity(LifecycleFeature.fsm.getPolicyTypesMap()
263                                         .get(new ToscaPolicyTypeIdentifier(policyName, policyVersion)))
264                         .build();
265         }
266
267         return Response.status(Response.Status.NOT_FOUND).build();
268     }
269 }