2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2020 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.api.main.rest.provider;
26 import java.util.HashMap;
27 import java.util.LinkedHashMap;
29 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.tosca.legacy.concepts.LegacyGuardPolicyInput;
37 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
40 * Class to provide all kinds of legacy guard policy operations.
42 * @author Chenfei Gao (cgao@research.att.com)
44 public class LegacyGuardPolicyProvider extends CommonModelProvider {
46 private static final Map<String, PfConceptKey> GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>();
49 GUARD_POLICY_TYPE_MAP.put("guard.frequency.",
50 new PfConceptKey("onap.policies.controlloop.guard.FrequencyLimiter:1.0.0"));
51 GUARD_POLICY_TYPE_MAP.put("guard.minmax.", new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0"));
52 GUARD_POLICY_TYPE_MAP.put("guard.blacklist.",
53 new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0"));
57 * Default constructor.
59 public LegacyGuardPolicyProvider() throws PfModelException {
64 * Retrieves a list of guard policies matching specified ID and version.
66 * @param policyId the ID of policy
67 * @param policyVersion the version of policy
69 * @return the map of LegacyGuardPolicyOutput objects
71 public Map<String, LegacyGuardPolicyOutput> fetchGuardPolicy(String policyId, String policyVersion)
72 throws PfModelException {
74 return modelsProvider.getGuardPolicy(policyId, policyVersion);
78 * Retrieves a list of deployed guard policies in each pdp group.
80 * @param policyId the ID of the policy
82 * @return a list of deployed policies in each pdp group
84 * @throws PfModelException the PfModel parsing exception
86 public Map<Pair<String, String>, Map<String, LegacyGuardPolicyOutput>> fetchDeployedGuardPolicies(String policyId)
87 throws PfModelException {
89 return collectDeployedPolicies(policyId, getGuardPolicyType(policyId), modelsProvider::getGuardPolicy,
90 Map::putAll, new HashMap<>());
94 * Creates a new guard policy.
96 * @param body the entity body of policy
98 * @return the map of LegacyGuardPolicyOutput objectst
100 public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body) throws PfModelException {
102 return modelsProvider.createGuardPolicy(body);
106 * Deletes the guard policies matching specified ID and version.
108 * @param policyId the ID of policy
109 * @param policyVersion the version of policy
111 * @return the map of LegacyGuardPolicyOutput objects
113 public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(String policyId, String policyVersion)
114 throws PfModelException {
116 return modelsProvider.deleteGuardPolicy(policyId, policyVersion);
120 * Retrieves guard policy type given guard policy ID.
122 * @param policyId the ID of guard policy
124 * @return the concept key of guard policy type
126 * @throws PfModelException the PfModel parsing exception
128 private PfConceptKey getGuardPolicyType(String policyId) throws PfModelException {
130 for (Entry<String, PfConceptKey> guardPolicyTypeEntry : GUARD_POLICY_TYPE_MAP.entrySet()) {
131 if (policyId.startsWith(guardPolicyTypeEntry.getKey())) {
132 return guardPolicyTypeEntry.getValue();
135 throw new PfModelException(Response.Status.BAD_REQUEST, "No policy type defined for " + policyId);