Fix bugs on filters
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.authorative.provider;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import lombok.NonNull;
30
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
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     /**
50      * Get policy types.
51      *
52      * @param dao the DAO to use to access the database
53      * @param name the name of the policy type to get.
54      * @param version the version of the policy type to get.
55      * @return the policy types found
56      * @throws PfModelException on errors getting policy types
57      */
58     public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
59             throws PfModelException {
60
61         return new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative();
62     }
63
64     /**
65      * Get policy types.
66      *
67      * @param dao the DAO to use to access the database
68      * @param name the name of the policy type to get, set to null to get all policy types
69      * @param version the version of the policy type to get, set to null to get all versions
70      * @return the policy types found
71      * @throws PfModelException on errors getting policy types
72      */
73     public List<ToscaPolicyType> getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version)
74             throws PfModelException {
75
76         return (asConceptList(
77                 new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative().getPolicyTypes()));
78     }
79
80     /**
81      * Get filtered policy types.
82      *
83      * @param dao the DAO to use to access the database
84      * @param filter the filter for the policy types to get
85      * @return the policy types found
86      * @throws PfModelException on errors getting policy types
87      */
88     public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao,
89             @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
90
91         ToscaServiceTemplate serviceTemplate =
92                 new SimpleToscaProvider().getPolicyTypes(dao, null, null).toAuthorative();
93
94         List<ToscaPolicyType> filteredPolicyTypes = asConceptList(serviceTemplate.getPolicyTypes());
95         filteredPolicyTypes = filter.filter(filteredPolicyTypes);
96
97         serviceTemplate.setPolicyTypes(asConceptMap(filteredPolicyTypes));
98
99         return serviceTemplate;
100     }
101
102     /**
103      * Get filtered policy types.
104      *
105      * @param dao the DAO to use to access the database
106      * @param filter the filter for the policy types to get
107      * @return the policy types found
108      * @throws PfModelException on errors getting policy types
109      */
110     public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull final PfDao dao,
111             @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
112
113         return filter.filter(getPolicyTypeList(dao, null, null));
114     }
115
116     /**
117      * Create policy types.
118      *
119      * @param dao the DAO to use to access the database
120      * @param serviceTemplate the service template containing the definition of the policy types to be created
121      * @return the TOSCA service template containing the created policy types
122      * @throws PfModelException on errors creating policy types
123      */
124     public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
125             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
126
127         return new SimpleToscaProvider().createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate))
128                 .toAuthorative();
129     }
130
131     /**
132      * Update policy types.
133      *
134      * @param dao the DAO to use to access the database
135      * @param serviceTemplate the service template containing the definition of the policy types to be modified
136      * @return the TOSCA service template containing the modified policy types
137      * @throws PfModelException on errors updating policy types
138      */
139     public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
140             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
141
142         return new SimpleToscaProvider().updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate))
143                 .toAuthorative();
144     }
145
146     /**
147      * Delete policy type.
148      *
149      * @param dao the DAO to use to access the database
150      * @param name the name of the policy type to delete.
151      * @param version the version of the policy type to delete.
152      * @return the TOSCA service template containing the policy type that was deleted
153      * @throws PfModelException on errors deleting policy types
154      */
155     public ToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final String name,
156             @NonNull final String version) throws PfModelException {
157
158         return new SimpleToscaProvider().deletePolicyType(dao, new PfConceptKey(name, version)).toAuthorative();
159     }
160
161     /**
162      * Get policies.
163      *
164      * @param dao the DAO to use to access the database
165      * @param name the name of the policy to get.
166      * @param version the version of the policy to get.
167      * @return the policies found
168      * @throws PfModelException on errors getting policies
169      */
170     public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
171             throws PfModelException {
172
173         return new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative();
174     }
175
176     /**
177      * Get policies.
178      *
179      * @param dao the DAO to use to access the database
180      * @param name the name of the policy to get, null to get all policies
181      * @param version the version of the policy to get, null to get all versions of a policy
182      * @return the policies found
183      * @throws PfModelException on errors getting policies
184      */
185     public List<ToscaPolicy> getPolicyList(@NonNull final PfDao dao, final String name, final String version)
186             throws PfModelException {
187
188         return asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative()
189                 .getToscaTopologyTemplate().getPolicies());
190     }
191
192     /**
193      * Get filtered policies.
194      *
195      * @param dao the DAO to use to access the database
196      * @param filter the filter for the policies to get
197      * @return the policies found
198      * @throws PfModelException on errors getting policies
199      */
200     public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
201             throws PfModelException {
202
203         ToscaServiceTemplate serviceTemplate = new SimpleToscaProvider().getPolicies(dao, null, null).toAuthorative();
204
205         List<ToscaPolicy> filteredPolicies = asConceptList(serviceTemplate.getToscaTopologyTemplate().getPolicies());
206         filteredPolicies = filter.filter(filteredPolicies);
207
208         serviceTemplate.getToscaTopologyTemplate().setPolicies(asConceptMap(filteredPolicies));
209
210         return serviceTemplate;
211     }
212
213     /**
214      * Get filtered policies.
215      *
216      * @param dao the DAO to use to access the database
217      * @param filter the filter for the policies to get
218      * @return the policies found
219      * @throws PfModelException on errors getting policies
220      */
221     public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
222             throws PfModelException {
223
224         return filter.filter(getPolicyList(dao, null, null));
225     }
226
227     /**
228      * Create policies.
229      *
230      * @param dao the DAO to use to access the database
231      * @param serviceTemplate the service template containing the definitions of the new policies to be created.
232      * @return the TOSCA service template containing the policy types that were created
233      * @throws PfModelException on errors creating policies
234      */
235     public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
236             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
237
238         return new SimpleToscaProvider().createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate))
239                 .toAuthorative();
240     }
241
242     /**
243      * Update policies.
244      *
245      * @param dao the DAO to use to access the database
246      * @param serviceTemplate the service template containing the definitions of the policies to be updated.
247      * @return the TOSCA service template containing the policies that were updated
248      * @throws PfModelException on errors updating policies
249      */
250     public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
251             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
252
253         return new SimpleToscaProvider().updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate))
254                 .toAuthorative();
255     }
256
257     /**
258      * Delete policy.
259      *
260      * @param dao the DAO to use to access the database
261      * @param name the name of the policy to delete.
262      * @param version the version of the policy to delete.
263      * @return the TOSCA service template containing the policy that weas deleted
264      * @throws PfModelException on errors deleting policies
265      */
266     public ToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final String name,
267             @NonNull final String version) throws PfModelException {
268
269         return new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative();
270     }
271
272     /**
273      * Return the contents of a list of maps as a plain list.
274      *
275      * @param listOfMaps the list of maps
276      * @return the plain list
277      */
278     private <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) {
279         if (listOfMaps == null) {
280             return Collections.emptyList();
281         }
282
283         List<T> returnList = new ArrayList<>();
284         for (Map<String, T> conceptMap : listOfMaps) {
285             for (T concept : conceptMap.values()) {
286                 returnList.add(concept);
287             }
288         }
289
290         return returnList;
291     }
292
293     /**
294      * Return the contents of a list of concepts as a list of maps of concepts.
295      *
296      * @param comceptList the concept list
297      * @return the concept map
298      */
299     private <T extends ToscaEntity> List<Map<String, T>> asConceptMap(List<T> conceptList) {
300         Map<String, T> conceptMap = new LinkedHashMap<>();
301         for (T concept : conceptList) {
302             conceptMap.put(concept.getName(), concept);
303         }
304
305         return Collections.singletonList(conceptMap);
306     }
307 }