6b123952f840fc37194aabf3d880e85a120aa411
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / PolicyProvider.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 java.util.ArrayList;\r
27 import java.util.List;\r
28 import java.util.Map;\r
29 \r
30 import javax.ws.rs.core.Response;\r
31 \r
32 import org.apache.commons.lang3.tuple.Pair;\r
33 import org.onap.policy.models.base.PfConceptKey;\r
34 import org.onap.policy.models.base.PfModelException;\r
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;\r
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;\r
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;\r
38 \r
39 /**\r
40  * Class to provide all kinds of policy operations.\r
41  *\r
42  * @author Chenfei Gao (cgao@research.att.com)\r
43  */\r
44 public class PolicyProvider extends CommonModelProvider {\r
45 \r
46     /**\r
47      * Default constructor.\r
48      */\r
49     public PolicyProvider() throws PfModelException {\r
50         super();\r
51     }\r
52 \r
53     /**\r
54      * Retrieves a list of policies matching specified ID and version of both policy type and policy.\r
55      *\r
56      * @param policyTypeId the ID of policy type\r
57      * @param policyTypeVersion the version of policy type\r
58      * @param policyId the ID of policy\r
59      * @param policyVersion the version of policy\r
60      *\r
61      * @return the ToscaServiceTemplate object\r
62      *\r
63      * @throws PfModelException the PfModel parsing exception\r
64      */\r
65     public ToscaServiceTemplate fetchPolicies(String policyTypeId, String policyTypeVersion, String policyId,\r
66             String policyVersion) throws PfModelException {\r
67 \r
68         ToscaServiceTemplate serviceTemplate =\r
69                 getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion);\r
70 \r
71         if (!hasPolicy(serviceTemplate)) {\r
72             throw new PfModelException(Response.Status.NOT_FOUND,\r
73                     constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, policyVersion));\r
74         }\r
75 \r
76         return serviceTemplate;\r
77     }\r
78 \r
79     /**\r
80      * Retrieves a list of policies with the latest versions that match specified policy type id and version.\r
81      *\r
82      * @param policyTypeId the ID of policy type\r
83      * @param policyTypeVersion the version of policy type\r
84      * @param policyId the ID of the policy\r
85      *\r
86      * @return the ToscaServiceTemplate object\r
87      *\r
88      * @throws PfModelException the PfModel parsing exception\r
89      */\r
90     public ToscaServiceTemplate fetchLatestPolicies(String policyTypeId, String policyTypeVersion, String policyId)\r
91             throws PfModelException {\r
92 \r
93         ToscaServiceTemplate serviceTemplate =\r
94                 getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, ToscaPolicyFilter.LATEST_VERSION);\r
95 \r
96         if (!hasPolicy(serviceTemplate)) {\r
97             throw new PfModelException(Response.Status.NOT_FOUND,\r
98                     constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, null));\r
99         }\r
100 \r
101         return serviceTemplate;\r
102     }\r
103 \r
104     /**\r
105      * Retrieves a list of deployed policies in each pdp group.\r
106      *\r
107      * @param policyTypeId the ID of policy type\r
108      * @param policyTypeVersion the version of policy type\r
109      * @param policyId the ID of the policy\r
110      *\r
111      * @return a list of deployed policies in each pdp group\r
112      *\r
113      * @throws PfModelException the PfModel parsing exception\r
114      */\r
115     public Map<Pair<String, String>, List<ToscaPolicy>> fetchDeployedPolicies(String policyTypeId,\r
116             String policyTypeVersion, String policyId) throws PfModelException {\r
117 \r
118         return collectDeployedPolicies(policyId, new PfConceptKey(policyTypeId, policyTypeVersion),\r
119                 modelsProvider::getPolicyList, List::addAll, new ArrayList<>(5));\r
120     }\r
121 \r
122     /**\r
123      * Creates one or more new policies for the same policy type ID and version.\r
124      *\r
125      * @param policyTypeId the ID of policy type\r
126      * @param policyTypeVersion the version of policy type\r
127      * @param body the entity body of polic(ies)\r
128      *\r
129      * @return the ToscaServiceTemplate object\r
130      *\r
131      * @throws PfModelException the PfModel parsing exception\r
132      */\r
133     public ToscaServiceTemplate createPolicy(String policyTypeId, String policyTypeVersion, ToscaServiceTemplate body)\r
134             throws PfModelException {\r
135 \r
136         return modelsProvider.createPolicies(body);\r
137     }\r
138 \r
139     /**\r
140      * Creates one or more new policies.\r
141      *\r
142      * @param body the entity body of policy\r
143      *\r
144      * @return the ToscaServiceTemplate object\r
145      *\r
146      * @throws PfModelException the PfModel parsing exception\r
147      */\r
148     public ToscaServiceTemplate createPolicies(ToscaServiceTemplate body) throws PfModelException {\r
149         return modelsProvider.createPolicies(body);\r
150     }\r
151 \r
152     /**\r
153      * Deletes the policy matching specified ID and version of both policy type and policy.\r
154      *\r
155      * @param policyTypeId the ID of policy type\r
156      * @param policyTypeVersion the version of policy type\r
157      * @param policyId the ID of policy\r
158      * @param policyVersion the version of policy\r
159      *\r
160      * @return the ToscaServiceTemplate object\r
161      *\r
162      * @throws PfModelException the PfModel parsing exception\r
163      */\r
164     public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion, String policyId,\r
165             String policyVersion) throws PfModelException {\r
166 \r
167         ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicy(policyId, policyVersion);\r
168 \r
169         if (!hasPolicy(serviceTemplate)) {\r
170             throw new PfModelException(Response.Status.NOT_FOUND,\r
171                     constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, policyVersion));\r
172         }\r
173 \r
174         return serviceTemplate;\r
175     }\r
176 \r
177     /**\r
178      * Retrieves the specified version of the policy.\r
179      *\r
180      * @param policyTypeName the name of the policy type\r
181      * @param policyTypeVersion the version of the policy type\r
182      * @param policyName the name of the policy\r
183      * @param policyVersion the version of the policy\r
184      *\r
185      * @return the TOSCA service template containing the specified version of the policy\r
186      *\r
187      * @throws PfModelException the PfModel parsing exception\r
188      */\r
189     private ToscaServiceTemplate getFilteredPolicies(String policyTypeName, String policyTypeVersion, String policyName,\r
190             String policyVersion) throws PfModelException {\r
191 \r
192         ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder().name(policyName).version(policyVersion)\r
193                 .type(policyTypeName).typeVersion(policyTypeVersion).build();\r
194         return modelsProvider.getFilteredPolicies(policyFilter);\r
195     }\r
196 \r
197     /**\r
198      * Constructs returned message for not found resource.\r
199      *\r
200      * @param policyTypeId the ID of policy type\r
201      * @param policyTypeVersion the version of policy type\r
202      * @param policyId the ID of policy\r
203      * @param policyVersion the version of policy\r
204      *\r
205      * @return constructed message\r
206      */\r
207     private String constructResourceNotFoundMessage(String policyTypeId, String policyTypeVersion, String policyId,\r
208             String policyVersion) {\r
209 \r
210         return "policy with ID " + policyId + ":" + policyVersion + " and type " + policyTypeId + ":"\r
211                 + policyTypeVersion + " does not exist";\r
212     }\r
213 }\r