Merge "Add filter obejcts for concepts"
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / provider / SimpleToscaProvider.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.simple.provider;
22
23 import java.util.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.ws.rs.core.Response;
28
29 import lombok.NonNull;
30
31 import org.onap.policy.models.base.PfConcept;
32 import org.onap.policy.models.base.PfConceptKey;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.base.PfModelRuntimeException;
35 import org.onap.policy.models.dao.PfDao;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
39 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
42 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
44 import org.onap.policy.models.tosca.utils.ToscaUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * This class provides the provision of information on TOSCA concepts in the database to callers.
50  *
51  * @author Liam Fallon (liam.fallon@est.tech)
52  */
53 public class SimpleToscaProvider {
54     private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class);
55
56     /**
57      * Get policy types.
58      *
59      * @param dao the DAO to use to access the database
60      * @param name the name of the policy type to get, set to null to get all policy types
61      * @param version the version of the policy type to get, set to null to get all versions
62      * @return the policy types found
63      * @throws PfModelException on errors getting policy types
64      */
65     public JpaToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
66             throws PfModelException {
67
68         // Create the structure of the TOSCA service template to contain the policy type
69         JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
70         serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
71
72         // Add the policy type to the TOSCA service template
73         List<JpaToscaPolicyType> jpaPolicyTypeList = dao.getFiltered(JpaToscaPolicyType.class, name, version);
74         if (jpaPolicyTypeList != null) {
75             serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList));
76             return serviceTemplate;
77         } else {
78             String errorMessage = "policy type not found: " + name + ":" + version;
79             LOGGER.warn(errorMessage);
80             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
81         }
82     }
83
84     /**
85      * Get filtered policy types.
86      *
87      * @param dao the DAO to use to access the database
88      * @param filter the filter for the policy types to get
89      * @return the policy types found
90      * @throws PfModelException on errors getting policy types
91      */
92     public JpaToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao,
93             @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
94
95         // Create the structure of the TOSCA service template to contain the policy type
96         JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
97         serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
98
99         List<JpaToscaPolicyType> jpaPolicyTypeList = dao.getAll(JpaToscaPolicyType.class);
100         // TODO: The actual filtering
101
102         serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList));
103         return serviceTemplate;
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 JpaToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
115             @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
116
117         ToscaUtils.assertPolicyTypesExist(serviceTemplate);
118
119         for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
120             dao.create(policyType);
121         }
122
123         // Return the created policy types
124         JpaToscaPolicyTypes returnPolicyTypes = new JpaToscaPolicyTypes();
125
126         for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
127             returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(JpaToscaPolicyType.class, policyTypeKey));
128         }
129
130         JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
131         returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
132
133         return returnServiceTemplate;
134     }
135
136     /**
137      * Create policy types.
138      *
139      * @param dao the DAO to use to access the database
140      * @param serviceTemplate the service template containing the definition of the policy types to be modified
141      * @return the TOSCA service template containing the modified policy types
142      * @throws PfModelException on errors updating policy types
143      */
144     public JpaToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
145             @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
146
147         ToscaUtils.assertPolicyTypesExist(serviceTemplate);
148
149         for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
150             dao.update(policyType);
151         }
152
153         // Return the created policy types
154         JpaToscaPolicyTypes returnPolicyTypes = new JpaToscaPolicyTypes();
155
156         for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
157             returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(JpaToscaPolicyType.class, policyTypeKey));
158         }
159
160         JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
161         returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
162
163         return returnServiceTemplate;
164     }
165
166     /**
167      * Delete policy types.
168      *
169      * @param dao the DAO to use to access the database
170      * @param policyTypeKey the policy type key for the policy types to be deleted, if the version of the key is null,
171      *        all versions of the policy type are deleted.
172      * @return the TOSCA service template containing the policy types that were deleted
173      * @throws PfModelException on errors deleting policy types
174      */
175     public JpaToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
176             throws PfModelException {
177
178         JpaToscaServiceTemplate serviceTemplate =
179                 getPolicyTypes(dao, policyTypeKey.getName(), policyTypeKey.getVersion());
180
181         dao.delete(JpaToscaPolicyType.class, policyTypeKey);
182
183         return serviceTemplate;
184     }
185
186     /**
187      * Get policies.
188      *
189      * @param dao the DAO to use to access the database
190      * @param name the name of the policy to get, set to null to get all policy types
191      * @param version the version of the policy to get, set to null to get all versions
192      * @return the policies found
193      * @throws PfModelException on errors getting policies
194      */
195     public JpaToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
196             throws PfModelException {
197
198         // Create the structure of the TOSCA service template to contain the policy type
199         JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
200         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
201         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
202
203         // Add the policy type to the TOSCA service template
204         List<JpaToscaPolicy> jpaPolicyList = dao.getFiltered(JpaToscaPolicy.class, name, version);
205         if (jpaPolicyList != null) {
206             serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList));
207             return serviceTemplate;
208         } else {
209             String errorMessage = "policy not found: " + name + ":" + version;
210             LOGGER.warn(errorMessage);
211             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
212         }
213     }
214
215     /**
216      * Get filtered policies.
217      *
218      * @param dao the DAO to use to access the database
219      * @param filter the filter for the policies to get
220      * @return the policies found
221      * @throws PfModelException on errors getting policies
222      */
223     public JpaToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao,
224             @NonNull final ToscaPolicyFilter filter) throws PfModelException {
225
226         // Create the structure of the TOSCA service template to contain the policy type
227         JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
228         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
229         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
230
231         List<JpaToscaPolicy> jpaPolicyList = dao.getAll(JpaToscaPolicy.class);
232         // TODO: Do the actual filtering
233
234         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList));
235         return serviceTemplate;
236     }
237
238     /**
239      * Create policies.
240      *
241      * @param dao the DAO to use to access the database
242      * @param serviceTemplate the service template containing the definitions of the new policies to be created.
243      * @return the TOSCA service template containing the policy types that were created
244      * @throws PfModelException on errors creating policies
245      */
246     public JpaToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
247             @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
248
249         ToscaUtils.assertPoliciesExist(serviceTemplate);
250
251         for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
252             dao.create(policy);
253         }
254
255         // Return the created policy types
256         JpaToscaPolicies returnPolicies = new JpaToscaPolicies();
257         returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
258
259         for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
260             returnPolicies.getConceptMap().put(policyKey, dao.get(JpaToscaPolicy.class, policyKey));
261         }
262
263         serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
264
265         return serviceTemplate;
266     }
267
268     /**
269      * Update policies.
270      *
271      * @param dao the DAO to use to access the database
272      * @param serviceTemplate the service template containing the definitions of the policies to be updated.
273      * @return the TOSCA service template containing the policies that were updated
274      * @throws PfModelException on errors updating policies
275      */
276     public JpaToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
277             @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
278
279         ToscaUtils.assertPoliciesExist(serviceTemplate);
280
281         for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
282             dao.update(policy);
283         }
284
285         // Return the created policy types
286         JpaToscaPolicies returnPolicies = new JpaToscaPolicies();
287         returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
288
289         for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
290             returnPolicies.getConceptMap().put(policyKey, dao.get(JpaToscaPolicy.class, policyKey));
291         }
292
293         serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
294
295         return serviceTemplate;
296     }
297
298     /**
299      * Delete policies.
300      *
301      * @param dao the DAO to use to access the database
302      * @param policyKey the policy key
303      * @return the TOSCA service template containing the policies that were deleted
304      * @throws PfModelException on errors deleting policies
305      */
306     public JpaToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
307             throws PfModelException {
308
309         JpaToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey.getName(), policyKey.getVersion());
310
311         dao.delete(JpaToscaPolicy.class, policyKey);
312
313         return serviceTemplate;
314     }
315
316     /**
317      * Convert a list of concepts to a map of concepts.
318      *
319      * @param conceptList the concept list
320      * @return the concept map
321      */
322     private <T extends PfConcept> Map<PfConceptKey, T> asConceptMap(List<T> conceptList) {
323         Map<PfConceptKey, T> conceptMap = new LinkedHashMap<>();
324         for (T concept : conceptList) {
325             conceptMap.put((PfConceptKey) concept.getKey(), concept);
326         }
327
328         return conceptMap;
329     }
330 }