Fix simultaneous get access to policy provider
[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     // TODO: In next release this locking mechanism should be removed and replaced with proper session handling
60     private static final Object providerLockObject = "providerLockObject";
61
62     /**
63      * Get policy types.
64      *
65      * @param dao the DAO to use to access the database
66      * @param name the name of the policy type to get.
67      * @param version the version of the policy type to get.
68      * @return the policy types found
69      * @throws PfModelException on errors getting policy types
70      */
71     public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
72         throws PfModelException {
73
74         synchronized (providerLockObject) {
75             LOGGER.debug("->getPolicyTypes: name={}, version={}", name, version);
76
77             JpaToscaServiceTemplate jpaServiceTemplate = new SimpleToscaProvider().getPolicyTypes(dao, name, version);
78
79             ToscaServiceTemplate serviceTemplate = jpaServiceTemplate.toAuthorative();
80
81             LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate);
82             return serviceTemplate;
83         }
84     }
85
86     /**
87      * Get policy types.
88      *
89      * @param dao the DAO to use to access the database
90      * @param name the name of the policy type to get, set to null to get all policy types
91      * @param version the version of the policy type to get, set to null to get all versions
92      * @return the policy types found
93      * @throws PfModelException on errors getting policy types
94      */
95     public List<ToscaPolicyType> getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version)
96         throws PfModelException {
97
98         synchronized (providerLockObject) {
99             LOGGER.debug("->getPolicyTypeList: name={}, version={}", name, version);
100
101             List<ToscaPolicyType> policyTypeList;
102
103             try {
104                 policyTypeList = new ArrayList<>(new SimpleToscaProvider().getPolicyTypes(dao, name, version)
105                     .toAuthorative().getPolicyTypes().values());
106             } catch (PfModelRuntimeException pfme) {
107                 return handlePfModelRuntimeException(pfme);
108             }
109
110             LOGGER.debug("<-getPolicyTypeList: name={}, version={}, policyTypeList={}", name, version, policyTypeList);
111             return policyTypeList;
112         }
113     }
114
115     /**
116      * Get filtered policy types.
117      *
118      * @param dao the DAO to use to access the database
119      * @param filter the filter for the policy types to get
120      * @return the policy types found
121      * @throws PfModelException on errors getting policy types
122      */
123     public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao,
124         @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
125
126         synchronized (providerLockObject) {
127             LOGGER.debug("->getFilteredPolicyTypes: filter={}", filter);
128             SimpleToscaProvider simpleToscaProvider = new SimpleToscaProvider();
129
130             final JpaToscaServiceTemplate dbServiceTemplate = simpleToscaProvider.getPolicyTypes(dao, null, null);
131
132             List<ToscaPolicyType> filteredPolicyTypes = dbServiceTemplate.getPolicyTypes().toAuthorativeList();
133             filteredPolicyTypes = filter.filter(filteredPolicyTypes);
134
135             if (CollectionUtils.isEmpty(filteredPolicyTypes)) {
136                 throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
137                     "policy types for filter " + filter.toString() + " do not exist");
138             }
139
140             JpaToscaServiceTemplate filteredServiceTemplate = new JpaToscaServiceTemplate();
141
142             for (ToscaPolicyType policyType : filteredPolicyTypes) {
143                 JpaToscaServiceTemplate cascadedServiceTemplate = simpleToscaProvider
144                     .getCascadedPolicyTypes(dbServiceTemplate, policyType.getName(), policyType.getVersion());
145
146                 filteredServiceTemplate =
147                     ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate);
148             }
149
150             ToscaServiceTemplate returnServiceTemplate = filteredServiceTemplate.toAuthorative();
151
152             LOGGER.debug("<-getFilteredPolicyTypes: filter={}, serviceTemplate={}", filter, returnServiceTemplate);
153             return returnServiceTemplate;
154         }
155     }
156
157     /**
158      * Get filtered policy types.
159      *
160      * @param dao the DAO to use to access the database
161      * @param filter the filter for the policy types to get
162      * @return the policy types found
163      * @throws PfModelException on errors getting policy types
164      */
165     public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull final PfDao dao,
166         @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
167
168         LOGGER.debug("->getFilteredPolicyTypeList: filter={}", filter);
169
170         List<ToscaPolicyType> filteredPolicyTypeList = filter.filter(getPolicyTypeList(dao, null, null));
171
172         LOGGER.debug("<-getFilteredPolicyTypeList: filter={}, filteredPolicyTypeList={}", filter,
173             filteredPolicyTypeList);
174
175         return filteredPolicyTypeList;
176     }
177
178     /**
179      * Create policy types.
180      *
181      * @param dao the DAO to use to access the database
182      * @param serviceTemplate the service template containing the definition of the policy types to be created
183      * @return the TOSCA service template containing the created policy types
184      * @throws PfModelException on errors creating policy types
185      */
186     public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
187         @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
188
189         synchronized (providerLockObject) {
190             LOGGER.debug("->createPolicyTypes: serviceTemplate={}", serviceTemplate);
191
192             ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
193                 .createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
194
195             LOGGER.debug("<-createPolicyTypes: createdServiceTempalate={}", createdServiceTempalate);
196             return createdServiceTempalate;
197         }
198     }
199
200     /**
201      * Update policy types.
202      *
203      * @param dao the DAO to use to access the database
204      * @param serviceTemplate the service template containing the definition of the policy types to be modified
205      * @return the TOSCA service template containing the modified policy types
206      * @throws PfModelException on errors updating policy types
207      */
208     public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
209         @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
210
211         synchronized (providerLockObject) {
212             LOGGER.debug("->updatePolicyTypes: serviceTempalate={}", serviceTemplate);
213
214             ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
215                 .updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
216
217             LOGGER.debug("<-updatePolicyTypes: updatedServiceTempalate={}", updatedServiceTempalate);
218             return updatedServiceTempalate;
219         }
220     }
221
222     /**
223      * Delete policy type.
224      *
225      * @param dao the DAO to use to access the database
226      * @param name the name of the policy type to delete.
227      * @param version the version of the policy type to delete.
228      * @return the TOSCA service template containing the policy type that was deleted
229      * @throws PfModelException on errors deleting policy types
230      */
231     public ToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final String name,
232         @NonNull final String version) throws PfModelException {
233
234         synchronized (providerLockObject) {
235             LOGGER.debug("->deletePolicyType: name={}, version={}", name, version);
236
237             ToscaServiceTemplate deletedServiceTempalate =
238                 new SimpleToscaProvider().deletePolicyType(dao, new PfConceptKey(name, version)).toAuthorative();
239
240             LOGGER.debug("<-deletePolicyType: name={}, version={}, deletedServiceTempalate={}", name, version,
241                 deletedServiceTempalate);
242             return deletedServiceTempalate;
243         }
244     }
245
246     /**
247      * Get policies.
248      *
249      * @param dao the DAO to use to access the database
250      * @param name the name of the policy to get.
251      * @param version the version of the policy to get.
252      * @return the policies found
253      * @throws PfModelException on errors getting policies
254      */
255     public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
256         throws PfModelException {
257
258         synchronized (providerLockObject) {
259             LOGGER.debug("->getPolicies: name={}, version={}", name, version);
260
261             ToscaServiceTemplate gotServiceTempalate =
262                 new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative();
263
264             LOGGER.debug("<-getPolicies: name={}, version={}, gotServiceTempalate={}", name, version,
265                 gotServiceTempalate);
266             return gotServiceTempalate;
267         }
268     }
269
270     /**
271      * Get policies.
272      *
273      * @param dao the DAO to use to access the database
274      * @param name the name of the policy to get, null to get all policies
275      * @param version the version of the policy to get, null to get all versions of a policy
276      * @return the policies found
277      * @throws PfModelException on errors getting policies
278      */
279     public List<ToscaPolicy> getPolicyList(@NonNull final PfDao dao, final String name, final String version)
280         throws PfModelException {
281
282         synchronized (providerLockObject) {
283             LOGGER.debug("->getPolicyList: name={}, version={}", name, version);
284
285             List<ToscaPolicy> policyList;
286
287             try {
288                 policyList = asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative()
289                     .getToscaTopologyTemplate().getPolicies());
290             } catch (PfModelRuntimeException pfme) {
291                 return handlePfModelRuntimeException(pfme);
292             }
293
294             LOGGER.debug("<-getPolicyList: name={}, version={}, policyTypeList={}", name, version, policyList);
295             return policyList;
296         }
297     }
298
299     /**
300      * Get filtered policies.
301      *
302      * @param dao the DAO to use to access the database
303      * @param filter the filter for the policies to get
304      * @return the policies found
305      * @throws PfModelException on errors getting policies
306      */
307     public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
308         throws PfModelException {
309
310         synchronized (providerLockObject) {
311             LOGGER.debug("->getFilteredPolicies: filter={}", filter);
312             String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
313
314             SimpleToscaProvider simpleToscaProvider = new SimpleToscaProvider();
315             final JpaToscaServiceTemplate dbServiceTemplate =
316                 simpleToscaProvider.getPolicies(dao, filter.getName(), version);
317
318             List<ToscaPolicy> filteredPolicies =
319                 dbServiceTemplate.getTopologyTemplate().getPolicies().toAuthorativeList();
320             filteredPolicies = filter.filter(filteredPolicies);
321
322             if (CollectionUtils.isEmpty(filteredPolicies)) {
323                 throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
324                     "policies for filter " + filter.toString() + " do not exist");
325             }
326
327             JpaToscaServiceTemplate filteredServiceTemplate = new JpaToscaServiceTemplate();
328
329             for (ToscaPolicy policy : filteredPolicies) {
330                 JpaToscaServiceTemplate cascadedServiceTemplate =
331                     simpleToscaProvider.getCascadedPolicies(dbServiceTemplate, policy.getName(), policy.getVersion());
332
333                 filteredServiceTemplate =
334                     ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate);
335             }
336
337             ToscaServiceTemplate returnServiceTemplate = filteredServiceTemplate.toAuthorative();
338
339             LOGGER.debug("<-getFilteredPolicies: filter={}, serviceTemplate={}", filter, returnServiceTemplate);
340             return returnServiceTemplate;
341         }
342     }
343
344     /**
345      * Get filtered policies.
346      *
347      * @param dao the DAO to use to access the database
348      * @param filter the filter for the policies to get
349      * @return the policies found
350      * @throws PfModelException on errors getting policies
351      */
352     public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
353         throws PfModelException {
354
355         LOGGER.debug("->getFilteredPolicyList: filter={}", filter);
356         String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
357
358         List<ToscaPolicy> policyList = filter.filter(getPolicyList(dao, filter.getName(), version));
359
360         LOGGER.debug("<-getFilteredPolicyList: filter={}, policyList={}", filter, policyList);
361         return policyList;
362     }
363
364     /**
365      * Create policies.
366      *
367      * @param dao the DAO to use to access the database
368      * @param serviceTemplate the service template containing the definitions of the new policies to be created.
369      * @return the TOSCA service template containing the policy types that were created
370      * @throws PfModelException on errors creating policies
371      */
372     public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
373         @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
374
375         synchronized (providerLockObject) {
376             LOGGER.debug("->createPolicies: serviceTempalate={}", serviceTemplate);
377
378             ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
379                 .createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
380
381             LOGGER.debug("<-createPolicies: createdServiceTempalate={}", createdServiceTempalate);
382             return createdServiceTempalate;
383         }
384     }
385
386     /**
387      * Update policies.
388      *
389      * @param dao the DAO to use to access the database
390      * @param serviceTemplate the service template containing the definitions of the policies to be updated.
391      * @return the TOSCA service template containing the policies that were updated
392      * @throws PfModelException on errors updating policies
393      */
394     public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
395         @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
396
397         synchronized (providerLockObject) {
398             LOGGER.debug("->updatePolicies: serviceTempalate={}", serviceTemplate);
399
400             ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
401                 .updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
402
403             LOGGER.debug("<-updatePolicies: updatedServiceTempalate={}", updatedServiceTempalate);
404             return updatedServiceTempalate;
405         }
406     }
407
408     /**
409      * Delete policy.
410      *
411      * @param dao the DAO to use to access the database
412      * @param name the name of the policy to delete.
413      * @param version the version of the policy to delete.
414      * @return the TOSCA service template containing the policy that weas deleted
415      * @throws PfModelException on errors deleting policies
416      */
417     public ToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final String name,
418         @NonNull final String version) throws PfModelException {
419
420         synchronized (providerLockObject) {
421             LOGGER.debug("->deletePolicy: name={}, version={}", name, version);
422
423             ToscaServiceTemplate deletedServiceTempalate =
424                 new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative();
425
426             LOGGER.debug("<-deletePolicy: name={}, version={}, deletedServiceTempalate={}", name, version,
427                 deletedServiceTempalate);
428             return deletedServiceTempalate;
429         }
430     }
431
432     /**
433      * Return the contents of a list of maps as a plain list.
434      *
435      * @param listOfMaps the list of maps
436      * @return the plain list
437      */
438     private <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) {
439         List<T> returnList = new ArrayList<>();
440         for (Map<String, T> conceptMap : listOfMaps) {
441             for (T concept : conceptMap.values()) {
442                 returnList.add(concept);
443             }
444         }
445
446         return returnList;
447     }
448
449     /**
450      * Handle a PfModelRuntimeException on a list call.
451      *
452      * @param pfme the model exception
453      * @return an empty list on 404
454      */
455     private <T extends ToscaEntity> List<T> handlePfModelRuntimeException(final PfModelRuntimeException pfme) {
456         if (Status.NOT_FOUND.equals(pfme.getErrorResponse().getResponseCode())) {
457             LOGGER.trace("request did not find any results", pfme);
458             return Collections.emptyList();
459         } else {
460             throw pfme;
461         }
462     }
463 }