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