Fix sonar issues in PAP
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PdpGroupStateChangeControllerV1.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
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
33 import java.util.UUID;
34
35 import javax.ws.rs.HeaderParam;
36 import javax.ws.rs.PUT;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.PathParam;
39 import javax.ws.rs.QueryParam;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.Response.Status;
42
43 import org.apache.commons.lang3.tuple.Pair;
44 import org.onap.policy.models.base.PfModelException;
45 import org.onap.policy.models.pap.concepts.PdpGroupStateChangeResponse;
46 import org.onap.policy.models.pdp.enums.PdpState;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * Class to provide REST end points for PAP component to change state of a PDP group.
52  *
53  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
54  */
55 public class PdpGroupStateChangeControllerV1 extends PapRestControllerV1 {
56
57     private static final Logger LOGGER = LoggerFactory.getLogger(PdpGroupQueryControllerV1.class);
58
59     private final PdpGroupStateChangeProvider provider = new PdpGroupStateChangeProvider();
60
61     /**
62      * Changes state of a PDP group.
63      *
64      * @param requestId request ID used in ONAP logging
65      * @param groupName name of the PDP group to be deleted
66      * @param state state of the PDP group
67      * @return a response
68      */
69     // @formatter:off
70     @PUT
71     @Path("pdps/groups/{name}")
72     @ApiOperation(value = "Change state of a PDP Group",
73         notes = "Changes state of PDP Group, returning optional error details",
74         response = PdpGroupStateChangeResponse.class,
75         tags = {"Policy Administration (PAP) API"},
76         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
77         responseHeaders = {
78             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
79                             response = String.class),
80             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
81                             response = String.class),
82             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
83                             response = String.class),
84             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
85                             response = UUID.class)},
86         extensions = {@Extension(name = EXTENSION_NAME,
87             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
88                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
89     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
90                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
91                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
92     // @formatter:on
93
94     public Response changeGroupState(
95             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId,
96             @ApiParam(value = "PDP Group Name", required = true) @PathParam("name") final String groupName,
97             @ApiParam(value = "PDP Group State", required = true) @QueryParam("state") final PdpState state) {
98
99         try {
100             final Pair<Status, PdpGroupStateChangeResponse> pair = provider.changeGroupState(groupName, state);
101             return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId)
102                     .entity(pair.getRight()).build();
103         } catch (final PfModelException exp) {
104             LOGGER.info("group state-change failed", exp);
105             return addLoggingHeaders(
106                     addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
107                             .entity(exp.getErrorResponse()).build();
108         }
109     }
110 }