Fix bugs on filters
[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.simple.concepts.JpaToscaPolicies;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
39 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
42 import org.onap.policy.models.tosca.utils.ToscaUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * This class provides the provision of information on TOSCA concepts in the database to callers.
48  *
49  * @author Liam Fallon (liam.fallon@est.tech)
50  */
51 public class SimpleToscaProvider {
52     private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class);
53
54     /**
55      * Get policy types.
56      *
57      * @param dao the DAO to use to access the database
58      * @param name the name of the policy type to get, set to null to get all policy types
59      * @param version the version of the policy type to get, set to null to get all versions
60      * @return the policy types found
61      * @throws PfModelException on errors getting policy types
62      */
63     public JpaToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
64             throws PfModelException {
65
66         // Create the structure of the TOSCA service template to contain the policy type
67         JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
68         serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
69
70         // Add the policy type to the TOSCA service template
71         List<JpaToscaPolicyType> jpaPolicyTypeList = dao.getFiltered(JpaToscaPolicyType.class, name, version);
72         if (jpaPolicyTypeList != null) {
73             serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList));
74             return serviceTemplate;
75         } else {
76             String errorMessage = "policy type not found: " + name + ":" + version;
77             LOGGER.warn(errorMessage);
78             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
79         }
80     }
81
82     /**
83      * Create policy types.
84      *
85      * @param dao the DAO to use to access the database
86      * @param serviceTemplate the service template containing the definition of the policy types to be created
87      * @return the TOSCA service template containing the created policy types
88      * @throws PfModelException on errors creating policy types
89      */
90     public JpaToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
91             @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
92
93         ToscaUtils.assertPolicyTypesExist(serviceTemplate);
94
95         for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
96             dao.create(policyType);
97         }
98
99         // Return the created policy types
100         JpaToscaPolicyTypes returnPolicyTypes = new JpaToscaPolicyTypes();
101
102         for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
103             returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(JpaToscaPolicyType.class, policyTypeKey));
104         }
105
106         JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
107         returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
108
109         return returnServiceTemplate;
110     }
111
112     /**
113      * Create policy types.
114      *
115      * @param dao the DAO to use to access the database
116      * @param serviceTemplate the service template containing the definition of the policy types to be modified
117      * @return the TOSCA service template containing the modified policy types
118      * @throws PfModelException on errors updating policy types
119      */
120     public JpaToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
121             @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
122
123         ToscaUtils.assertPolicyTypesExist(serviceTemplate);
124
125         for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
126             dao.update(policyType);
127         }
128
129         // Return the created policy types
130         JpaToscaPolicyTypes returnPolicyTypes = new JpaToscaPolicyTypes();
131
132         for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
133             returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(JpaToscaPolicyType.class, policyTypeKey));
134         }
135
136         JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
137         returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
138
139         return returnServiceTemplate;
140     }
141
142     /**
143      * Delete policy types.
144      *
145      * @param dao the DAO to use to access the database
146      * @param policyTypeKey the policy type key for the policy types to be deleted, if the version of the key is null,
147      *        all versions of the policy type are deleted.
148      * @return the TOSCA service template containing the policy types that were deleted
149      * @throws PfModelException on errors deleting policy types
150      */
151     public JpaToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
152             throws PfModelException {
153
154         JpaToscaServiceTemplate serviceTemplate =
155                 getPolicyTypes(dao, policyTypeKey.getName(), policyTypeKey.getVersion());
156
157         dao.delete(JpaToscaPolicyType.class, policyTypeKey);
158
159         return serviceTemplate;
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, set to null to get all policy types
167      * @param version the version of the policy to get, set to null to get all versions
168      * @return the policies found
169      * @throws PfModelException on errors getting policies
170      */
171     public JpaToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
172             throws PfModelException {
173
174         // Create the structure of the TOSCA service template to contain the policy type
175         JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
176         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
177         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
178
179         // Add the policy type to the TOSCA service template
180         List<JpaToscaPolicy> jpaPolicyList = dao.getFiltered(JpaToscaPolicy.class, name, version);
181         if (jpaPolicyList != null) {
182             serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList));
183             return serviceTemplate;
184         } else {
185             String errorMessage = "policy not found: " + name + ":" + version;
186             LOGGER.warn(errorMessage);
187             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
188         }
189     }
190
191     /**
192      * Create policies.
193      *
194      * @param dao the DAO to use to access the database
195      * @param serviceTemplate the service template containing the definitions of the new policies to be created.
196      * @return the TOSCA service template containing the policy types that were created
197      * @throws PfModelException on errors creating policies
198      */
199     public JpaToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
200             @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
201
202         ToscaUtils.assertPoliciesExist(serviceTemplate);
203
204         for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
205             dao.create(policy);
206         }
207
208         // Return the created policy types
209         JpaToscaPolicies returnPolicies = new JpaToscaPolicies();
210         returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
211
212         for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
213             returnPolicies.getConceptMap().put(policyKey, dao.get(JpaToscaPolicy.class, policyKey));
214         }
215
216         serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
217
218         return serviceTemplate;
219     }
220
221     /**
222      * Update policies.
223      *
224      * @param dao the DAO to use to access the database
225      * @param serviceTemplate the service template containing the definitions of the policies to be updated.
226      * @return the TOSCA service template containing the policies that were updated
227      * @throws PfModelException on errors updating policies
228      */
229     public JpaToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
230             @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
231
232         ToscaUtils.assertPoliciesExist(serviceTemplate);
233
234         for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
235             dao.update(policy);
236         }
237
238         // Return the created policy types
239         JpaToscaPolicies returnPolicies = new JpaToscaPolicies();
240         returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
241
242         for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
243             returnPolicies.getConceptMap().put(policyKey, dao.get(JpaToscaPolicy.class, policyKey));
244         }
245
246         serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
247
248         return serviceTemplate;
249     }
250
251     /**
252      * Delete policies.
253      *
254      * @param dao the DAO to use to access the database
255      * @param policyKey the policy key
256      * @return the TOSCA service template containing the policies that were deleted
257      * @throws PfModelException on errors deleting policies
258      */
259     public JpaToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
260             throws PfModelException {
261
262         JpaToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey.getName(), policyKey.getVersion());
263
264         dao.delete(JpaToscaPolicy.class, policyKey);
265
266         return serviceTemplate;
267     }
268
269     /**
270      * Convert a list of concepts to a map of concepts.
271      *
272      * @param conceptList the concept list
273      * @return the concept map
274      */
275     private <T extends PfConcept> Map<PfConceptKey, T> asConceptMap(List<T> conceptList) {
276         Map<PfConceptKey, T> conceptMap = new LinkedHashMap<>();
277         for (T concept : conceptList) {
278             conceptMap.put((PfConceptKey) concept.getKey(), concept);
279         }
280
281         return conceptMap;
282     }
283 }