Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / service / PolicyStatusService.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.service;
23
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;
39
40 @Service
41 @Transactional
42 @RequiredArgsConstructor
43 public class PolicyStatusService {
44
45     private final PolicyStatusRepository policyStatusRepository;
46
47     /**
48      * Gets all status for policies in a group.
49      *
50      * @param pdpGroup the group's name
51      * @return the policy status list found
52      */
53     public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull String pdpGroup) {
54         return asPolicyStatusList(policyStatusRepository.findByPdpGroup(pdpGroup));
55     }
56
57     /**
58      * Gets all status for policies.
59      *
60      * @return the policy status list found
61      */
62     public List<PdpPolicyStatus> getAllPolicyStatus() {
63         return asPolicyStatusList(policyStatusRepository.findAll());
64     }
65
66     /**
67      * Gets all status for a policy.
68      *
69      * @param policy the policy
70      * @return the policy status list found
71      */
72     public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy) {
73
74         if (policy.getVersion() != null) {
75             return asPolicyStatusList(policyStatusRepository
76                 .findByKeyParentKeyNameAndKeyParentKeyVersion(policy.getName(), policy.getVersion()));
77
78         } else {
79             return asPolicyStatusList(policyStatusRepository.findByKeyParentKeyName(policy.getName()));
80         }
81     }
82
83     /**
84      * Gets all status for a policy in a group.
85      *
86      * @param pdpGroup the group's name
87      * @param policy the policy
88      * @return the policy status list found
89      */
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()));
95         } else {
96             return asPolicyStatusList(
97                 policyStatusRepository.findByPdpGroupAndKeyParentKeyName(pdpGroup, policy.getName()));
98         }
99     }
100
101     /**
102      * Creates, updates, and deletes collections of policy status.
103      *
104      * @param createObjs the objects to create
105      * @param updateObjs the objects to update
106      * @param deleteObjs the objects to delete
107      */
108     public void cudPolicyStatus(Collection<PdpPolicyStatus> createObjs, Collection<PdpPolicyStatus> updateObjs,
109         Collection<PdpPolicyStatus> deleteObjs) {
110         try {
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);
117         }
118     }
119
120     /**
121      * Converts a collection of authorative policy status to a collection of JPA policy
122      * status. Validates the resulting list.
123      *
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
127      */
128     private List<JpaPdpPolicyStatus> fromAuthorativeStatus(Collection<PdpPolicyStatus> objs, String fieldName) {
129         if (objs == null) {
130             return Collections.emptyList();
131         }
132
133         List<JpaPdpPolicyStatus> jpas = objs.stream().map(JpaPdpPolicyStatus::new).collect(Collectors.toList());
134
135         // validate the objects
136         var result = new BeanValidationResult(fieldName, jpas);
137
138         var count = 0;
139         for (JpaPdpPolicyStatus jpa : jpas) {
140             result.addResult(jpa.validate(String.valueOf(count++)));
141         }
142
143         if (!result.isValid()) {
144             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult());
145         }
146
147         return jpas;
148     }
149
150     private List<PdpPolicyStatus> asPolicyStatusList(List<JpaPdpPolicyStatus> jpaPdpPolicyStatusList) {
151         return jpaPdpPolicyStatusList.stream().map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
152     }
153 }