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 javax.ws.rs.core.Response;
25 import lombok.NonNull;
27 import org.onap.policy.models.base.PfConceptKey;
28 import org.onap.policy.models.base.PfModelException;
29 import org.onap.policy.models.base.PfModelRuntimeException;
30 import org.onap.policy.models.dao.PfDao;
31 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
32 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
33 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
34 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
35 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
37 import org.onap.policy.models.tosca.utils.ToscaUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
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 {
47 private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class);
52 * @param dao the DAO to use to access the database
53 * @param policyTypeKey the policy type key for the policy types to be retrieved. A null key name returns all policy
54 * types. A null key version returns all versions of the policy type name specified in the key.
55 * @return the policy types found
56 * @throws PfModelException on errors getting policy types
58 public JpaToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
59 throws PfModelException {
61 // Create the structure of the TOSCA service template to contain the policy type
62 JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
63 serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
65 // Add the policy type to the TOSCA service template
66 JpaToscaPolicyType policyType = dao.get(JpaToscaPolicyType.class, policyTypeKey);
67 if (policyType != null) {
68 serviceTemplate.getPolicyTypes().getConceptMap().put(policyTypeKey, policyType);
69 return serviceTemplate;
71 String errorMessage = "policy type not found: " + policyTypeKey.getId();
72 LOGGER.warn(errorMessage);
73 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
78 * Create policy types.
80 * @param dao the DAO to use to access the database
81 * @param serviceTemplate the service template containing the definition of the policy types to be created
82 * @return the TOSCA service template containing the created policy types
83 * @throws PfModelException on errors creating policy types
85 public JpaToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
86 @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
88 ToscaUtils.assertPolicyTypesExist(serviceTemplate);
90 for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
91 dao.create(policyType);
94 // Return the created policy types
95 JpaToscaPolicyTypes returnPolicyTypes = new JpaToscaPolicyTypes();
97 for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
98 returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(JpaToscaPolicyType.class, policyTypeKey));
101 JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
102 returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
104 return returnServiceTemplate;
108 * Create policy types.
110 * @param dao the DAO to use to access the database
111 * @param serviceTemplate the service template containing the definition of the policy types to be modified
112 * @return the TOSCA service template containing the modified policy types
113 * @throws PfModelException on errors updating policy types
115 public JpaToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
116 @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
118 ToscaUtils.assertPolicyTypesExist(serviceTemplate);
120 for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
121 dao.update(policyType);
124 // Return the created policy types
125 JpaToscaPolicyTypes returnPolicyTypes = new JpaToscaPolicyTypes();
127 for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
128 returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(JpaToscaPolicyType.class, policyTypeKey));
131 JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
132 returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
134 return returnServiceTemplate;
138 * Delete policy types.
140 * @param dao the DAO to use to access the database
141 * @param policyTypeKey the policy type key for the policy types to be deleted, if the version of the key is null,
142 * all versions of the policy type are deleted.
143 * @return the TOSCA service template containing the policy types that were deleted
144 * @throws PfModelException on errors deleting policy types
146 public JpaToscaServiceTemplate deletePolicyTypes(@NonNull final PfDao dao,
147 @NonNull final PfConceptKey policyTypeKey)
148 throws PfModelException {
150 JpaToscaServiceTemplate serviceTemplate = getPolicyTypes(dao, policyTypeKey);
152 dao.delete(JpaToscaPolicyType.class, policyTypeKey);
154 return serviceTemplate;
160 * @param dao the DAO to use to access the database
161 * @param policyKey the policy key for the policies to be retrieved. The parent name and version must be specified.
162 * A null local name returns all policies for a parent policy type.
163 * @return the policies found
164 * @throws PfModelException on errors getting policies
166 public JpaToscaServiceTemplate getPolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
167 throws PfModelException {
169 // Create the structure of the TOSCA service template to contain the policy type
170 JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
171 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
172 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
174 // Add the policy to the TOSCA service template
175 JpaToscaPolicy policy = dao.get(JpaToscaPolicy.class, policyKey);
176 if (policy != null) {
177 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, policy);
178 return serviceTemplate;
180 String errorMessage = "policy not found: " + policyKey.getId();
181 LOGGER.warn(errorMessage);
182 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
189 * @param dao the DAO to use to access the database
190 * @param serviceTemplate the service template containing the definitions of the new policies to be created.
191 * @return the TOSCA service template containing the policy types that were created
192 * @throws PfModelException on errors creating policies
194 public JpaToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
195 @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
197 ToscaUtils.assertPoliciesExist(serviceTemplate);
199 for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
203 // Return the created policy types
204 JpaToscaPolicies returnPolicies = new JpaToscaPolicies();
205 returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
207 for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
208 returnPolicies.getConceptMap().put(policyKey, dao.get(JpaToscaPolicy.class, policyKey));
211 serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
213 return serviceTemplate;
219 * @param dao the DAO to use to access the database
220 * @param serviceTemplate the service template containing the definitions of the policies to be updated.
221 * @return the TOSCA service template containing the policies that were updated
222 * @throws PfModelException on errors updating policies
224 public JpaToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
225 @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
227 ToscaUtils.assertPoliciesExist(serviceTemplate);
229 for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
233 // Return the created policy types
234 JpaToscaPolicies returnPolicies = new JpaToscaPolicies();
235 returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
237 for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
238 returnPolicies.getConceptMap().put(policyKey, dao.get(JpaToscaPolicy.class, policyKey));
241 serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
243 return serviceTemplate;
249 * @param dao the DAO to use to access the database
250 * @param policyKey the policy key
251 * @return the TOSCA service template containing the policies that were deleted
252 * @throws PfModelException on errors deleting policies
254 public JpaToscaServiceTemplate deletePolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
255 throws PfModelException {
257 JpaToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey);
259 dao.delete(JpaToscaPolicy.class, policyKey);
261 return serviceTemplate;