Use keys to speed DB queries
[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
44 /**
45  * This class provides the provision of information on TOSCA concepts in the database to callers.
46  *
47  * @author Liam Fallon (liam.fallon@est.tech)
48  */
49 public class AuthorativeToscaProvider {
50     /**
51      * Get policy types.
52      *
53      * @param dao the DAO to use to access the database
54      * @param name the name of the policy type to get.
55      * @param version the version of the policy type to get.
56      * @return the policy types found
57      * @throws PfModelException on errors getting policy types
58      */
59     public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
60             throws PfModelException {
61
62         return new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative();
63     }
64
65     /**
66      * Get policy types.
67      *
68      * @param dao the DAO to use to access the database
69      * @param name the name of the policy type to get, set to null to get all policy types
70      * @param version the version of the policy type to get, set to null to get all versions
71      * @return the policy types found
72      * @throws PfModelException on errors getting policy types
73      */
74     public List<ToscaPolicyType> getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version)
75             throws PfModelException {
76
77         return (asConceptList(
78                 new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative().getPolicyTypes()));
79     }
80
81     /**
82      * Get filtered policy types.
83      *
84      * @param dao the DAO to use to access the database
85      * @param filter the filter for the policy types to get
86      * @return the policy types found
87      * @throws PfModelException on errors getting policy types
88      */
89     public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao,
90             @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
91
92         ToscaServiceTemplate serviceTemplate =
93                 new SimpleToscaProvider().getPolicyTypes(dao, null, null).toAuthorative();
94
95         List<ToscaPolicyType> filteredPolicyTypes = asConceptList(serviceTemplate.getPolicyTypes());
96         filteredPolicyTypes = filter.filter(filteredPolicyTypes);
97
98         serviceTemplate.setPolicyTypes(asConceptMap(filteredPolicyTypes));
99
100         return serviceTemplate;
101     }
102
103     /**
104      * Get filtered policy types.
105      *
106      * @param dao the DAO to use to access the database
107      * @param filter the filter for the policy types to get
108      * @return the policy types found
109      * @throws PfModelException on errors getting policy types
110      */
111     public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull final PfDao dao,
112             @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
113
114         return filter.filter(getPolicyTypeList(dao, null, null));
115     }
116
117     /**
118      * Create policy types.
119      *
120      * @param dao the DAO to use to access the database
121      * @param serviceTemplate the service template containing the definition of the policy types to be created
122      * @return the TOSCA service template containing the created policy types
123      * @throws PfModelException on errors creating policy types
124      */
125     public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
126             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
127
128         return new SimpleToscaProvider().createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate))
129                 .toAuthorative();
130     }
131
132     /**
133      * Update policy types.
134      *
135      * @param dao the DAO to use to access the database
136      * @param serviceTemplate the service template containing the definition of the policy types to be modified
137      * @return the TOSCA service template containing the modified policy types
138      * @throws PfModelException on errors updating policy types
139      */
140     public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
141             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
142
143         return new SimpleToscaProvider().updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate))
144                 .toAuthorative();
145     }
146
147     /**
148      * Delete policy type.
149      *
150      * @param dao the DAO to use to access the database
151      * @param name the name of the policy type to delete.
152      * @param version the version of the policy type to delete.
153      * @return the TOSCA service template containing the policy type that was deleted
154      * @throws PfModelException on errors deleting policy types
155      */
156     public ToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final String name,
157             @NonNull final String version) throws PfModelException {
158
159         return new SimpleToscaProvider().deletePolicyType(dao, new PfConceptKey(name, version)).toAuthorative();
160     }
161
162     /**
163      * Get policies.
164      *
165      * @param dao the DAO to use to access the database
166      * @param name the name of the policy to get.
167      * @param version the version of the policy to get.
168      * @return the policies found
169      * @throws PfModelException on errors getting policies
170      */
171     public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
172             throws PfModelException {
173
174         return new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative();
175     }
176
177     /**
178      * Get policies.
179      *
180      * @param dao the DAO to use to access the database
181      * @param name the name of the policy to get, null to get all policies
182      * @param version the version of the policy to get, null to get all versions of a policy
183      * @return the policies found
184      * @throws PfModelException on errors getting policies
185      */
186     public List<ToscaPolicy> getPolicyList(@NonNull final PfDao dao, final String name, final String version)
187             throws PfModelException {
188
189         return asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative()
190                 .getToscaTopologyTemplate().getPolicies());
191     }
192
193     /**
194      * Get filtered policies.
195      *
196      * @param dao the DAO to use to access the database
197      * @param filter the filter for the policies to get
198      * @return the policies found
199      * @throws PfModelException on errors getting policies
200      */
201     public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
202             throws PfModelException {
203
204         String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
205
206         ToscaServiceTemplate serviceTemplate =
207                         new SimpleToscaProvider().getPolicies(dao, filter.getName(), version).toAuthorative();
208
209         List<ToscaPolicy> filteredPolicies = asConceptList(serviceTemplate.getToscaTopologyTemplate().getPolicies());
210         filteredPolicies = filter.filter(filteredPolicies);
211
212         serviceTemplate.getToscaTopologyTemplate().setPolicies(asConceptMap(filteredPolicies));
213
214         return serviceTemplate;
215     }
216
217     /**
218      * Get filtered policies.
219      *
220      * @param dao the DAO to use to access the database
221      * @param filter the filter for the policies to get
222      * @return the policies found
223      * @throws PfModelException on errors getting policies
224      */
225     public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
226             throws PfModelException {
227
228         String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
229         return filter.filter(getPolicyList(dao, filter.getName(), version));
230     }
231
232     /**
233      * Create policies.
234      *
235      * @param dao the DAO to use to access the database
236      * @param serviceTemplate the service template containing the definitions of the new policies to be created.
237      * @return the TOSCA service template containing the policy types that were created
238      * @throws PfModelException on errors creating policies
239      */
240     public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
241             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
242
243         return new SimpleToscaProvider().createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate))
244                 .toAuthorative();
245     }
246
247     /**
248      * Update policies.
249      *
250      * @param dao the DAO to use to access the database
251      * @param serviceTemplate the service template containing the definitions of the policies to be updated.
252      * @return the TOSCA service template containing the policies that were updated
253      * @throws PfModelException on errors updating policies
254      */
255     public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
256             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
257
258         return new SimpleToscaProvider().updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate))
259                 .toAuthorative();
260     }
261
262     /**
263      * Delete policy.
264      *
265      * @param dao the DAO to use to access the database
266      * @param name the name of the policy to delete.
267      * @param version the version of the policy to delete.
268      * @return the TOSCA service template containing the policy that weas deleted
269      * @throws PfModelException on errors deleting policies
270      */
271     public ToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final String name,
272             @NonNull final String version) throws PfModelException {
273
274         return new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative();
275     }
276
277     /**
278      * Return the contents of a list of maps as a plain list.
279      *
280      * @param listOfMaps the list of maps
281      * @return the plain list
282      */
283     private <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) {
284         List<T> returnList = new ArrayList<>();
285         for (Map<String, T> conceptMap : listOfMaps) {
286             for (T concept : conceptMap.values()) {
287                 returnList.add(concept);
288             }
289         }
290
291         return returnList;
292     }
293
294     /**
295      * Return the contents of a list of concepts as a list of maps of concepts.
296      *
297      * @param comceptList the concept list
298      * @return the concept map
299      */
300     private <T extends ToscaEntity> List<Map<String, T>> asConceptMap(List<T> conceptList) {
301         Map<String, T> conceptMap = new LinkedHashMap<>();
302         for (T concept : conceptList) {
303             conceptMap.put(concept.getName(), concept);
304         }
305
306         return Collections.singletonList(conceptMap);
307     }
308 }