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);
59 // TODO: In next release this locking mechanism should be removed and replaced with proper session handling
60 private static final Object providerLockObject = "providerLockObject";
65 * @param dao the DAO to use to access the database
66 * @param name the name of the policy type to get.
67 * @param version the version of the policy type to get.
68 * @return the policy types found
69 * @throws PfModelException on errors getting policy types
71 public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
72 throws PfModelException {
74 synchronized (providerLockObject) {
75 LOGGER.debug("->getPolicyTypes: name={}, version={}", name, version);
77 JpaToscaServiceTemplate jpaServiceTemplate = new SimpleToscaProvider().getPolicyTypes(dao, name, version);
79 ToscaServiceTemplate serviceTemplate = jpaServiceTemplate.toAuthorative();
81 LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate);
82 return serviceTemplate;
89 * @param dao the DAO to use to access the database
90 * @param name the name of the policy type to get, set to null to get all policy types
91 * @param version the version of the policy type to get, set to null to get all versions
92 * @return the policy types found
93 * @throws PfModelException on errors getting policy types
95 public List<ToscaPolicyType> getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version)
96 throws PfModelException {
98 synchronized (providerLockObject) {
99 LOGGER.debug("->getPolicyTypeList: name={}, version={}", name, version);
101 List<ToscaPolicyType> policyTypeList;
104 policyTypeList = new ArrayList<>(new SimpleToscaProvider().getPolicyTypes(dao, name, version)
105 .toAuthorative().getPolicyTypes().values());
106 } catch (PfModelRuntimeException pfme) {
107 return handlePfModelRuntimeException(pfme);
110 LOGGER.debug("<-getPolicyTypeList: name={}, version={}, policyTypeList={}", name, version, policyTypeList);
111 return policyTypeList;
116 * Get filtered policy types.
118 * @param dao the DAO to use to access the database
119 * @param filter the filter for the policy types to get
120 * @return the policy types found
121 * @throws PfModelException on errors getting policy types
123 public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao,
124 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
126 synchronized (providerLockObject) {
127 LOGGER.debug("->getFilteredPolicyTypes: filter={}", filter);
128 SimpleToscaProvider simpleToscaProvider = new SimpleToscaProvider();
130 final JpaToscaServiceTemplate dbServiceTemplate = simpleToscaProvider.getPolicyTypes(dao, null, null);
132 List<ToscaPolicyType> filteredPolicyTypes = dbServiceTemplate.getPolicyTypes().toAuthorativeList();
133 filteredPolicyTypes = filter.filter(filteredPolicyTypes);
135 if (CollectionUtils.isEmpty(filteredPolicyTypes)) {
136 throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
137 "policy types for filter " + filter.toString() + " do not exist");
140 JpaToscaServiceTemplate filteredServiceTemplate = new JpaToscaServiceTemplate();
142 for (ToscaPolicyType policyType : filteredPolicyTypes) {
143 JpaToscaServiceTemplate cascadedServiceTemplate = simpleToscaProvider
144 .getCascadedPolicyTypes(dbServiceTemplate, policyType.getName(), policyType.getVersion());
146 filteredServiceTemplate =
147 ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate);
150 ToscaServiceTemplate returnServiceTemplate = filteredServiceTemplate.toAuthorative();
152 LOGGER.debug("<-getFilteredPolicyTypes: filter={}, serviceTemplate={}", filter, returnServiceTemplate);
153 return returnServiceTemplate;
158 * Get filtered policy types.
160 * @param dao the DAO to use to access the database
161 * @param filter the filter for the policy types to get
162 * @return the policy types found
163 * @throws PfModelException on errors getting policy types
165 public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull final PfDao dao,
166 @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
168 LOGGER.debug("->getFilteredPolicyTypeList: filter={}", filter);
170 List<ToscaPolicyType> filteredPolicyTypeList = filter.filter(getPolicyTypeList(dao, null, null));
172 LOGGER.debug("<-getFilteredPolicyTypeList: filter={}, filteredPolicyTypeList={}", filter,
173 filteredPolicyTypeList);
175 return filteredPolicyTypeList;
179 * Create policy types.
181 * @param dao the DAO to use to access the database
182 * @param serviceTemplate the service template containing the definition of the policy types to be created
183 * @return the TOSCA service template containing the created policy types
184 * @throws PfModelException on errors creating policy types
186 public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
187 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
189 synchronized (providerLockObject) {
190 LOGGER.debug("->createPolicyTypes: serviceTemplate={}", serviceTemplate);
192 ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
193 .createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
195 LOGGER.debug("<-createPolicyTypes: createdServiceTempalate={}", createdServiceTempalate);
196 return createdServiceTempalate;
201 * Update policy types.
203 * @param dao the DAO to use to access the database
204 * @param serviceTemplate the service template containing the definition of the policy types to be modified
205 * @return the TOSCA service template containing the modified policy types
206 * @throws PfModelException on errors updating policy types
208 public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
209 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
211 synchronized (providerLockObject) {
212 LOGGER.debug("->updatePolicyTypes: serviceTempalate={}", serviceTemplate);
214 ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
215 .updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
217 LOGGER.debug("<-updatePolicyTypes: updatedServiceTempalate={}", updatedServiceTempalate);
218 return updatedServiceTempalate;
223 * Delete policy type.
225 * @param dao the DAO to use to access the database
226 * @param name the name of the policy type to delete.
227 * @param version the version of the policy type to delete.
228 * @return the TOSCA service template containing the policy type that was deleted
229 * @throws PfModelException on errors deleting policy types
231 public ToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final String name,
232 @NonNull final String version) throws PfModelException {
234 synchronized (providerLockObject) {
235 LOGGER.debug("->deletePolicyType: name={}, version={}", name, version);
237 ToscaServiceTemplate deletedServiceTempalate =
238 new SimpleToscaProvider().deletePolicyType(dao, new PfConceptKey(name, version)).toAuthorative();
240 LOGGER.debug("<-deletePolicyType: name={}, version={}, deletedServiceTempalate={}", name, version,
241 deletedServiceTempalate);
242 return deletedServiceTempalate;
249 * @param dao the DAO to use to access the database
250 * @param name the name of the policy to get.
251 * @param version the version of the policy to get.
252 * @return the policies found
253 * @throws PfModelException on errors getting policies
255 public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
256 throws PfModelException {
258 synchronized (providerLockObject) {
259 LOGGER.debug("->getPolicies: name={}, version={}", name, version);
261 ToscaServiceTemplate gotServiceTempalate =
262 new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative();
264 LOGGER.debug("<-getPolicies: name={}, version={}, gotServiceTempalate={}", name, version,
265 gotServiceTempalate);
266 return gotServiceTempalate;
273 * @param dao the DAO to use to access the database
274 * @param name the name of the policy to get, null to get all policies
275 * @param version the version of the policy to get, null to get all versions of a policy
276 * @return the policies found
277 * @throws PfModelException on errors getting policies
279 public List<ToscaPolicy> getPolicyList(@NonNull final PfDao dao, final String name, final String version)
280 throws PfModelException {
282 synchronized (providerLockObject) {
283 LOGGER.debug("->getPolicyList: name={}, version={}", name, version);
285 List<ToscaPolicy> policyList;
288 policyList = asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative()
289 .getToscaTopologyTemplate().getPolicies());
290 } catch (PfModelRuntimeException pfme) {
291 return handlePfModelRuntimeException(pfme);
294 LOGGER.debug("<-getPolicyList: name={}, version={}, policyTypeList={}", name, version, policyList);
300 * Get filtered policies.
302 * @param dao the DAO to use to access the database
303 * @param filter the filter for the policies to get
304 * @return the policies found
305 * @throws PfModelException on errors getting policies
307 public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
308 throws PfModelException {
310 synchronized (providerLockObject) {
311 LOGGER.debug("->getFilteredPolicies: filter={}", filter);
312 String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
314 SimpleToscaProvider simpleToscaProvider = new SimpleToscaProvider();
315 final JpaToscaServiceTemplate dbServiceTemplate =
316 simpleToscaProvider.getPolicies(dao, filter.getName(), version);
318 List<ToscaPolicy> filteredPolicies =
319 dbServiceTemplate.getTopologyTemplate().getPolicies().toAuthorativeList();
320 filteredPolicies = filter.filter(filteredPolicies);
322 if (CollectionUtils.isEmpty(filteredPolicies)) {
323 throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
324 "policies for filter " + filter.toString() + " do not exist");
327 JpaToscaServiceTemplate filteredServiceTemplate = new JpaToscaServiceTemplate();
329 for (ToscaPolicy policy : filteredPolicies) {
330 JpaToscaServiceTemplate cascadedServiceTemplate =
331 simpleToscaProvider.getCascadedPolicies(dbServiceTemplate, policy.getName(), policy.getVersion());
333 filteredServiceTemplate =
334 ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate);
337 ToscaServiceTemplate returnServiceTemplate = filteredServiceTemplate.toAuthorative();
339 LOGGER.debug("<-getFilteredPolicies: filter={}, serviceTemplate={}", filter, returnServiceTemplate);
340 return returnServiceTemplate;
345 * Get filtered policies.
347 * @param dao the DAO to use to access the database
348 * @param filter the filter for the policies to get
349 * @return the policies found
350 * @throws PfModelException on errors getting policies
352 public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
353 throws PfModelException {
355 LOGGER.debug("->getFilteredPolicyList: filter={}", filter);
356 String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
358 List<ToscaPolicy> policyList = filter.filter(getPolicyList(dao, filter.getName(), version));
360 LOGGER.debug("<-getFilteredPolicyList: filter={}, policyList={}", filter, policyList);
367 * @param dao the DAO to use to access the database
368 * @param serviceTemplate the service template containing the definitions of the new policies to be created.
369 * @return the TOSCA service template containing the policy types that were created
370 * @throws PfModelException on errors creating policies
372 public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
373 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
375 synchronized (providerLockObject) {
376 LOGGER.debug("->createPolicies: serviceTempalate={}", serviceTemplate);
378 ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
379 .createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
381 LOGGER.debug("<-createPolicies: createdServiceTempalate={}", createdServiceTempalate);
382 return createdServiceTempalate;
389 * @param dao the DAO to use to access the database
390 * @param serviceTemplate the service template containing the definitions of the policies to be updated.
391 * @return the TOSCA service template containing the policies that were updated
392 * @throws PfModelException on errors updating policies
394 public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
395 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
397 synchronized (providerLockObject) {
398 LOGGER.debug("->updatePolicies: serviceTempalate={}", serviceTemplate);
400 ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
401 .updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
403 LOGGER.debug("<-updatePolicies: updatedServiceTempalate={}", updatedServiceTempalate);
404 return updatedServiceTempalate;
411 * @param dao the DAO to use to access the database
412 * @param name the name of the policy to delete.
413 * @param version the version of the policy to delete.
414 * @return the TOSCA service template containing the policy that weas deleted
415 * @throws PfModelException on errors deleting policies
417 public ToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final String name,
418 @NonNull final String version) throws PfModelException {
420 synchronized (providerLockObject) {
421 LOGGER.debug("->deletePolicy: name={}, version={}", name, version);
423 ToscaServiceTemplate deletedServiceTempalate =
424 new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative();
426 LOGGER.debug("<-deletePolicy: name={}, version={}, deletedServiceTempalate={}", name, version,
427 deletedServiceTempalate);
428 return deletedServiceTempalate;
433 * Return the contents of a list of maps as a plain list.
435 * @param listOfMaps the list of maps
436 * @return the plain list
438 private <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) {
439 List<T> returnList = new ArrayList<>();
440 for (Map<String, T> conceptMap : listOfMaps) {
441 for (T concept : conceptMap.values()) {
442 returnList.add(concept);
450 * Handle a PfModelRuntimeException on a list call.
452 * @param pfme the model exception
453 * @return an empty list on 404
455 private <T extends ToscaEntity> List<T> handlePfModelRuntimeException(final PfModelRuntimeException pfme) {
456 if (Status.NOT_FOUND.equals(pfme.getErrorResponse().getResponseCode())) {
457 LOGGER.trace("request did not find any results", pfme);
458 return Collections.emptyList();