Merge "Misc simple sonar issue fixes"
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / authorative / provider / AuthorativeToscaProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.authorative.provider;
23
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Map;
28
29 import javax.ws.rs.core.Response;
30 import javax.ws.rs.core.Response.Status;
31
32 import lombok.NonNull;
33
34 import org.apache.commons.collections4.CollectionUtils;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfModelException;
37 import org.onap.policy.models.base.PfModelRuntimeException;
38 import org.onap.policy.models.dao.PfDao;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
46 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
47 import org.onap.policy.models.tosca.utils.ToscaServiceTemplateUtils;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 /**
52  * This class provides the provision of information on TOSCA concepts in the database to callers.
53  *
54  * @author Liam Fallon (liam.fallon@est.tech)
55  */
56 public class AuthorativeToscaProvider {
57     private static final Logger LOGGER = LoggerFactory.getLogger(AuthorativeToscaProvider.class);
58
59     /**
60      * Get policy types.
61      *
62      * @param dao the DAO to use to access the database
63      * @param name the name of the policy type to get.
64      * @param version the version of the policy type to get.
65      * @return the policy types found
66      * @throws PfModelException on errors getting policy types
67      */
68     public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
69         throws PfModelException {
70
71         LOGGER.debug("->getPolicyTypes: name={}, version={}", name, version);
72
73         JpaToscaServiceTemplate jpaServiceTemplate = new SimpleToscaProvider().getPolicyTypes(dao, name, version);
74
75         ToscaServiceTemplate serviceTemplate = jpaServiceTemplate.toAuthorative();
76
77         LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate);
78         return serviceTemplate;
79     }
80
81     /**
82      * Get policy types.
83      *
84      * @param dao the DAO to use to access the database
85      * @param name the name of the policy type to get, set to null to get all policy types
86      * @param version the version of the policy type to get, set to null to get all versions
87      * @return the policy types found
88      * @throws PfModelException on errors getting policy types
89      */
90     public List<ToscaPolicyType> getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version)
91         throws PfModelException {
92
93         LOGGER.debug("->getPolicyTypeList: name={}, version={}", name, version);
94
95         List<ToscaPolicyType> policyTypeList;
96
97         try {
98             policyTypeList = new ArrayList<>(
99                 new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative().getPolicyTypes().values());
100         } catch (PfModelRuntimeException pfme) {
101             return handlePfModelRuntimeException(pfme);
102         }
103
104         LOGGER.debug("<-getPolicyTypeList: name={}, version={}, policyTypeList={}", name, version, policyTypeList);
105         return policyTypeList;
106     }
107
108     /**
109      * Get filtered policy types.
110      *
111      * @param dao the DAO to use to access the database
112      * @param filter the filter for the policy types to get
113      * @return the policy types found
114      * @throws PfModelException on errors getting policy types
115      */
116     public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao,
117         @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
118
119         LOGGER.debug("->getFilteredPolicyTypes: filter={}", filter);
120         SimpleToscaProvider simpleToscaProvider = new SimpleToscaProvider();
121
122         final JpaToscaServiceTemplate dbServiceTemplate = simpleToscaProvider.getPolicyTypes(dao, null, null);
123
124         List<ToscaPolicyType> filteredPolicyTypes = dbServiceTemplate.getPolicyTypes().toAuthorativeList();
125         filteredPolicyTypes = filter.filter(filteredPolicyTypes);
126
127         if (CollectionUtils.isEmpty(filteredPolicyTypes)) {
128             throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
129                 "policy types for filter " + filter.toString() + " do not exist");
130         }
131
132         JpaToscaServiceTemplate filteredServiceTemplate = new JpaToscaServiceTemplate();
133
134         for (ToscaPolicyType policyType : filteredPolicyTypes) {
135             JpaToscaServiceTemplate cascadedServiceTemplate = simpleToscaProvider
136                 .getCascadedPolicyTypes(dbServiceTemplate, policyType.getName(), policyType.getVersion());
137
138             filteredServiceTemplate =
139                 ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate);
140         }
141
142         ToscaServiceTemplate returnServiceTemplate = filteredServiceTemplate.toAuthorative();
143
144         LOGGER.debug("<-getFilteredPolicyTypes: filter={}, serviceTemplate={}", filter, returnServiceTemplate);
145         return returnServiceTemplate;
146
147     }
148
149     /**
150      * Get filtered policy types.
151      *
152      * @param dao the DAO to use to access the database
153      * @param filter the filter for the policy types to get
154      * @return the policy types found
155      * @throws PfModelException on errors getting policy types
156      */
157     public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull final PfDao dao,
158         @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
159
160         LOGGER.debug("->getFilteredPolicyTypeList: filter={}", filter);
161
162         List<ToscaPolicyType> filteredPolicyTypeList = filter.filter(getPolicyTypeList(dao, null, null));
163
164         LOGGER.debug("<-getFilteredPolicyTypeList: filter={}, filteredPolicyTypeList={}", filter,
165             filteredPolicyTypeList);
166
167         return filteredPolicyTypeList;
168     }
169
170     /**
171      * Create policy types.
172      *
173      * @param dao the DAO to use to access the database
174      * @param serviceTemplate the service template containing the definition of the policy types to be created
175      * @return the TOSCA service template containing the created policy types
176      * @throws PfModelException on errors creating policy types
177      */
178     public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
179         @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
180
181         LOGGER.debug("->createPolicyTypes: serviceTemplate={}", serviceTemplate);
182
183         ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
184             .createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
185
186         LOGGER.debug("<-createPolicyTypes: createdServiceTempalate={}", createdServiceTempalate);
187         return createdServiceTempalate;
188     }
189
190     /**
191      * Update policy types.
192      *
193      * @param dao the DAO to use to access the database
194      * @param serviceTemplate the service template containing the definition of the policy types to be modified
195      * @return the TOSCA service template containing the modified policy types
196      * @throws PfModelException on errors updating policy types
197      */
198     public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
199         @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
200
201         LOGGER.debug("->updatePolicyTypes: serviceTempalate={}", serviceTemplate);
202
203         ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
204             .updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
205
206         LOGGER.debug("<-updatePolicyTypes: updatedServiceTempalate={}", updatedServiceTempalate);
207         return updatedServiceTempalate;
208     }
209
210     /**
211      * Delete policy type.
212      *
213      * @param dao the DAO to use to access the database
214      * @param name the name of the policy type to delete.
215      * @param version the version of the policy type to delete.
216      * @return the TOSCA service template containing the policy type that was deleted
217      * @throws PfModelException on errors deleting policy types
218      */
219     public ToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final String name,
220         @NonNull final String version) throws PfModelException {
221
222         LOGGER.debug("->deletePolicyType: name={}, version={}", name, version);
223
224         ToscaServiceTemplate deletedServiceTempalate =
225             new SimpleToscaProvider().deletePolicyType(dao, new PfConceptKey(name, version)).toAuthorative();
226
227         LOGGER.debug("<-deletePolicyType: name={}, version={}, deletedServiceTempalate={}", name, version,
228             deletedServiceTempalate);
229         return deletedServiceTempalate;
230     }
231
232     /**
233      * Get policies.
234      *
235      * @param dao the DAO to use to access the database
236      * @param name the name of the policy to get.
237      * @param version the version of the policy to get.
238      * @return the policies found
239      * @throws PfModelException on errors getting policies
240      */
241     public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
242         throws PfModelException {
243         LOGGER.debug("->getPolicies: name={}, version={}", name, version);
244
245         ToscaServiceTemplate gotServiceTempalate =
246             new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative();
247
248         LOGGER.debug("<-getPolicies: name={}, version={}, gotServiceTempalate={}", name, version, gotServiceTempalate);
249         return gotServiceTempalate;
250     }
251
252     /**
253      * Get policies.
254      *
255      * @param dao the DAO to use to access the database
256      * @param name the name of the policy to get, null to get all policies
257      * @param version the version of the policy to get, null to get all versions of a policy
258      * @return the policies found
259      * @throws PfModelException on errors getting policies
260      */
261     public List<ToscaPolicy> getPolicyList(@NonNull final PfDao dao, final String name, final String version)
262         throws PfModelException {
263         LOGGER.debug("->getPolicyList: name={}, version={}", name, version);
264
265         List<ToscaPolicy> policyList;
266
267         try {
268             policyList = asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative()
269                 .getToscaTopologyTemplate().getPolicies());
270         } catch (PfModelRuntimeException pfme) {
271             return handlePfModelRuntimeException(pfme);
272         }
273
274         LOGGER.debug("<-getPolicyList: name={}, version={}, policyTypeList={}", name, version, policyList);
275         return policyList;
276     }
277
278     /**
279      * Get filtered policies.
280      *
281      * @param dao the DAO to use to access the database
282      * @param filter the filter for the policies to get
283      * @return the policies found
284      * @throws PfModelException on errors getting policies
285      */
286     public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
287         throws PfModelException {
288
289         LOGGER.debug("->getFilteredPolicies: filter={}", filter);
290         String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
291
292         SimpleToscaProvider simpleToscaProvider = new SimpleToscaProvider();
293         final JpaToscaServiceTemplate dbServiceTemplate =
294             simpleToscaProvider.getPolicies(dao, filter.getName(), version);
295
296         List<ToscaPolicy> filteredPolicies = dbServiceTemplate.getTopologyTemplate().getPolicies().toAuthorativeList();
297         filteredPolicies = filter.filter(filteredPolicies);
298
299         if (CollectionUtils.isEmpty(filteredPolicies)) {
300             throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
301                 "policies for filter " + filter.toString() + " do not exist");
302         }
303
304         JpaToscaServiceTemplate filteredServiceTemplate = new JpaToscaServiceTemplate();
305
306         for (ToscaPolicy policy : filteredPolicies) {
307             JpaToscaServiceTemplate cascadedServiceTemplate =
308                 simpleToscaProvider.getCascadedPolicies(dbServiceTemplate, policy.getName(), policy.getVersion());
309
310             filteredServiceTemplate =
311                 ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate);
312         }
313
314         ToscaServiceTemplate returnServiceTemplate = filteredServiceTemplate.toAuthorative();
315
316         LOGGER.debug("<-getFilteredPolicies: filter={}, serviceTemplate={}", filter, returnServiceTemplate);
317         return returnServiceTemplate;
318     }
319
320     /**
321      * Get filtered policies.
322      *
323      * @param dao the DAO to use to access the database
324      * @param filter the filter for the policies to get
325      * @return the policies found
326      * @throws PfModelException on errors getting policies
327      */
328     public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
329         throws PfModelException {
330
331         LOGGER.debug("->getFilteredPolicyList: filter={}", filter);
332         String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
333
334         List<ToscaPolicy> policyList = filter.filter(getPolicyList(dao, filter.getName(), version));
335
336         LOGGER.debug("<-getFilteredPolicyList: filter={}, policyList={}", filter, policyList);
337         return policyList;
338     }
339
340     /**
341      * Create policies.
342      *
343      * @param dao the DAO to use to access the database
344      * @param serviceTemplate the service template containing the definitions of the new policies to be created.
345      * @return the TOSCA service template containing the policy types that were created
346      * @throws PfModelException on errors creating policies
347      */
348     public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
349         @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
350
351         LOGGER.debug("->createPolicies: serviceTempalate={}", serviceTemplate);
352
353         ToscaServiceTemplate createdServiceTempalate =
354             new SimpleToscaProvider().createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
355
356         LOGGER.debug("<-createPolicies: createdServiceTempalate={}", createdServiceTempalate);
357         return createdServiceTempalate;
358     }
359
360     /**
361      * Update policies.
362      *
363      * @param dao the DAO to use to access the database
364      * @param serviceTemplate the service template containing the definitions of the policies to be updated.
365      * @return the TOSCA service template containing the policies that were updated
366      * @throws PfModelException on errors updating policies
367      */
368     public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
369         @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
370
371         LOGGER.debug("->updatePolicies: serviceTempalate={}", serviceTemplate);
372
373         ToscaServiceTemplate updatedServiceTempalate =
374             new SimpleToscaProvider().updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
375
376         LOGGER.debug("<-updatePolicies: updatedServiceTempalate={}", updatedServiceTempalate);
377         return updatedServiceTempalate;
378     }
379
380     /**
381      * Delete policy.
382      *
383      * @param dao the DAO to use to access the database
384      * @param name the name of the policy to delete.
385      * @param version the version of the policy to delete.
386      * @return the TOSCA service template containing the policy that weas deleted
387      * @throws PfModelException on errors deleting policies
388      */
389     public ToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final String name,
390         @NonNull final String version) throws PfModelException {
391
392         LOGGER.debug("->deletePolicy: name={}, version={}", name, version);
393
394         ToscaServiceTemplate deletedServiceTempalate =
395             new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative();
396
397         LOGGER.debug("<-deletePolicy: name={}, version={}, deletedServiceTempalate={}", name, version,
398             deletedServiceTempalate);
399         return deletedServiceTempalate;
400     }
401
402     /**
403      * Return the contents of a list of maps as a plain list.
404      *
405      * @param listOfMaps the list of maps
406      * @return the plain list
407      */
408     private <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) {
409         List<T> returnList = new ArrayList<>();
410         for (Map<String, T> conceptMap : listOfMaps) {
411             for (T concept : conceptMap.values()) {
412                 returnList.add(concept);
413             }
414         }
415
416         return returnList;
417     }
418
419     /**
420      * Handle a PfModelRuntimeException on a list call.
421      *
422      * @param pfme the model exception
423      * @return an empty list on 404
424      */
425     private <T extends ToscaEntity> List<T> handlePfModelRuntimeException(final PfModelRuntimeException pfme) {
426         if (Status.NOT_FOUND.equals(pfme.getErrorResponse().getResponseCode())) {
427             LOGGER.trace("request did not find any results", pfme);
428             return Collections.emptyList();
429         } else {
430             throw pfme;
431         }
432     }
433 }