2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.service;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.stream.Collectors;
27 import javax.ws.rs.core.Response;
28 import lombok.NonNull;
29 import lombok.RequiredArgsConstructor;
30 import org.onap.policy.common.parameters.BeanValidationResult;
31 import org.onap.policy.models.base.PfModelRuntimeException;
32 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
33 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpPolicyStatus;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
35 import org.onap.policy.pap.main.repository.PolicyStatusRepository;
36 import org.springframework.stereotype.Service;
37 import org.springframework.transaction.annotation.Transactional;
41 @RequiredArgsConstructor
42 public class PolicyStatusService {
44 private final PolicyStatusRepository policyStatusRepository;
47 * Gets all status for policies in a group.
49 * @param pdpGroup the group's name
50 * @return the policy status list found
52 public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull String pdpGroup) {
53 return asPolicyStatusList(policyStatusRepository.findByPdpGroup(pdpGroup));
57 * Gets all status for policies.
59 * @return the policy status list found
61 public List<PdpPolicyStatus> getAllPolicyStatus() {
62 return asPolicyStatusList(policyStatusRepository.findAll());
66 * Gets all status for a policy.
68 * @param policy the policy
69 * @return the policy status list found
71 public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy) {
73 if (policy.getVersion() != null) {
74 return asPolicyStatusList(policyStatusRepository
75 .findByKeyParentKeyNameAndKeyParentKeyVersion(policy.getName(), policy.getVersion()));
78 return asPolicyStatusList(policyStatusRepository.findByKeyParentKeyName(policy.getName()));
83 * Gets all status for a policy in a group.
85 * @param pdpGroup the group's name
86 * @param policy the policy
87 * @return the policy status list found
89 public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull String pdpGroup,
90 @NonNull ToscaConceptIdentifierOptVersion policy) {
91 if (policy.getVersion() != null) {
92 return asPolicyStatusList(policyStatusRepository.findByPdpGroupAndKeyParentKeyNameAndKeyParentKeyVersion(
93 pdpGroup, policy.getName(), policy.getVersion()));
95 return asPolicyStatusList(
96 policyStatusRepository.findByPdpGroupAndKeyParentKeyName(pdpGroup, policy.getName()));
101 * Creates, updates, and deletes collections of policy status.
103 * @param createObjs the objects to create
104 * @param updateObjs the objects to update
105 * @param deleteObjs the objects to delete
107 public void cudPolicyStatus(Collection<PdpPolicyStatus> createObjs, Collection<PdpPolicyStatus> updateObjs,
108 Collection<PdpPolicyStatus> deleteObjs) {
110 policyStatusRepository.deleteAll(fromAuthorativeStatus(deleteObjs, "deletePdpPolicyStatusList"));
111 policyStatusRepository.saveAll(fromAuthorativeStatus(createObjs, "createPdpPolicyStatusList"));
112 policyStatusRepository.saveAll(fromAuthorativeStatus(updateObjs, "updatePdpPolicyStatusList"));
113 } catch (Exception exc) {
114 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
115 "Policy status operation failed." + exc.getMessage(), exc);
120 * Converts a collection of authorative policy status to a collection of JPA policy
121 * status. Validates the resulting list.
123 * @param objs authorative policy status to convert
124 * @param fieldName name of the field containing the collection
125 * @return a list of JPA policy status
127 private List<JpaPdpPolicyStatus> fromAuthorativeStatus(Collection<PdpPolicyStatus> objs, String fieldName) {
129 return Collections.emptyList();
132 List<JpaPdpPolicyStatus> jpas = objs.stream().map(JpaPdpPolicyStatus::new).collect(Collectors.toList());
134 // validate the objects
135 var result = new BeanValidationResult(fieldName, jpas);
138 for (JpaPdpPolicyStatus jpa : jpas) {
139 result.addResult(jpa.validate(String.valueOf(count++)));
142 if (!result.isValid()) {
143 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult());
149 private List<PdpPolicyStatus> asPolicyStatusList(List<JpaPdpPolicyStatus> jpaPdpPolicyStatusList) {
150 return jpaPdpPolicyStatusList.stream().map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());