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