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.ToscaPolicies;
32 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy;
33 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyType;
34 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyTypes;
35 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
36 import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * This class provides the provision of information on TOSCA concepts in the database to callers.
43 * @author Liam Fallon (liam.fallon@est.tech)
45 public class SimpleToscaProvider {
46 private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class);
51 * @param dao the DAO to use to access the database
52 * @param policyTypeKey the policy type key for the policy types to be retrieved. A null key name returns all policy
53 * types. A null key version returns all versions of the policy type name specified in the key.
54 * @return the policy types found
55 * @throws PfModelException on errors getting policy types
57 public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
58 throws PfModelException {
60 // Create the structure of the TOSCA service template to contain the policy type
61 ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
62 serviceTemplate.setPolicyTypes(new ToscaPolicyTypes());
64 // Add the policy type to the TOSCA service template
65 ToscaPolicyType policyType = dao.get(ToscaPolicyType.class, policyTypeKey);
66 if (policyType != null) {
67 serviceTemplate.getPolicyTypes().getConceptMap().put(policyTypeKey, policyType);
68 return serviceTemplate;
70 String errorMessage = "policy type not found: " + policyTypeKey.getId();
71 LOGGER.warn(errorMessage);
72 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
77 * Create policy types.
79 * @param dao the DAO to use to access the database
80 * @param serviceTemplate the service template containing the definition of the policy types to be created
81 * @return the TOSCA service template containing the created policy types
82 * @throws PfModelException on errors creating policy types
84 public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
85 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
87 assertPolicyTypesExist(serviceTemplate);
89 for (ToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
90 dao.create(policyType);
93 // Return the created policy types
94 ToscaPolicyTypes returnPolicyTypes = new ToscaPolicyTypes();
96 for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
97 returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(ToscaPolicyType.class, policyTypeKey));
100 ToscaServiceTemplate returnServiceTemplate = new ToscaServiceTemplate();
101 returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
103 return returnServiceTemplate;
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 modified
111 * @return the TOSCA service template containing the modified policy types
112 * @throws PfModelException on errors updating policy types
114 public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
115 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
117 assertPolicyTypesExist(serviceTemplate);
119 for (ToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
120 dao.update(policyType);
123 // Return the created policy types
124 ToscaPolicyTypes returnPolicyTypes = new ToscaPolicyTypes();
126 for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
127 returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(ToscaPolicyType.class, policyTypeKey));
130 ToscaServiceTemplate returnServiceTemplate = new ToscaServiceTemplate();
131 returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
133 return returnServiceTemplate;
137 * Delete policy types.
139 * @param dao the DAO to use to access the database
140 * @param policyTypeKey the policy type key for the policy types to be deleted, if the version of the key is null,
141 * all versions of the policy type are deleted.
142 * @return the TOSCA service template containing the policy types that were deleted
143 * @throws PfModelException on errors deleting policy types
145 public ToscaServiceTemplate deletePolicyTypes(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
146 throws PfModelException {
148 ToscaServiceTemplate serviceTemplate = getPolicyTypes(dao, policyTypeKey);
150 dao.delete(ToscaPolicyType.class, policyTypeKey);
152 return serviceTemplate;
158 * @param dao the DAO to use to access the database
159 * @param policyKey the policy key for the policies to be retrieved. The parent name and version must be specified.
160 * A null local name returns all policies for a parent policy type.
161 * @return the policies found
162 * @throws PfModelException on errors getting policies
164 public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
165 throws PfModelException {
167 // Create the structure of the TOSCA service template to contain the policy type
168 ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
169 serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate());
170 serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies());
172 // Add the policy to the TOSCA service template
173 ToscaPolicy policy = dao.get(ToscaPolicy.class, policyKey);
174 if (policy != null) {
175 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, policy);
176 return serviceTemplate;
178 String errorMessage = "policy not found: " + policyKey.getId();
179 LOGGER.warn(errorMessage);
180 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
187 * @param dao the DAO to use to access the database
188 * @param serviceTemplate the service template containing the definitions of the new policies to be created.
189 * @return the TOSCA service template containing the policy types that were created
190 * @throws PfModelException on errors creating policies
192 public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
193 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
195 assertPoliciesExist(serviceTemplate);
197 for (ToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
201 // Return the created policy types
202 ToscaPolicies returnPolicies = new ToscaPolicies();
203 returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
205 for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
206 returnPolicies.getConceptMap().put(policyKey, dao.get(ToscaPolicy.class, policyKey));
209 serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
211 return serviceTemplate;
217 * @param dao the DAO to use to access the database
218 * @param serviceTemplate the service template containing the definitions of the policies to be updated.
219 * @return the TOSCA service template containing the policies that were updated
220 * @throws PfModelException on errors updating policies
222 public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
223 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
225 assertPoliciesExist(serviceTemplate);
227 for (ToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
231 // Return the created policy types
232 ToscaPolicies returnPolicies = new ToscaPolicies();
233 returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
235 for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
236 returnPolicies.getConceptMap().put(policyKey, dao.get(ToscaPolicy.class, policyKey));
239 serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
241 return serviceTemplate;
247 * @param dao the DAO to use to access the database
248 * @param policyKey the policy key
249 * @return the TOSCA service template containing the policies that were deleted
250 * @throws PfModelException on errors deleting policies
252 public ToscaServiceTemplate deletePolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
253 throws PfModelException {
255 ToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey);
257 dao.delete(ToscaPolicy.class, policyKey);
259 return serviceTemplate;
263 * Check if policy types have been specified is initialized.
265 private void assertPolicyTypesExist(final ToscaServiceTemplate serviceTemplate) {
266 if (serviceTemplate.getPolicyTypes() == null) {
267 String errorMessage = "no policy types specified on service template";
268 LOGGER.warn(errorMessage);
269 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
272 if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) {
273 String errorMessage = "list of policy types specified on service template is empty";
274 LOGGER.warn(errorMessage);
275 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
280 * Check if policy types have been specified is initialized.
282 private void assertPoliciesExist(final ToscaServiceTemplate serviceTemplate) {
283 if (serviceTemplate.getTopologyTemplate() == null) {
284 String errorMessage = "topology template not specified on service template";
285 LOGGER.warn(errorMessage);
286 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
289 if (serviceTemplate.getTopologyTemplate().getPolicies() == null) {
290 String errorMessage = "no policies specified on topology template of service template";
291 LOGGER.warn(errorMessage);
292 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
295 if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) {
296 String errorMessage = "list of policies specified on topology template of service template is empty";
297 LOGGER.warn(errorMessage);
298 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);