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.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 ToscaServiceTemplate 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 ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
63 serviceTemplate.setPolicyTypes(new ToscaPolicyTypes());
65 // Add the policy type to the TOSCA service template
66 ToscaPolicyType policyType = dao.get(ToscaPolicyType.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 ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
86 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
88 ToscaUtils.assertPolicyTypesExist(serviceTemplate);
90 for (ToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
91 dao.create(policyType);
94 // Return the created policy types
95 ToscaPolicyTypes returnPolicyTypes = new ToscaPolicyTypes();
97 for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
98 returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(ToscaPolicyType.class, policyTypeKey));
101 ToscaServiceTemplate returnServiceTemplate = new ToscaServiceTemplate();
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 ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
116 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
118 ToscaUtils.assertPolicyTypesExist(serviceTemplate);
120 for (ToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
121 dao.update(policyType);
124 // Return the created policy types
125 ToscaPolicyTypes returnPolicyTypes = new ToscaPolicyTypes();
127 for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
128 returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(ToscaPolicyType.class, policyTypeKey));
131 ToscaServiceTemplate returnServiceTemplate = new ToscaServiceTemplate();
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 ToscaServiceTemplate deletePolicyTypes(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
147 throws PfModelException {
149 ToscaServiceTemplate serviceTemplate = getPolicyTypes(dao, policyTypeKey);
151 dao.delete(ToscaPolicyType.class, policyTypeKey);
153 return serviceTemplate;
159 * @param dao the DAO to use to access the database
160 * @param policyKey the policy key for the policies to be retrieved. The parent name and version must be specified.
161 * A null local name returns all policies for a parent policy type.
162 * @return the policies found
163 * @throws PfModelException on errors getting policies
165 public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
166 throws PfModelException {
168 // Create the structure of the TOSCA service template to contain the policy type
169 ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
170 serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate());
171 serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies());
173 // Add the policy to the TOSCA service template
174 ToscaPolicy policy = dao.get(ToscaPolicy.class, policyKey);
175 if (policy != null) {
176 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, policy);
177 return serviceTemplate;
179 String errorMessage = "policy not found: " + policyKey.getId();
180 LOGGER.warn(errorMessage);
181 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
188 * @param dao the DAO to use to access the database
189 * @param serviceTemplate the service template containing the definitions of the new policies to be created.
190 * @return the TOSCA service template containing the policy types that were created
191 * @throws PfModelException on errors creating policies
193 public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
194 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
196 ToscaUtils.assertPoliciesExist(serviceTemplate);
198 for (ToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
202 // Return the created policy types
203 ToscaPolicies returnPolicies = new ToscaPolicies();
204 returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
206 for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
207 returnPolicies.getConceptMap().put(policyKey, dao.get(ToscaPolicy.class, policyKey));
210 serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
212 return serviceTemplate;
218 * @param dao the DAO to use to access the database
219 * @param serviceTemplate the service template containing the definitions of the policies to be updated.
220 * @return the TOSCA service template containing the policies that were updated
221 * @throws PfModelException on errors updating policies
223 public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
224 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
226 ToscaUtils.assertPoliciesExist(serviceTemplate);
228 for (ToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
232 // Return the created policy types
233 ToscaPolicies returnPolicies = new ToscaPolicies();
234 returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
236 for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
237 returnPolicies.getConceptMap().put(policyKey, dao.get(ToscaPolicy.class, policyKey));
240 serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
242 return serviceTemplate;
248 * @param dao the DAO to use to access the database
249 * @param policyKey the policy key
250 * @return the TOSCA service template containing the policies that were deleted
251 * @throws PfModelException on errors deleting policies
253 public ToscaServiceTemplate deletePolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
254 throws PfModelException {
256 ToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey);
258 dao.delete(ToscaPolicy.class, policyKey);
260 return serviceTemplate;