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
10 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.policy.drools.server.restful;
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;
49 * REST Lifecycle Manager.
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 {
57 private static final Logger logger = LoggerFactory.getLogger(RestLifecycleManager.class);
59 private static final StandardCoder coder = new StandardCoder();
68 public Response group() {
69 return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getGroup()).build();
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();
91 public Response subgroup1() {
92 return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getSubGroup()).build();
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();
114 public Response propertiesLifecycle() {
115 return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getProperties()).build();
125 public Response state() {
126 return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().state()).build();
135 @Path("state/{state}")
136 public Response updateState(@PathParam("state") String state) {
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());
144 return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().stateChange(change)).build();
153 @Path("topic/source")
154 public Response sourceLifecycle() {
155 return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getSource()).build();
165 public Response sink() {
166 return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getClient()).build();
170 * GET status interval.
175 @Path("status/interval")
176 public Response updateStatusTimer() {
177 return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getStatusTimerSeconds()).build();
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();
199 public Response policyTypes() {
200 return Response.status(Response.Status.OK)
201 .entity(LifecycleFeature.getFsm().getPolicyTypesMap().keySet())
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();
222 return Response.status(Response.Status.OK)
223 .entity(typeController)
234 public Response policies() {
235 return Response.status(Response.Status.OK)
236 .entity(LifecycleFeature.getFsm().getPoliciesMap().keySet())
248 public Response deployTrackedPolicy(String policy) {
250 var toscaPolicy = getToscaPolicy(policy);
251 if (toscaPolicy == null) {
252 return Response.status(Response.Status.NOT_ACCEPTABLE).build();
255 var typeController = getPolicyTypeController(toscaPolicy);
256 if (typeController == null) {
257 return Response.status(Response.Status.NOT_FOUND).build();
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)
272 @Path("policies/{policyName}/{policyVersion}")
273 public Response policy(
274 @PathParam("policyName") String policyName,
275 @PathParam("policyVersion") String policyVersion) {
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();
286 if (policy == null) {
287 return Response.status(Response.Status.NOT_FOUND).build();
290 return Response.status(Response.Status.OK).entity(policy).build();
299 @Path("policies/{policyName}/{policyVersion}")
300 public Response undeployPolicy(
301 @PathParam("policyName") String policyName,
302 @PathParam("policyVersion") String policyVersion) {
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();
313 if (policy == null) {
314 return Response.status(Response.Status.NOT_FOUND).build();
317 return Response.status(Response.Status.OK)
318 .entity(LifecycleFeature.getFsm().update(getUndeployPolicyUpdate(List.of(policy))))
323 * List of policies individual Operations supported.
328 @Path("policies/operations")
329 public Response policiesOperations() {
330 return Response.status(Response.Status.OK).entity(List.of("deployment", "undeployment", "validation")).build();
334 * POST a deployment operation on a policy.
339 @Path("policies/operations/deployment")
340 public Response deployOperation(String policy) {
341 return deployUndeployOperation(policy, true);
345 * POST an undeployment operation on a policy.
350 @Path("policies/operations/undeployment")
351 public Response undeployOperation(String policy) {
352 return deployUndeployOperation(policy, false);
356 * POST a policy for validation.
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();
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();
375 return Response.status(Response.Status.OK).entity(Collections.emptyList()).build();
379 * Get current counts.
385 public Response stats() {
386 return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().statisticsPayload()).build();
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();
395 var typeController = getPolicyTypeController(toscaPolicy);
396 if (typeController == null) {
397 return Response.status(Response.Status.NOT_FOUND).build();
400 return Response.status(Response.Status.OK)
401 .entity((deploy) ? typeController.deploy(toscaPolicy) : typeController.undeploy(toscaPolicy))
405 private ToscaPolicy getToscaPolicy(String policy) {
407 return coder.decode(policy, ToscaPolicy.class);
408 } catch (CoderException | RuntimeException e) {
413 private PolicyTypeController getPolicyTypeController(ToscaPolicy policy) {
414 return LifecycleFeature.getFsm().getPolicyTypesMap().get(policy.getTypeIdentifier());
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());
425 private PdpUpdate getDeployPolicyUpdate(List<ToscaPolicy> policies) {
426 PdpUpdate update = getPolicyUpdate();
427 update.setPoliciesToBeDeployed(policies);
431 private PdpUpdate getUndeployPolicyUpdate(List<ToscaPolicy> policies) {
432 PdpUpdate update = getPolicyUpdate();
433 update.setPoliciesToBeUndeployed(LifecycleFeature.fsm.getPolicyIds(policies));