2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021-2023 Nordix Foundation.
7 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pap.main.rest;
25 import io.micrometer.core.instrument.MeterRegistry;
26 import io.micrometer.core.instrument.Timer;
27 import java.time.Duration;
28 import java.time.Instant;
29 import java.util.UUID;
30 import lombok.RequiredArgsConstructor;
31 import org.onap.policy.common.utils.resources.PrometheusUtils;
32 import org.onap.policy.models.base.PfModelException;
33 import org.onap.policy.models.base.PfModelRuntimeException;
34 import org.onap.policy.models.pap.concepts.PdpGroupDeleteResponse;
35 import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
36 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.context.annotation.Profile;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.web.bind.annotation.RestController;
46 * Class to provide REST end points for PAP component to delete a PDP group.
49 @RequiredArgsConstructor
51 public class PdpGroupDeleteControllerV1 extends PapRestControllerV1
52 implements PdpGroupDeleteControllerV1Api {
54 private static final Logger logger = LoggerFactory.getLogger(PdpGroupDeleteControllerV1.class);
56 private final PdpGroupDeleteProvider provider;
57 private Timer undeploySuccessTimer;
58 private Timer undeployFailureTimer;
62 public PdpGroupDeleteControllerV1(PdpGroupDeleteProvider provider, MeterRegistry meterRegistry) {
63 this.provider = provider;
64 initMetrics(meterRegistry);
68 * Initializes the metrics for delete operation.
70 * @param meterRegistry spring bean for MeterRegistry to add the new metric
72 public void initMetrics(MeterRegistry meterRegistry) {
73 String metricName = String.join(".", "pap", "policy", "deployments");
74 String description = "Timer for HTTP request to deploy/undeploy a policy";
75 undeploySuccessTimer = Timer.builder(metricName).description(description)
76 .tags(PrometheusUtils.OPERATION_METRIC_LABEL, PrometheusUtils.UNDEPLOY_OPERATION,
77 PrometheusUtils.STATUS_METRIC_LABEL, PdpPolicyStatus.State.SUCCESS.name())
78 .register(meterRegistry);
79 undeployFailureTimer = Timer.builder(metricName).description(description)
80 .tags(PrometheusUtils.OPERATION_METRIC_LABEL, PrometheusUtils.UNDEPLOY_OPERATION,
81 PrometheusUtils.STATUS_METRIC_LABEL, PdpPolicyStatus.State.FAILURE.name())
82 .register(meterRegistry);
86 * Deletes a PDP group.
88 * @param requestId request ID used in ONAP logging
89 * @param groupName name of the PDP group to be deleted
93 public ResponseEntity<PdpGroupDeleteResponse> deleteGroup(String groupName, UUID requestId) {
94 return doOperation(requestId, () -> provider.deleteGroup(groupName));
98 * Undeploys the latest version of a policy from the PDPs.
100 * @param requestId request ID used in ONAP logging
101 * @param policyName name of the PDP Policy to be deleted
105 public ResponseEntity<PdpGroupDeployResponse> deletePolicy(String policyName, UUID requestId) {
106 return doUndeployOperation(requestId,
107 () -> provider.undeploy(new ToscaConceptIdentifierOptVersion(policyName, null), getPrincipal()));
111 * Undeploys a specific version of a policy from the PDPs.
113 * @param requestId request ID used in ONAP logging
114 * @param policyName name of the PDP Policy to be deleted
115 * @param version version to be deleted
119 public ResponseEntity<PdpGroupDeployResponse> deletePolicyVersion(
123 return doUndeployOperation(requestId,
124 () -> provider.undeploy(new ToscaConceptIdentifierOptVersion(policyName, version), getPrincipal()));
128 * Invokes an operation.
130 * @param requestId request ID
131 * @param runnable operation to invoke
132 * @return a {@link PdpGroupDeleteResponse} response entity
134 private ResponseEntity<PdpGroupDeleteResponse> doOperation(UUID requestId, RunnableWithPfEx runnable) {
137 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
138 .body(new PdpGroupDeleteResponse());
140 } catch (PfModelException | PfModelRuntimeException e) {
141 logger.warn("delete group failed", e);
142 var resp = new PdpGroupDeleteResponse();
143 resp.setErrorDetails(e.getErrorResponse().getErrorMessage());
144 return addLoggingHeaders(
145 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
146 requestId).body(resp);
151 * Invokes the undeployment operation.
153 * @param requestId request ID
154 * @param runnable operation to invoke
155 * @return a {@link PdpGroupDeployResponse} response entity
157 private ResponseEntity<PdpGroupDeployResponse> doUndeployOperation(UUID requestId, RunnableWithPfEx runnable) {
158 Instant start = Instant.now();
161 undeploySuccessTimer.record(Duration.between(start, Instant.now()));
162 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.accepted()), requestId)
163 .body(new PdpGroupDeployResponse(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG,
164 PdpGroupDeployControllerV1.POLICY_STATUS_URI));
166 } catch (PfModelException | PfModelRuntimeException e) {
167 logger.warn("undeploy policy failed", e);
168 var resp = new PdpGroupDeployResponse();
169 resp.setErrorDetails(e.getErrorResponse().getErrorMessage());
170 undeployFailureTimer.record(Duration.between(start, Instant.now()));
171 return addLoggingHeaders(
172 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
173 requestId).body(resp);