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