Fix sonar issues in policy-api
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / PolicyTypeProvider.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP Policy API\r
4  * ================================================================================\r
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  *\r
19  * SPDX-License-Identifier: Apache-2.0\r
20  * ============LICENSE_END=========================================================\r
21  */\r
22 \r
23 package org.onap.policy.api.main.rest.provider;\r
24 \r
25 import java.util.List;\r
26 import javax.ws.rs.core.Response;\r
27 import org.onap.policy.models.base.PfModelException;\r
28 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;\r
29 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;\r
30 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;\r
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;\r
32 \r
33 /**\r
34  * Class to provide all kinds of policy type operations.\r
35  *\r
36  * @author Chenfei Gao (cgao@research.att.com)\r
37  */\r
38 public class PolicyTypeProvider extends CommonModelProvider {\r
39 \r
40     /**\r
41      * Default constructor.\r
42      */\r
43     public PolicyTypeProvider() throws PfModelException {\r
44         super();\r
45     }\r
46 \r
47     /**\r
48      * Retrieves a list of policy types matching specified policy type ID and version.\r
49      *\r
50      * @param policyTypeId the ID of policy type\r
51      * @param policyTypeVersion the version of policy type\r
52      *\r
53      * @return the ToscaServiceTemplate object\r
54      *\r
55      * @throws PfModelException the PfModel parsing exception\r
56      */\r
57     public ToscaServiceTemplate fetchPolicyTypes(String policyTypeId, String policyTypeVersion)\r
58             throws PfModelException {\r
59 \r
60         ToscaPolicyTypeFilter policyTypeFilter = ToscaPolicyTypeFilter.builder()\r
61                 .name(policyTypeId).version(policyTypeVersion).build();\r
62         ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicyTypes(policyTypeFilter);\r
63 \r
64         if (policyTypeId != null && !hasPolicyType(serviceTemplate)) {\r
65             throw new PfModelException(Response.Status.NOT_FOUND,\r
66                     constructResourceNotFoundMessage(policyTypeId, policyTypeVersion));\r
67         }\r
68 \r
69         return serviceTemplate;\r
70     }\r
71 \r
72     /**\r
73      * Retrieves a list of policy types with the latest versions.\r
74      *\r
75      * @param policyTypeId the ID of policy type\r
76      *\r
77      * @return the ToscaServiceTemplate object\r
78      *\r
79      * @throws PfModelException the PfModel parsing exception\r
80      */\r
81     public ToscaServiceTemplate fetchLatestPolicyTypes(String policyTypeId) throws PfModelException {\r
82 \r
83         ToscaPolicyTypeFilter policyTypeFilter = ToscaPolicyTypeFilter.builder()\r
84                 .name(policyTypeId).version(ToscaPolicyTypeFilter.LATEST_VERSION).build();\r
85         ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicyTypes(policyTypeFilter);\r
86         if (!hasPolicyType(serviceTemplate)) {\r
87             throw new PfModelException(Response.Status.NOT_FOUND,\r
88                     constructResourceNotFoundMessage(policyTypeId, null));\r
89         }\r
90 \r
91         return serviceTemplate;\r
92     }\r
93 \r
94     /**\r
95      * Creates a new policy type.\r
96      *\r
97      * @param body the entity body of policy type\r
98      *\r
99      * @return the ToscaServiceTemplate object\r
100      * @throws PfModelException the PfModel parsing exception\r
101      */\r
102     public ToscaServiceTemplate createPolicyType(ToscaServiceTemplate body) throws PfModelException {\r
103 \r
104         return modelsProvider.createPolicyTypes(body);\r
105     }\r
106 \r
107     /**\r
108      * Delete the policy type matching specified policy type ID and version.\r
109      *\r
110      * @param policyTypeId the ID of policy type\r
111      * @param policyTypeVersion the version of policy type\r
112      *\r
113      * @return the ToscaServiceTemplate object\r
114      *\r
115      * @throws PfModelException the PfModel parsing exception\r
116      */\r
117     public ToscaServiceTemplate deletePolicyType(String policyTypeId, String policyTypeVersion)\r
118             throws PfModelException {\r
119 \r
120         validateDeleteEligibility(policyTypeId, policyTypeVersion);\r
121 \r
122         ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicyType(policyTypeId, policyTypeVersion);\r
123 \r
124         if (!hasPolicyType(serviceTemplate)) {\r
125             throw new PfModelException(Response.Status.NOT_FOUND,\r
126                     constructResourceNotFoundMessage(policyTypeId, policyTypeVersion));\r
127         }\r
128 \r
129         return serviceTemplate;\r
130     }\r
131 \r
132     /**\r
133      * Validates whether specified policy type can be deleted based on the rule that\r
134      * policy type parameterized by at least one policies cannot be deleted.\r
135      *\r
136      * @param policyTypeId the ID of policy type\r
137      * @param policyTypeVersion the version of policy type\r
138      *\r
139      * @throws PfModelException the PfModel parsing exception\r
140      */\r
141     private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion) throws PfModelException {\r
142 \r
143         ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder()\r
144                 .type(policyTypeId).typeVersion(policyTypeVersion).build();\r
145         List<ToscaPolicy> policies = modelsProvider.getFilteredPolicyList(policyFilter);\r
146         if (!policies.isEmpty()) {\r
147             throw new PfModelException(Response.Status.CONFLICT,\r
148                     constructDeletePolicyTypeViolationMessage(policyTypeId, policyTypeVersion, policies));\r
149         }\r
150     }\r
151 \r
152     /**\r
153      * Constructs returned message for not found resource.\r
154      *\r
155      * @param policyTypeId the ID of policy type\r
156      * @param policyTypeVersion the version of policy type\r
157      *\r
158      * @return constructed message\r
159      */\r
160     private String constructResourceNotFoundMessage(String policyTypeId, String policyTypeVersion) {\r
161 \r
162         return "policy type with ID " + policyTypeId + ":" + policyTypeVersion + " does not exist";\r
163     }\r
164 }\r