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