aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/main/java/org/onap/policy/pap/main/service/PolicyStatusService.java
blob: ac7830d209e41ae2e2046894540e5325ea4c5016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2022 Bell Canada. All rights reserved.
 *  Modifications Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pap.main.service;

import jakarta.ws.rs.core.Response;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.onap.policy.common.parameters.BeanValidationResult;
import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
import org.onap.policy.models.pdp.persistence.concepts.JpaPdpPolicyStatus;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
import org.onap.policy.pap.main.repository.PolicyStatusRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class PolicyStatusService {

    private final PolicyStatusRepository policyStatusRepository;

    /**
     * Gets all status for policies in a group.
     *
     * @param pdpGroup the group's name
     * @return the policy status list found
     */
    public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull String pdpGroup) {
        return asPolicyStatusList(policyStatusRepository.findByPdpGroup(pdpGroup));
    }

    /**
     * Gets all status for policies.
     *
     * @return the policy status list found
     */
    public List<PdpPolicyStatus> getAllPolicyStatus() {
        return asPolicyStatusList(policyStatusRepository.findAll());
    }

    /**
     * Gets all status for a policy.
     *
     * @param policy the policy
     * @return the policy status list found
     */
    public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy) {

        if (policy.getVersion() != null) {
            return asPolicyStatusList(policyStatusRepository
                .findByKeyParentKeyNameAndKeyParentKeyVersion(policy.getName(), policy.getVersion()));

        } else {
            return asPolicyStatusList(policyStatusRepository.findByKeyParentKeyName(policy.getName()));
        }
    }

    /**
     * Gets all status for a policy in a group.
     *
     * @param pdpGroup the group's name
     * @param policy the policy
     * @return the policy status list found
     */
    public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull String pdpGroup,
        @NonNull ToscaConceptIdentifierOptVersion policy) {
        if (policy.getVersion() != null) {
            return asPolicyStatusList(policyStatusRepository.findByPdpGroupAndKeyParentKeyNameAndKeyParentKeyVersion(
                pdpGroup, policy.getName(), policy.getVersion()));
        } else {
            return asPolicyStatusList(
                policyStatusRepository.findByPdpGroupAndKeyParentKeyName(pdpGroup, policy.getName()));
        }
    }

    /**
     * Creates, updates, and deletes collections of policy status.
     *
     * @param createObjs the objects to create
     * @param updateObjs the objects to update
     * @param deleteObjs the objects to delete
     */
    public void cudPolicyStatus(Collection<PdpPolicyStatus> createObjs, Collection<PdpPolicyStatus> updateObjs,
        Collection<PdpPolicyStatus> deleteObjs) {
        try {
            policyStatusRepository.deleteAll(fromAuthorativeStatus(deleteObjs, "deletePdpPolicyStatusList"));
            policyStatusRepository.saveAll(fromAuthorativeStatus(createObjs, "createPdpPolicyStatusList"));
            policyStatusRepository.saveAll(fromAuthorativeStatus(updateObjs, "updatePdpPolicyStatusList"));
        } catch (Exception exc) {
            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
                "Policy status operation failed." + exc.getMessage(), exc);
        }
    }

    /**
     * Converts a collection of authorative policy status to a collection of JPA policy
     * status. Validates the resulting list.
     *
     * @param objs authorative policy status to convert
     * @param fieldName name of the field containing the collection
     * @return a list of JPA policy status
     */
    private List<JpaPdpPolicyStatus> fromAuthorativeStatus(Collection<PdpPolicyStatus> objs, String fieldName) {
        if (objs == null) {
            return Collections.emptyList();
        }

        List<JpaPdpPolicyStatus> jpas = objs.stream().map(JpaPdpPolicyStatus::new).collect(Collectors.toList());

        // validate the objects
        var result = new BeanValidationResult(fieldName, jpas);

        var count = 0;
        for (JpaPdpPolicyStatus jpa : jpas) {
            result.addResult(jpa.validate(String.valueOf(count++)));
        }

        if (!result.isValid()) {
            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult());
        }

        return jpas;
    }

    private List<PdpPolicyStatus> asPolicyStatusList(List<JpaPdpPolicyStatus> jpaPdpPolicyStatusList) {
        return jpaPdpPolicyStatusList.stream().map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
    }
}