2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada. All rights reserved.
4 * Modifications Copyright (C) 2023 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.service;
24 import jakarta.ws.rs.core.Response;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.stream.Collectors;
29 import lombok.NonNull;
30 import lombok.RequiredArgsConstructor;
31 import org.onap.policy.common.parameters.BeanValidationResult;
32 import org.onap.policy.models.base.PfModelRuntimeException;
33 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
34 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpPolicyStatus;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
36 import org.onap.policy.pap.main.repository.PolicyStatusRepository;
37 import org.springframework.stereotype.Service;
38 import org.springframework.transaction.annotation.Transactional;
42 @RequiredArgsConstructor
43 public class PolicyStatusService {
45 private final PolicyStatusRepository policyStatusRepository;
48 * Gets all status for policies in a group.
50 * @param pdpGroup the group's name
51 * @return the policy status list found
53 public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull String pdpGroup) {
54 return asPolicyStatusList(policyStatusRepository.findByPdpGroup(pdpGroup));
58 * Gets all status for policies.
60 * @return the policy status list found
62 public List<PdpPolicyStatus> getAllPolicyStatus() {
63 return asPolicyStatusList(policyStatusRepository.findAll());
67 * Gets all status for a policy.
69 * @param policy the policy
70 * @return the policy status list found
72 public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy) {
74 if (policy.getVersion() != null) {
75 return asPolicyStatusList(policyStatusRepository
76 .findByKeyParentKeyNameAndKeyParentKeyVersion(policy.getName(), policy.getVersion()));
79 return asPolicyStatusList(policyStatusRepository.findByKeyParentKeyName(policy.getName()));
84 * Gets all status for a policy in a group.
86 * @param pdpGroup the group's name
87 * @param policy the policy
88 * @return the policy status list found
90 public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull String pdpGroup,
91 @NonNull ToscaConceptIdentifierOptVersion policy) {
92 if (policy.getVersion() != null) {
93 return asPolicyStatusList(policyStatusRepository.findByPdpGroupAndKeyParentKeyNameAndKeyParentKeyVersion(
94 pdpGroup, policy.getName(), policy.getVersion()));
96 return asPolicyStatusList(
97 policyStatusRepository.findByPdpGroupAndKeyParentKeyName(pdpGroup, policy.getName()));
102 * Creates, updates, and deletes collections of policy status.
104 * @param createObjs the objects to create
105 * @param updateObjs the objects to update
106 * @param deleteObjs the objects to delete
108 public void cudPolicyStatus(Collection<PdpPolicyStatus> createObjs, Collection<PdpPolicyStatus> updateObjs,
109 Collection<PdpPolicyStatus> deleteObjs) {
111 policyStatusRepository.deleteAll(fromAuthorativeStatus(deleteObjs, "deletePdpPolicyStatusList"));
112 policyStatusRepository.saveAll(fromAuthorativeStatus(createObjs, "createPdpPolicyStatusList"));
113 policyStatusRepository.saveAll(fromAuthorativeStatus(updateObjs, "updatePdpPolicyStatusList"));
114 } catch (Exception exc) {
115 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
116 "Policy status operation failed." + exc.getMessage(), exc);
121 * Converts a collection of authorative policy status to a collection of JPA policy
122 * status. Validates the resulting list.
124 * @param objs authorative policy status to convert
125 * @param fieldName name of the field containing the collection
126 * @return a list of JPA policy status
128 private List<JpaPdpPolicyStatus> fromAuthorativeStatus(Collection<PdpPolicyStatus> objs, String fieldName) {
130 return Collections.emptyList();
133 List<JpaPdpPolicyStatus> jpas = objs.stream().map(JpaPdpPolicyStatus::new).collect(Collectors.toList());
135 // validate the objects
136 var result = new BeanValidationResult(fieldName, jpas);
139 for (JpaPdpPolicyStatus jpa : jpas) {
140 result.addResult(jpa.validate(String.valueOf(count++)));
143 if (!result.isValid()) {
144 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult());
150 private List<PdpPolicyStatus> asPolicyStatusList(List<JpaPdpPolicyStatus> jpaPdpPolicyStatusList) {
151 return jpaPdpPolicyStatusList.stream().map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());