Fix api sonar bugs - logging exceptions
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / LegacyApiRestController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.api.main.rest;
24
25 import io.swagger.annotations.Api;
26 import io.swagger.annotations.ApiOperation;
27 import io.swagger.annotations.ApiParam;
28 import io.swagger.annotations.ApiResponse;
29 import io.swagger.annotations.ApiResponses;
30 import io.swagger.annotations.Authorization;
31 import io.swagger.annotations.Extension;
32 import io.swagger.annotations.ExtensionProperty;
33 import io.swagger.annotations.ResponseHeader;
34 import java.util.Map;
35 import java.util.UUID;
36 import javax.ws.rs.Consumes;
37 import javax.ws.rs.DELETE;
38 import javax.ws.rs.GET;
39 import javax.ws.rs.HeaderParam;
40 import javax.ws.rs.POST;
41 import javax.ws.rs.Path;
42 import javax.ws.rs.PathParam;
43 import javax.ws.rs.Produces;
44 import javax.ws.rs.core.Response;
45 import javax.ws.rs.core.Response.ResponseBuilder;
46 import org.onap.policy.api.main.rest.provider.LegacyGuardPolicyProvider;
47 import org.onap.policy.api.main.rest.provider.LegacyOperationalPolicyProvider;
48 import org.onap.policy.models.base.PfModelException;
49 import org.onap.policy.models.base.PfModelRuntimeException;
50 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
51 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
52 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 /**
57  * Class to provide legacy REST API services.
58  *
59  * @author Chenfei Gao (cgao@research.att.com)
60  */
61 @Path("/policy/api/v1")
62 @Api(value = "Legacy Policy Design API")
63 @Produces("application/json")
64 @Consumes("application/json")
65 public class LegacyApiRestController {
66
67     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyApiRestController.class);
68
69     /**
70      * Retrieves all versions of a particular guard policy.
71      *
72      * @param policyId the ID of specified guard policy
73      *
74      * @return the Response object containing the results of the API operation
75      */
76     @GET
77     @Path("/policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/{policyId}")
78     @ApiOperation(value = "Retrieve all versions of a particular guard policy",
79             notes = "Returns a list of all versions of the specified guard policy",
80             response = LegacyGuardPolicyOutput.class, responseContainer = "Map",
81             responseHeaders = {
82                     @ResponseHeader(name = "X-MinorVersion",
83                                     description = "Used to request or communicate a MINOR version back from the client"
84                                                 + " to the server, and from the server back to the client",
85                                     response = String.class),
86                     @ResponseHeader(name = "X-PatchVersion",
87                                     description = "Used only to communicate a PATCH version in a response for"
88                                                 + " troubleshooting purposes only, and will not be provided by"
89                                                 + " the client on request",
90                                     response = String.class),
91                     @ResponseHeader(name = "X-LatestVersion",
92                                     description = "Used only to communicate an API's latest version",
93                                     response = String.class),
94                     @ResponseHeader(name = "X-ONAP-RequestID",
95                                     description = "Used to track REST transactions for logging purpose",
96                                     response = UUID.class)
97             },
98             authorizations = @Authorization(value = "basicAuth"),
99             tags = { "Legacy Guard Policy", },
100             extensions = {
101                     @Extension(name = "interface info", properties = {
102                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
103                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
104                     })
105             })
106     @ApiResponses(value = {
107             @ApiResponse(code = 401, message = "Authentication Error"),
108             @ApiResponse(code = 403, message = "Authorization Error"),
109             @ApiResponse(code = 404, message = "Resource Not Found"),
110             @ApiResponse(code = 500, message = "Internal Server Error")
111         })
112     public Response getAllVersionsOfGuardPolicy(
113             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
114             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
115
116         try (LegacyGuardPolicyProvider guardPolicyProvider = new LegacyGuardPolicyProvider()) {
117             Map<String, LegacyGuardPolicyOutput> policies = guardPolicyProvider.fetchGuardPolicy(policyId, null);
118             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
119                     .entity(policies).build();
120         } catch (PfModelException | PfModelRuntimeException pfme) {
121             LOGGER.error("GET /policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/{}",
122                     policyId, pfme);
123             return addLoggingHeaders(addVersionControlHeaders(
124                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
125                     .entity(pfme.getErrorResponse()).build();
126         }
127     }
128
129     /**
130      * Retrieves the specified version of a particular guard policy.
131      *
132      * @param policyId the ID of specified policy
133      * @param policyVersion the version of specified policy
134      *
135      * @return the Response object containing the results of the API operation
136      */
137     @GET
138     @Path("/policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/{policyId}/versions/{policyVersion}")
139     @ApiOperation(value = "Retrieve one version of a particular guard policy",
140             notes = "Returns a particular version of a specified guard policy",
141             response = LegacyGuardPolicyOutput.class, responseContainer = "Map",
142             responseHeaders = {
143                     @ResponseHeader(name = "X-MinorVersion",
144                                     description = "Used to request or communicate a MINOR version back from the client"
145                                                 + " to the server, and from the server back to the client",
146                                     response = String.class),
147                     @ResponseHeader(name = "X-PatchVersion",
148                                     description = "Used only to communicate a PATCH version in a response for"
149                                                 + " troubleshooting purposes only, and will not be provided by"
150                                                 + " the client on request",
151                                     response = String.class),
152                     @ResponseHeader(name = "X-LatestVersion",
153                                     description = "Used only to communicate an API's latest version",
154                                     response = String.class),
155                     @ResponseHeader(name = "X-ONAP-RequestID",
156                                     description = "Used to track REST transactions for logging purpose",
157                                     response = UUID.class)
158             },
159             authorizations = @Authorization(value = "basicAuth"),
160             tags = { "Legacy Guard Policy", },
161             extensions = {
162                     @Extension(name = "interface info", properties = {
163                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
164                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
165                     })
166             })
167     @ApiResponses(value = {
168             @ApiResponse(code = 401, message = "Authentication Error"),
169             @ApiResponse(code = 403, message = "Authorization Error"),
170             @ApiResponse(code = 404, message = "Resource Not Found"),
171             @ApiResponse(code = 500, message = "Internal Server Error")
172         })
173     public Response getSpecificVersionOfGuardPolicy(
174             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
175             @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,
176             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
177
178         try (LegacyGuardPolicyProvider guardPolicyProvider = new LegacyGuardPolicyProvider()) {
179             Map<String, LegacyGuardPolicyOutput> policies = guardPolicyProvider
180                     .fetchGuardPolicy(policyId, policyVersion);
181             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
182                     .entity(policies).build();
183         } catch (PfModelException | PfModelRuntimeException pfme) {
184             LOGGER.error("GET /policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/{}/versions/{}",
185                     policyId, policyVersion, pfme);
186             return addLoggingHeaders(addVersionControlHeaders(
187                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
188                     .entity(pfme.getErrorResponse()).build();
189         }
190     }
191
192     /**
193      * Creates a new guard policy.
194      *
195      * @param body the body of policy
196      *
197      * @return the Response object containing the results of the API operation
198      */
199     @POST
200     @Path("/policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies")
201     @ApiOperation(value = "Create a new guard policy",
202             notes = "Client should provide entity body of the new guard policy",
203             authorizations = @Authorization(value = "basicAuth"),
204             tags = { "Legacy Guard Policy", },
205             response = LegacyGuardPolicyOutput.class, responseContainer = "Map",
206             responseHeaders = {
207                     @ResponseHeader(name = "X-MinorVersion",
208                                     description = "Used to request or communicate a MINOR version back from the client"
209                                                 + " to the server, and from the server back to the client",
210                                     response = String.class),
211                     @ResponseHeader(name = "X-PatchVersion",
212                                     description = "Used only to communicate a PATCH version in a response for"
213                                                 + " troubleshooting purposes only, and will not be provided by"
214                                                 + " the client on request",
215                                     response = String.class),
216                     @ResponseHeader(name = "X-LatestVersion",
217                                     description = "Used only to communicate an API's latest version",
218                                     response = String.class),
219                     @ResponseHeader(name = "X-ONAP-RequestID",
220                                     description = "Used to track REST transactions for logging purpose",
221                                     response = UUID.class)
222             },
223             extensions = {
224                     @Extension(name = "interface info", properties = {
225                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
226                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
227                     })
228             })
229     @ApiResponses(value = {
230             @ApiResponse(code = 400, message = "Invalid Body"),
231             @ApiResponse(code = 401, message = "Authentication Error"),
232             @ApiResponse(code = 403, message = "Authorization Error"),
233             @ApiResponse(code = 500, message = "Internal Server Error")
234         })
235     public Response createGuardPolicy(
236             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,
237             @ApiParam(value = "Entity body of policy", required = true) LegacyGuardPolicyInput body) {
238
239         try (LegacyGuardPolicyProvider guardPolicyProvider = new LegacyGuardPolicyProvider()) {
240             Map<String, LegacyGuardPolicyOutput> policy = guardPolicyProvider.createGuardPolicy(body);
241             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
242                     .entity(policy).build();
243         } catch (PfModelException | PfModelRuntimeException pfme) {
244             LOGGER.error("POST /policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies", pfme);
245             return addLoggingHeaders(addVersionControlHeaders(
246                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
247                     .entity(pfme.getErrorResponse()).build();
248         }
249     }
250
251     /**
252      * Deletes the specified version of a particular guard policy.
253      *
254      * @param policyId the ID of specified policy
255      * @param policyVersion the version of specified policy
256      *
257      * @return the Response object containing the results of the API operation
258      */
259     @DELETE
260     @Path("/policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/{policyId}/versions/{policyVersion}")
261     @ApiOperation(value = "Delete a particular version of a guard policy",
262             notes = "Rule: the version that has been deployed in PDP group(s) cannot be deleted",
263             authorizations = @Authorization(value = "basicAuth"),
264             tags = { "Legacy Guard Policy", },
265             response = LegacyGuardPolicyOutput.class, responseContainer = "Map",
266             responseHeaders = {
267                     @ResponseHeader(name = "X-MinorVersion",
268                                     description = "Used to request or communicate a MINOR version back from the client"
269                                                 + " to the server, and from the server back to the client",
270                                     response = String.class),
271                     @ResponseHeader(name = "X-PatchVersion",
272                                     description = "Used only to communicate a PATCH version in a response for"
273                                                 + " troubleshooting purposes only, and will not be provided by"
274                                                 + " the client on request",
275                                     response = String.class),
276                     @ResponseHeader(name = "X-LatestVersion",
277                                     description = "Used only to communicate an API's latest version",
278                                     response = String.class),
279                     @ResponseHeader(name = "X-ONAP-RequestID",
280                                     description = "Used to track REST transactions for logging purpose",
281                                     response = UUID.class)
282             },
283             extensions = {
284                     @Extension(name = "interface info", properties = {
285                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
286                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
287                     })
288             })
289     @ApiResponses(value = {
290             @ApiResponse(code = 401, message = "Authentication Error"),
291             @ApiResponse(code = 403, message = "Authorization Error"),
292             @ApiResponse(code = 404, message = "Resource Not Found"),
293             @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
294             @ApiResponse(code = 500, message = "Internal Server Error")
295         })
296     public Response deleteSpecificVersionOfGuardPolicy(
297             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
298             @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,
299             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
300
301         try (LegacyGuardPolicyProvider guardPolicyProvider = new LegacyGuardPolicyProvider()) {
302             Map<String, LegacyGuardPolicyOutput> policies = guardPolicyProvider
303                     .deleteGuardPolicy(policyId, policyVersion);
304             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
305                     .entity(policies).build();
306         } catch (PfModelException | PfModelRuntimeException pfme) {
307             LOGGER.error("DELETE /policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/{}/versions/{}",
308                     policyId, policyVersion, pfme);
309             return addLoggingHeaders(addVersionControlHeaders(
310                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
311                     .entity(pfme.getErrorResponse()).build();
312         }
313     }
314
315     /**
316      * Retrieves all versions of a particular operational policy.
317      *
318      * @param policyId the ID of specified operational policy
319      *
320      * @return the Response object containing the results of the API operation
321      */
322     @GET
323     @Path("/policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies/{policyId}")
324     @ApiOperation(value = "Retrieve all versions of a particular operational policy",
325             notes = "Returns a list of all versions of the specified operational policy",
326             response = LegacyOperationalPolicy.class,
327             responseHeaders = {
328                     @ResponseHeader(name = "X-MinorVersion",
329                                     description = "Used to request or communicate a MINOR version back from the client"
330                                                 + " to the server, and from the server back to the client",
331                                     response = String.class),
332                     @ResponseHeader(name = "X-PatchVersion",
333                                     description = "Used only to communicate a PATCH version in a response for"
334                                                 + " troubleshooting purposes only, and will not be provided by"
335                                                 + " the client on request",
336                                     response = String.class),
337                     @ResponseHeader(name = "X-LatestVersion",
338                                     description = "Used only to communicate an API's latest version",
339                                     response = String.class),
340                     @ResponseHeader(name = "X-ONAP-RequestID",
341                                     description = "Used to track REST transactions for logging purpose",
342                                     response = UUID.class)
343             },
344             authorizations = @Authorization(value = "basicAuth"),
345             tags = { "Legacy Operational Policy", },
346             extensions = {
347                     @Extension(name = "interface info", properties = {
348                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
349                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
350                     })
351             })
352     @ApiResponses(value = {
353             @ApiResponse(code = 401, message = "Authentication Error"),
354             @ApiResponse(code = 403, message = "Authorization Error"),
355             @ApiResponse(code = 404, message = "Resource Not Found"),
356             @ApiResponse(code = 500, message = "Internal Server Error")
357         })
358     public Response getAllVersionsOfOperationalPolicy(
359             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
360             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
361
362         try (LegacyOperationalPolicyProvider operationalPolicyProvider = new LegacyOperationalPolicyProvider()) {
363             LegacyOperationalPolicy policy = operationalPolicyProvider.fetchOperationalPolicy(policyId, null);
364             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
365                     .entity(policy).build();
366         } catch (PfModelException | PfModelRuntimeException pfme) {
367             LOGGER.error("GET /policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies/{}",
368                     policyId, pfme);
369             return addLoggingHeaders(addVersionControlHeaders(
370                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
371                     .entity(pfme.getErrorResponse()).build();
372         }
373     }
374
375     /**
376      * Retrieves the specified version of a particular operational policy.
377      *
378      * @param policyId the ID of specified policy
379      * @param policyVersion the version of specified policy
380      *
381      * @return the Response object containing the results of the API operation
382      */
383     @GET
384     @Path("/policytypes/onap.policies.controlloop.Operational/versions/1.0.0/"
385          + "policies/{policyId}/versions/{policyVersion}")
386     @ApiOperation(value = "Retrieve one version of a particular operational policy",
387             notes = "Returns a particular version of a specified operational policy",
388             response = LegacyOperationalPolicy.class,
389             responseHeaders = {
390                     @ResponseHeader(name = "X-MinorVersion",
391                                     description = "Used to request or communicate a MINOR version back from the client"
392                                                 + " to the server, and from the server back to the client",
393                                     response = String.class),
394                     @ResponseHeader(name = "X-PatchVersion",
395                                     description = "Used only to communicate a PATCH version in a response for"
396                                                 + " troubleshooting purposes only, and will not be provided by"
397                                                 + " the client on request",
398                                     response = String.class),
399                     @ResponseHeader(name = "X-LatestVersion",
400                                     description = "Used only to communicate an API's latest version",
401                                     response = String.class),
402                     @ResponseHeader(name = "X-ONAP-RequestID",
403                                     description = "Used to track REST transactions for logging purpose",
404                                     response = UUID.class)
405             },
406             authorizations = @Authorization(value = "basicAuth"),
407             tags = { "Legacy Operational Policy", },
408             extensions = {
409                     @Extension(name = "interface info", properties = {
410                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
411                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
412                     })
413             })
414     @ApiResponses(value = {
415             @ApiResponse(code = 401, message = "Authentication Error"),
416             @ApiResponse(code = 403, message = "Authorization Error"),
417             @ApiResponse(code = 404, message = "Resource Not Found"),
418             @ApiResponse(code = 500, message = "Internal Server Error")
419         })
420     public Response getSpecificVersionOfOperationalPolicy(
421             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
422             @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,
423             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
424
425         try (LegacyOperationalPolicyProvider operationalPolicyProvider = new LegacyOperationalPolicyProvider()) {
426             LegacyOperationalPolicy policy = operationalPolicyProvider.fetchOperationalPolicy(policyId, policyVersion);
427             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
428                     .entity(policy).build();
429         } catch (PfModelException | PfModelRuntimeException pfme) {
430             LOGGER.error("GET /policytypes/onap.policies.controlloop.Operational/versions/1.0.0/"
431                 + "policies/{}/versions/{}", policyId, policyVersion, pfme);
432             return addLoggingHeaders(addVersionControlHeaders(
433                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
434                     .entity(pfme.getErrorResponse()).build();
435         }
436     }
437
438     /**
439      * Creates a new operational policy.
440      *
441      * @param body the body of policy
442      *
443      * @return the Response object containing the results of the API operation
444      */
445     @POST
446     @Path("/policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies")
447     @ApiOperation(value = "Create a new operational policy",
448             notes = "Client should provide entity body of the new operational policy",
449             authorizations = @Authorization(value = "basicAuth"),
450             tags = { "Legacy Operational Policy", },
451             response = LegacyOperationalPolicy.class,
452             responseHeaders = {
453                     @ResponseHeader(name = "X-MinorVersion",
454                                     description = "Used to request or communicate a MINOR version back from the client"
455                                                 + " to the server, and from the server back to the client",
456                                     response = String.class),
457                     @ResponseHeader(name = "X-PatchVersion",
458                                     description = "Used only to communicate a PATCH version in a response for"
459                                                 + " troubleshooting purposes only, and will not be provided by"
460                                                 + " the client on request",
461                                     response = String.class),
462                     @ResponseHeader(name = "X-LatestVersion",
463                                     description = "Used only to communicate an API's latest version",
464                                     response = String.class),
465                     @ResponseHeader(name = "X-ONAP-RequestID",
466                                     description = "Used to track REST transactions for logging purpose",
467                                     response = UUID.class)
468             },
469             extensions = {
470                     @Extension(name = "interface info", properties = {
471                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
472                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
473                     })
474             })
475     @ApiResponses(value = {
476             @ApiResponse(code = 400, message = "Invalid Body"),
477             @ApiResponse(code = 401, message = "Authentication Error"),
478             @ApiResponse(code = 403, message = "Authorization Error"),
479             @ApiResponse(code = 500, message = "Internal Server Error")
480         })
481     public Response createOperationalPolicy(
482             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,
483             @ApiParam(value = "Entity body of policy", required = true) LegacyOperationalPolicy body) {
484
485         try (LegacyOperationalPolicyProvider operationalPolicyProvider = new LegacyOperationalPolicyProvider()) {
486             LegacyOperationalPolicy policy = operationalPolicyProvider.createOperationalPolicy(body);
487             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
488                     .entity(policy).build();
489         } catch (PfModelException | PfModelRuntimeException pfme) {
490             LOGGER.error("POST /policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies", pfme);
491             return addLoggingHeaders(addVersionControlHeaders(
492                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
493                     .entity(pfme.getErrorResponse()).build();
494         }
495     }
496
497     /**
498      * Deletes the specified version of a particular operational policy.
499      *
500      * @param policyId the ID of specified policy
501      * @param policyVersion the version of specified policy
502      *
503      * @return the Response object containing the results of the API operation
504      */
505     @DELETE
506     @Path("/policytypes/onap.policies.controlloop.Operational/versions/1.0.0/"
507          + "policies/{policyId}/versions/{policyVersion}")
508     @ApiOperation(value = "Delete a particular version of a specified operational policy",
509             notes = "Rule: the version that has been deployed in PDP group(s) cannot be deleted",
510             authorizations = @Authorization(value = "basicAuth"),
511             tags = { "Legacy Operational Policy", },
512             response = LegacyOperationalPolicy.class,
513             responseHeaders = {
514                     @ResponseHeader(name = "X-MinorVersion",
515                                     description = "Used to request or communicate a MINOR version back from the client"
516                                                 + " to the server, and from the server back to the client",
517                                     response = String.class),
518                     @ResponseHeader(name = "X-PatchVersion",
519                                     description = "Used only to communicate a PATCH version in a response for"
520                                                 + " troubleshooting purposes only, and will not be provided by"
521                                                 + " the client on request",
522                                     response = String.class),
523                     @ResponseHeader(name = "X-LatestVersion",
524                                     description = "Used only to communicate an API's latest version",
525                                     response = String.class),
526                     @ResponseHeader(name = "X-ONAP-RequestID",
527                                     description = "Used to track REST transactions for logging purpose",
528                                     response = UUID.class)
529             },
530             extensions = {
531                     @Extension(name = "interface info", properties = {
532                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
533                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
534                     })
535             })
536     @ApiResponses(value = {
537             @ApiResponse(code = 401, message = "Authentication Error"),
538             @ApiResponse(code = 403, message = "Authorization Error"),
539             @ApiResponse(code = 404, message = "Resource Not Found"),
540             @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
541             @ApiResponse(code = 500, message = "Internal Server Error")
542         })
543     public Response deleteSpecificVersionOfOperationalPolicy(
544             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
545             @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,
546             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
547
548         try (LegacyOperationalPolicyProvider operationalPolicyProvider = new LegacyOperationalPolicyProvider()) {
549             LegacyOperationalPolicy policy = operationalPolicyProvider
550                     .deleteOperationalPolicy(policyId, policyVersion);
551             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
552                     .entity(policy).build();
553         } catch (PfModelException | PfModelRuntimeException pfme) {
554             LOGGER.error("DELETE /policytypes/onap.policies.controlloop.Operational/versions/1.0.0/"
555                 + "policies/{}/versions/{}", policyId, policyVersion, pfme);
556             return addLoggingHeaders(addVersionControlHeaders(
557                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
558                     .entity(pfme.getErrorResponse()).build();
559         }
560     }
561
562     private ResponseBuilder addVersionControlHeaders(ResponseBuilder rb) {
563         return rb.header("X-MinorVersion", "0").header("X-PatchVersion", "0").header("X-LatestVersion", "1.0.0");
564     }
565
566     private ResponseBuilder addLoggingHeaders(ResponseBuilder rb, UUID requestId) {
567         if (requestId == null) {
568             // Generate a random uuid if client does not embed requestId in rest request
569             return rb.header("X-ONAP-RequestID", UUID.randomUUID());
570         }
571         return rb.header("X-ONAP-RequestID", requestId);
572     }
573 }