Removing Drools-pdp swagger annotations
[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-2022 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2021,2023 Nordix Foundation.
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  * ============LICENSE_END=========================================================
18  */
19
20 package org.onap.policy.drools.server.restful;
21
22 import com.worldturner.medeia.api.ValidationFailedException;
23 import java.util.Collections;
24 import java.util.List;
25 import javax.ws.rs.Consumes;
26 import javax.ws.rs.DELETE;
27 import javax.ws.rs.GET;
28 import javax.ws.rs.POST;
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.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.drools.lifecycle.LifecycleFeature;
39 import org.onap.policy.drools.lifecycle.PolicyTypeController;
40 import org.onap.policy.models.pdp.concepts.PdpStateChange;
41 import org.onap.policy.models.pdp.concepts.PdpUpdate;
42 import org.onap.policy.models.pdp.enums.PdpState;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * REST Lifecycle Manager.
50  */
51
52 @Path("/policy/pdp/engine/lifecycle")
53 @Produces({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML})
54 @Consumes({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML})
55 public class RestLifecycleManager implements LifecycleApi {
56
57     private static final Logger logger = LoggerFactory.getLogger(RestLifecycleManager.class);
58
59     private static final StandardCoder coder = new StandardCoder();
60
61     /**
62      * GET group.
63      */
64
65     @Override
66     @GET
67     @Path("group")
68     public Response group() {
69         return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getGroup()).build();
70     }
71
72     /**
73      * PUT group.
74      */
75
76     @Override
77     @PUT
78     @Path("group/{group}")
79     public Response updateGroup(@PathParam("group") String group) {
80         LifecycleFeature.getFsm().setGroup(group);
81         return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getGroup()).build();
82     }
83
84     /**
85      * GET subgroup.
86      */
87
88     @Override
89     @GET
90     @Path("subgroup")
91     public Response subgroup1() {
92         return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getSubGroup()).build();
93     }
94
95     /**
96      * PUT subgroup.
97      */
98
99     @Override
100     @PUT
101     @Path("subgroup/{subgroup}")
102     public Response subgroup(@PathParam("subgroup") String subgroup) {
103         LifecycleFeature.getFsm().setSubGroup(subgroup);
104         return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getSubGroup()).build();
105     }
106
107     /**
108      * GET properties.
109      */
110
111     @Override
112     @GET
113     @Path("properties")
114     public Response propertiesLifecycle() {
115         return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getProperties()).build();
116     }
117
118     /**
119      * GET state.
120      */
121
122     @Override
123     @GET
124     @Path("state")
125     public Response state() {
126         return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().state()).build();
127     }
128
129     /**
130      * PUT state.
131      */
132
133     @Override
134     @PUT
135     @Path("state/{state}")
136     public Response updateState(@PathParam("state") String state) {
137
138         var change = new PdpStateChange();
139         change.setPdpGroup(LifecycleFeature.getFsm().getGroup());
140         change.setPdpSubgroup(LifecycleFeature.getFsm().getSubGroup());
141         change.setState(PdpState.valueOf(state));
142         change.setName(LifecycleFeature.getFsm().getPdpName());
143
144         return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().stateChange(change)).build();
145     }
146
147     /**
148      * GET topic source.
149      */
150
151     @Override
152     @GET
153     @Path("topic/source")
154     public Response sourceLifecycle() {
155         return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getSource()).build();
156     }
157
158     /**
159      * GET topic sink.
160      */
161
162     @Override
163     @GET
164     @Path("topic/sink")
165     public Response sink() {
166         return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getClient()).build();
167     }
168
169     /**
170      * GET status interval.
171      */
172
173     @Override
174     @GET
175     @Path("status/interval")
176     public Response updateStatusTimer() {
177         return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getStatusTimerSeconds()).build();
178     }
179
180     /**
181      * PUT timeout.
182      */
183
184     @Override
185     @PUT
186     @Path("status/interval/{timeout}")
187     public Response statusTimer(@PathParam("timeout") Long timeout) {
188         LifecycleFeature.getFsm().setStatusTimerSeconds(timeout);
189         return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getStatusTimerSeconds()).build();
190     }
191
192     /**
193      * GET policy types.
194      */
195
196     @Override
197     @GET
198     @Path("policyTypes")
199     public Response policyTypes() {
200         return Response.status(Response.Status.OK)
201             .entity(LifecycleFeature.getFsm().getPolicyTypesMap().keySet())
202             .build();
203     }
204
205     /**
206      * GET controllers.
207      */
208
209     @Override
210     @GET
211     @Path("policyTypes/{policyType}/{policyTypeVersion}")
212     public Response policyType(
213         @PathParam("policyType") String policyType,
214         @PathParam("policyTypeVersion") String policyTypeVersion) {
215         PolicyTypeController typeController =
216             LifecycleFeature.getFsm().getPolicyTypesMap()
217                 .get(new ToscaConceptIdentifier(policyType, policyTypeVersion));
218         if (typeController == null) {
219             return Response.status(Response.Status.NOT_FOUND).build();
220         }
221
222         return Response.status(Response.Status.OK)
223             .entity(typeController)
224             .build();
225     }
226
227     /**
228      * GET policies.
229      */
230
231     @Override
232     @GET
233     @Path("policies")
234     public Response policies() {
235         return Response.status(Response.Status.OK)
236             .entity(LifecycleFeature.getFsm().getPoliciesMap().keySet())
237             .build();
238
239     }
240
241     /**
242      * POST a Policy.
243      */
244
245     @Override
246     @POST
247     @Path("policies")
248     public Response deployTrackedPolicy(String policy) {
249
250         var toscaPolicy = getToscaPolicy(policy);
251         if (toscaPolicy == null) {
252             return Response.status(Response.Status.NOT_ACCEPTABLE).build();
253         }
254
255         var typeController = getPolicyTypeController(toscaPolicy);
256         if (typeController == null) {
257             return Response.status(Response.Status.NOT_FOUND).build();
258         }
259
260         boolean updateResult = LifecycleFeature.getFsm().update(getDeployPolicyUpdate(List.of(toscaPolicy)));
261         return Response.status((updateResult ? Response.Status.OK : Response.Status.NOT_ACCEPTABLE))
262             .entity(updateResult)
263             .build();
264     }
265
266     /**
267      * GET a policy.
268      */
269
270     @Override
271     @GET
272     @Path("policies/{policyName}/{policyVersion}")
273     public Response policy(
274         @PathParam("policyName") String policyName,
275         @PathParam("policyVersion") String policyVersion) {
276
277         ToscaPolicy policy;
278         try {
279             policy =
280                 LifecycleFeature.getFsm().getPoliciesMap().get(new ToscaConceptIdentifier(policyName, policyVersion));
281         } catch (RuntimeException r) {
282             logger.debug("policy {}:{} has not been found", policyName, policyVersion, r);
283             return Response.status(Response.Status.NOT_FOUND).build();
284         }
285
286         if (policy == null) {
287             return Response.status(Response.Status.NOT_FOUND).build();
288         }
289
290         return Response.status(Response.Status.OK).entity(policy).build();
291     }
292
293     /**
294      * DELETE a policy.
295      */
296
297     @Override
298     @DELETE
299     @Path("policies/{policyName}/{policyVersion}")
300     public Response undeployPolicy(
301         @PathParam("policyName") String policyName,
302         @PathParam("policyVersion") String policyVersion) {
303
304         ToscaPolicy policy;
305         try {
306             policy =
307                 LifecycleFeature.getFsm().getPoliciesMap().get(new ToscaConceptIdentifier(policyName, policyVersion));
308         } catch (RuntimeException r) {
309             logger.debug("policy {}:{} has not been found", policyName, policyVersion, r);
310             return Response.status(Response.Status.NOT_FOUND).build();
311         }
312
313         if (policy == null) {
314             return Response.status(Response.Status.NOT_FOUND).build();
315         }
316
317         return Response.status(Response.Status.OK)
318             .entity(LifecycleFeature.getFsm().update(getUndeployPolicyUpdate(List.of(policy))))
319             .build();
320     }
321
322     /**
323      * List of policies individual Operations supported.
324      */
325
326     @Override
327     @GET
328     @Path("policies/operations")
329     public Response policiesOperations() {
330         return Response.status(Response.Status.OK).entity(List.of("deployment", "undeployment", "validation")).build();
331     }
332
333     /**
334      * POST a deployment operation on a policy.
335      */
336
337     @Override
338     @POST
339     @Path("policies/operations/deployment")
340     public Response deployOperation(String policy) {
341         return deployUndeployOperation(policy, true);
342     }
343
344     /**
345      * POST an undeployment operation on a policy.
346      */
347
348     @Override
349     @POST
350     @Path("policies/operations/undeployment")
351     public Response undeployOperation(String policy) {
352         return deployUndeployOperation(policy, false);
353     }
354
355     /**
356      * POST a policy for validation.
357      */
358
359     @Override
360     @POST
361     @Path("policies/operations/validation")
362     public Response validateOperation(String policy) {
363         var toscaPolicy = getToscaPolicy(policy);
364         if (toscaPolicy == null) {
365             return Response.status(Response.Status.NOT_ACCEPTABLE).build();
366         }
367
368         try {
369             LifecycleFeature.getFsm().getDomainMaker().conformance(toscaPolicy);
370         } catch (ValidationFailedException v) {
371             logger.trace("policy {} validation errors: {}", toscaPolicy, v.getMessage(), v);
372             return Response.status(Response.Status.NOT_ACCEPTABLE).entity(v.getFailures()).build();
373         }
374
375         return Response.status(Response.Status.OK).entity(Collections.emptyList()).build();
376     }
377
378     /**
379      * Get current counts.
380      */
381
382     @Override
383     @GET
384     @Path("statistics")
385     public Response stats() {
386         return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().statisticsPayload()).build();
387     }
388
389     private Response deployUndeployOperation(String policy, boolean deploy) {
390         var toscaPolicy = getToscaPolicy(policy);
391         if (toscaPolicy == null) {
392             return Response.status(Response.Status.NOT_ACCEPTABLE).build();
393         }
394
395         var typeController = getPolicyTypeController(toscaPolicy);
396         if (typeController == null) {
397             return Response.status(Response.Status.NOT_FOUND).build();
398         }
399
400         return Response.status(Response.Status.OK)
401             .entity((deploy) ? typeController.deploy(toscaPolicy) : typeController.undeploy(toscaPolicy))
402             .build();
403     }
404
405     private ToscaPolicy getToscaPolicy(String policy) {
406         try {
407             return coder.decode(policy, ToscaPolicy.class);
408         } catch (CoderException | RuntimeException e) {
409             return null;
410         }
411     }
412
413     private PolicyTypeController getPolicyTypeController(ToscaPolicy policy) {
414         return LifecycleFeature.getFsm().getPolicyTypesMap().get(policy.getTypeIdentifier());
415     }
416
417     private PdpUpdate getPolicyUpdate() {
418         var update = new PdpUpdate();
419         update.setName(LifecycleFeature.getFsm().getPdpName());
420         update.setPdpGroup(LifecycleFeature.getFsm().getGroup());
421         update.setPdpSubgroup(LifecycleFeature.getFsm().getSubGroup());
422         return update;
423     }
424
425     private PdpUpdate getDeployPolicyUpdate(List<ToscaPolicy> policies) {
426         PdpUpdate update = getPolicyUpdate();
427         update.setPoliciesToBeDeployed(policies);
428         return update;
429     }
430
431     private PdpUpdate getUndeployPolicyUpdate(List<ToscaPolicy> policies) {
432         PdpUpdate update = getPolicyUpdate();
433         update.setPoliciesToBeUndeployed(LifecycleFeature.fsm.getPolicyIds(policies));
434         return update;
435     }
436
437 }