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.models.base.PfModelException;
30 import org.onap.policy.models.pdp.concepts.PdpGroup;
31 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
33 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
34 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
37 * Class to provide all kinds of legacy guard policy operations.
39 * @author Chenfei Gao (cgao@research.att.com)
41 public class LegacyGuardPolicyProvider extends CommonModelProvider {
43 private static final String INVALID_POLICY_VERSION = "legacy policy version is not an integer";
47 * Default constructor.
49 public LegacyGuardPolicyProvider() throws PfModelException {
54 * Retrieves a list of guard policies matching specified ID and version.
56 * @param policyId the ID of policy
57 * @param policyVersion the version of policy
59 * @return the map of LegacyGuardPolicyOutput objects
61 public Map<String, LegacyGuardPolicyOutput> fetchGuardPolicy(String policyId, String policyVersion)
62 throws PfModelException {
64 if (policyVersion != null) {
65 validNumber(policyVersion, INVALID_POLICY_VERSION);
67 return modelsProvider.getGuardPolicy(policyId, policyVersion);
71 * Creates a new guard policy.
73 * @param body the entity body of policy
75 * @return the map of LegacyGuardPolicyOutput objectst
77 public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body)
78 throws PfModelException {
80 return modelsProvider.createGuardPolicy(body);
84 * Deletes the guard policies matching specified ID and version.
86 * @param policyId the ID of policy
87 * @param policyVersion the version of policy
89 * @return the map of LegacyGuardPolicyOutput objects
91 public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(String policyId, String policyVersion)
92 throws PfModelException {
94 validNumber(policyVersion, INVALID_POLICY_VERSION);
95 validateDeleteEligibility(policyId, policyVersion);
97 return modelsProvider.deleteGuardPolicy(policyId, policyVersion);
101 * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted.
103 * @param policyId the ID of policy
104 * @param policyVersion the version of policy
106 * @throws PfModelException the PfModel parsing exception
108 private void validateDeleteEligibility(String policyId, String policyVersion) throws PfModelException {
110 List<ToscaPolicyIdentifier> policies = new ArrayList<>();
111 policies.add(new ToscaPolicyIdentifier(policyId, policyVersion));
112 PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyList(policies).build();
114 List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);
116 if (!pdpGroups.isEmpty()) {
117 throw new PfModelException(Response.Status.CONFLICT,
118 constructDeletePolicyViolationMessage(policyId, policyVersion, pdpGroups));