a931f59340d011d05c0003c77da079c34abe7125
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / LegacyOperationalPolicyProvider.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 javax.ws.rs.core.Response;
28 import org.onap.policy.api.main.parameters.ApiParameterGroup;
29 import org.onap.policy.common.parameters.ParameterService;
30 import org.onap.policy.models.base.PfModelException;
31 import org.onap.policy.models.pdp.concepts.PdpGroup;
32 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
33 import org.onap.policy.models.provider.PolicyModelsProvider;
34 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
35 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
37 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
38
39 /**
40  * Class to provide all kinds of legacy operational policy operations.
41  *
42  * @author Chenfei Gao (cgao@research.att.com)
43  */
44 public class LegacyOperationalPolicyProvider implements AutoCloseable {
45
46     private PolicyModelsProvider modelsProvider;
47
48     /**
49      * Default constructor.
50      */
51     public LegacyOperationalPolicyProvider() throws PfModelException {
52
53         ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
54         PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
55         modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
56     }
57
58     /**
59      * Retrieves a list of operational policies matching specified ID and version.
60      *
61      * @param policyId the ID of policy
62      * @param policyVersion the version of policy
63      *
64      * @return the LegacyOperationalPolicy object
65      */
66     public LegacyOperationalPolicy fetchOperationalPolicy(String policyId, String policyVersion)
67             throws PfModelException {
68
69         if (policyVersion != null) {
70             validateLegacyOperationalPolicyVersion(policyVersion);
71         }
72         return modelsProvider.getOperationalPolicy(policyId, policyVersion);
73     }
74
75     /**
76      * Creates a new operational policy.
77      *
78      * @param body the entity body of policy
79      *
80      * @return the LegacyOperationalPolicy object
81      */
82     public LegacyOperationalPolicy createOperationalPolicy(LegacyOperationalPolicy body) throws PfModelException {
83
84         return modelsProvider.createOperationalPolicy(body);
85     }
86
87     /**
88      * Deletes the operational policies matching specified ID and version.
89      *
90      * @param policyId the ID of policy
91      * @param policyVersion the version of policy
92      *
93      * @return the LegacyOperationalPolicy object
94      */
95     public LegacyOperationalPolicy deleteOperationalPolicy(String policyId, String policyVersion)
96             throws PfModelException {
97
98         validateLegacyOperationalPolicyVersion(policyVersion);
99         validateDeleteEligibility(policyId, policyVersion);
100
101         return modelsProvider.deleteOperationalPolicy(policyId, policyVersion);
102     }
103
104     /**
105      * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted.
106      *
107      * @param policyId the ID of policy
108      * @param policyVersion the version of policy
109      *
110      * @throws PfModelException the PfModel parsing exception
111      */
112     private void validateDeleteEligibility(String policyId, String policyVersion) throws PfModelException {
113
114         List<ToscaPolicyIdentifier> policies = new ArrayList<>();
115         policies.add(new ToscaPolicyIdentifier(policyId, policyVersion));
116         PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyList(policies).build();
117
118         List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);
119
120         if (!pdpGroups.isEmpty()) {
121             throw new PfModelException(Response.Status.CONFLICT,
122                     constructDeleteRuleViolationMessage(policyId, policyVersion, pdpGroups));
123         }
124     }
125
126     /**
127      * Validates whether the legacy operational policy version is an integer.
128      *
129      * @param policyVersion the version of policy
130      *
131      * @throws PfModelException the PfModel parsing exception
132      */
133     private void validateLegacyOperationalPolicyVersion(String policyVersion) throws PfModelException {
134
135         try {
136             Integer.valueOf(policyVersion);
137         } catch (NumberFormatException exc) {
138             throw new PfModelException(Response.Status.BAD_REQUEST,
139                     "legacy policy version is not an integer", exc);
140         }
141     }
142
143     /**
144      * Constructs returned message for policy delete rule violation.
145      *
146      * @param policyId the ID of policy
147      * @param policyVersion the version of policy
148      * @param pdpGroups the list of pdp groups
149      *
150      * @return the constructed message
151      */
152     private String constructDeleteRuleViolationMessage(
153             String policyId, String policyVersion, List<PdpGroup> pdpGroups) {
154
155         List<String> pdpGroupNameVersionList = new ArrayList<>();
156         for (PdpGroup pdpGroup : pdpGroups) {
157             pdpGroupNameVersionList.add(pdpGroup.getName() + ":" + pdpGroup.getVersion());
158         }
159         String deployedPdpGroups = String.join(",", pdpGroupNameVersionList);
160         return "policy with ID " + policyId + ":" + policyVersion
161                 + " cannot be deleted as it is deployed in pdp groups " + deployedPdpGroups;
162     }
163
164     /**
165      * Closes the connection to database.
166      *
167      * @throws PfModelException the PfModel parsing exception
168      */
169     @Override
170     public void close() throws PfModelException {
171
172         modelsProvider.close();
173     }
174 }