978a8c0ab1ac78231a8dba333012a364a7b3abb1
[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.HashMap;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31 import javax.ws.rs.core.Response;
32
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;
41
42 /**
43  * Class to provide all kinds of legacy guard policy operations.
44  *
45  * @author Chenfei Gao (cgao@research.att.com)
46  */
47 public class LegacyGuardPolicyProvider extends CommonModelProvider {
48
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<>();
52
53     static {
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"));
60     }
61
62     /**
63      * Default constructor.
64      */
65     public LegacyGuardPolicyProvider() throws PfModelException {
66         super();
67     }
68
69     /**
70      * Retrieves a list of guard policies matching specified ID and version.
71      *
72      * @param policyId the ID of policy
73      * @param policyVersion the version of policy
74      *
75      * @return the map of LegacyGuardPolicyOutput objects
76      */
77     public Map<String, LegacyGuardPolicyOutput> fetchGuardPolicy(String policyId, String policyVersion)
78             throws PfModelException {
79
80         if (policyVersion != null) {
81             validNumber(policyVersion, INVALID_POLICY_VERSION);
82         }
83         return modelsProvider.getGuardPolicy(policyId, policyVersion);
84     }
85
86     /**
87      * Retrieves a list of deployed guard policies in each pdp group.
88      *
89      * @param policyId the ID of the policy
90      *
91      * @return a list of deployed policies in each pdp group
92      *
93      * @throws PfModelException the PfModel parsing exception
94      */
95     public Map<Pair<String, String>, Map<String, LegacyGuardPolicyOutput>> fetchDeployedGuardPolicies(String policyId)
96             throws PfModelException {
97
98         return collectDeployedPolicies(
99                 policyId, getGuardPolicyType(policyId), modelsProvider::getGuardPolicy, Map::putAll, new HashMap<>());
100     }
101
102     /**
103      * Creates a new guard policy.
104      *
105      * @param body the entity body of policy
106      *
107      * @return the map of LegacyGuardPolicyOutput objectst
108      */
109     public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body)
110             throws PfModelException {
111
112         return modelsProvider.createGuardPolicy(body);
113     }
114
115     /**
116      * Deletes the guard policies matching specified ID and version.
117      *
118      * @param policyId the ID of policy
119      * @param policyVersion the version of policy
120      *
121      * @return the map of LegacyGuardPolicyOutput objects
122      */
123     public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(String policyId, String policyVersion)
124             throws PfModelException {
125
126         validNumber(policyVersion, INVALID_POLICY_VERSION);
127         validateDeleteEligibility(policyId, policyVersion);
128
129         return modelsProvider.deleteGuardPolicy(policyId, policyVersion);
130     }
131
132     /**
133      * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted.
134      *
135      * @param policyId the ID of policy
136      * @param policyVersion the version of policy
137      *
138      * @throws PfModelException the PfModel parsing exception
139      */
140     private void validateDeleteEligibility(String policyId, String policyVersion) throws PfModelException {
141
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();
145
146         List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);
147
148         if (!pdpGroups.isEmpty()) {
149             throw new PfModelException(Response.Status.CONFLICT,
150                     constructDeletePolicyViolationMessage(policyId, policyVersion, pdpGroups));
151         }
152     }
153
154     /**
155      * Retrieves guard policy type given guard policy ID.
156      *
157      * @param policyId the ID of guard policy
158      *
159      * @return the concept key of guard policy type
160      *
161      * @throws PfModelException the PfModel parsing exception
162      */
163     private PfConceptKey getGuardPolicyType(String policyId) throws PfModelException {
164
165         for (Entry<String, PfConceptKey> guardPolicyTypeEntry : GUARD_POLICY_TYPE_MAP.entrySet()) {
166             if (policyId.startsWith(guardPolicyTypeEntry.getKey())) {
167                 return guardPolicyTypeEntry.getValue();
168             }
169         }
170         throw new PfModelException(Response.Status.BAD_REQUEST, "No policy type defined for " + policyId);
171     }
172 }