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<>(new SimpleToscaProvider().getPolicyTypes(dao, name, version)
99 .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 =
125 new ArrayList<>(dbServiceTemplate.toAuthorative().getPolicyTypes().values());
126 filteredPolicyTypes = filter.filter(filteredPolicyTypes);
128 if (CollectionUtils.isEmpty(filteredPolicyTypes)) {
129 throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
130 "policy types for filter " + filter.toString() + " do not exist");
133 JpaToscaServiceTemplate filteredServiceTemplate = new JpaToscaServiceTemplate();
135 for (ToscaPolicyType policyType : filteredPolicyTypes) {
136 JpaToscaServiceTemplate cascadedServiceTemplate = simpleToscaProvider
137 .getCascadedPolicyTypes(dbServiceTemplate, policyType.getName(), policyType.getVersion());
139 filteredServiceTemplate =
140 ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate);
143 ToscaServiceTemplate returnServiceTemplate = filteredServiceTemplate.toAuthorative();
145 LOGGER.debug("<-getFilteredPolicyTypes: filter={}, serviceTemplate={}", filter, returnServiceTemplate);
146 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 =
297 asConceptList(dbServiceTemplate.toAuthorative().getToscaTopologyTemplate().getPolicies());
298 filteredPolicies = filter.filter(filteredPolicies);
300 if (CollectionUtils.isEmpty(filteredPolicies)) {
301 throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
302 "policies for filter " + filter.toString() + " do not exist");
305 JpaToscaServiceTemplate filteredServiceTemplate = new JpaToscaServiceTemplate();
307 for (ToscaPolicy policy : filteredPolicies) {
308 JpaToscaServiceTemplate cascadedServiceTemplate =
309 simpleToscaProvider.getCascadedPolicies(dbServiceTemplate, policy.getName(), policy.getVersion());
311 filteredServiceTemplate =
312 ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate);
315 ToscaServiceTemplate returnServiceTemplate = filteredServiceTemplate.toAuthorative();
317 LOGGER.debug("<-getFilteredPolicies: filter={}, serviceTemplate={}", filter, returnServiceTemplate);
318 return returnServiceTemplate;
322 * Get filtered policies.
324 * @param dao the DAO to use to access the database
325 * @param filter the filter for the policies to get
326 * @return the policies found
327 * @throws PfModelException on errors getting policies
329 public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
330 throws PfModelException {
332 LOGGER.debug("->getFilteredPolicyList: filter={}", filter);
333 String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
335 List<ToscaPolicy> policyList = filter.filter(getPolicyList(dao, filter.getName(), version));
337 LOGGER.debug("<-getFilteredPolicyList: filter={}, policyList={}", filter, policyList);
344 * @param dao the DAO to use to access the database
345 * @param serviceTemplate the service template containing the definitions of the new policies to be created.
346 * @return the TOSCA service template containing the policy types that were created
347 * @throws PfModelException on errors creating policies
349 public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
350 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
352 LOGGER.debug("->createPolicies: serviceTempalate={}", serviceTemplate);
354 ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
355 .createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
357 LOGGER.debug("<-createPolicies: createdServiceTempalate={}", createdServiceTempalate);
358 return createdServiceTempalate;
364 * @param dao the DAO to use to access the database
365 * @param serviceTemplate the service template containing the definitions of the policies to be updated.
366 * @return the TOSCA service template containing the policies that were updated
367 * @throws PfModelException on errors updating policies
369 public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
370 @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
372 LOGGER.debug("->updatePolicies: serviceTempalate={}", serviceTemplate);
374 ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
375 .updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
377 LOGGER.debug("<-updatePolicies: updatedServiceTempalate={}", updatedServiceTempalate);
378 return updatedServiceTempalate;
384 * @param dao the DAO to use to access the database
385 * @param name the name of the policy to delete.
386 * @param version the version of the policy to delete.
387 * @return the TOSCA service template containing the policy that weas deleted
388 * @throws PfModelException on errors deleting policies
390 public ToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final String name,
391 @NonNull final String version) throws PfModelException {
393 LOGGER.debug("->deletePolicy: name={}, version={}", name, version);
395 ToscaServiceTemplate deletedServiceTempalate =
396 new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative();
398 LOGGER.debug("<-deletePolicy: name={}, version={}, deletedServiceTempalate={}", name, version,
399 deletedServiceTempalate);
400 return deletedServiceTempalate;
404 * Return the contents of a list of maps as a plain list.
406 * @param listOfMaps the list of maps
407 * @return the plain list
409 private <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) {
410 List<T> returnList = new ArrayList<>();
411 for (Map<String, T> conceptMap : listOfMaps) {
412 for (T concept : conceptMap.values()) {
413 returnList.add(concept);
421 * Handle a PfModelRuntimeException on a list call.
423 * @param pfme the model exception
424 * @return an empty list on 404
426 private <T extends ToscaEntity> List<T> handlePfModelRuntimeException(final PfModelRuntimeException pfme) {
427 if (Status.NOT_FOUND.equals(pfme.getErrorResponse().getResponseCode())) {
428 LOGGER.trace("request did not find any results", pfme);
429 return Collections.emptyList();