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