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