2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.clamp.models.acm.persistence.provider;
25 import java.util.List;
26 import java.util.Optional;
27 import javax.persistence.EntityNotFoundException;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.Response.Status;
30 import lombok.AllArgsConstructor;
31 import lombok.NonNull;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
33 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationComposition;
34 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionRepository;
35 import org.onap.policy.clamp.models.acm.persistence.repository.ToscaNodeTemplateRepository;
36 import org.onap.policy.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfModelException;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;
42 import org.springframework.stereotype.Service;
43 import org.springframework.transaction.annotation.Transactional;
46 * This class provides information on automation composition concepts in the database to callers.
51 public class AutomationCompositionProvider {
53 private final AutomationCompositionRepository automationCompositionRepository;
54 private final ToscaNodeTemplateRepository toscaNodeTemplateRepository;
57 * Get automation composition.
59 * @param automationCompositionId the ID of the automation composition to get
60 * @return the automation composition found
61 * @throws PfModelException on errors getting the automation composition
63 @Transactional(readOnly = true)
64 public AutomationComposition getAutomationComposition(final ToscaConceptIdentifier automationCompositionId)
65 throws PfModelException {
67 return automationCompositionRepository.getById(automationCompositionId.asConceptKey()).toAuthorative();
68 } catch (EntityNotFoundException e) {
69 throw new PfModelException(Status.NOT_FOUND, "AutomationComposition not found", e);
74 * Find automation composition by automationCompositionId.
76 * @param name the name of the automation composition to get, null to get all automation compositions
77 * @param version the version of the automation composition to get, null to get all automation compositions
78 * @return the automation composition found
79 * @throws PfModelException on errors getting the automation composition
81 @Transactional(readOnly = true)
82 public Optional<AutomationComposition> findAutomationComposition(@NonNull final String name,
83 @NonNull final String version) throws PfModelException {
84 return findAutomationComposition(new PfConceptKey(name, version));
88 * Find automation composition by automationCompositionId.
90 * @param automationCompositionId the ID of the automation composition to get
91 * @return the automation composition found
92 * @throws PfModelException on errors getting the automation composition
94 @Transactional(readOnly = true)
95 public Optional<AutomationComposition> findAutomationComposition(
96 final ToscaConceptIdentifier automationCompositionId) throws PfModelException {
97 return findAutomationComposition(automationCompositionId.asConceptKey());
100 private Optional<AutomationComposition> findAutomationComposition(@NonNull final PfConceptKey key)
101 throws PfModelException {
103 return automationCompositionRepository.findById(key).map(JpaAutomationComposition::toAuthorative);
104 } catch (IllegalArgumentException e) {
105 throw new PfModelException(Status.BAD_REQUEST, "Not valid parameter", e);
110 * Save automation composition.
112 * @param automationComposition the automation composition to update
113 * @return the updated automation composition
114 * @throws PfModelException on errors updating the automation composition
116 public AutomationComposition saveAutomationComposition(final AutomationComposition automationComposition)
117 throws PfModelException {
119 var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
120 JpaAutomationComposition::new, "automation composition"));
122 // Return the saved participant
123 return result.toAuthorative();
124 } catch (IllegalArgumentException e) {
125 throw new PfModelException(Status.BAD_REQUEST, "Error in save automationComposition", e);
130 * Get all automation compositions.
132 * @return all automation compositions found
134 @Transactional(readOnly = true)
135 public List<AutomationComposition> getAutomationCompositions() {
137 return ProviderUtils.asEntityList(automationCompositionRepository.findAll());
141 * Get automation compositions.
143 * @param name the name of the automation composition to get, null to get all automation compositions
144 * @param version the version of the automation composition to get, null to get all automation compositions
145 * @return the automation compositions found
147 @Transactional(readOnly = true)
148 public List<AutomationComposition> getAutomationCompositions(final String name, final String version) {
151 .asEntityList(automationCompositionRepository.getFiltered(JpaAutomationComposition.class, name, version));
155 * Saves automation compositions.
157 * @param automationCompositions a specification of the automation compositions to create
158 * @return the automation compositions created
159 * @throws PfModelException on errors creating automation compositions
161 public List<AutomationComposition> saveAutomationCompositions(
162 @NonNull final List<AutomationComposition> automationCompositions) throws PfModelException {
165 automationCompositionRepository.saveAll(ProviderUtils.getJpaAndValidateList(automationCompositions,
166 JpaAutomationComposition::new, "automation compositions"));
168 // Return the saved participant
169 return ProviderUtils.asEntityList(result);
170 } catch (IllegalArgumentException e) {
171 throw new PfModelException(Status.BAD_REQUEST, "Error in save AutomationCompositions", e);
176 * Delete a automation composition.
178 * @param name the name of the automation composition to delete
179 * @param version the version of the automation composition to delete
180 * @return the automation composition deleted
181 * @throws PfModelException on errors deleting the automation composition
183 public AutomationComposition deleteAutomationComposition(@NonNull final String name, @NonNull final String version)
184 throws PfModelException {
186 var automationCompositionKey = new PfConceptKey(name, version);
187 var jpaDeleteAutomationComposition = automationCompositionRepository.findById(automationCompositionKey);
189 if (jpaDeleteAutomationComposition.isEmpty()) {
190 String errorMessage = "delete of automation composition \"" + automationCompositionKey.getId()
191 + "\" failed, automation composition does not exist";
192 throw new PfModelException(Response.Status.BAD_REQUEST, errorMessage);
195 automationCompositionRepository.deleteById(automationCompositionKey);
197 return jpaDeleteAutomationComposition.get().toAuthorative();
201 * Get All Node Templates.
203 * @return the list of node templates found
205 @Transactional(readOnly = true)
206 public List<ToscaNodeTemplate> getAllNodeTemplates() {
207 return ProviderUtils.asEntityList(toscaNodeTemplateRepository.findAll());
211 * Get Node Templates.
213 * @param name the name of the node template to get, null to get all node templates
214 * @param version the version of the node template to get, null to get all node templates
215 * @return the node templates found
216 * @throws PfModelException on errors getting node templates
218 @Transactional(readOnly = true)
219 public List<ToscaNodeTemplate> getNodeTemplates(final String name, final String version) {
221 .asEntityList(toscaNodeTemplateRepository.getFiltered(JpaToscaNodeTemplate.class, name, version));
225 * Get filtered node templates.
227 * @param filter the filter for the node templates to get
228 * @return the node templates found
229 * @throws PfModelException on errors getting node templates
231 @Transactional(readOnly = true)
232 public List<ToscaNodeTemplate> getFilteredNodeTemplates(
233 @NonNull final ToscaTypedEntityFilter<ToscaNodeTemplate> filter) {
235 return filter.filter(ProviderUtils.asEntityList(toscaNodeTemplateRepository
236 .getFiltered(JpaToscaNodeTemplate.class, filter.getName(), filter.getVersion())));