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