d0032b0361f945868292110834360b265ac1c689
[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.List;
27 import java.util.Map;
28
29 import javax.ws.rs.core.Response;
30
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.tosca.authorative.concepts.ToscaPolicyIdentifier;
35 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
36 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
37
38 /**
39  * Class to provide all kinds of legacy guard policy operations.
40  *
41  * @author Chenfei Gao (cgao@research.att.com)
42  */
43 public class LegacyGuardPolicyProvider extends CommonModelProvider {
44
45     private static final String INVALID_POLICY_VERSION = "legacy policy version is not an integer";
46     private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
47
48
49     /**
50      * Default constructor.
51      */
52     public LegacyGuardPolicyProvider() throws PfModelException {
53         super();
54     }
55
56     /**
57      * Retrieves a list of guard policies matching specified ID and version.
58      *
59      * @param policyId the ID of policy
60      * @param policyVersion the version of policy
61      *
62      * @return the map of LegacyGuardPolicyOutput objects
63      */
64     public Map<String, LegacyGuardPolicyOutput> fetchGuardPolicy(String policyId, String policyVersion)
65             throws PfModelException {
66
67         if (policyVersion != null) {
68             validNumber(policyVersion, INVALID_POLICY_VERSION);
69         }
70         return modelsProvider.getGuardPolicy(policyId, policyVersion);
71     }
72
73     /**
74      * Creates a new guard policy.
75      *
76      * @param body the entity body of policy
77      *
78      * @return the map of LegacyGuardPolicyOutput objectst
79      */
80     public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body)
81             throws PfModelException {
82
83         return modelsProvider.createGuardPolicy(body);
84     }
85
86     /**
87      * Deletes the guard policies matching specified ID and version.
88      *
89      * @param policyId the ID of policy
90      * @param policyVersion the version of policy
91      *
92      * @return the map of LegacyGuardPolicyOutput objects
93      */
94     public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(String policyId, String policyVersion)
95             throws PfModelException {
96
97         validNumber(policyVersion, INVALID_POLICY_VERSION);
98         validateDeleteEligibility(policyId, policyVersion);
99
100         return modelsProvider.deleteGuardPolicy(policyId, policyVersion);
101     }
102
103     /**
104      * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted.
105      *
106      * @param policyId the ID of policy
107      * @param policyVersion the version of policy
108      *
109      * @throws PfModelException the PfModel parsing exception
110      */
111     private void validateDeleteEligibility(String policyId, String policyVersion) throws PfModelException {
112
113         List<ToscaPolicyIdentifier> policies = new ArrayList<>();
114         policies.add(new ToscaPolicyIdentifier(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
115         PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyList(policies).build();
116
117         List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);
118
119         if (!pdpGroups.isEmpty()) {
120             throw new PfModelException(Response.Status.CONFLICT,
121                     constructDeletePolicyViolationMessage(policyId, policyVersion, pdpGroups));
122         }
123     }
124 }