Merge "Reserve version integer check and delete eligibility check"
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / LegacyGuardPolicyProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.api.main.rest.provider;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28 import javax.ws.rs.core.Response;
29 import org.onap.policy.api.main.parameters.ApiParameterGroup;
30 import org.onap.policy.common.parameters.ParameterService;
31 import org.onap.policy.models.base.PfModelException;
32 import org.onap.policy.models.pdp.concepts.PdpGroup;
33 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
34 import org.onap.policy.models.provider.PolicyModelsProvider;
35 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
36 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
38 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
39 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
40
41 /**
42  * Class to provide all kinds of legacy guard policy operations.
43  *
44  * @author Chenfei Gao (cgao@research.att.com)
45  */
46 public class LegacyGuardPolicyProvider implements AutoCloseable {
47
48     private PolicyModelsProvider modelsProvider;
49
50     /**
51      * Default constructor.
52      */
53     public LegacyGuardPolicyProvider() throws PfModelException {
54
55         ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
56         PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
57         modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
58     }
59
60     /**
61      * Retrieves a list of guard policies matching specified ID and version.
62      *
63      * @param policyId the ID of policy
64      * @param policyVersion the version of policy
65      *
66      * @return the map of LegacyGuardPolicyOutput objects
67      */
68     public Map<String, LegacyGuardPolicyOutput> fetchGuardPolicy(String policyId, String policyVersion)
69             throws PfModelException {
70
71         if (policyVersion != null) {
72             validateLegacyGuardPolicyVersion(policyVersion);
73         }
74         return modelsProvider.getGuardPolicy(policyId, policyVersion);
75     }
76
77     /**
78      * Creates a new guard policy.
79      *
80      * @param body the entity body of policy
81      *
82      * @return the map of LegacyGuardPolicyOutput objectst
83      */
84     public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body)
85             throws PfModelException {
86
87         return modelsProvider.createGuardPolicy(body);
88     }
89
90     /**
91      * Deletes the guard policies matching specified ID and version.
92      *
93      * @param policyId the ID of policy
94      * @param policyVersion the version of policy
95      *
96      * @return the map of LegacyGuardPolicyOutput objects
97      */
98     public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(String policyId, String policyVersion)
99             throws PfModelException {
100
101         validateLegacyGuardPolicyVersion(policyVersion);
102         validateDeleteEligibility(policyId, policyVersion);
103
104         return modelsProvider.deleteGuardPolicy(policyId, policyVersion);
105     }
106
107     /**
108      * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted.
109      *
110      * @param policyId the ID of policy
111      * @param policyVersion the version of policy
112      *
113      * @throws PfModelException the PfModel parsing exception
114      */
115     private void validateDeleteEligibility(String policyId, String policyVersion) throws PfModelException {
116
117         List<ToscaPolicyIdentifier> policies = new ArrayList<>();
118         policies.add(new ToscaPolicyIdentifier(policyId, policyVersion));
119         PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyList(policies).build();
120
121         List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);
122
123         if (!pdpGroups.isEmpty()) {
124             throw new PfModelException(Response.Status.CONFLICT,
125                     constructDeleteRuleViolationMessage(policyId, policyVersion, pdpGroups));
126         }
127     }
128
129     /**
130      * Validates whether the legacy guard policy version is an integer.
131      *
132      * @param policyVersion the version of policy
133      *
134      * @throws PfModelException the PfModel parsing exception
135      */
136     private void validateLegacyGuardPolicyVersion(String policyVersion) throws PfModelException {
137
138         try {
139             Integer.valueOf(policyVersion);
140         } catch (NumberFormatException exc) {
141             throw new PfModelException(Response.Status.BAD_REQUEST,
142                     "legacy policy version is not an integer", exc);
143         }
144     }
145
146     /**
147      * Constructs returned message for policy delete rule violation.
148      *
149      * @param policyId the ID of policy
150      * @param policyVersion the version of policy
151      * @param pdpGroups the list of pdp groups
152      *
153      * @return the constructed message
154      */
155     private String constructDeleteRuleViolationMessage(
156             String policyId, String policyVersion, List<PdpGroup> pdpGroups) {
157
158         List<String> pdpGroupNameVersionList = new ArrayList<>();
159         for (PdpGroup pdpGroup : pdpGroups) {
160             pdpGroupNameVersionList.add(pdpGroup.getName() + ":" + pdpGroup.getVersion());
161         }
162         String deployedPdpGroups = String.join(",", pdpGroupNameVersionList);
163         return "policy with ID " + policyId + ":" + policyVersion
164                 + " cannot be deleted as it is deployed in pdp groups " + deployedPdpGroups;
165     }
166
167     /**
168      * Closes the connection to database.
169      *
170      * @throws PfModelException the PfModel parsing exception
171      */
172     @Override
173     public void close() throws PfModelException {
174
175         modelsProvider.close();
176     }
177 }