05bb92f8daef6be0911032b34ae06c3a52df48bc
[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.common.endpoints.event.comm.Topic.CommInfrastructure;
49 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
50 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
51 import org.onap.policy.common.utils.coder.Coder;
52 import org.onap.policy.common.utils.coder.CoderException;
53 import org.onap.policy.common.utils.coder.StandardCoder;
54 import org.onap.policy.models.base.PfModelException;
55 import org.onap.policy.models.base.PfModelRuntimeException;
56 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
57 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
58 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62 /**
63  * Class to provide legacy REST API services.
64  *
65  * @author Chenfei Gao (cgao@research.att.com)
66  */
67 @Path("/policy/api/v1")
68 @Api(value = "Legacy Policy Design API")
69 @Produces("application/json")
70 @Consumes("application/json")
71 public class LegacyApiRestController {
72
73     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyApiRestController.class);
74
75     private final Coder coder = new StandardCoder();
76
77     /**
78      * Retrieves the latest version of a particular guard policy.
79      *
80      * @param policyId the ID of specified guard policy
81      *
82      * @return the Response object containing the results of the API operation
83      */
84     @GET
85     @Path("/policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/{policyId}/versions/latest")
86     @ApiOperation(value = "Retrieve the latest version of a particular guard policy",
87             notes = "Returns the latest version of the specified guard policy",
88             response = LegacyGuardPolicyOutput.class, responseContainer = "Map",
89             responseHeaders = {
90                     @ResponseHeader(name = "X-MinorVersion",
91                                     description = "Used to request or communicate a MINOR version back from the client"
92                                                 + " to the server, and from the server back to the client",
93                                     response = String.class),
94                     @ResponseHeader(name = "X-PatchVersion",
95                                     description = "Used only to communicate a PATCH version in a response for"
96                                                 + " troubleshooting purposes only, and will not be provided by"
97                                                 + " the client on request",
98                                     response = String.class),
99                     @ResponseHeader(name = "X-LatestVersion",
100                                     description = "Used only to communicate an API's latest version",
101                                     response = String.class),
102                     @ResponseHeader(name = "X-ONAP-RequestID",
103                                     description = "Used to track REST transactions for logging purpose",
104                                     response = UUID.class)
105             },
106             authorizations = @Authorization(value = "basicAuth"),
107             tags = { "Legacy Guard Policy", },
108             extensions = {
109                     @Extension(name = "interface info", properties = {
110                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
111                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
112                     })
113             })
114     @ApiResponses(value = {
115             @ApiResponse(code = 401, message = "Authentication Error"),
116             @ApiResponse(code = 403, message = "Authorization Error"),
117             @ApiResponse(code = 404, message = "Resource Not Found"),
118             @ApiResponse(code = 500, message = "Internal Server Error")
119         })
120     public Response getLatestVersionOfGuardPolicy(
121             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
122             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
123
124         try (LegacyGuardPolicyProvider guardPolicyProvider = new LegacyGuardPolicyProvider()) {
125             Map<String, LegacyGuardPolicyOutput> policies = guardPolicyProvider.fetchGuardPolicy(policyId, null);
126             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
127                     .entity(policies).build();
128         } catch (PfModelException | PfModelRuntimeException pfme) {
129             LOGGER.error("GET /policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/{}"
130                 + "/versions/latest", policyId, pfme);
131             return addLoggingHeaders(addVersionControlHeaders(
132                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
133                     .entity(pfme.getErrorResponse()).build();
134         }
135     }
136
137     /**
138      * Retrieves the specified version of a particular guard policy.
139      *
140      * @param policyId the ID of specified policy
141      * @param policyVersion the version of specified policy
142      *
143      * @return the Response object containing the results of the API operation
144      */
145     @GET
146     @Path("/policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/{policyId}/versions/{policyVersion}")
147     @ApiOperation(value = "Retrieve one version of a particular guard policy",
148             notes = "Returns a particular version of a specified guard policy",
149             response = LegacyGuardPolicyOutput.class, responseContainer = "Map",
150             responseHeaders = {
151                     @ResponseHeader(name = "X-MinorVersion",
152                                     description = "Used to request or communicate a MINOR version back from the client"
153                                                 + " to the server, and from the server back to the client",
154                                     response = String.class),
155                     @ResponseHeader(name = "X-PatchVersion",
156                                     description = "Used only to communicate a PATCH version in a response for"
157                                                 + " troubleshooting purposes only, and will not be provided by"
158                                                 + " the client on request",
159                                     response = String.class),
160                     @ResponseHeader(name = "X-LatestVersion",
161                                     description = "Used only to communicate an API's latest version",
162                                     response = String.class),
163                     @ResponseHeader(name = "X-ONAP-RequestID",
164                                     description = "Used to track REST transactions for logging purpose",
165                                     response = UUID.class)
166             },
167             authorizations = @Authorization(value = "basicAuth"),
168             tags = { "Legacy Guard Policy", },
169             extensions = {
170                     @Extension(name = "interface info", properties = {
171                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
172                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
173                     })
174             })
175     @ApiResponses(value = {
176             @ApiResponse(code = 401, message = "Authentication Error"),
177             @ApiResponse(code = 403, message = "Authorization Error"),
178             @ApiResponse(code = 404, message = "Resource Not Found"),
179             @ApiResponse(code = 500, message = "Internal Server Error")
180         })
181     public Response getSpecificVersionOfGuardPolicy(
182             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
183             @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,
184             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
185
186         try (LegacyGuardPolicyProvider guardPolicyProvider = new LegacyGuardPolicyProvider()) {
187             Map<String, LegacyGuardPolicyOutput> policies = guardPolicyProvider
188                     .fetchGuardPolicy(policyId, policyVersion);
189             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
190                     .entity(policies).build();
191         } catch (PfModelException | PfModelRuntimeException pfme) {
192             LOGGER.error("GET /policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/{}/versions/{}",
193                     policyId, policyVersion, pfme);
194             return addLoggingHeaders(addVersionControlHeaders(
195                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
196                     .entity(pfme.getErrorResponse()).build();
197         }
198     }
199
200     /**
201      * Creates a new guard policy.
202      *
203      * @param body the body of policy
204      *
205      * @return the Response object containing the results of the API operation
206      */
207     @POST
208     @Path("/policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies")
209     @ApiOperation(value = "Create a new guard policy",
210             notes = "Client should provide entity body of the new guard policy",
211             authorizations = @Authorization(value = "basicAuth"),
212             tags = { "Legacy Guard Policy", },
213             response = LegacyGuardPolicyOutput.class, responseContainer = "Map",
214             responseHeaders = {
215                     @ResponseHeader(name = "X-MinorVersion",
216                                     description = "Used to request or communicate a MINOR version back from the client"
217                                                 + " to the server, and from the server back to the client",
218                                     response = String.class),
219                     @ResponseHeader(name = "X-PatchVersion",
220                                     description = "Used only to communicate a PATCH version in a response for"
221                                                 + " troubleshooting purposes only, and will not be provided by"
222                                                 + " the client on request",
223                                     response = String.class),
224                     @ResponseHeader(name = "X-LatestVersion",
225                                     description = "Used only to communicate an API's latest version",
226                                     response = String.class),
227                     @ResponseHeader(name = "X-ONAP-RequestID",
228                                     description = "Used to track REST transactions for logging purpose",
229                                     response = UUID.class)
230             },
231             extensions = {
232                     @Extension(name = "interface info", properties = {
233                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
234                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
235                     })
236             })
237     @ApiResponses(value = {
238             @ApiResponse(code = 400, message = "Invalid Body"),
239             @ApiResponse(code = 401, message = "Authentication Error"),
240             @ApiResponse(code = 403, message = "Authorization Error"),
241             @ApiResponse(code = 500, message = "Internal Server Error")
242         })
243     public Response createGuardPolicy(
244             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,
245             @ApiParam(value = "Entity body of policy", required = true) LegacyGuardPolicyInput body) {
246
247         if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
248             NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST,
249                             "/policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies", toJson(body));
250         }
251
252         try (LegacyGuardPolicyProvider guardPolicyProvider = new LegacyGuardPolicyProvider()) {
253             Map<String, LegacyGuardPolicyOutput> policy = guardPolicyProvider.createGuardPolicy(body);
254             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
255                     .entity(policy).build();
256         } catch (PfModelException | PfModelRuntimeException pfme) {
257             LOGGER.error("POST /policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies", pfme);
258             return addLoggingHeaders(addVersionControlHeaders(
259                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
260                     .entity(pfme.getErrorResponse()).build();
261         }
262     }
263
264     /**
265      * Deletes the specified version of a particular guard policy.
266      *
267      * @param policyId the ID of specified policy
268      * @param policyVersion the version of specified policy
269      *
270      * @return the Response object containing the results of the API operation
271      */
272     @DELETE
273     @Path("/policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/{policyId}/versions/{policyVersion}")
274     @ApiOperation(value = "Delete a particular version of a guard policy",
275             notes = "Rule: the version that has been deployed in PDP group(s) cannot be deleted",
276             authorizations = @Authorization(value = "basicAuth"),
277             tags = { "Legacy Guard Policy", },
278             response = LegacyGuardPolicyOutput.class, responseContainer = "Map",
279             responseHeaders = {
280                     @ResponseHeader(name = "X-MinorVersion",
281                                     description = "Used to request or communicate a MINOR version back from the client"
282                                                 + " to the server, and from the server back to the client",
283                                     response = String.class),
284                     @ResponseHeader(name = "X-PatchVersion",
285                                     description = "Used only to communicate a PATCH version in a response for"
286                                                 + " troubleshooting purposes only, and will not be provided by"
287                                                 + " the client on request",
288                                     response = String.class),
289                     @ResponseHeader(name = "X-LatestVersion",
290                                     description = "Used only to communicate an API's latest version",
291                                     response = String.class),
292                     @ResponseHeader(name = "X-ONAP-RequestID",
293                                     description = "Used to track REST transactions for logging purpose",
294                                     response = UUID.class)
295             },
296             extensions = {
297                     @Extension(name = "interface info", properties = {
298                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
299                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
300                     })
301             })
302     @ApiResponses(value = {
303             @ApiResponse(code = 401, message = "Authentication Error"),
304             @ApiResponse(code = 403, message = "Authorization Error"),
305             @ApiResponse(code = 404, message = "Resource Not Found"),
306             @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
307             @ApiResponse(code = 500, message = "Internal Server Error")
308         })
309     public Response deleteSpecificVersionOfGuardPolicy(
310             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
311             @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,
312             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
313
314         try (LegacyGuardPolicyProvider guardPolicyProvider = new LegacyGuardPolicyProvider()) {
315             Map<String, LegacyGuardPolicyOutput> policies = guardPolicyProvider
316                     .deleteGuardPolicy(policyId, policyVersion);
317             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
318                     .entity(policies).build();
319         } catch (PfModelException | PfModelRuntimeException pfme) {
320             LOGGER.error("DELETE /policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/{}/versions/{}",
321                     policyId, policyVersion, pfme);
322             return addLoggingHeaders(addVersionControlHeaders(
323                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
324                     .entity(pfme.getErrorResponse()).build();
325         }
326     }
327
328     /**
329      * Retrieves the latest version of a particular operational policy.
330      *
331      * @param policyId the ID of specified operational policy
332      *
333      * @return the Response object containing the results of the API operation
334      */
335     @GET
336     @Path("/policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies/{policyId}/versions/latest")
337     @ApiOperation(value = "Retrieve the latest version of a particular operational policy",
338             notes = "Returns the latest version of the specified operational policy",
339             response = LegacyOperationalPolicy.class,
340             responseHeaders = {
341                     @ResponseHeader(name = "X-MinorVersion",
342                                     description = "Used to request or communicate a MINOR version back from the client"
343                                                 + " to the server, and from the server back to the client",
344                                     response = String.class),
345                     @ResponseHeader(name = "X-PatchVersion",
346                                     description = "Used only to communicate a PATCH version in a response for"
347                                                 + " troubleshooting purposes only, and will not be provided by"
348                                                 + " the client on request",
349                                     response = String.class),
350                     @ResponseHeader(name = "X-LatestVersion",
351                                     description = "Used only to communicate an API's latest version",
352                                     response = String.class),
353                     @ResponseHeader(name = "X-ONAP-RequestID",
354                                     description = "Used to track REST transactions for logging purpose",
355                                     response = UUID.class)
356             },
357             authorizations = @Authorization(value = "basicAuth"),
358             tags = { "Legacy Operational Policy", },
359             extensions = {
360                     @Extension(name = "interface info", properties = {
361                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
362                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
363                     })
364             })
365     @ApiResponses(value = {
366             @ApiResponse(code = 401, message = "Authentication Error"),
367             @ApiResponse(code = 403, message = "Authorization Error"),
368             @ApiResponse(code = 404, message = "Resource Not Found"),
369             @ApiResponse(code = 500, message = "Internal Server Error")
370         })
371     public Response getLatestVersionOfOperationalPolicy(
372             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
373             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
374
375         try (LegacyOperationalPolicyProvider operationalPolicyProvider = new LegacyOperationalPolicyProvider()) {
376             LegacyOperationalPolicy policy = operationalPolicyProvider.fetchOperationalPolicy(policyId, null);
377             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
378                     .entity(policy).build();
379         } catch (PfModelException | PfModelRuntimeException pfme) {
380             LOGGER.error("GET /policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies/{}"
381                 + "/versions/latest", policyId, pfme);
382             return addLoggingHeaders(addVersionControlHeaders(
383                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
384                     .entity(pfme.getErrorResponse()).build();
385         }
386     }
387
388     /**
389      * Retrieves the specified version of a particular operational policy.
390      *
391      * @param policyId the ID of specified policy
392      * @param policyVersion the version of specified policy
393      *
394      * @return the Response object containing the results of the API operation
395      */
396     @GET
397     @Path("/policytypes/onap.policies.controlloop.Operational/versions/1.0.0/"
398          + "policies/{policyId}/versions/{policyVersion}")
399     @ApiOperation(value = "Retrieve one version of a particular operational policy",
400             notes = "Returns a particular version of a specified operational policy",
401             response = LegacyOperationalPolicy.class,
402             responseHeaders = {
403                     @ResponseHeader(name = "X-MinorVersion",
404                                     description = "Used to request or communicate a MINOR version back from the client"
405                                                 + " to the server, and from the server back to the client",
406                                     response = String.class),
407                     @ResponseHeader(name = "X-PatchVersion",
408                                     description = "Used only to communicate a PATCH version in a response for"
409                                                 + " troubleshooting purposes only, and will not be provided by"
410                                                 + " the client on request",
411                                     response = String.class),
412                     @ResponseHeader(name = "X-LatestVersion",
413                                     description = "Used only to communicate an API's latest version",
414                                     response = String.class),
415                     @ResponseHeader(name = "X-ONAP-RequestID",
416                                     description = "Used to track REST transactions for logging purpose",
417                                     response = UUID.class)
418             },
419             authorizations = @Authorization(value = "basicAuth"),
420             tags = { "Legacy Operational Policy", },
421             extensions = {
422                     @Extension(name = "interface info", properties = {
423                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
424                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
425                     })
426             })
427     @ApiResponses(value = {
428             @ApiResponse(code = 401, message = "Authentication Error"),
429             @ApiResponse(code = 403, message = "Authorization Error"),
430             @ApiResponse(code = 404, message = "Resource Not Found"),
431             @ApiResponse(code = 500, message = "Internal Server Error")
432         })
433     public Response getSpecificVersionOfOperationalPolicy(
434             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
435             @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,
436             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
437
438         try (LegacyOperationalPolicyProvider operationalPolicyProvider = new LegacyOperationalPolicyProvider()) {
439             LegacyOperationalPolicy policy = operationalPolicyProvider.fetchOperationalPolicy(policyId, policyVersion);
440             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
441                     .entity(policy).build();
442         } catch (PfModelException | PfModelRuntimeException pfme) {
443             LOGGER.error("GET /policytypes/onap.policies.controlloop.Operational/versions/1.0.0/"
444                 + "policies/{}/versions/{}", policyId, policyVersion, pfme);
445             return addLoggingHeaders(addVersionControlHeaders(
446                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
447                     .entity(pfme.getErrorResponse()).build();
448         }
449     }
450
451     /**
452      * Creates a new operational policy.
453      *
454      * @param body the body of policy
455      *
456      * @return the Response object containing the results of the API operation
457      */
458     @POST
459     @Path("/policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies")
460     @ApiOperation(value = "Create a new operational policy",
461             notes = "Client should provide entity body of the new operational policy",
462             authorizations = @Authorization(value = "basicAuth"),
463             tags = { "Legacy Operational Policy", },
464             response = LegacyOperationalPolicy.class,
465             responseHeaders = {
466                     @ResponseHeader(name = "X-MinorVersion",
467                                     description = "Used to request or communicate a MINOR version back from the client"
468                                                 + " to the server, and from the server back to the client",
469                                     response = String.class),
470                     @ResponseHeader(name = "X-PatchVersion",
471                                     description = "Used only to communicate a PATCH version in a response for"
472                                                 + " troubleshooting purposes only, and will not be provided by"
473                                                 + " the client on request",
474                                     response = String.class),
475                     @ResponseHeader(name = "X-LatestVersion",
476                                     description = "Used only to communicate an API's latest version",
477                                     response = String.class),
478                     @ResponseHeader(name = "X-ONAP-RequestID",
479                                     description = "Used to track REST transactions for logging purpose",
480                                     response = UUID.class)
481             },
482             extensions = {
483                     @Extension(name = "interface info", properties = {
484                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
485                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
486                     })
487             })
488     @ApiResponses(value = {
489             @ApiResponse(code = 400, message = "Invalid Body"),
490             @ApiResponse(code = 401, message = "Authentication Error"),
491             @ApiResponse(code = 403, message = "Authorization Error"),
492             @ApiResponse(code = 500, message = "Internal Server Error")
493         })
494     public Response createOperationalPolicy(
495             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,
496             @ApiParam(value = "Entity body of policy", required = true) LegacyOperationalPolicy body) {
497
498         if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
499             NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST,
500                             "/policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies", toJson(body));
501         }
502
503         try (LegacyOperationalPolicyProvider operationalPolicyProvider = new LegacyOperationalPolicyProvider()) {
504             LegacyOperationalPolicy policy = operationalPolicyProvider.createOperationalPolicy(body);
505             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
506                     .entity(policy).build();
507         } catch (PfModelException | PfModelRuntimeException pfme) {
508             LOGGER.error("POST /policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies", pfme);
509             return addLoggingHeaders(addVersionControlHeaders(
510                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
511                     .entity(pfme.getErrorResponse()).build();
512         }
513     }
514
515     /**
516      * Deletes the specified version of a particular operational policy.
517      *
518      * @param policyId the ID of specified policy
519      * @param policyVersion the version of specified policy
520      *
521      * @return the Response object containing the results of the API operation
522      */
523     @DELETE
524     @Path("/policytypes/onap.policies.controlloop.Operational/versions/1.0.0/"
525          + "policies/{policyId}/versions/{policyVersion}")
526     @ApiOperation(value = "Delete a particular version of a specified operational policy",
527             notes = "Rule: the version that has been deployed in PDP group(s) cannot be deleted",
528             authorizations = @Authorization(value = "basicAuth"),
529             tags = { "Legacy Operational Policy", },
530             response = LegacyOperationalPolicy.class,
531             responseHeaders = {
532                     @ResponseHeader(name = "X-MinorVersion",
533                                     description = "Used to request or communicate a MINOR version back from the client"
534                                                 + " to the server, and from the server back to the client",
535                                     response = String.class),
536                     @ResponseHeader(name = "X-PatchVersion",
537                                     description = "Used only to communicate a PATCH version in a response for"
538                                                 + " troubleshooting purposes only, and will not be provided by"
539                                                 + " the client on request",
540                                     response = String.class),
541                     @ResponseHeader(name = "X-LatestVersion",
542                                     description = "Used only to communicate an API's latest version",
543                                     response = String.class),
544                     @ResponseHeader(name = "X-ONAP-RequestID",
545                                     description = "Used to track REST transactions for logging purpose",
546                                     response = UUID.class)
547             },
548             extensions = {
549                     @Extension(name = "interface info", properties = {
550                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
551                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
552                     })
553             })
554     @ApiResponses(value = {
555             @ApiResponse(code = 401, message = "Authentication Error"),
556             @ApiResponse(code = 403, message = "Authorization Error"),
557             @ApiResponse(code = 404, message = "Resource Not Found"),
558             @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
559             @ApiResponse(code = 500, message = "Internal Server Error")
560         })
561     public Response deleteSpecificVersionOfOperationalPolicy(
562             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
563             @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,
564             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
565
566         try (LegacyOperationalPolicyProvider operationalPolicyProvider = new LegacyOperationalPolicyProvider()) {
567             LegacyOperationalPolicy policy = operationalPolicyProvider
568                     .deleteOperationalPolicy(policyId, policyVersion);
569             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
570                     .entity(policy).build();
571         } catch (PfModelException | PfModelRuntimeException pfme) {
572             LOGGER.error("DELETE /policytypes/onap.policies.controlloop.Operational/versions/1.0.0/"
573                 + "policies/{}/versions/{}", policyId, policyVersion, pfme);
574             return addLoggingHeaders(addVersionControlHeaders(
575                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
576                     .entity(pfme.getErrorResponse()).build();
577         }
578     }
579
580     private ResponseBuilder addVersionControlHeaders(ResponseBuilder rb) {
581         return rb.header("X-MinorVersion", "0").header("X-PatchVersion", "0").header("X-LatestVersion", "1.0.0");
582     }
583
584     private ResponseBuilder addLoggingHeaders(ResponseBuilder rb, UUID requestId) {
585         if (requestId == null) {
586             // Generate a random uuid if client does not embed requestId in rest request
587             return rb.header("X-ONAP-RequestID", UUID.randomUUID());
588         }
589         return rb.header("X-ONAP-RequestID", requestId);
590     }
591
592     /**
593      * Converts an object to a JSON string.
594      *
595      * @param object object to convert
596      * @return a JSON string representing the object
597      */
598     private String toJson(Object object) {
599         if (object == null) {
600             return null;
601         }
602
603         try {
604             return coder.encode(object);
605
606         } catch (CoderException e) {
607             LOGGER.warn("cannot convert {} to JSON", object.getClass().getName(), e);
608             return null;
609         }
610     }
611 }