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.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;
47 * This class provides the provision of information on TOSCA concepts in the database to callers.
49 * @author Liam Fallon (liam.fallon@est.tech)
51 public class SimpleToscaProvider {
52 private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class);
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
63 public JpaToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
64 throws PfModelException {
66 // Create the structure of the TOSCA service template to contain the policy type
67 JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
68 serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
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;
76 String errorMessage = "policy type not found: " + name + ":" + version;
77 LOGGER.warn(errorMessage);
78 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
83 * Create policy types.
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
90 public JpaToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
91 @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
93 ToscaUtils.assertPolicyTypesExist(serviceTemplate);
95 for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
96 dao.create(policyType);
99 // Return the created policy types
100 JpaToscaPolicyTypes returnPolicyTypes = new JpaToscaPolicyTypes();
102 for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
103 returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(JpaToscaPolicyType.class, policyTypeKey));
106 JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
107 returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
109 return returnServiceTemplate;
113 * Create policy types.
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
120 public JpaToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
121 @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
123 ToscaUtils.assertPolicyTypesExist(serviceTemplate);
125 for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
126 dao.update(policyType);
129 // Return the created policy types
130 JpaToscaPolicyTypes returnPolicyTypes = new JpaToscaPolicyTypes();
132 for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
133 returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(JpaToscaPolicyType.class, policyTypeKey));
136 JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
137 returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
139 return returnServiceTemplate;
143 * Delete policy types.
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
151 public JpaToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
152 throws PfModelException {
154 JpaToscaServiceTemplate serviceTemplate =
155 getPolicyTypes(dao, policyTypeKey.getName(), policyTypeKey.getVersion());
157 dao.delete(JpaToscaPolicyType.class, policyTypeKey);
159 return serviceTemplate;
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
171 public JpaToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
172 throws PfModelException {
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());
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;
185 String errorMessage = "policy not found: " + name + ":" + version;
186 LOGGER.warn(errorMessage);
187 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
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
199 public JpaToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
200 @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
202 ToscaUtils.assertPoliciesExist(serviceTemplate);
204 for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
208 // Return the created policy types
209 JpaToscaPolicies returnPolicies = new JpaToscaPolicies();
210 returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
212 for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
213 returnPolicies.getConceptMap().put(policyKey, dao.get(JpaToscaPolicy.class, policyKey));
216 serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
218 return serviceTemplate;
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
229 public JpaToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
230 @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
232 ToscaUtils.assertPoliciesExist(serviceTemplate);
234 for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
238 // Return the created policy types
239 JpaToscaPolicies returnPolicies = new JpaToscaPolicies();
240 returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
242 for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
243 returnPolicies.getConceptMap().put(policyKey, dao.get(JpaToscaPolicy.class, policyKey));
246 serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
248 return serviceTemplate;
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
259 public JpaToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
260 throws PfModelException {
262 JpaToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey.getName(), policyKey.getVersion());
264 dao.delete(JpaToscaPolicy.class, policyKey);
266 return serviceTemplate;
270 * Convert a list of concepts to a map of concepts.
272 * @param conceptList the concept list
273 * @return the concept map
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);