2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.authorative.provider;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
29 import javax.ws.rs.core.Response;
30 import javax.ws.rs.core.Response.Status;
32 import lombok.NonNull;
34 import org.apache.commons.collections4.CollectionUtils;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfModelException;
37 import org.onap.policy.models.base.PfModelRuntimeException;
38 import org.onap.policy.models.dao.PfDao;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
46 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
47 import org.onap.policy.models.tosca.utils.ToscaServiceTemplateUtils;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * This class provides the provision of information on TOSCA concepts in the database to callers.
54 * @author Liam Fallon (liam.fallon@est.tech)
56 public class AuthorativeToscaProvider {
57 private static final Logger LOGGER = LoggerFactory.getLogger(AuthorativeToscaProvider.class);
62 * @param dao the DAO to use to access the database
63 * @param name the name of the policy type to get.
64 * @param version the version of the policy type to get.
65 * @return the policy types found
66 * @throws PfModelException on errors getting policy types
68 public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
69 throws PfModelException {
71 LOGGER.debug("->getPolicyTypes: name={}, version={}", name, version);
73 JpaToscaServiceTemplate jpaServiceTemplate = new SimpleToscaProvider().getPolicyTypes(dao, name, version);
75 ToscaServiceTemplate serviceTemplate = jpaServiceTemplate.toAuthorative();
77 LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate);
78 return serviceTemplate;
84 * @param dao the DAO to use to access the database
85 * @param name the name of the policy type to get, set to null to get all policy types
86 * @param version the version of the policy type to get, set to null to get all versions
87 * @return the policy types found
88 * @throws PfModelException on errors getting policy types
90 public List<ToscaPolicyType> getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version)
91 throws PfModelException {
93 LOGGER.debug("->getPolicyTypeList: name={}, version={}", name, version);
95 List<ToscaPolicyType> policyTypeList;
98 policyTypeList = new ArrayList<>(
99 new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative().getPolicyTypes().values());
100 } catch (PfModelRuntimeException pfme) {
101 return handlePfModelRuntimeException(pfme);
104 LOGGER.debug("<-getPolicyTypeList: name={}, version={}, policyTypeList={}", name, version, policyTypeList);
105 return policyTypeList;
109 * Get filtered policy types.
111 * @param dao the DAO to use to access the database
112 * @param filter the filter for the policy types to get
113 * @return the policy types found
114 * @throws PfModelException on errors getting policy types
116 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao,
117 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
119 LOGGER.debug("->getFilteredPolicyTypes: filter={}", filter);
120 SimpleToscaProvider simpleToscaProvider = new SimpleToscaProvider();
122 final JpaToscaServiceTemplate dbServiceTemplate = simpleToscaProvider.getPolicyTypes(dao, null, null);
124 List<ToscaPolicyType> filteredPolicyTypes = dbServiceTemplate.getPolicyTypes().toAuthorativeList();
125 filteredPolicyTypes = filter.filter(filteredPolicyTypes);
127 if (CollectionUtils.isEmpty(filteredPolicyTypes)) {
128 throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
129 "policy types for filter " + filter.toString() + " do not exist");
132 JpaToscaServiceTemplate filteredServiceTemplate = new JpaToscaServiceTemplate();
134 for (ToscaPolicyType policyType : filteredPolicyTypes) {
135 JpaToscaServiceTemplate cascadedServiceTemplate = simpleToscaProvider
136 .getCascadedPolicyTypes(dbServiceTemplate, policyType.getName(), policyType.getVersion());
138 filteredServiceTemplate =
139 ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate);
142 ToscaServiceTemplate returnServiceTemplate = filteredServiceTemplate.toAuthorative();
144 LOGGER.debug("<-getFilteredPolicyTypes: filter={}, serviceTemplate={}", filter, returnServiceTemplate);
145 return returnServiceTemplate;
150 * Get filtered policy types.
152 * @param dao the DAO to use to access the database
153 * @param filter the filter for the policy types to get
154 * @return the policy types found
155 * @throws PfModelException on errors getting policy types
157 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull final PfDao dao,
158 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
160 LOGGER.debug("->getFilteredPolicyTypeList: filter={}", filter);
162 List<ToscaPolicyType> filteredPolicyTypeList = filter.filter(getPolicyTypeList(dao, null, null));
164 LOGGER.debug("<-getFilteredPolicyTypeList: filter={}, filteredPolicyTypeList={}", filter,
165 filteredPolicyTypeList);
167 return filteredPolicyTypeList;
171 * Create policy types.
173 * @param dao the DAO to use to access the database
174 * @param serviceTemplate the service template containing the definition of the policy types to be created
175 * @return the TOSCA service template containing the created policy types
176 * @throws PfModelException on errors creating policy types
178 public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
179 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
181 LOGGER.debug("->createPolicyTypes: serviceTemplate={}", serviceTemplate);
183 ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
184 .createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
186 LOGGER.debug("<-createPolicyTypes: createdServiceTempalate={}", createdServiceTempalate);
187 return createdServiceTempalate;
191 * Update policy types.
193 * @param dao the DAO to use to access the database
194 * @param serviceTemplate the service template containing the definition of the policy types to be modified
195 * @return the TOSCA service template containing the modified policy types
196 * @throws PfModelException on errors updating policy types
198 public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
199 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
201 LOGGER.debug("->updatePolicyTypes: serviceTempalate={}", serviceTemplate);
203 ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
204 .updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
206 LOGGER.debug("<-updatePolicyTypes: updatedServiceTempalate={}", updatedServiceTempalate);
207 return updatedServiceTempalate;
211 * Delete policy type.
213 * @param dao the DAO to use to access the database
214 * @param name the name of the policy type to delete.
215 * @param version the version of the policy type to delete.
216 * @return the TOSCA service template containing the policy type that was deleted
217 * @throws PfModelException on errors deleting policy types
219 public ToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final String name,
220 @NonNull final String version) throws PfModelException {
222 LOGGER.debug("->deletePolicyType: name={}, version={}", name, version);
224 ToscaServiceTemplate deletedServiceTempalate =
225 new SimpleToscaProvider().deletePolicyType(dao, new PfConceptKey(name, version)).toAuthorative();
227 LOGGER.debug("<-deletePolicyType: name={}, version={}, deletedServiceTempalate={}", name, version,
228 deletedServiceTempalate);
229 return deletedServiceTempalate;
235 * @param dao the DAO to use to access the database
236 * @param name the name of the policy to get.
237 * @param version the version of the policy to get.
238 * @return the policies found
239 * @throws PfModelException on errors getting policies
241 public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
242 throws PfModelException {
243 LOGGER.debug("->getPolicies: name={}, version={}", name, version);
245 ToscaServiceTemplate gotServiceTempalate =
246 new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative();
248 LOGGER.debug("<-getPolicies: name={}, version={}, gotServiceTempalate={}", name, version, gotServiceTempalate);
249 return gotServiceTempalate;
255 * @param dao the DAO to use to access the database
256 * @param name the name of the policy to get, null to get all policies
257 * @param version the version of the policy to get, null to get all versions of a policy
258 * @return the policies found
259 * @throws PfModelException on errors getting policies
261 public List<ToscaPolicy> getPolicyList(@NonNull final PfDao dao, final String name, final String version)
262 throws PfModelException {
263 LOGGER.debug("->getPolicyList: name={}, version={}", name, version);
265 List<ToscaPolicy> policyList;
268 policyList = asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative()
269 .getToscaTopologyTemplate().getPolicies());
270 } catch (PfModelRuntimeException pfme) {
271 return handlePfModelRuntimeException(pfme);
274 LOGGER.debug("<-getPolicyList: name={}, version={}, policyTypeList={}", name, version, policyList);
279 * Get filtered policies.
281 * @param dao the DAO to use to access the database
282 * @param filter the filter for the policies to get
283 * @return the policies found
284 * @throws PfModelException on errors getting policies
286 public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
287 throws PfModelException {
289 LOGGER.debug("->getFilteredPolicies: filter={}", filter);
290 String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
292 SimpleToscaProvider simpleToscaProvider = new SimpleToscaProvider();
293 final JpaToscaServiceTemplate dbServiceTemplate =
294 simpleToscaProvider.getPolicies(dao, filter.getName(), version);
296 List<ToscaPolicy> filteredPolicies = dbServiceTemplate.getTopologyTemplate().getPolicies().toAuthorativeList();
297 filteredPolicies = filter.filter(filteredPolicies);
299 if (CollectionUtils.isEmpty(filteredPolicies)) {
300 throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
301 "policies for filter " + filter.toString() + " do not exist");
304 JpaToscaServiceTemplate filteredServiceTemplate = new JpaToscaServiceTemplate();
306 for (ToscaPolicy policy : filteredPolicies) {
307 JpaToscaServiceTemplate cascadedServiceTemplate =
308 simpleToscaProvider.getCascadedPolicies(dbServiceTemplate, policy.getName(), policy.getVersion());
310 filteredServiceTemplate =
311 ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate);
314 ToscaServiceTemplate returnServiceTemplate = filteredServiceTemplate.toAuthorative();
316 LOGGER.debug("<-getFilteredPolicies: filter={}, serviceTemplate={}", filter, returnServiceTemplate);
317 return returnServiceTemplate;
321 * Get filtered policies.
323 * @param dao the DAO to use to access the database
324 * @param filter the filter for the policies to get
325 * @return the policies found
326 * @throws PfModelException on errors getting policies
328 public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
329 throws PfModelException {
331 LOGGER.debug("->getFilteredPolicyList: filter={}", filter);
332 String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
334 List<ToscaPolicy> policyList = filter.filter(getPolicyList(dao, filter.getName(), version));
336 LOGGER.debug("<-getFilteredPolicyList: filter={}, policyList={}", filter, policyList);
343 * @param dao the DAO to use to access the database
344 * @param serviceTemplate the service template containing the definitions of the new policies to be created.
345 * @return the TOSCA service template containing the policy types that were created
346 * @throws PfModelException on errors creating policies
348 public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
349 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
351 LOGGER.debug("->createPolicies: serviceTempalate={}", serviceTemplate);
353 ToscaServiceTemplate createdServiceTempalate =
354 new SimpleToscaProvider().createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
356 LOGGER.debug("<-createPolicies: createdServiceTempalate={}", createdServiceTempalate);
357 return createdServiceTempalate;
363 * @param dao the DAO to use to access the database
364 * @param serviceTemplate the service template containing the definitions of the policies to be updated.
365 * @return the TOSCA service template containing the policies that were updated
366 * @throws PfModelException on errors updating policies
368 public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
369 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
371 LOGGER.debug("->updatePolicies: serviceTempalate={}", serviceTemplate);
373 ToscaServiceTemplate updatedServiceTempalate =
374 new SimpleToscaProvider().updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
376 LOGGER.debug("<-updatePolicies: updatedServiceTempalate={}", updatedServiceTempalate);
377 return updatedServiceTempalate;
383 * @param dao the DAO to use to access the database
384 * @param name the name of the policy to delete.
385 * @param version the version of the policy to delete.
386 * @return the TOSCA service template containing the policy that weas deleted
387 * @throws PfModelException on errors deleting policies
389 public ToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final String name,
390 @NonNull final String version) throws PfModelException {
392 LOGGER.debug("->deletePolicy: name={}, version={}", name, version);
394 ToscaServiceTemplate deletedServiceTempalate =
395 new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative();
397 LOGGER.debug("<-deletePolicy: name={}, version={}, deletedServiceTempalate={}", name, version,
398 deletedServiceTempalate);
399 return deletedServiceTempalate;
403 * Return the contents of a list of maps as a plain list.
405 * @param listOfMaps the list of maps
406 * @return the plain list
408 private <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) {
409 List<T> returnList = new ArrayList<>();
410 for (Map<String, T> conceptMap : listOfMaps) {
411 for (T concept : conceptMap.values()) {
412 returnList.add(concept);
420 * Handle a PfModelRuntimeException on a list call.
422 * @param pfme the model exception
423 * @return an empty list on 404
425 private <T extends ToscaEntity> List<T> handlePfModelRuntimeException(final PfModelRuntimeException pfme) {
426 if (Status.NOT_FOUND.equals(pfme.getErrorResponse().getResponseCode())) {
427 LOGGER.trace("request did not find any results", pfme);
428 return Collections.emptyList();