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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.provider;
23 import java.util.LinkedHashMap;
24 import java.util.List;
27 import javax.ws.rs.core.Response;
29 import lombok.NonNull;
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;
49 * This class provides the provision of information on TOSCA concepts in the database to callers.
51 * @author Liam Fallon (liam.fallon@est.tech)
53 public class SimpleToscaProvider {
54 private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class);
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
65 public JpaToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
66 throws PfModelException {
68 // Create the structure of the TOSCA service template to contain the policy type
69 JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
70 serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
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;
78 String errorMessage = "policy type not found: " + name + ":" + version;
79 LOGGER.warn(errorMessage);
80 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
85 * Get filtered policy types.
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
92 public JpaToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao,
93 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
95 // Create the structure of the TOSCA service template to contain the policy type
96 JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
97 serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
99 List<JpaToscaPolicyType> jpaPolicyTypeList = dao.getAll(JpaToscaPolicyType.class);
100 // TODO: The actual filtering
102 serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList));
103 return serviceTemplate;
107 * Create policy types.
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
114 public JpaToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
115 @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
117 ToscaUtils.assertPolicyTypesExist(serviceTemplate);
119 for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
120 dao.create(policyType);
123 // Return the created policy types
124 JpaToscaPolicyTypes returnPolicyTypes = new JpaToscaPolicyTypes();
126 for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
127 returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(JpaToscaPolicyType.class, policyTypeKey));
130 JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
131 returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
133 return returnServiceTemplate;
137 * Create policy types.
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
144 public JpaToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
145 @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
147 ToscaUtils.assertPolicyTypesExist(serviceTemplate);
149 for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
150 dao.update(policyType);
153 // Return the created policy types
154 JpaToscaPolicyTypes returnPolicyTypes = new JpaToscaPolicyTypes();
156 for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
157 returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(JpaToscaPolicyType.class, policyTypeKey));
160 JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
161 returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
163 return returnServiceTemplate;
167 * Delete policy types.
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
175 public JpaToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
176 throws PfModelException {
178 JpaToscaServiceTemplate serviceTemplate =
179 getPolicyTypes(dao, policyTypeKey.getName(), policyTypeKey.getVersion());
181 dao.delete(JpaToscaPolicyType.class, policyTypeKey);
183 return serviceTemplate;
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
195 public JpaToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
196 throws PfModelException {
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());
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;
209 String errorMessage = "policy not found: " + name + ":" + version;
210 LOGGER.warn(errorMessage);
211 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
216 * Get filtered policies.
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
223 public JpaToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao,
224 @NonNull final ToscaPolicyFilter filter) throws PfModelException {
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());
231 List<JpaToscaPolicy> jpaPolicyList = dao.getAll(JpaToscaPolicy.class);
232 // TODO: Do the actual filtering
234 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList));
235 return serviceTemplate;
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
246 public JpaToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
247 @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
249 ToscaUtils.assertPoliciesExist(serviceTemplate);
251 for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
255 // Return the created policy types
256 JpaToscaPolicies returnPolicies = new JpaToscaPolicies();
257 returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
259 for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
260 returnPolicies.getConceptMap().put(policyKey, dao.get(JpaToscaPolicy.class, policyKey));
263 serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
265 return serviceTemplate;
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
276 public JpaToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
277 @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
279 ToscaUtils.assertPoliciesExist(serviceTemplate);
281 for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
285 // Return the created policy types
286 JpaToscaPolicies returnPolicies = new JpaToscaPolicies();
287 returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
289 for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
290 returnPolicies.getConceptMap().put(policyKey, dao.get(JpaToscaPolicy.class, policyKey));
293 serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
295 return serviceTemplate;
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
306 public JpaToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
307 throws PfModelException {
309 JpaToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey.getName(), policyKey.getVersion());
311 dao.delete(JpaToscaPolicy.class, policyKey);
313 return serviceTemplate;
317 * Convert a list of concepts to a map of concepts.
319 * @param conceptList the concept list
320 * @return the concept map
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);