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 lombok.NonNull;
29 import org.onap.policy.models.base.PfConcept;
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.simple.concepts.JpaToscaPolicies;
34 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
35 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
39 import org.onap.policy.models.tosca.utils.ToscaUtils;
42 * This class provides the provision of information on TOSCA concepts in the database to callers.
44 * @author Liam Fallon (liam.fallon@est.tech)
46 public class SimpleToscaProvider {
50 * @param dao the DAO to use to access the database
51 * @param name the name of the policy type to get, set to null to get all policy types
52 * @param version the version of the policy type to get, set to null to get all versions
53 * @return the policy types found
54 * @throws PfModelException on errors getting policy types
56 public JpaToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
57 throws PfModelException {
59 // Create the structure of the TOSCA service template to contain the policy type
60 JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
61 serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
63 // Add the policy type to the TOSCA service template
64 List<JpaToscaPolicyType> jpaPolicyTypeList = dao.getFiltered(JpaToscaPolicyType.class, name, version);
65 serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList));
67 return serviceTemplate;
71 * Create policy types.
73 * @param dao the DAO to use to access the database
74 * @param serviceTemplate the service template containing the definition of the policy types to be created
75 * @return the TOSCA service template containing the created policy types
76 * @throws PfModelException on errors creating policy types
78 public JpaToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
79 @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
81 ToscaUtils.assertPolicyTypesExist(serviceTemplate);
83 for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
84 dao.create(policyType);
87 // Return the created policy types
88 JpaToscaPolicyTypes returnPolicyTypes = new JpaToscaPolicyTypes();
90 for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
91 returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(JpaToscaPolicyType.class, policyTypeKey));
94 JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
95 returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
97 return returnServiceTemplate;
101 * Create policy types.
103 * @param dao the DAO to use to access the database
104 * @param serviceTemplate the service template containing the definition of the policy types to be modified
105 * @return the TOSCA service template containing the modified policy types
106 * @throws PfModelException on errors updating policy types
108 public JpaToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
109 @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
111 ToscaUtils.assertPolicyTypesExist(serviceTemplate);
113 for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
114 dao.update(policyType);
117 // Return the created policy types
118 JpaToscaPolicyTypes returnPolicyTypes = new JpaToscaPolicyTypes();
120 for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
121 returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(JpaToscaPolicyType.class, policyTypeKey));
124 JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
125 returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
127 return returnServiceTemplate;
131 * Delete policy types.
133 * @param dao the DAO to use to access the database
134 * @param policyTypeKey the policy type key for the policy types to be deleted, if the version of the key is null,
135 * all versions of the policy type are deleted.
136 * @return the TOSCA service template containing the policy types that were deleted
137 * @throws PfModelException on errors deleting policy types
139 public JpaToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
140 throws PfModelException {
142 JpaToscaServiceTemplate serviceTemplate =
143 getPolicyTypes(dao, policyTypeKey.getName(), policyTypeKey.getVersion());
145 dao.delete(JpaToscaPolicyType.class, policyTypeKey);
147 return serviceTemplate;
153 * @param dao the DAO to use to access the database
154 * @param name the name of the policy to get, set to null to get all policy types
155 * @param version the version of the policy to get, set to null to get all versions
156 * @return the policies found
157 * @throws PfModelException on errors getting policies
159 public JpaToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
160 throws PfModelException {
162 // Create the structure of the TOSCA service template to contain the policy type
163 JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
164 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
165 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
167 // Add the policy type to the TOSCA service template
168 List<JpaToscaPolicy> jpaPolicyList = dao.getFiltered(JpaToscaPolicy.class, name, version);
169 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList));
170 return serviceTemplate;
176 * @param dao the DAO to use to access the database
177 * @param serviceTemplate the service template containing the definitions of the new policies to be created.
178 * @return the TOSCA service template containing the policy types that were created
179 * @throws PfModelException on errors creating policies
181 public JpaToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
182 @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
184 ToscaUtils.assertPoliciesExist(serviceTemplate);
186 for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
190 // Return the created policy types
191 JpaToscaPolicies returnPolicies = new JpaToscaPolicies();
192 returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
194 for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
195 returnPolicies.getConceptMap().put(policyKey, dao.get(JpaToscaPolicy.class, policyKey));
198 serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
200 return serviceTemplate;
206 * @param dao the DAO to use to access the database
207 * @param serviceTemplate the service template containing the definitions of the policies to be updated.
208 * @return the TOSCA service template containing the policies that were updated
209 * @throws PfModelException on errors updating policies
211 public JpaToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
212 @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
214 ToscaUtils.assertPoliciesExist(serviceTemplate);
216 for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
220 // Return the created policy types
221 JpaToscaPolicies returnPolicies = new JpaToscaPolicies();
222 returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
224 for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
225 returnPolicies.getConceptMap().put(policyKey, dao.get(JpaToscaPolicy.class, policyKey));
228 serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
230 return serviceTemplate;
236 * @param dao the DAO to use to access the database
237 * @param policyKey the policy key
238 * @return the TOSCA service template containing the policies that were deleted
239 * @throws PfModelException on errors deleting policies
241 public JpaToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
242 throws PfModelException {
244 JpaToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey.getName(), policyKey.getVersion());
246 dao.delete(JpaToscaPolicy.class, policyKey);
248 return serviceTemplate;
252 * Convert a list of concepts to a map of concepts.
254 * @param conceptList the concept list
255 * @return the concept map
257 private <T extends PfConcept> Map<PfConceptKey, T> asConceptMap(List<T> conceptList) {
258 Map<PfConceptKey, T> conceptMap = new LinkedHashMap<>();
259 for (T concept : conceptList) {
260 conceptMap.put((PfConceptKey) concept.getKey(), concept);