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;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.Response.Status;
30 import lombok.NonNull;
31 import org.apache.commons.collections4.CollectionUtils;
32 import org.onap.policy.models.base.PfConceptKey;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.base.PfModelRuntimeException;
35 import org.onap.policy.models.dao.PfDao;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
42 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
43 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
44 import org.onap.policy.models.tosca.utils.ToscaServiceTemplateUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * This class provides the provision of information on TOSCA concepts in the database to callers.
51 * @author Liam Fallon (liam.fallon@est.tech)
53 public class AuthorativeToscaProvider {
54 private static final Logger LOGGER = LoggerFactory.getLogger(AuthorativeToscaProvider.class);
56 // TODO: In next release this locking mechanism should be removed and replaced with proper session handling
57 private static final Object providerLockObject = "providerLockObject";
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 synchronized (providerLockObject) {
72 LOGGER.debug("->getPolicyTypes: name={}, version={}", name, version);
74 JpaToscaServiceTemplate jpaServiceTemplate = new SimpleToscaProvider().getPolicyTypes(dao, name, version);
76 ToscaServiceTemplate serviceTemplate = jpaServiceTemplate.toAuthorative();
78 LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate);
79 return serviceTemplate;
86 * @param dao the DAO to use to access the database
87 * @param name the name of the policy type to get, set to null to get all policy types
88 * @param version the version of the policy type to get, set to null to get all versions
89 * @return the policy types found
90 * @throws PfModelException on errors getting policy types
92 public List<ToscaPolicyType> getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version)
93 throws PfModelException {
95 synchronized (providerLockObject) {
96 LOGGER.debug("->getPolicyTypeList: name={}, version={}", name, version);
98 List<ToscaPolicyType> policyTypeList;
101 policyTypeList = new ArrayList<>(new SimpleToscaProvider().getPolicyTypes(dao, name, version)
102 .toAuthorative().getPolicyTypes().values());
103 } catch (PfModelRuntimeException pfme) {
104 return handlePfModelRuntimeException(pfme);
107 LOGGER.debug("<-getPolicyTypeList: name={}, version={}, policyTypeList={}", name, version, policyTypeList);
108 return policyTypeList;
113 * Get filtered policy types.
115 * @param dao the DAO to use to access the database
116 * @param filter the filter for the policy types to get
117 * @return the policy types found
118 * @throws PfModelException on errors getting policy types
120 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao,
121 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
123 synchronized (providerLockObject) {
124 LOGGER.debug("->getFilteredPolicyTypes: filter={}", filter);
125 SimpleToscaProvider simpleToscaProvider = new SimpleToscaProvider();
127 final JpaToscaServiceTemplate dbServiceTemplate = simpleToscaProvider.getPolicyTypes(dao, null, null);
129 List<ToscaPolicyType> filteredPolicyTypes = dbServiceTemplate.getPolicyTypes().toAuthorativeList();
130 filteredPolicyTypes = filter.filter(filteredPolicyTypes);
132 if (CollectionUtils.isEmpty(filteredPolicyTypes)) {
133 throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
134 "policy types for filter " + filter.toString() + " do not exist");
137 JpaToscaServiceTemplate filteredServiceTemplate = new JpaToscaServiceTemplate();
139 for (ToscaPolicyType policyType : filteredPolicyTypes) {
140 JpaToscaServiceTemplate cascadedServiceTemplate = simpleToscaProvider
141 .getCascadedPolicyTypes(dbServiceTemplate, policyType.getName(), policyType.getVersion());
143 filteredServiceTemplate =
144 ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate);
147 ToscaServiceTemplate returnServiceTemplate = filteredServiceTemplate.toAuthorative();
149 LOGGER.debug("<-getFilteredPolicyTypes: filter={}, serviceTemplate={}", filter, returnServiceTemplate);
150 return returnServiceTemplate;
155 * Get filtered policy types.
157 * @param dao the DAO to use to access the database
158 * @param filter the filter for the policy types to get
159 * @return the policy types found
160 * @throws PfModelException on errors getting policy types
162 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull final PfDao dao,
163 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
165 LOGGER.debug("->getFilteredPolicyTypeList: filter={}", filter);
167 List<ToscaPolicyType> filteredPolicyTypeList = filter.filter(getPolicyTypeList(dao, null, null));
169 LOGGER.debug("<-getFilteredPolicyTypeList: filter={}, filteredPolicyTypeList={}", filter,
170 filteredPolicyTypeList);
172 return filteredPolicyTypeList;
176 * Create policy types.
178 * @param dao the DAO to use to access the database
179 * @param serviceTemplate the service template containing the definition of the policy types to be created
180 * @return the TOSCA service template containing the created policy types
181 * @throws PfModelException on errors creating policy types
183 public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
184 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
186 synchronized (providerLockObject) {
187 LOGGER.debug("->createPolicyTypes: serviceTemplate={}", serviceTemplate);
189 ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
190 .createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
192 LOGGER.debug("<-createPolicyTypes: createdServiceTempalate={}", createdServiceTempalate);
193 return createdServiceTempalate;
198 * Update policy types.
200 * @param dao the DAO to use to access the database
201 * @param serviceTemplate the service template containing the definition of the policy types to be modified
202 * @return the TOSCA service template containing the modified policy types
203 * @throws PfModelException on errors updating policy types
205 public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
206 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
208 synchronized (providerLockObject) {
209 LOGGER.debug("->updatePolicyTypes: serviceTempalate={}", serviceTemplate);
211 ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
212 .updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
214 LOGGER.debug("<-updatePolicyTypes: updatedServiceTempalate={}", updatedServiceTempalate);
215 return updatedServiceTempalate;
220 * Delete policy type.
222 * @param dao the DAO to use to access the database
223 * @param name the name of the policy type to delete.
224 * @param version the version of the policy type to delete.
225 * @return the TOSCA service template containing the policy type that was deleted
226 * @throws PfModelException on errors deleting policy types
228 public ToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final String name,
229 @NonNull final String version) throws PfModelException {
231 synchronized (providerLockObject) {
232 LOGGER.debug("->deletePolicyType: name={}, version={}", name, version);
234 ToscaServiceTemplate deletedServiceTempalate =
235 new SimpleToscaProvider().deletePolicyType(dao, new PfConceptKey(name, version)).toAuthorative();
237 LOGGER.debug("<-deletePolicyType: name={}, version={}, deletedServiceTempalate={}", name, version,
238 deletedServiceTempalate);
239 return deletedServiceTempalate;
246 * @param dao the DAO to use to access the database
247 * @param name the name of the policy to get.
248 * @param version the version of the policy to get.
249 * @return the policies found
250 * @throws PfModelException on errors getting policies
252 public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
253 throws PfModelException {
255 synchronized (providerLockObject) {
256 LOGGER.debug("->getPolicies: name={}, version={}", name, version);
258 ToscaServiceTemplate gotServiceTempalate =
259 new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative();
261 LOGGER.debug("<-getPolicies: name={}, version={}, gotServiceTempalate={}", name, version,
262 gotServiceTempalate);
263 return gotServiceTempalate;
270 * @param dao the DAO to use to access the database
271 * @param name the name of the policy to get, null to get all policies
272 * @param version the version of the policy to get, null to get all versions of a policy
273 * @return the policies found
274 * @throws PfModelException on errors getting policies
276 public List<ToscaPolicy> getPolicyList(@NonNull final PfDao dao, final String name, final String version)
277 throws PfModelException {
279 synchronized (providerLockObject) {
280 LOGGER.debug("->getPolicyList: name={}, version={}", name, version);
282 List<ToscaPolicy> policyList;
285 policyList = asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative()
286 .getToscaTopologyTemplate().getPolicies());
287 } catch (PfModelRuntimeException pfme) {
288 return handlePfModelRuntimeException(pfme);
291 LOGGER.debug("<-getPolicyList: name={}, version={}, policyTypeList={}", name, version, policyList);
297 * Get filtered policies.
299 * @param dao the DAO to use to access the database
300 * @param filter the filter for the policies to get
301 * @return the policies found
302 * @throws PfModelException on errors getting policies
304 public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
305 throws PfModelException {
307 synchronized (providerLockObject) {
308 LOGGER.debug("->getFilteredPolicies: filter={}", filter);
309 String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
311 SimpleToscaProvider simpleToscaProvider = new SimpleToscaProvider();
312 final JpaToscaServiceTemplate dbServiceTemplate =
313 simpleToscaProvider.getPolicies(dao, filter.getName(), version);
315 List<ToscaPolicy> filteredPolicies =
316 dbServiceTemplate.getTopologyTemplate().getPolicies().toAuthorativeList();
317 filteredPolicies = filter.filter(filteredPolicies);
319 if (CollectionUtils.isEmpty(filteredPolicies)) {
320 throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
321 "policies for filter " + filter.toString() + " do not exist");
324 JpaToscaServiceTemplate filteredServiceTemplate = new JpaToscaServiceTemplate();
326 for (ToscaPolicy policy : filteredPolicies) {
327 JpaToscaServiceTemplate cascadedServiceTemplate =
328 simpleToscaProvider.getCascadedPolicies(dbServiceTemplate, policy.getName(), policy.getVersion());
330 filteredServiceTemplate =
331 ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate);
334 ToscaServiceTemplate returnServiceTemplate = filteredServiceTemplate.toAuthorative();
336 LOGGER.debug("<-getFilteredPolicies: filter={}, serviceTemplate={}", filter, returnServiceTemplate);
337 return returnServiceTemplate;
342 * Get filtered policies.
344 * @param dao the DAO to use to access the database
345 * @param filter the filter for the policies to get
346 * @return the policies found
347 * @throws PfModelException on errors getting policies
349 public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
350 throws PfModelException {
352 LOGGER.debug("->getFilteredPolicyList: filter={}", filter);
353 String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
355 List<ToscaPolicy> policyList = filter.filter(getPolicyList(dao, filter.getName(), version));
357 LOGGER.debug("<-getFilteredPolicyList: filter={}, policyList={}", filter, policyList);
364 * @param dao the DAO to use to access the database
365 * @param serviceTemplate the service template containing the definitions of the new policies to be created.
366 * @return the TOSCA service template containing the policy types that were created
367 * @throws PfModelException on errors creating policies
369 public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
370 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
372 synchronized (providerLockObject) {
373 LOGGER.debug("->createPolicies: serviceTempalate={}", serviceTemplate);
375 ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
376 .createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
378 LOGGER.debug("<-createPolicies: createdServiceTempalate={}", createdServiceTempalate);
379 return createdServiceTempalate;
386 * @param dao the DAO to use to access the database
387 * @param serviceTemplate the service template containing the definitions of the policies to be updated.
388 * @return the TOSCA service template containing the policies that were updated
389 * @throws PfModelException on errors updating policies
391 public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
392 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
394 synchronized (providerLockObject) {
395 LOGGER.debug("->updatePolicies: serviceTempalate={}", serviceTemplate);
397 ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
398 .updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
400 LOGGER.debug("<-updatePolicies: updatedServiceTempalate={}", updatedServiceTempalate);
401 return updatedServiceTempalate;
408 * @param dao the DAO to use to access the database
409 * @param name the name of the policy to delete.
410 * @param version the version of the policy to delete.
411 * @return the TOSCA service template containing the policy that weas deleted
412 * @throws PfModelException on errors deleting policies
414 public ToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final String name,
415 @NonNull final String version) throws PfModelException {
417 synchronized (providerLockObject) {
418 LOGGER.debug("->deletePolicy: name={}, version={}", name, version);
420 ToscaServiceTemplate deletedServiceTempalate =
421 new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative();
423 LOGGER.debug("<-deletePolicy: name={}, version={}, deletedServiceTempalate={}", name, version,
424 deletedServiceTempalate);
425 return deletedServiceTempalate;
430 * Return the contents of a list of maps as a plain list.
432 * @param listOfMaps the list of maps
433 * @return the plain list
435 private <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) {
436 List<T> returnList = new ArrayList<>();
437 for (Map<String, T> conceptMap : listOfMaps) {
438 for (T concept : conceptMap.values()) {
439 returnList.add(concept);
447 * Handle a PfModelRuntimeException on a list call.
449 * @param pfme the model exception
450 * @return an empty list on 404
452 private <T extends ToscaEntity> List<T> handlePfModelRuntimeException(final PfModelRuntimeException pfme) {
453 if (Status.NOT_FOUND.equals(pfme.getErrorResponse().getResponseCode())) {
454 LOGGER.trace("request did not find any results", pfme);
455 return Collections.emptyList();