62d0f3699f98a4638171e4baa92d3cc6eb9ea127
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PdpGroupDeployControllerV1.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
7  * Modifications Copyright (C) 2022-2023 Nordix Foundation.
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest;
24
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.PdpDeployPolicies;
35 import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
36 import org.onap.policy.models.pdp.concepts.DeploymentGroups;
37 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.RestController;
43
44 /**
45  * Class to provide REST end points for PAP component to deploy a PDP group.
46  */
47 @RestController
48 @RequiredArgsConstructor
49 public class PdpGroupDeployControllerV1 extends PapRestControllerV1 implements PdpGroupDeployControllerV1Api {
50     public static final String POLICY_STATUS_URI = "/policy/pap/v1/policies/status";
51
52     public static final String DEPLOYMENT_RESPONSE_MSG = "Use the policy status url to fetch the latest status. "
53             + "Kindly note that when a policy is successfully undeployed,"
54             + " it will no longer appear in policy status response.";
55
56     private static final Logger logger = LoggerFactory.getLogger(PdpGroupDeployControllerV1.class);
57
58     private final PdpGroupDeployProvider provider;
59     private Timer deploySuccessTimer;
60     private Timer deployFailureTimer;
61
62
63     @Autowired
64     public PdpGroupDeployControllerV1(PdpGroupDeployProvider provider, MeterRegistry meterRegistry) {
65         this.provider = provider;
66         initMetrics(meterRegistry);
67     }
68
69     /**
70      * Initializes the metrics for delete operation.
71      *
72      * @param meterRegistry spring bean for MeterRegistry to add the new metric
73      */
74     public void initMetrics(MeterRegistry meterRegistry) {
75         String metricName = String.join(".", "pap", "policy", "deployments");
76         String description = "Timer for HTTP request to deploy/undeploy a policy";
77         deploySuccessTimer = Timer.builder(metricName).description(description)
78             .tags(PrometheusUtils.OPERATION_METRIC_LABEL, PrometheusUtils.DEPLOY_OPERATION,
79                 PrometheusUtils.STATUS_METRIC_LABEL, PdpPolicyStatus.State.SUCCESS.name())
80             .register(meterRegistry);
81         deployFailureTimer = Timer.builder(metricName).description(description)
82             .tags(PrometheusUtils.OPERATION_METRIC_LABEL, PrometheusUtils.DEPLOY_OPERATION,
83                 PrometheusUtils.STATUS_METRIC_LABEL, PdpPolicyStatus.State.FAILURE.name())
84             .register(meterRegistry);
85     }
86
87     /**
88      * Updates policy deployments within specific PDP groups.
89      *
90      * @param requestId request ID used in ONAP logging
91      * @param groups PDP group configuration
92      * @return a response
93      */
94     @Override
95     public ResponseEntity<PdpGroupDeployResponse> updateGroupPolicies(UUID requestId, DeploymentGroups groups) {
96         return doOperation(requestId, "update policy deployments failed",
97             () -> provider.updateGroupPolicies(groups, getPrincipal()));
98     }
99
100     /**
101      * Deploys or updates PDP policies.
102      *
103      * @param requestId request ID used in ONAP logging
104      * @param policies PDP policies
105      * @return a response
106      */
107     @Override
108     public ResponseEntity<PdpGroupDeployResponse> deployPolicies(UUID requestId, PdpDeployPolicies policies) {
109         return doOperation(requestId, "deploy policies failed",
110             () -> provider.deployPolicies(policies, getPrincipal()));
111     }
112
113     /**
114      * Invokes an operation.
115      *
116      * @param requestId request ID
117      * @param errmsg error message to log if the operation throws an exception
118      * @param runnable operation to invoke
119      * @return a {@link PdpGroupDeployResponse} response entity
120      */
121     private ResponseEntity<PdpGroupDeployResponse> doOperation(UUID requestId, String errmsg,
122         RunnableWithPfEx runnable) {
123         Instant start = Instant.now();
124         try {
125             runnable.run();
126             deploySuccessTimer.record(Duration.between(start, Instant.now()));
127             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.accepted()), requestId)
128                 .body(new PdpGroupDeployResponse(DEPLOYMENT_RESPONSE_MSG, POLICY_STATUS_URI));
129
130         } catch (PfModelException | PfModelRuntimeException e) {
131             logger.warn(errmsg, e);
132             var resp = new PdpGroupDeployResponse();
133             resp.setErrorDetails(e.getErrorResponse().getErrorMessage());
134             deployFailureTimer.record(Duration.between(start, Instant.now()));
135             return addLoggingHeaders(
136                 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
137                 requestId).body(resp);
138         }
139     }
140
141 }