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