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