2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.pap.main.rest;
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;
37 * Class to provide REST end points for PAP component to create or update PDP groups.
40 @RequiredArgsConstructor
41 public class PdpGroupCreateOrUpdateControllerV1 extends PapRestControllerV1
42 implements PdpGroupCreateOrUpdateControllerV1Api {
44 private static final Logger logger = LoggerFactory.getLogger(PdpGroupCreateOrUpdateControllerV1.class);
46 private final PdpGroupCreateOrUpdateProvider provider;
49 * Creates or updates one or more PDP groups.
51 * @param requestId request ID used in ONAP logging
52 * @param groups PDP group configuration
56 public ResponseEntity<PdpGroupUpdateResponse> createOrUpdateGroups(UUID requestId, PdpGroups groups) {
58 return doOperation(requestId, "create groups failed", () -> provider.createOrUpdateGroups(groups));
62 * Invokes an operation.
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
69 private ResponseEntity<PdpGroupUpdateResponse> doOperation(UUID requestId, String errmsg,
70 RunnableWithPfEx runnable) {
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);