Changes for Checkstyle 8.32
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / ApiRestController.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP Policy API\r
4  * ================================================================================\r
5  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.\r
6  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.\r
7  * Modifications Copyright (C) 2020 Nordix Foundation.\r
8  * ================================================================================\r
9  * Licensed under the Apache License, Version 2.0 (the "License");\r
10  * you may not use this file except in compliance with the License.\r
11  * You may obtain a copy of the License at\r
12  *\r
13  *      http://www.apache.org/licenses/LICENSE-2.0\r
14  *\r
15  * Unless required by applicable law or agreed to in writing, software\r
16  * distributed under the License is distributed on an "AS IS" BASIS,\r
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
18  * See the License for the specific language governing permissions and\r
19  * limitations under the License.\r
20  *\r
21  * SPDX-License-Identifier: Apache-2.0\r
22  * ============LICENSE_END=========================================================\r
23  */\r
24 \r
25 package org.onap.policy.api.main.rest;\r
26 \r
27 import io.swagger.annotations.Api;\r
28 import io.swagger.annotations.ApiOperation;\r
29 import io.swagger.annotations.ApiParam;\r
30 import io.swagger.annotations.ApiResponse;\r
31 import io.swagger.annotations.ApiResponses;\r
32 import io.swagger.annotations.Authorization;\r
33 import io.swagger.annotations.BasicAuthDefinition;\r
34 import io.swagger.annotations.Extension;\r
35 import io.swagger.annotations.ExtensionProperty;\r
36 import io.swagger.annotations.Info;\r
37 import io.swagger.annotations.ResponseHeader;\r
38 import io.swagger.annotations.SecurityDefinition;\r
39 import io.swagger.annotations.SwaggerDefinition;\r
40 import java.util.UUID;\r
41 import javax.ws.rs.Consumes;\r
42 import javax.ws.rs.DELETE;\r
43 import javax.ws.rs.DefaultValue;\r
44 import javax.ws.rs.GET;\r
45 import javax.ws.rs.HeaderParam;\r
46 import javax.ws.rs.POST;\r
47 import javax.ws.rs.Path;\r
48 import javax.ws.rs.PathParam;\r
49 import javax.ws.rs.Produces;\r
50 import javax.ws.rs.QueryParam;\r
51 import javax.ws.rs.core.Response;\r
52 import org.onap.policy.api.main.rest.provider.HealthCheckProvider;\r
53 import org.onap.policy.api.main.rest.provider.PolicyProvider;\r
54 import org.onap.policy.api.main.rest.provider.PolicyTypeProvider;\r
55 import org.onap.policy.api.main.rest.provider.StatisticsProvider;\r
56 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;\r
57 import org.onap.policy.common.endpoints.report.HealthCheckReport;\r
58 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;\r
59 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;\r
60 import org.onap.policy.models.base.PfModelException;\r
61 import org.onap.policy.models.base.PfModelRuntimeException;\r
62 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;\r
63 import org.slf4j.Logger;\r
64 import org.slf4j.LoggerFactory;\r
65 \r
66 /**\r
67  * Class to provide REST API services.\r
68  *\r
69  * @author Chenfei Gao (cgao@research.att.com)\r
70  */\r
71 @Path("/policy/api/v1")\r
72 @Api(value = "Policy Design API")\r
73 @Produces({"application/json", "application/yaml"})\r
74 @Consumes({"application/json", "application/yaml"})\r
75 @SwaggerDefinition(\r
76     info = @Info(\r
77         description = "Policy Design API is publicly exposed for clients to Create/Read/Update/Delete"\r
78             + " policy types, policy type implementation and policies which can be recognized"\r
79             + " and executable by incorporated policy engines. It is an"\r
80             + " independent component running rest service that takes all policy design API calls"\r
81             + " from clients and then assign them to different API working functions. Besides"\r
82             + " that, API is also exposed for clients to retrieve healthcheck status of this API"\r
83             + " rest service and the statistics report including the counters of API invocation.",\r
84         version = "1.0.0", title = "Policy Design",\r
85         extensions = {@Extension(properties = {@ExtensionProperty(name = "planned-retirement-date", value = "tbd"),\r
86             @ExtensionProperty(name = "component", value = "Policy Framework")})}),\r
87     schemes = {SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS},\r
88     securityDefinition = @SecurityDefinition(basicAuthDefinitions = {@BasicAuthDefinition(key = "basicAuth")}))\r
89 public class ApiRestController extends CommonRestController {\r
90 \r
91     private static final Logger LOGGER = LoggerFactory.getLogger(ApiRestController.class);\r
92 \r
93     /**\r
94      * Retrieves the healthcheck status of the API component.\r
95      *\r
96      * @return the Response object containing the results of the API operation\r
97      */\r
98     @GET\r
99     @Path("/healthcheck")\r
100     @ApiOperation(value = "Perform a system healthcheck", notes = "Returns healthy status of the Policy API component",\r
101         response = HealthCheckReport.class,\r
102         responseHeaders = {\r
103             @ResponseHeader(name = "X-MinorVersion",\r
104                 description = "Used to request or communicate a MINOR version back from the client"\r
105                     + " to the server, and from the server back to the client",\r
106                 response = String.class),\r
107             @ResponseHeader(name = "X-PatchVersion",\r
108                 description = "Used only to communicate a PATCH version in a response for"\r
109                     + " troubleshooting purposes only, and will not be provided by" + " the client on request",\r
110                 response = String.class),\r
111             @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version",\r
112                 response = String.class),\r
113             @ResponseHeader(name = "X-ONAP-RequestID",\r
114                 description = "Used to track REST transactions for logging purpose", response = UUID.class)},\r
115         authorizations = @Authorization(value = "basicAuth"), tags = {"HealthCheck", },\r
116         extensions = {\r
117             @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"),\r
118                 @ExtensionProperty(name = "last-mod-release", value = "Dublin")})})\r
119     @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"),\r
120         @ApiResponse(code = 403, message = "Authorization Error"),\r
121         @ApiResponse(code = 500, message = "Internal Server Error")})\r
122     public Response\r
123         getHealthCheck(@HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
124 \r
125         updateApiStatisticsCounter(Target.OTHER, Result.SUCCESS, HttpMethod.GET);\r
126         return makeOkResponse(requestId, new HealthCheckProvider().performHealthCheck());\r
127     }\r
128 \r
129     /**\r
130      * Retrieves the statistics report of the API component.\r
131      *\r
132      * @return the Response object containing the results of the API operation\r
133      */\r
134     @GET\r
135     @Path("/statistics")\r
136     @ApiOperation(value = "Retrieve current statistics",\r
137         notes = "Returns current statistics including the counters of API invocation",\r
138         response = StatisticsReport.class,\r
139         responseHeaders = {\r
140             @ResponseHeader(name = "X-MinorVersion",\r
141                 description = "Used to request or communicate a MINOR version back from the client"\r
142                     + " to the server, and from the server back to the client",\r
143                 response = String.class),\r
144             @ResponseHeader(name = "X-PatchVersion",\r
145                 description = "Used only to communicate a PATCH version in a response for"\r
146                     + " troubleshooting purposes only, and will not be provided by" + " the client on request",\r
147                 response = String.class),\r
148             @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version",\r
149                 response = String.class),\r
150             @ResponseHeader(name = "X-ONAP-RequestID",\r
151                 description = "Used to track REST transactions for logging purpose", response = UUID.class)},\r
152         authorizations = @Authorization(value = "basicAuth"), tags = {"Statistics", },\r
153         extensions = {\r
154             @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"),\r
155                 @ExtensionProperty(name = "last-mod-release", value = "Dublin")})})\r
156     @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"),\r
157         @ApiResponse(code = 403, message = "Authorization Error"),\r
158         @ApiResponse(code = 500, message = "Internal Server Error")})\r
159     public Response\r
160         getStatistics(@HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
161 \r
162         updateApiStatisticsCounter(Target.OTHER, Result.SUCCESS, HttpMethod.GET);\r
163 \r
164         return makeOkResponse(requestId, new StatisticsProvider().fetchCurrentStatistics());\r
165     }\r
166 \r
167     /**\r
168      * Retrieves all available policy types.\r
169      *\r
170      * @return the Response object containing the results of the API operation\r
171      */\r
172     @GET\r
173     @Path("/policytypes")\r
174     @ApiOperation(value = "Retrieve existing policy types",\r
175         notes = "Returns a list of existing policy types stored in Policy Framework",\r
176         response = ToscaServiceTemplate.class,\r
177         responseHeaders = {\r
178             @ResponseHeader(name = "X-MinorVersion",\r
179                 description = "Used to request or communicate a MINOR version back from the client"\r
180                     + " to the server, and from the server back to the client",\r
181                 response = String.class),\r
182             @ResponseHeader(name = "X-PatchVersion",\r
183                 description = "Used only to communicate a PATCH version in a response for"\r
184                     + " troubleshooting purposes only, and will not be provided by" + " the client on request",\r
185                 response = String.class),\r
186             @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version",\r
187                 response = String.class),\r
188             @ResponseHeader(name = "X-ONAP-RequestID",\r
189                 description = "Used to track REST transactions for logging purpose", response = UUID.class)},\r
190         authorizations = @Authorization(value = "basicAuth"), tags = {"PolicyType", },\r
191         extensions = {\r
192             @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"),\r
193                 @ExtensionProperty(name = "last-mod-release", value = "Dublin")})})\r
194     @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"),\r
195         @ApiResponse(code = 403, message = "Authorization Error"),\r
196         @ApiResponse(code = 500, message = "Internal Server Error")})\r
197     public Response\r
198         getAllPolicyTypes(@HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
199 \r
200         try (PolicyTypeProvider policyTypeProvider = new PolicyTypeProvider()) {\r
201             ToscaServiceTemplate serviceTemplate = policyTypeProvider.fetchPolicyTypes(null, null);\r
202             updateApiStatisticsCounter(Target.POLICY_TYPE, Result.SUCCESS, HttpMethod.GET);\r
203             return makeOkResponse(requestId, serviceTemplate);\r
204         } catch (PfModelException | PfModelRuntimeException pfme) {\r
205             LOGGER.debug("GET /policytypes", pfme);\r
206             updateApiStatisticsCounter(Target.POLICY_TYPE, Result.FAILURE, HttpMethod.GET);\r
207             return makeErrorResponse(requestId, pfme);\r
208         }\r
209     }\r
210 \r
211     /**\r
212      * Retrieves all versions of a particular policy type.\r
213      *\r
214      * @param policyTypeId the ID of specified policy type\r
215      *\r
216      * @return the Response object containing the results of the API operation\r
217      */\r
218     @GET\r
219     @Path("/policytypes/{policyTypeId}")\r
220     @ApiOperation(value = "Retrieve all available versions of a policy type",\r
221         notes = "Returns a list of all available versions for the specified policy type",\r
222         response = ToscaServiceTemplate.class,\r
223         responseHeaders = {\r
224             @ResponseHeader(name = "X-MinorVersion",\r
225                 description = "Used to request or communicate a MINOR version back from the client"\r
226                     + " to the server, and from the server back to the client",\r
227                 response = String.class),\r
228             @ResponseHeader(name = "X-PatchVersion",\r
229                 description = "Used only to communicate a PATCH version in a response for"\r
230                     + " troubleshooting purposes only, and will not be provided by" + " the client on request",\r
231                 response = String.class),\r
232             @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version",\r
233                 response = String.class),\r
234             @ResponseHeader(name = "X-ONAP-RequestID",\r
235                 description = "Used to track REST transactions for logging purpose", response = UUID.class)},\r
236         authorizations = @Authorization(value = "basicAuth"), tags = {"PolicyType", },\r
237         extensions = {\r
238             @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"),\r
239                 @ExtensionProperty(name = "last-mod-release", value = "Dublin")})})\r
240     @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"),\r
241         @ApiResponse(code = 403, message = "Authorization Error"),\r
242         @ApiResponse(code = 404, message = "Resource Not Found"),\r
243         @ApiResponse(code = 500, message = "Internal Server Error")})\r
244     public Response getAllVersionsOfPolicyType(\r
245         @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,\r
246         @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
247 \r
248         try (PolicyTypeProvider policyTypeProvider = new PolicyTypeProvider()) {\r
249             ToscaServiceTemplate serviceTemplate = policyTypeProvider.fetchPolicyTypes(policyTypeId, null);\r
250             updateApiStatisticsCounter(Target.POLICY_TYPE, Result.SUCCESS, HttpMethod.GET);\r
251             return makeOkResponse(requestId, serviceTemplate);\r
252         } catch (PfModelException | PfModelRuntimeException pfme) {\r
253             LOGGER.debug("GET /policytypes/{}", policyTypeId, pfme);\r
254             updateApiStatisticsCounter(Target.POLICY_TYPE, Result.FAILURE, HttpMethod.GET);\r
255             return makeErrorResponse(requestId, pfme);\r
256         }\r
257     }\r
258 \r
259     /**\r
260      * Retrieves specified version of a particular policy type.\r
261      *\r
262      * @param policyTypeId the ID of specified policy type\r
263      * @param versionId the version of specified policy type\r
264      *\r
265      * @return the Response object containing the results of the API operation\r
266      */\r
267     @GET\r
268     @Path("/policytypes/{policyTypeId}/versions/{versionId}")\r
269     @ApiOperation(value = "Retrieve one particular version of a policy type",\r
270         notes = "Returns a particular version for the specified policy type", response = ToscaServiceTemplate.class,\r
271         responseHeaders = {\r
272             @ResponseHeader(name = "X-MinorVersion",\r
273                 description = "Used to request or communicate a MINOR version back from the client"\r
274                     + " to the server, and from the server back to the client",\r
275                 response = String.class),\r
276             @ResponseHeader(name = "X-PatchVersion",\r
277                 description = "Used only to communicate a PATCH version in a response for"\r
278                     + " troubleshooting purposes only, and will not be provided by" + " the client on request",\r
279                 response = String.class),\r
280             @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version",\r
281                 response = String.class),\r
282             @ResponseHeader(name = "X-ONAP-RequestID",\r
283                 description = "Used to track REST transactions for logging purpose", response = UUID.class)},\r
284         authorizations = @Authorization(value = "basicAuth"), tags = {"PolicyType", },\r
285         extensions = {\r
286             @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"),\r
287                 @ExtensionProperty(name = "last-mod-release", value = "Dublin")})})\r
288     @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"),\r
289         @ApiResponse(code = 403, message = "Authorization Error"),\r
290         @ApiResponse(code = 404, message = "Resource Not Found"),\r
291         @ApiResponse(code = 500, message = "Internal Server Error")})\r
292     public Response getSpecificVersionOfPolicyType(\r
293         @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,\r
294         @PathParam("versionId") @ApiParam(value = "Version of policy type", required = true) String versionId,\r
295         @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
296 \r
297         try (PolicyTypeProvider policyTypeProvider = new PolicyTypeProvider()) {\r
298             ToscaServiceTemplate serviceTemplate = policyTypeProvider.fetchPolicyTypes(policyTypeId, versionId);\r
299             updateApiStatisticsCounter(Target.POLICY_TYPE, Result.SUCCESS, HttpMethod.GET);\r
300             return makeOkResponse(requestId, serviceTemplate);\r
301         } catch (PfModelException | PfModelRuntimeException pfme) {\r
302             LOGGER.debug("GET /policytypes/{}/versions/{}", policyTypeId, versionId, pfme);\r
303             updateApiStatisticsCounter(Target.POLICY_TYPE, Result.FAILURE, HttpMethod.GET);\r
304             return makeErrorResponse(requestId, pfme);\r
305         }\r
306     }\r
307 \r
308     /**\r
309      * Retrieves latest version of a particular policy type.\r
310      *\r
311      * @param policyTypeId the ID of specified policy type\r
312      *\r
313      * @return the Response object containing the results of the API operation\r
314      */\r
315     @GET\r
316     @Path("/policytypes/{policyTypeId}/versions/latest")\r
317     @ApiOperation(value = "Retrieve latest version of a policy type",\r
318         notes = "Returns latest version for the specified policy type", response = ToscaServiceTemplate.class,\r
319         responseHeaders = {\r
320             @ResponseHeader(name = "X-MinorVersion",\r
321                 description = "Used to request or communicate a MINOR version back from the client"\r
322                     + " to the server, and from the server back to the client",\r
323                 response = String.class),\r
324             @ResponseHeader(name = "X-PatchVersion",\r
325                 description = "Used only to communicate a PATCH version in a response for"\r
326                     + " troubleshooting purposes only, and will not be provided by" + " the client on request",\r
327                 response = String.class),\r
328             @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version",\r
329                 response = String.class),\r
330             @ResponseHeader(name = "X-ONAP-RequestID",\r
331                 description = "Used to track REST transactions for logging purpose", response = UUID.class)},\r
332         authorizations = @Authorization(value = "basicAuth"), tags = {"PolicyType", },\r
333         extensions = {\r
334             @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"),\r
335                 @ExtensionProperty(name = "last-mod-release", value = "Dublin")})})\r
336     @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"),\r
337         @ApiResponse(code = 403, message = "Authorization Error"),\r
338         @ApiResponse(code = 404, message = "Resource Not Found"),\r
339         @ApiResponse(code = 500, message = "Internal Server Error")})\r
340     public Response getLatestVersionOfPolicyType(\r
341         @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,\r
342         @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
343 \r
344         try (PolicyTypeProvider policyTypeProvider = new PolicyTypeProvider()) {\r
345             ToscaServiceTemplate serviceTemplate = policyTypeProvider.fetchLatestPolicyTypes(policyTypeId);\r
346             updateApiStatisticsCounter(Target.POLICY_TYPE, Result.SUCCESS, HttpMethod.GET);\r
347             return makeOkResponse(requestId, serviceTemplate);\r
348         } catch (PfModelException | PfModelRuntimeException pfme) {\r
349             LOGGER.debug("GET /policytypes/{}/versions/latest", policyTypeId, pfme);\r
350             updateApiStatisticsCounter(Target.POLICY_TYPE, Result.FAILURE, HttpMethod.GET);\r
351             return makeErrorResponse(requestId, pfme);\r
352         }\r
353     }\r
354 \r
355     /**\r
356      * Creates a new policy type.\r
357      *\r
358      * @param body the body of policy type following TOSCA definition\r
359      *\r
360      * @return the Response object containing the results of the API operation\r
361      */\r
362     @POST\r
363     @Path("/policytypes")\r
364     @ApiOperation(value = "Create a new policy type", notes = "Client should provide TOSCA body of the new policy type",\r
365         authorizations = @Authorization(value = "basicAuth"), tags = {"PolicyType", },\r
366         response = ToscaServiceTemplate.class,\r
367         responseHeaders = {\r
368             @ResponseHeader(name = "X-MinorVersion",\r
369                 description = "Used to request or communicate a MINOR version back from the client"\r
370                     + " to the server, and from the server back to the client",\r
371                 response = String.class),\r
372             @ResponseHeader(name = "X-PatchVersion",\r
373                 description = "Used only to communicate a PATCH version in a response for"\r
374                     + " troubleshooting purposes only, and will not be provided by" + " the client on request",\r
375                 response = String.class),\r
376             @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version",\r
377                 response = String.class),\r
378             @ResponseHeader(name = "X-ONAP-RequestID",\r
379                 description = "Used to track REST transactions for logging purpose", response = UUID.class)},\r
380         extensions = {\r
381             @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"),\r
382                 @ExtensionProperty(name = "last-mod-release", value = "Dublin")})})\r
383     @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid Body"),\r
384         @ApiResponse(code = 401, message = "Authentication Error"),\r
385         @ApiResponse(code = 403, message = "Authorization Error"),\r
386         @ApiResponse(code = 406, message = "Not Acceptable Payload"),\r
387         @ApiResponse(code = 500, message = "Internal Server Error")})\r
388     public Response createPolicyType(\r
389         @ApiParam(value = "Entity body of policy type", required = true) ToscaServiceTemplate body,\r
390         @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
391 \r
392         if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {\r
393             NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, "/policytypes", toJson(body));\r
394         }\r
395 \r
396         try (PolicyTypeProvider policyTypeProvider = new PolicyTypeProvider()) {\r
397             ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(body);\r
398             updateApiStatisticsCounter(Target.POLICY_TYPE, Result.SUCCESS, HttpMethod.POST);\r
399             return makeOkResponse(requestId, serviceTemplate);\r
400         } catch (PfModelException | PfModelRuntimeException pfme) {\r
401             LOGGER.debug("POST /policytypes", pfme);\r
402             updateApiStatisticsCounter(Target.POLICY_TYPE, Result.FAILURE, HttpMethod.POST);\r
403             return makeErrorResponse(requestId, pfme);\r
404         }\r
405     }\r
406 \r
407     /**\r
408      * Deletes specified version of a particular policy type.\r
409      *\r
410      * @param policyTypeId the ID of specified policy type\r
411      * @param versionId the version of specified policy type\r
412      *\r
413      * @return the Response object containing the results of the API operation\r
414      */\r
415     @DELETE\r
416     @Path("/policytypes/{policyTypeId}/versions/{versionId}")\r
417     @ApiOperation(value = "Delete one version of a policy type",\r
418         notes = "Rule 1: pre-defined policy types cannot be deleted;"\r
419             + "Rule 2: policy types that are in use (parameterized by a TOSCA policy) cannot be deleted."\r
420             + "The parameterizing TOSCA policies must be deleted first;",\r
421         authorizations = @Authorization(value = "basicAuth"), tags = {"PolicyType", },\r
422         response = ToscaServiceTemplate.class,\r
423         responseHeaders = {\r
424             @ResponseHeader(name = "X-MinorVersion",\r
425                 description = "Used to request or communicate a MINOR version back from the client"\r
426                     + " to the server, and from the server back to the client",\r
427                 response = String.class),\r
428             @ResponseHeader(name = "X-PatchVersion",\r
429                 description = "Used only to communicate a PATCH version in a response for"\r
430                     + " troubleshooting purposes only, and will not be provided by" + " the client on request",\r
431                 response = String.class),\r
432             @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version",\r
433                 response = String.class),\r
434             @ResponseHeader(name = "X-ONAP-RequestID",\r
435                 description = "Used to track REST transactions for logging purpose", response = UUID.class)},\r
436         extensions = {\r
437             @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"),\r
438                 @ExtensionProperty(name = "last-mod-release", value = "Dublin")})})\r
439     @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"),\r
440         @ApiResponse(code = 403, message = "Authorization Error"),\r
441         @ApiResponse(code = 404, message = "Resource Not Found"),\r
442         @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),\r
443         @ApiResponse(code = 500, message = "Internal Server Error")})\r
444     public Response deleteSpecificVersionOfPolicyType(\r
445         @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,\r
446         @PathParam("versionId") @ApiParam(value = "Version of policy type", required = true) String versionId,\r
447         @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
448 \r
449         try (PolicyTypeProvider policyTypeProvider = new PolicyTypeProvider()) {\r
450             ToscaServiceTemplate serviceTemplate = policyTypeProvider.deletePolicyType(policyTypeId, versionId);\r
451             return makeOkResponse(requestId, serviceTemplate);\r
452         } catch (PfModelException | PfModelRuntimeException pfme) {\r
453             LOGGER.debug("DELETE /policytypes/{}/versions/{}", policyTypeId, versionId, pfme);\r
454             return makeErrorResponse(requestId, pfme);\r
455         }\r
456     }\r
457 \r
458     /**\r
459      * Retrieves all versions of a particular policy.\r
460      *\r
461      * @param policyTypeId the ID of specified policy type\r
462      * @param policyTypeVersion the version of specified policy type\r
463      *\r
464      * @return the Response object containing the results of the API operation\r
465      */\r
466     // @formatter:off\r
467     @GET\r
468     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies")\r
469     @ApiOperation(\r
470         value = "Retrieve all versions of a policy created for a particular policy type version",\r
471         notes = "Returns a list of all versions of specified policy created for the specified policy type version",\r
472         response = ToscaServiceTemplate.class,\r
473         responseHeaders = {\r
474             @ResponseHeader(name = "X-MinorVersion",\r
475                 description = "Used to request or communicate a MINOR version back from the client"\r
476                     + " to the server, and from the server back to the client",\r
477                 response = String.class),\r
478             @ResponseHeader(name = "X-PatchVersion",\r
479                 description = "Used only to communicate a PATCH version in a response for"\r
480                     + " troubleshooting purposes only, and will not be provided by" + " the client on request",\r
481                 response = String.class),\r
482             @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version",\r
483                 response = String.class),\r
484             @ResponseHeader(name = "X-ONAP-RequestID",\r
485                 description = "Used to track REST transactions for logging purpose", response = UUID.class)},\r
486         authorizations = @Authorization(value = "basicAuth"), tags = {"Policy,"},\r
487         extensions = {\r
488             @Extension(name = "interface info", properties = {\r
489                 @ExtensionProperty(name = "api-version", value = "1.0.0"),\r
490                 @ExtensionProperty(name = "last-mod-release", value = "Dublin")\r
491             })\r
492         }\r
493     )\r
494     @ApiResponses(value = {\r
495         @ApiResponse(code = 401, message = "Authentication Error"),\r
496         @ApiResponse(code = 403, message = "Authorization Error"),\r
497         @ApiResponse(code = 404, message = "Resource Not Found"),\r
498         @ApiResponse(code = 500, message = "Internal Server Error")\r
499     })\r
500     public Response getAllPolicies(\r
501         @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,\r
502         @PathParam("policyTypeVersion") @ApiParam(value = "Version of policy type",\r
503             required = true) String policyTypeVersion,\r
504         @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,\r
505         @QueryParam("mode") @DefaultValue("bare") @ApiParam("Fetch mode for policies, BARE for bare policies (default),"\r
506             + " REFERENCED for fully referenced policies") PolicyFetchMode mode\r
507     ) {\r
508 \r
509         try (PolicyProvider policyProvider = new PolicyProvider()) {\r
510             ToscaServiceTemplate serviceTemplate =\r
511                 policyProvider.fetchPolicies(policyTypeId, policyTypeVersion, null, null, mode);\r
512             updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.GET);\r
513             return makeOkResponse(requestId, serviceTemplate);\r
514         } catch (PfModelException | PfModelRuntimeException pfme) {\r
515             LOGGER.debug("GET /policytypes/{}/versions/{}/policies", policyTypeId, policyTypeVersion, pfme);\r
516             updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.GET);\r
517             return makeErrorResponse(requestId, pfme);\r
518         }\r
519     }\r
520     // @formatter:on\r
521 \r
522     /**\r
523      * Retrieves all versions of a particular policy.\r
524      *\r
525      * @param policyTypeId the ID of specified policy type\r
526      * @param policyTypeVersion the version of specified policy type\r
527      * @param policyId the ID of specified policy\r
528      *\r
529      * @return the Response object containing the results of the API operation\r
530      */\r
531     // @formatter:off\r
532     @GET\r
533     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}")\r
534     @ApiOperation(value = "Retrieve all version details of a policy created for a particular policy type version",\r
535         notes = "Returns a list of all version details of the specified policy", response = ToscaServiceTemplate.class,\r
536         responseHeaders = {\r
537             @ResponseHeader(name = "X-MinorVersion",\r
538                 description = "Used to request or communicate a MINOR version back from the client"\r
539                     + " to the server, and from the server back to the client",\r
540                 response = String.class),\r
541             @ResponseHeader(name = "X-PatchVersion",\r
542                 description = "Used only to communicate a PATCH version in a response for"\r
543                     + " troubleshooting purposes only, and will not be provided by" + " the client on request",\r
544                 response = String.class),\r
545             @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version",\r
546                 response = String.class),\r
547             @ResponseHeader(name = "X-ONAP-RequestID",\r
548                 description = "Used to track REST transactions for logging purpose", response = UUID.class)},\r
549         authorizations = @Authorization(value = "basicAuth"), tags = {"Policy", },\r
550         extensions = {\r
551             @Extension(name = "interface info", properties = {\r
552                 @ExtensionProperty(name = "api-version", value = "1.0.0"),\r
553                 @ExtensionProperty(name = "last-mod-release", value = "Dublin")\r
554             })\r
555         }\r
556     )\r
557     @ApiResponses(value = {\r
558         @ApiResponse(code = 401, message = "Authentication Error"),\r
559         @ApiResponse(code = 403, message = "Authorization Error"),\r
560         @ApiResponse(code = 404, message = "Resource Not Found"),\r
561         @ApiResponse(code = 500, message = "Internal Server Error")\r
562     })\r
563     public Response getAllVersionsOfPolicy(\r
564         @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,\r
565         @PathParam("policyTypeVersion") @ApiParam(value = "Version of policy type",\r
566             required = true) String policyTypeVersion,\r
567         @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,\r
568         @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,\r
569         @QueryParam("mode") @DefaultValue("bare") @ApiParam("Fetch mode for policies, BARE for bare policies (default),"\r
570             + " REFERENCED for fully referenced policies") PolicyFetchMode mode\r
571     ) {\r
572         try (PolicyProvider policyProvider = new PolicyProvider()) {\r
573             ToscaServiceTemplate serviceTemplate =\r
574                 policyProvider.fetchPolicies(policyTypeId, policyTypeVersion, policyId, null, mode);\r
575             updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.GET);\r
576             return makeOkResponse(requestId, serviceTemplate);\r
577         } catch (PfModelException | PfModelRuntimeException pfme) {\r
578             LOGGER.debug("/policytypes/{}/versions/{}/policies/{}", policyTypeId, policyTypeVersion, policyId, pfme);\r
579             updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.GET);\r
580             return makeErrorResponse(requestId, pfme);\r
581         }\r
582     }\r
583     // @formatter:on\r
584 \r
585     /**\r
586      * Retrieves the specified version of a particular policy.\r
587      *\r
588      * @param policyTypeId the ID of specified policy type\r
589      * @param policyTypeVersion the version of specified policy type\r
590      * @param policyId the ID of specified policy\r
591      * @param policyVersion the version of specified policy\r
592      *\r
593      * @return the Response object containing the results of the API operation\r
594      */\r
595     // @formatter:off\r
596     @GET\r
597     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/{policyVersion}")\r
598     @ApiOperation(value = "Retrieve one version of a policy created for a particular policy type version",\r
599         notes = "Returns a particular version of specified policy created for the specified policy type version",\r
600         response = ToscaServiceTemplate.class,\r
601         responseHeaders = {\r
602             @ResponseHeader(name = "X-MinorVersion",\r
603                 description = "Used to request or communicate a MINOR version back from the client"\r
604                     + " to the server, and from the server back to the client",\r
605                 response = String.class),\r
606             @ResponseHeader(name = "X-PatchVersion",\r
607                 description = "Used only to communicate a PATCH version in a response for"\r
608                     + " troubleshooting purposes only, and will not be provided by" + " the client on request",\r
609                 response = String.class),\r
610             @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version",\r
611                 response = String.class),\r
612             @ResponseHeader(name = "X-ONAP-RequestID",\r
613                 description = "Used to track REST transactions for logging purpose", response = UUID.class)},\r
614         authorizations = @Authorization(value = "basicAuth"), tags = {"Policy", },\r
615         extensions = {\r
616             @Extension(name = "interface info", properties = {\r
617                 @ExtensionProperty(name = "api-version", value = "1.0.0"),\r
618                 @ExtensionProperty(name = "last-mod-release", value = "Dublin")\r
619             })\r
620         }\r
621     )\r
622     @ApiResponses(value = {\r
623         @ApiResponse(code = 401, message = "Authentication Error"),\r
624         @ApiResponse(code = 403, message = "Authorization Error"),\r
625         @ApiResponse(code = 404, message = "Resource Not Found"),\r
626         @ApiResponse(code = 500, message = "Internal Server Error")\r
627     })\r
628     public Response getSpecificVersionOfPolicy(\r
629         @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,\r
630         @PathParam("policyTypeVersion") @ApiParam(value = "Version of policy type",\r
631             required = true) String policyTypeVersion,\r
632         @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,\r
633         @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,\r
634         @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,\r
635         @QueryParam("mode") @DefaultValue("bare") @ApiParam("Fetch mode for policies, BARE for bare policies (default),"\r
636             + " REFERENCED for fully referenced policies") PolicyFetchMode mode\r
637     ) {\r
638         try (PolicyProvider policyProvider = new PolicyProvider()) {\r
639             ToscaServiceTemplate serviceTemplate =\r
640                 policyProvider.fetchPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion, mode);\r
641             updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.GET);\r
642             return makeOkResponse(requestId, serviceTemplate);\r
643         } catch (PfModelException | PfModelRuntimeException pfme) {\r
644             LOGGER.debug("GET /policytypes/{}/versions/{}/policies/{}/versions/{}", policyTypeId, policyTypeVersion,\r
645                 policyId, policyVersion, pfme);\r
646             updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.GET);\r
647             return makeErrorResponse(requestId, pfme);\r
648         }\r
649     }\r
650     // @formatter:on\r
651 \r
652     /**\r
653      * Retrieves the latest version of a particular policy.\r
654      *\r
655      * @param policyTypeId the ID of specified policy type\r
656      * @param policyTypeVersion the version of specified policy type\r
657      * @param policyId the ID of specified policy\r
658      *\r
659      * @return the Response object containing the results of the API operation\r
660      */\r
661     @GET\r
662     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/latest")\r
663     @ApiOperation(value = "Retrieve the latest version of a particular policy",\r
664         notes = "Returns the latest version of specified policy", response = ToscaServiceTemplate.class,\r
665         responseHeaders = {\r
666             @ResponseHeader(name = "X-MinorVersion",\r
667                 description = "Used to request or communicate a MINOR version back from the client"\r
668                     + " to the server, and from the server back to the client",\r
669                 response = String.class),\r
670             @ResponseHeader(name = "X-PatchVersion",\r
671                 description = "Used only to communicate a PATCH version in a response for"\r
672                     + " troubleshooting purposes only, and will not be provided by" + " the client on request",\r
673                 response = String.class),\r
674             @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version",\r
675                 response = String.class),\r
676             @ResponseHeader(name = "X-ONAP-RequestID",\r
677                 description = "Used to track REST transactions for logging purpose", response = UUID.class)},\r
678         authorizations = @Authorization(value = "basicAuth"), tags = {"Policy", },\r
679         extensions = {\r
680             @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"),\r
681                 @ExtensionProperty(name = "last-mod-release", value = "Dublin")})})\r
682     @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"),\r
683         @ApiResponse(code = 403, message = "Authorization Error"),\r
684         @ApiResponse(code = 404, message = "Resource Not Found"),\r
685         @ApiResponse(code = 500, message = "Internal Server Error")})\r
686     public Response getLatestVersionOfPolicy(\r
687         @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,\r
688         @PathParam("policyTypeVersion") @ApiParam(value = "Version of policy type",\r
689             required = true) String policyTypeVersion,\r
690         @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,\r
691         @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,\r
692         @QueryParam("mode") @ApiParam("Fetch mode for policies, TERSE for bare policies (default), "\r
693             + "REFERENCED for fully referenced policies") PolicyFetchMode mode) {\r
694 \r
695         try (PolicyProvider policyProvider = new PolicyProvider()) {\r
696             ToscaServiceTemplate serviceTemplate =\r
697                 policyProvider.fetchLatestPolicies(policyTypeId, policyTypeVersion, policyId, mode);\r
698             updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.GET);\r
699             return makeOkResponse(requestId, serviceTemplate);\r
700         } catch (PfModelException | PfModelRuntimeException pfme) {\r
701             LOGGER.debug("GET /policytypes/{}/versions/{}/policies/{}/versions/latest", policyTypeId, policyTypeVersion,\r
702                 policyId, pfme);\r
703             updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.GET);\r
704             return makeErrorResponse(requestId, pfme);\r
705         }\r
706     }\r
707 \r
708     /**\r
709      * Creates a new policy for a particular policy type and version.\r
710      *\r
711      * @param policyTypeId the ID of specified policy type\r
712      * @param policyTypeVersion the version of specified policy type\r
713      * @param body the body of policy following TOSCA definition\r
714      *\r
715      * @return the Response object containing the results of the API operation\r
716      */\r
717     @POST\r
718     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies")\r
719     @ApiOperation(value = "Create a new policy for a policy type version",\r
720         notes = "Client should provide TOSCA body of the new policy",\r
721         authorizations = @Authorization(value = "basicAuth"), tags = {"Policy", },\r
722             response = ToscaServiceTemplate.class,\r
723         responseHeaders = {\r
724             @ResponseHeader(name = "X-MinorVersion",\r
725                 description = "Used to request or communicate a MINOR version back from the client"\r
726                     + " to the server, and from the server back to the client",\r
727                 response = String.class),\r
728             @ResponseHeader(name = "X-PatchVersion",\r
729                 description = "Used only to communicate a PATCH version in a response for"\r
730                     + " troubleshooting purposes only, and will not be provided by" + " the client on request",\r
731                 response = String.class),\r
732             @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version",\r
733                 response = String.class),\r
734             @ResponseHeader(name = "X-ONAP-RequestID",\r
735                 description = "Used to track REST transactions for logging purpose", response = UUID.class)},\r
736         extensions = {\r
737             @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"),\r
738                 @ExtensionProperty(name = "last-mod-release", value = "Dublin")})})\r
739     @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid Body"),\r
740         @ApiResponse(code = 401, message = "Authentication Error"),\r
741         @ApiResponse(code = 403, message = "Authorization Error"),\r
742         @ApiResponse(code = 404, message = "Resource Not Found"),\r
743         @ApiResponse(code = 406, message = "Not Acceptable Payload"),\r
744         @ApiResponse(code = 500, message = "Internal Server Error")})\r
745     public Response createPolicy(\r
746         @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,\r
747         @PathParam("policyTypeVersion") @ApiParam(value = "Version of policy type",\r
748             required = true) String policyTypeVersion,\r
749         @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,\r
750         @ApiParam(value = "Entity body of policy", required = true) ToscaServiceTemplate body) {\r
751 \r
752         if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {\r
753             NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST,\r
754                 "/policytypes/" + policyTypeId + "/versions/" + policyTypeVersion + "/policies", toJson(body));\r
755         }\r
756 \r
757         try (PolicyProvider policyProvider = new PolicyProvider()) {\r
758             ToscaServiceTemplate serviceTemplate = policyProvider.createPolicy(policyTypeId, policyTypeVersion, body);\r
759             updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.POST);\r
760             return makeOkResponse(requestId, serviceTemplate);\r
761         } catch (PfModelException | PfModelRuntimeException pfme) {\r
762             LOGGER.debug("POST /policytypes/{}/versions/{}/policies", policyTypeId, policyTypeVersion, pfme);\r
763             updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.POST);\r
764             return makeErrorResponse(requestId, pfme);\r
765         }\r
766     }\r
767 \r
768     /**\r
769      * Creates one or more new policies in one call.\r
770      *\r
771      * @param body the body of policy following TOSCA definition\r
772      *\r
773      * @return the Response object containing the results of the API operation\r
774      */\r
775     @POST\r
776     @Path("/policies")\r
777     @ApiOperation(value = "Create one or more new policies",\r
778         notes = "Client should provide TOSCA body of the new polic(ies)",\r
779         authorizations = @Authorization(value = "basicAuth"), tags = {"Policy", },\r
780             response = ToscaServiceTemplate.class,\r
781         responseHeaders = {\r
782             @ResponseHeader(name = "X-MinorVersion",\r
783                 description = "Used to request or communicate a MINOR version back from the client"\r
784                     + " to the server, and from the server back to the client",\r
785                 response = String.class),\r
786             @ResponseHeader(name = "X-PatchVersion",\r
787                 description = "Used only to communicate a PATCH version in a response for"\r
788                     + " troubleshooting purposes only, and will not be provided by" + " the client on request",\r
789                 response = String.class),\r
790             @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version",\r
791                 response = String.class),\r
792             @ResponseHeader(name = "X-ONAP-RequestID",\r
793                 description = "Used to track REST transactions for logging purpose", response = UUID.class)},\r
794         extensions = {\r
795             @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"),\r
796                 @ExtensionProperty(name = "last-mod-release", value = "El Alto")})})\r
797     @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid Body"),\r
798         @ApiResponse(code = 401, message = "Authentication Error"),\r
799         @ApiResponse(code = 403, message = "Authorization Error"),\r
800         @ApiResponse(code = 404, message = "Resource Not Found"),\r
801         @ApiResponse(code = 406, message = "Not Acceptable Payload"),\r
802         @ApiResponse(code = 500, message = "Internal Server Error")})\r
803     public Response createPolicies(\r
804         @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,\r
805         @ApiParam(value = "Entity body of policy", required = true) ToscaServiceTemplate body) {\r
806 \r
807         if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {\r
808             NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, "/policies", toJson(body));\r
809         }\r
810 \r
811         try (PolicyProvider policyProvider = new PolicyProvider()) {\r
812             ToscaServiceTemplate serviceTemplate = policyProvider.createPolicies(body);\r
813             updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.POST);\r
814             return makeOkResponse(requestId, serviceTemplate);\r
815         } catch (PfModelException | PfModelRuntimeException pfme) {\r
816             LOGGER.debug("POST /policies", pfme);\r
817             updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.POST);\r
818             return makeErrorResponse(requestId, pfme);\r
819         }\r
820     }\r
821 \r
822     /**\r
823      * Deletes the specified version of a particular policy.\r
824      *\r
825      * @param policyTypeId the ID of specified policy type\r
826      * @param policyTypeVersion the version of specified policy type\r
827      * @param policyId the ID of specified policy\r
828      * @param policyVersion the version of specified policy\r
829      *\r
830      * @return the Response object containing the results of the API operation\r
831      */\r
832     @DELETE\r
833     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/{policyVersion}")\r
834     @ApiOperation(value = "Delete a particular version of a policy",\r
835         notes = "Rule: the version that has been deployed in PDP group(s) cannot be deleted",\r
836         authorizations = @Authorization(value = "basicAuth"), tags = {"Policy", },\r
837             response = ToscaServiceTemplate.class,\r
838         responseHeaders = {\r
839             @ResponseHeader(name = "X-MinorVersion",\r
840                 description = "Used to request or communicate a MINOR version back from the client"\r
841                     + " to the server, and from the server back to the client",\r
842                 response = String.class),\r
843             @ResponseHeader(name = "X-PatchVersion",\r
844                 description = "Used only to communicate a PATCH version in a response for"\r
845                     + " troubleshooting purposes only, and will not be provided by" + " the client on request",\r
846                 response = String.class),\r
847             @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version",\r
848                 response = String.class),\r
849             @ResponseHeader(name = "X-ONAP-RequestID",\r
850                 description = "Used to track REST transactions for logging purpose", response = UUID.class)},\r
851         extensions = {\r
852             @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"),\r
853                 @ExtensionProperty(name = "last-mod-release", value = "Dublin")})})\r
854     @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"),\r
855         @ApiResponse(code = 403, message = "Authorization Error"),\r
856         @ApiResponse(code = 404, message = "Resource Not Found"),\r
857         @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),\r
858         @ApiResponse(code = 500, message = "Internal Server Error")})\r
859     public Response deleteSpecificVersionOfPolicy(\r
860         @PathParam("policyTypeId") @ApiParam(value = "PolicyType ID", required = true) String policyTypeId,\r
861         @PathParam("policyTypeVersion") @ApiParam(value = "Version of policy type",\r
862             required = true) String policyTypeVersion,\r
863         @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,\r
864         @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,\r
865         @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
866 \r
867         try (PolicyProvider policyProvider = new PolicyProvider()) {\r
868             ToscaServiceTemplate serviceTemplate =\r
869                 policyProvider.deletePolicy(policyTypeId, policyTypeVersion, policyId, policyVersion);\r
870             return makeOkResponse(requestId, serviceTemplate);\r
871         } catch (PfModelException | PfModelRuntimeException pfme) {\r
872             LOGGER.debug("DELETE /policytypes/{}/versions/{}/policies/{}/versions/{}", policyTypeId, policyTypeVersion,\r
873                 policyId, policyVersion, pfme);\r
874             return makeErrorResponse(requestId, pfme);\r
875         }\r
876     }\r
877 \r
878     private enum Target {\r
879         POLICY,\r
880         POLICY_TYPE,\r
881         OTHER\r
882     }\r
883 \r
884     private enum Result {\r
885         SUCCESS,\r
886         FAILURE\r
887     }\r
888 \r
889     private enum HttpMethod {\r
890         POST,\r
891         GET\r
892     }\r
893 \r
894     private void updateApiStatisticsCounter(Target target, Result result, HttpMethod http) {\r
895 \r
896         ApiStatisticsManager.updateTotalApiCallCount();\r
897 \r
898         switch (target) {\r
899             case POLICY:\r
900                 updatePolicyStats(result, http);\r
901                 break;\r
902             case POLICY_TYPE:\r
903                 updatePolicyTypeStats(result, http);\r
904                 break;\r
905             default:\r
906                 ApiStatisticsManager.updateApiCallSuccessCount();\r
907                 break;\r
908         }\r
909     }\r
910 \r
911     private void updatePolicyStats(Result result, HttpMethod http) {\r
912         if (result == Result.SUCCESS) {\r
913             if (http == HttpMethod.GET) {\r
914                 ApiStatisticsManager.updateApiCallSuccessCount();\r
915                 ApiStatisticsManager.updateTotalPolicyGetCount();\r
916                 ApiStatisticsManager.updatePolicyGetSuccessCount();\r
917             } else if (http == HttpMethod.POST) {\r
918                 ApiStatisticsManager.updateApiCallSuccessCount();\r
919                 ApiStatisticsManager.updateTotalPolicyPostCount();\r
920                 ApiStatisticsManager.updatePolicyPostSuccessCount();\r
921             }\r
922         } else {\r
923             if (http == HttpMethod.GET) {\r
924                 ApiStatisticsManager.updateApiCallFailureCount();\r
925                 ApiStatisticsManager.updateTotalPolicyGetCount();\r
926                 ApiStatisticsManager.updatePolicyGetFailureCount();\r
927             } else {\r
928                 ApiStatisticsManager.updateApiCallFailureCount();\r
929                 ApiStatisticsManager.updateTotalPolicyPostCount();\r
930                 ApiStatisticsManager.updatePolicyPostFailureCount();\r
931             }\r
932         }\r
933     }\r
934 \r
935     private void updatePolicyTypeStats(Result result, HttpMethod http) {\r
936         if (result == Result.SUCCESS) {\r
937             if (http == HttpMethod.GET) {\r
938                 ApiStatisticsManager.updateApiCallSuccessCount();\r
939                 ApiStatisticsManager.updateTotalPolicyTypeGetCount();\r
940                 ApiStatisticsManager.updatePolicyTypeGetSuccessCount();\r
941             } else if (http == HttpMethod.POST) {\r
942                 ApiStatisticsManager.updateApiCallSuccessCount();\r
943                 ApiStatisticsManager.updatePolicyTypePostSuccessCount();\r
944                 ApiStatisticsManager.updatePolicyTypePostSuccessCount();\r
945             }\r
946         } else {\r
947             if (http == HttpMethod.GET) {\r
948                 ApiStatisticsManager.updateApiCallFailureCount();\r
949                 ApiStatisticsManager.updateTotalPolicyTypeGetCount();\r
950                 ApiStatisticsManager.updatePolicyTypeGetFailureCount();\r
951             } else {\r
952                 ApiStatisticsManager.updateApiCallFailureCount();\r
953                 ApiStatisticsManager.updateTotalPolicyTypePostCount();\r
954                 ApiStatisticsManager.updatePolicyTypePostFailureCount();\r
955             }\r
956         }\r
957     }\r
958 }