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.context.annotation.Profile;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.web.bind.annotation.RestController;
38 * Class to provide REST end points for PAP component to create or update PDP groups.
41 @RequiredArgsConstructor
43 public class PdpGroupCreateOrUpdateControllerV1 extends PapRestControllerV1
44 implements PdpGroupCreateOrUpdateControllerV1Api {
46 private static final Logger logger = LoggerFactory.getLogger(PdpGroupCreateOrUpdateControllerV1.class);
48 private final PdpGroupCreateOrUpdateProvider provider;
51 * Creates or updates one or more PDP groups.
53 * @param requestId request ID used in ONAP logging
54 * @param groups PDP group configuration
58 public ResponseEntity<PdpGroupUpdateResponse> createOrUpdateGroups(UUID requestId, PdpGroups groups) {
60 return doOperation(requestId, "create groups failed", () -> provider.createOrUpdateGroups(groups));
64 * Invokes an operation.
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
71 private ResponseEntity<PdpGroupUpdateResponse> doOperation(UUID requestId, String errmsg,
72 RunnableWithPfEx runnable) {
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);