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.HashMap;
27 import java.util.LinkedHashMap;
28 import java.util.List;
30 import java.util.Map.Entry;
31 import javax.ws.rs.core.Response;
33 import org.apache.commons.lang3.tuple.Pair;
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.base.PfModelException;
36 import org.onap.policy.models.pdp.concepts.PdpGroup;
37 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
39 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
40 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
43 * Class to provide all kinds of legacy guard policy operations.
45 * @author Chenfei Gao (cgao@research.att.com)
47 public class LegacyGuardPolicyProvider extends CommonModelProvider {
49 private static final String INVALID_POLICY_VERSION = "legacy policy version is not an integer";
50 private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
51 private static final Map<String, PfConceptKey> GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>();
54 GUARD_POLICY_TYPE_MAP.put("guard.frequency.",
55 new PfConceptKey("onap.policies.controlloop.guard.FrequencyLimiter:1.0.0"));
56 GUARD_POLICY_TYPE_MAP.put("guard.minmax.",
57 new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0"));
58 GUARD_POLICY_TYPE_MAP.put("guard.blacklist.",
59 new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0"));
63 * Default constructor.
65 public LegacyGuardPolicyProvider() throws PfModelException {
70 * Retrieves a list of guard policies matching specified ID and version.
72 * @param policyId the ID of policy
73 * @param policyVersion the version of policy
75 * @return the map of LegacyGuardPolicyOutput objects
77 public Map<String, LegacyGuardPolicyOutput> fetchGuardPolicy(String policyId, String policyVersion)
78 throws PfModelException {
80 if (policyVersion != null) {
81 validNumber(policyVersion, INVALID_POLICY_VERSION);
83 return modelsProvider.getGuardPolicy(policyId, policyVersion);
87 * Retrieves a list of deployed guard policies in each pdp group.
89 * @param policyId the ID of the policy
91 * @return a list of deployed policies in each pdp group
93 * @throws PfModelException the PfModel parsing exception
95 public Map<Pair<String, String>, Map<String, LegacyGuardPolicyOutput>> fetchDeployedGuardPolicies(String policyId)
96 throws PfModelException {
98 return collectDeployedPolicies(
99 policyId, getGuardPolicyType(policyId), modelsProvider::getGuardPolicy, Map::putAll, new HashMap<>());
103 * Creates a new guard policy.
105 * @param body the entity body of policy
107 * @return the map of LegacyGuardPolicyOutput objectst
109 public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body)
110 throws PfModelException {
112 return modelsProvider.createGuardPolicy(body);
116 * Deletes the guard policies matching specified ID and version.
118 * @param policyId the ID of policy
119 * @param policyVersion the version of policy
121 * @return the map of LegacyGuardPolicyOutput objects
123 public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(String policyId, String policyVersion)
124 throws PfModelException {
126 validNumber(policyVersion, INVALID_POLICY_VERSION);
127 validateDeleteEligibility(policyId, policyVersion);
129 return modelsProvider.deleteGuardPolicy(policyId, policyVersion);
133 * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted.
135 * @param policyId the ID of policy
136 * @param policyVersion the version of policy
138 * @throws PfModelException the PfModel parsing exception
140 private void validateDeleteEligibility(String policyId, String policyVersion) throws PfModelException {
142 List<ToscaPolicyIdentifier> policies = new ArrayList<>();
143 policies.add(new ToscaPolicyIdentifier(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
144 PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyList(policies).build();
146 List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);
148 if (!pdpGroups.isEmpty()) {
149 throw new PfModelException(Response.Status.CONFLICT,
150 constructDeletePolicyViolationMessage(policyId, policyVersion, pdpGroups));
155 * Retrieves guard policy type given guard policy ID.
157 * @param policyId the ID of guard policy
159 * @return the concept key of guard policy type
161 * @throws PfModelException the PfModel parsing exception
163 private PfConceptKey getGuardPolicyType(String policyId) throws PfModelException {
165 for (Entry<String, PfConceptKey> guardPolicyTypeEntry : GUARD_POLICY_TYPE_MAP.entrySet()) {
166 if (policyId.startsWith(guardPolicyTypeEntry.getKey())) {
167 return guardPolicyTypeEntry.getValue();
170 throw new PfModelException(Response.Status.BAD_REQUEST, "No policy type defined for " + policyId);