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