Fix sonars in policy-pap
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PdpGroupCreateOrUpdateControllerV1.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019 Nordix Foundation.
6  * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.rest;
23
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiParam;
26 import io.swagger.annotations.ApiResponse;
27 import io.swagger.annotations.ApiResponses;
28 import io.swagger.annotations.Authorization;
29 import io.swagger.annotations.Extension;
30 import io.swagger.annotations.ExtensionProperty;
31 import io.swagger.annotations.ResponseHeader;
32 import java.util.UUID;
33 import javax.ws.rs.HeaderParam;
34 import javax.ws.rs.POST;
35 import javax.ws.rs.Path;
36 import javax.ws.rs.core.Response;
37 import javax.ws.rs.core.Response.Status;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.base.PfModelRuntimeException;
40 import org.onap.policy.models.pap.concepts.PdpGroupUpdateResponse;
41 import org.onap.policy.models.pdp.concepts.PdpGroups;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * Class to provide REST end points for PAP component to create or update PDP groups.
47  */
48 public class PdpGroupCreateOrUpdateControllerV1 extends PapRestControllerV1 {
49     private static final Logger logger = LoggerFactory.getLogger(PdpGroupCreateOrUpdateControllerV1.class);
50
51     private final PdpGroupCreateOrUpdateProvider provider = new PdpGroupCreateOrUpdateProvider();
52
53     /**
54      * Creates or updates one or more PDP groups.
55      *
56      * @param requestId request ID used in ONAP logging
57      * @param groups PDP group configuration
58      * @return a response
59      */
60     // @formatter:off
61     @POST
62     @Path("pdps/groups/batch")
63     @ApiOperation(value = "Create or update PDP Groups",
64         notes = "Create or update one or more PDP Groups, returning optional error details",
65         response = PdpGroupUpdateResponse.class,
66         tags = {"Policy Administration (PAP) API"},
67         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
68         responseHeaders = {
69             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
70                             response = String.class),
71             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
72                             response = String.class),
73             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
74                             response = String.class),
75             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
76                             response = UUID.class)},
77         extensions = {@Extension(name = EXTENSION_NAME,
78             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
79                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
80     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
81                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
82                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
83     // @formatter:on
84
85     public Response createOrUpdateGroups(
86         @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
87         @ApiParam(value = "List of PDP Group Configuration", required = true) PdpGroups groups) {
88
89         return doOperation(requestId, "create groups failed", () -> provider.createOrUpdateGroups(groups));
90     }
91
92     /**
93      * Invokes an operation.
94      *
95      * @param requestId request ID
96      * @param errmsg error message to log if the operation throws an exception
97      * @param runnable operation to invoke
98      * @return a {@link PdpGroupUpdateResponse} response entity
99      */
100     private Response doOperation(UUID requestId, String errmsg, RunnableWithPfEx runnable) {
101         try {
102             runnable.run();
103             return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId)
104                 .entity(new PdpGroupUpdateResponse()).build();
105
106         } catch (PfModelException | PfModelRuntimeException e) {
107             logger.warn(errmsg, e);
108             var resp = new PdpGroupUpdateResponse();
109             resp.setErrorDetails(e.getErrorResponse().getErrorMessage());
110             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
111                 requestId).entity(resp).build();
112         }
113     }
114 }