Merge "Change getCanonicalName to getName in models"
[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 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.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import lombok.NonNull;
29 import org.onap.policy.models.base.PfConceptKey;
30 import org.onap.policy.models.base.PfModelException;
31 import org.onap.policy.models.dao.PfDao;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
39 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * This class provides the provision of information on TOSCA concepts in the database to callers.
45  *
46  * @author Liam Fallon (liam.fallon@est.tech)
47  */
48 public class AuthorativeToscaProvider {
49     private static final Logger LOGGER = LoggerFactory.getLogger(AuthorativeToscaProvider.class);
50
51     /**
52      * Get policy types.
53      *
54      * @param dao the DAO to use to access the database
55      * @param name the name of the policy type to get.
56      * @param version the version of the policy type to get.
57      * @return the policy types found
58      * @throws PfModelException on errors getting policy types
59      */
60     public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
61             throws PfModelException {
62
63         LOGGER.debug("->getPolicyTypes: name={}, version={}", name, version);
64
65         ToscaServiceTemplate serviceTemplate =
66                 new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative();
67
68         LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate);
69         return serviceTemplate;
70     }
71
72     /**
73      * Get policy types.
74      *
75      * @param dao the DAO to use to access the database
76      * @param name the name of the policy type to get, set to null to get all policy types
77      * @param version the version of the policy type to get, set to null to get all versions
78      * @return the policy types found
79      * @throws PfModelException on errors getting policy types
80      */
81     public List<ToscaPolicyType> getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version)
82             throws PfModelException {
83
84         LOGGER.debug("->getPolicyTypeList: name={}, version={}", name, version);
85
86         List<ToscaPolicyType> policyTypeList = asConceptList(
87                 new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative().getPolicyTypes());
88
89         LOGGER.debug("<-getPolicyTypeList: name={}, version={}, policyTypeList={}", name, version, policyTypeList);
90         return policyTypeList;
91     }
92
93     /**
94      * Get filtered policy types.
95      *
96      * @param dao the DAO to use to access the database
97      * @param filter the filter for the policy types to get
98      * @return the policy types found
99      * @throws PfModelException on errors getting policy types
100      */
101     public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao,
102             @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
103
104         LOGGER.debug("->getFilteredPolicyTypes: filter={}", filter);
105
106         ToscaServiceTemplate serviceTemplate =
107                 new SimpleToscaProvider().getPolicyTypes(dao, null, null).toAuthorative();
108
109         List<ToscaPolicyType> filteredPolicyTypes = asConceptList(serviceTemplate.getPolicyTypes());
110         filteredPolicyTypes = filter.filter(filteredPolicyTypes);
111
112         serviceTemplate.setPolicyTypes(asConceptMap(filteredPolicyTypes));
113
114         LOGGER.debug("<-getFilteredPolicyTypes: filter={}, serviceTemplate={}", filter, serviceTemplate);
115         return serviceTemplate;
116     }
117
118     /**
119      * Get filtered policy types.
120      *
121      * @param dao the DAO to use to access the database
122      * @param filter the filter for the policy types to get
123      * @return the policy types found
124      * @throws PfModelException on errors getting policy types
125      */
126     public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull final PfDao dao,
127             @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
128
129         LOGGER.debug("->getFilteredPolicyTypeList: filter={}", filter);
130
131         List<ToscaPolicyType> filteredPolicyTypeList = filter.filter(getPolicyTypeList(dao, null, null));
132
133         LOGGER.debug("<-getFilteredPolicyTypeList: filter={}, filteredPolicyTypeList={}", filter,
134                 filteredPolicyTypeList);
135         return filteredPolicyTypeList;
136     }
137
138     /**
139      * Create policy types.
140      *
141      * @param dao the DAO to use to access the database
142      * @param serviceTemplate the service template containing the definition of the policy types to be created
143      * @return the TOSCA service template containing the created policy types
144      * @throws PfModelException on errors creating policy types
145      */
146     public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
147             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
148
149         LOGGER.debug("->createPolicyTypes: serviceTemplate={}", serviceTemplate);
150
151         ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
152                 .createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
153
154         LOGGER.debug("<-createPolicyTypes: createdServiceTempalate={}", createdServiceTempalate);
155         return createdServiceTempalate;
156     }
157
158     /**
159      * Update policy types.
160      *
161      * @param dao the DAO to use to access the database
162      * @param serviceTemplate the service template containing the definition of the policy types to be modified
163      * @return the TOSCA service template containing the modified policy types
164      * @throws PfModelException on errors updating policy types
165      */
166     public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
167             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
168
169         LOGGER.debug("->updatePolicyTypes: serviceTempalate={}", serviceTemplate);
170
171         ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
172                 .updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
173
174         LOGGER.debug("<-updatePolicyTypes: updatedServiceTempalate={}", updatedServiceTempalate);
175         return updatedServiceTempalate;
176     }
177
178     /**
179      * Delete policy type.
180      *
181      * @param dao the DAO to use to access the database
182      * @param name the name of the policy type to delete.
183      * @param version the version of the policy type to delete.
184      * @return the TOSCA service template containing the policy type that was deleted
185      * @throws PfModelException on errors deleting policy types
186      */
187     public ToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final String name,
188             @NonNull final String version) throws PfModelException {
189
190         LOGGER.debug("->deletePolicyType: name={}, version={}", name, version);
191
192         ToscaServiceTemplate deletedServiceTempalate =
193                 new SimpleToscaProvider().deletePolicyType(dao, new PfConceptKey(name, version)).toAuthorative();
194
195         LOGGER.debug("<-deletePolicyType: name={}, version={}, deletedServiceTempalate={}", name, version,
196                 deletedServiceTempalate);
197         return deletedServiceTempalate;
198     }
199
200     /**
201      * Get policies.
202      *
203      * @param dao the DAO to use to access the database
204      * @param name the name of the policy to get.
205      * @param version the version of the policy to get.
206      * @return the policies found
207      * @throws PfModelException on errors getting policies
208      */
209     public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
210             throws PfModelException {
211         LOGGER.debug("->getPolicies: name={}, version={}", name, version);
212
213         ToscaServiceTemplate gotServiceTempalate =
214                 new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative();
215
216         LOGGER.debug("<-getPolicies: name={}, version={}, gotServiceTempalate={}", name, version, gotServiceTempalate);
217         return gotServiceTempalate;
218     }
219
220     /**
221      * Get policies.
222      *
223      * @param dao the DAO to use to access the database
224      * @param name the name of the policy to get, null to get all policies
225      * @param version the version of the policy to get, null to get all versions of a policy
226      * @return the policies found
227      * @throws PfModelException on errors getting policies
228      */
229     public List<ToscaPolicy> getPolicyList(@NonNull final PfDao dao, final String name, final String version)
230             throws PfModelException {
231         LOGGER.debug("->getPolicyList: name={}, version={}", name, version);
232
233         List<ToscaPolicy> policyList = asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version)
234                 .toAuthorative().getToscaTopologyTemplate().getPolicies());
235
236         LOGGER.debug("<-getPolicyList: name={}, version={}, policyTypeList={}", name, version, policyList);
237         return policyList;
238     }
239
240     /**
241      * Get filtered policies.
242      *
243      * @param dao the DAO to use to access the database
244      * @param filter the filter for the policies to get
245      * @return the policies found
246      * @throws PfModelException on errors getting policies
247      */
248     public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
249             throws PfModelException {
250
251         LOGGER.debug("->getFilteredPolicies: filter={}", filter);
252         String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
253
254         ToscaServiceTemplate serviceTemplate =
255                 new SimpleToscaProvider().getPolicies(dao, filter.getName(), version).toAuthorative();
256
257         List<ToscaPolicy> filteredPolicies = asConceptList(serviceTemplate.getToscaTopologyTemplate().getPolicies());
258         filteredPolicies = filter.filter(filteredPolicies);
259
260         serviceTemplate.getToscaTopologyTemplate().setPolicies(asConceptMap(filteredPolicies));
261
262         LOGGER.debug("<-getFilteredPolicies: filter={}, serviceTemplate={}", filter, serviceTemplate);
263         return serviceTemplate;
264     }
265
266     /**
267      * Get filtered policies.
268      *
269      * @param dao the DAO to use to access the database
270      * @param filter the filter for the policies to get
271      * @return the policies found
272      * @throws PfModelException on errors getting policies
273      */
274     public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
275             throws PfModelException {
276
277         LOGGER.debug("->getFilteredPolicyList: filter={}", filter);
278         String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
279
280         List<ToscaPolicy> policyList = filter.filter(getPolicyList(dao, filter.getName(), version));
281
282         LOGGER.debug("<-getFilteredPolicyList: filter={}, policyList={}", filter, policyList);
283         return policyList;
284     }
285
286     /**
287      * Create policies.
288      *
289      * @param dao the DAO to use to access the database
290      * @param serviceTemplate the service template containing the definitions of the new policies to be created.
291      * @return the TOSCA service template containing the policy types that were created
292      * @throws PfModelException on errors creating policies
293      */
294     public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
295             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
296
297         LOGGER.debug("->createPolicies: serviceTempalate={}", serviceTemplate);
298
299         ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
300                 .createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
301
302         LOGGER.debug("<-createPolicies: createdServiceTempalate={}", createdServiceTempalate);
303         return createdServiceTempalate;
304     }
305
306     /**
307      * Update policies.
308      *
309      * @param dao the DAO to use to access the database
310      * @param serviceTemplate the service template containing the definitions of the policies to be updated.
311      * @return the TOSCA service template containing the policies that were updated
312      * @throws PfModelException on errors updating policies
313      */
314     public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
315             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
316
317         LOGGER.debug("->updatePolicies: serviceTempalate={}", serviceTemplate);
318
319         ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
320                 .updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
321
322         LOGGER.debug("<-updatePolicies: updatedServiceTempalate={}", updatedServiceTempalate);
323         return updatedServiceTempalate;
324     }
325
326     /**
327      * Delete policy.
328      *
329      * @param dao the DAO to use to access the database
330      * @param name the name of the policy to delete.
331      * @param version the version of the policy to delete.
332      * @return the TOSCA service template containing the policy that weas deleted
333      * @throws PfModelException on errors deleting policies
334      */
335     public ToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final String name,
336             @NonNull final String version) throws PfModelException {
337
338         LOGGER.debug("->deletePolicy: name={}, version={}", name, version);
339
340         ToscaServiceTemplate deletedServiceTempalate =
341                 new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative();
342
343         LOGGER.debug("<-deletePolicy: name={}, version={}, deletedServiceTempalate={}", name, version,
344                 deletedServiceTempalate);
345         return deletedServiceTempalate;
346     }
347
348     /**
349      * Return the contents of a list of maps as a plain list.
350      *
351      * @param listOfMaps the list of maps
352      * @return the plain list
353      */
354     private <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) {
355         List<T> returnList = new ArrayList<>();
356         for (Map<String, T> conceptMap : listOfMaps) {
357             for (T concept : conceptMap.values()) {
358                 returnList.add(concept);
359             }
360         }
361
362         return returnList;
363     }
364
365     /**
366      * Return the contents of a list of concepts as a list of maps of concepts.
367      *
368      * @param conceptList the concept list
369      * @return the list of concept map
370      */
371     private <T extends ToscaEntity> List<Map<String, T>> asConceptMap(List<T> conceptList) {
372         List<Map<String, T>> toscaEntityMapList = new ArrayList<>();
373         for (T concept : conceptList) {
374             Map<String, T> conceptMap = new LinkedHashMap<>();
375             conceptMap.put(concept.getName(), concept);
376             toscaEntityMapList.add(conceptMap);
377         }
378
379         return toscaEntityMapList;
380     }
381 }