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