2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.api.main.rest.provider;
25 import java.util.ArrayList;
26 import java.util.List;
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;
42 * Class to provide all kinds of legacy guard policy operations.
44 * @author Chenfei Gao (cgao@research.att.com)
46 public class LegacyGuardPolicyProvider implements AutoCloseable {
48 private PolicyModelsProvider modelsProvider;
51 * Default constructor.
53 public LegacyGuardPolicyProvider() throws PfModelException {
55 ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
56 PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
57 modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
61 * Retrieves a list of guard policies matching specified ID and version.
63 * @param policyId the ID of policy
64 * @param policyVersion the version of policy
66 * @return the map of LegacyGuardPolicyOutput objects
68 public Map<String, LegacyGuardPolicyOutput> fetchGuardPolicy(String policyId, String policyVersion)
69 throws PfModelException {
71 return modelsProvider.getGuardPolicy(policyId);
75 * Creates a new guard policy.
77 * @param body the entity body of policy
79 * @return the map of LegacyGuardPolicyOutput objectst
81 public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body) throws PfModelException {
83 return modelsProvider.createGuardPolicy(body);
87 * Deletes the guard policies matching specified ID and version.
89 * @param policyId the ID of policy
90 * @param policyVersion the version of policy
92 * @return the map of LegacyGuardPolicyOutput objects
94 public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(String policyId, String policyVersion)
95 throws PfModelException {
97 validateDeleteEligibility(policyId, policyVersion);
99 return modelsProvider.deleteGuardPolicy(policyId);
103 * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted.
105 * @param policyId the ID of policy
106 * @param policyVersion the version of policy
108 * @throws PfModelException the PfModel parsing exception
110 private void validateDeleteEligibility(String policyId, String policyVersion) throws PfModelException {
112 List<ToscaPolicyIdentifier> policies = new ArrayList<>();
113 policies.add(new ToscaPolicyIdentifier(policyId, policyVersion));
114 PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyList(policies).build();
116 List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);
118 if (!pdpGroups.isEmpty()) {
119 throw new PfModelException(Response.Status.CONFLICT,
120 constructDeleteRuleViolationMessage(policyId, policyVersion, pdpGroups));
125 * Constructs returned message for policy delete rule violation.
127 * @param policyId the ID of policy
128 * @param policyVersion the version of policy
129 * @param pdpGroups the list of pdp groups
131 * @return the constructed message
133 private String constructDeleteRuleViolationMessage(
134 String policyId, String policyVersion, List<PdpGroup> pdpGroups) {
136 List<String> pdpGroupNameVersionList = new ArrayList<>();
137 for (PdpGroup pdpGroup : pdpGroups) {
138 pdpGroupNameVersionList.add(pdpGroup.getName() + ":" + pdpGroup.getVersion());
140 String deployedPdpGroups = String.join(",", pdpGroupNameVersionList);
141 return "policy with ID " + policyId + ":" + policyVersion
142 + " cannot be deleted as it is deployed in pdp groups " + deployedPdpGroups;
146 * Closes the connection to database.
148 * @throws PfModelException the PfModel parsing exception
151 public void close() throws PfModelException {
153 modelsProvider.close();