Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / service / PdpGroupService.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.api.main.service;
23
24 import jakarta.ws.rs.core.Response;
25 import java.util.List;
26 import java.util.stream.Collectors;
27 import lombok.RequiredArgsConstructor;
28 import org.onap.policy.api.main.repository.PdpGroupRepository;
29 import org.onap.policy.models.base.PfModelRuntimeException;
30 import org.onap.policy.models.pdp.concepts.PdpGroup;
31 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
32 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
34 import org.springframework.stereotype.Service;
35 import org.springframework.transaction.annotation.Transactional;
36
37 @Service
38 @Transactional(readOnly = true)
39 @RequiredArgsConstructor
40 public class PdpGroupService {
41
42     private final PdpGroupRepository pdpGroupRepository;
43
44     /**
45      * Fetch all the PDP groups from the DB.
46      * @return a list of {@link PdpGroup}
47      */
48     private List<PdpGroup> getAllPdpGroups() {
49         return pdpGroupRepository.findAll().stream().map(JpaPdpGroup::toAuthorative).collect(Collectors.toList());
50     }
51
52     /**
53      * Assert that the policy type is not supported in any PDP group.
54      *
55      * @param policyTypeName the policy type name
56      * @param policyTypeVersion the policy type version
57      * @throws PfModelRuntimeException if the policy type is supported in a PDP group
58      */
59     public void assertPolicyTypeNotSupportedInPdpGroup(final String policyTypeName, final String policyTypeVersion)
60         throws PfModelRuntimeException {
61         final var policyTypeIdentifier = new ToscaConceptIdentifier(policyTypeName, policyTypeVersion);
62         for (PdpGroup pdpGroup : getAllPdpGroups()) {
63             for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
64                 if (pdpSubGroup.getSupportedPolicyTypes().contains(policyTypeIdentifier)) {
65                     throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
66                         "policy type is in use, it is referenced in PDP group " + pdpGroup.getName() + " subgroup "
67                             + pdpSubGroup.getPdpType());
68                 }
69             }
70         }
71     }
72
73     /**
74      * Assert that the policy is not deployed in a PDP group.
75      *
76      * @param policyName the policy name
77      * @param policyVersion the policy version
78      * @throws PfModelRuntimeException thrown if the policy is deployed in a PDP group
79      */
80     public void assertPolicyNotDeployedInPdpGroup(final String policyName, final String policyVersion)
81         throws PfModelRuntimeException {
82         final var policyIdentifier = new ToscaConceptIdentifier(policyName, policyVersion);
83         for (PdpGroup pdpGroup : getAllPdpGroups()) {
84             for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
85                 if (pdpSubGroup.getPolicies().contains(policyIdentifier)) {
86                     throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
87                         "policy is in use, it is deployed in PDP group " + pdpGroup.getName() + " subgroup "
88                             + pdpSubGroup.getPdpType());
89                 }
90             }
91         }
92     }
93 }