Add simple deploy/undeploy REST API
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PdpGroupDeployControllerV1.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.rest;
22
23 import io.swagger.annotations.ApiOperation;
24 import io.swagger.annotations.ApiParam;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
27 import io.swagger.annotations.Authorization;
28 import io.swagger.annotations.Extension;
29 import io.swagger.annotations.ExtensionProperty;
30 import io.swagger.annotations.ResponseHeader;
31 import java.util.UUID;
32 import javax.ws.rs.HeaderParam;
33 import javax.ws.rs.POST;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.Response.Status;
37 import org.apache.commons.lang3.tuple.Pair;
38 import org.onap.policy.models.pap.concepts.PdpGroup;
39 import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
40 import org.onap.policy.models.pap.concepts.PdpPolicies;
41
42 /**
43  * Class to provide REST end points for PAP component to deploy a PDP group.
44  */
45 public class PdpGroupDeployControllerV1 extends PapRestControllerV1 {
46
47     private final PdpGroupDeployProvider provider = new PdpGroupDeployProvider();
48
49     /**
50      * Deploys or updates a PDP group.
51      *
52      * @param requestId request ID used in ONAP logging
53      * @param group PDP group configuration
54      * @return a response
55      */
56     // @formatter:off
57     @POST
58     @Path("pdps")
59     @ApiOperation(value = "Deploy or update PDP Group",
60         notes = "Deploys or updates a PDP Group, returning optional error details",
61         response = PdpGroupDeployResponse.class,
62         tags = {"Policy Administration (PAP) API"},
63         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
64         responseHeaders = {
65             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
66                             response = String.class),
67             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
68                             response = String.class),
69             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
70                             response = String.class),
71             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
72                             response = UUID.class)},
73         extensions = {@Extension(name = EXTENSION_NAME,
74             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
75                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
76     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
77                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
78                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
79     // @formatter:on
80
81     public Response deployGroup(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
82                     @ApiParam(value = "PDP Group Configuration", required = true) PdpGroup group) {
83
84         Pair<Status, PdpGroupDeployResponse> pair = provider.deployGroup(group);
85
86         return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId)
87                         .entity(pair.getRight()).build();
88     }
89
90     /**
91      * Deploys or updates PDP policies.
92      *
93      * @param requestId request ID used in ONAP logging
94      * @param policies PDP policies
95      * @return a response
96      */
97     // @formatter:off
98     @POST
99     @Path("pdps/policies")
100     @ApiOperation(value = "Deploy or update PDP Policies",
101         notes = "Deploys or updates PDP Policies, returning optional error details",
102         response = PdpGroupDeployResponse.class,
103         tags = {"Policy Administration (PAP) API"},
104         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
105         responseHeaders = {
106             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
107                             response = String.class),
108             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
109                             response = String.class),
110             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
111                             response = String.class),
112             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
113                             response = UUID.class)},
114         extensions = {@Extension(name = EXTENSION_NAME,
115             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
116                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
117     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
118                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
119                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
120     // @formatter:on
121
122     public Response deployPolicies(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
123                     @ApiParam(value = "PDP Policies; only the name and policyVersion are required",
124                                     required = true) PdpPolicies policies) {
125
126         Pair<Status, PdpGroupDeployResponse> pair = provider.deployPolicies(policies);
127
128         return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId)
129                         .entity(pair.getRight()).build();
130     }
131 }