Use generated PAP interface for Swagger
[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-2023 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 java.util.UUID;
26 import lombok.RequiredArgsConstructor;
27 import org.onap.policy.models.base.PfModelException;
28 import org.onap.policy.models.base.PfModelRuntimeException;
29 import org.onap.policy.models.pap.concepts.PdpGroupUpdateResponse;
30 import org.onap.policy.models.pdp.concepts.PdpGroups;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.web.bind.annotation.RestController;
35
36 /**
37  * Class to provide REST end points for PAP component to create or update PDP groups.
38  */
39 @RestController
40 @RequiredArgsConstructor
41 public class PdpGroupCreateOrUpdateControllerV1 extends PapRestControllerV1
42     implements PdpGroupCreateOrUpdateControllerV1Api {
43
44     private static final Logger logger = LoggerFactory.getLogger(PdpGroupCreateOrUpdateControllerV1.class);
45
46     private final PdpGroupCreateOrUpdateProvider provider;
47
48     /**
49      * Creates or updates one or more PDP groups.
50      *
51      * @param requestId request ID used in ONAP logging
52      * @param groups PDP group configuration
53      * @return a response
54      */
55     @Override
56     public ResponseEntity<PdpGroupUpdateResponse> createOrUpdateGroups(UUID requestId, PdpGroups groups) {
57
58         return doOperation(requestId, "create groups failed", () -> provider.createOrUpdateGroups(groups));
59     }
60
61     /**
62      * Invokes an operation.
63      *
64      * @param requestId request ID
65      * @param errmsg error message to log if the operation throws an exception
66      * @param runnable operation to invoke
67      * @return a {@link PdpGroupUpdateResponse} response entity
68      */
69     private ResponseEntity<PdpGroupUpdateResponse> doOperation(UUID requestId, String errmsg,
70         RunnableWithPfEx runnable) {
71         try {
72             runnable.run();
73             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
74                 .body(new PdpGroupUpdateResponse());
75         } catch (PfModelException | PfModelRuntimeException e) {
76             logger.warn(errmsg, e);
77             var resp = new PdpGroupUpdateResponse();
78             resp.setErrorDetails(e.getErrorResponse().getErrorMessage());
79             return addLoggingHeaders(
80                 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
81                 requestId).body(resp);
82         }
83     }
84 }