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