2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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 java.util.UUID;
28 import javax.persistence.EntityNotFoundException;
29 import javax.ws.rs.core.Response;
30 import javax.ws.rs.core.Response.Status;
31 import lombok.AllArgsConstructor;
32 import lombok.NonNull;
33 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
34 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationComposition;
35 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionRepository;
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.springframework.stereotype.Service;
40 import org.springframework.transaction.annotation.Transactional;
43 * This class provides information on automation composition concepts in the database to callers.
48 public class AutomationCompositionProvider {
50 private final AutomationCompositionRepository automationCompositionRepository;
53 * Get automation composition.
55 * @param automationCompositionId the ID of the automation composition to get
56 * @return the automation composition found
57 * @throws PfModelException on errors getting the automation composition
59 @Transactional(readOnly = true)
60 public AutomationComposition getAutomationComposition(final ToscaConceptIdentifier automationCompositionId)
61 throws PfModelException {
63 return automationCompositionRepository.getById(automationCompositionId.asConceptKey()).toAuthorative();
64 } catch (EntityNotFoundException e) {
65 throw new PfModelException(Status.NOT_FOUND, "AutomationComposition not found", e);
70 * Find automation composition by automationCompositionId.
72 * @param name the name of the automation composition to get, null to get all automation compositions
73 * @param version the version of the automation composition to get, null to get all automation compositions
74 * @return the automation composition found
75 * @throws PfModelException on errors getting the automation composition
77 @Transactional(readOnly = true)
78 public Optional<AutomationComposition> findAutomationComposition(@NonNull final String name,
79 @NonNull final String version) throws PfModelException {
80 return findAutomationComposition(new PfConceptKey(name, version));
84 * Find automation composition by automationCompositionId.
86 * @param automationCompositionId the ID of the automation composition to get
87 * @return the automation composition found
88 * @throws PfModelException on errors getting the automation composition
90 @Transactional(readOnly = true)
91 public Optional<AutomationComposition> findAutomationComposition(
92 final ToscaConceptIdentifier automationCompositionId) throws PfModelException {
93 return findAutomationComposition(automationCompositionId.asConceptKey());
96 private Optional<AutomationComposition> findAutomationComposition(@NonNull final PfConceptKey key)
97 throws PfModelException {
99 return automationCompositionRepository.findById(key).map(JpaAutomationComposition::toAuthorative);
100 } catch (IllegalArgumentException e) {
101 throw new PfModelException(Status.BAD_REQUEST, "Not valid parameter", e);
106 * Save automation composition.
108 * @param automationComposition the automation composition to update
109 * @return the updated automation composition
110 * @throws PfModelException on errors updating the automation composition
112 public AutomationComposition saveAutomationComposition(final AutomationComposition automationComposition)
113 throws PfModelException {
115 var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
116 JpaAutomationComposition::new, "automation composition"));
118 // Return the saved participant
119 return result.toAuthorative();
120 } catch (IllegalArgumentException e) {
121 throw new PfModelException(Status.BAD_REQUEST, "Error in save automationComposition", e);
126 * Get all automation compositions by compositionId.
128 * @param compositionId the compositionId of the automation composition definition
129 * @return all automation compositions found
131 @Transactional(readOnly = true)
132 public List<AutomationComposition> getAcInstancesByCompositionId(UUID compositionId) {
134 .asEntityList(automationCompositionRepository.findByCompositionId(compositionId.toString()));
138 * Get automation compositions.
140 * @param name the name of the automation composition to get, null to get all automation compositions
141 * @param version the version of the automation composition to get, null to get all automation compositions
142 * @return the automation compositions found
144 @Transactional(readOnly = true)
145 public List<AutomationComposition> getAutomationCompositions(final String name, final String version) {
147 return ProviderUtils.asEntityList(
148 automationCompositionRepository.getFiltered(JpaAutomationComposition.class, name, version));
152 * Saves automation compositions.
154 * @param automationCompositions a specification of the automation compositions to create
155 * @return the automation compositions created
156 * @throws PfModelException on errors creating automation compositions
158 public List<AutomationComposition> saveAutomationCompositions(
159 @NonNull final List<AutomationComposition> automationCompositions) throws PfModelException {
162 automationCompositionRepository.saveAll(ProviderUtils.getJpaAndValidateList(automationCompositions,
163 JpaAutomationComposition::new, "automation compositions"));
165 // Return the saved participant
166 return ProviderUtils.asEntityList(result);
167 } catch (IllegalArgumentException e) {
168 throw new PfModelException(Status.BAD_REQUEST, "Error in save AutomationCompositions", e);
173 * Delete a automation composition.
175 * @param name the name of the automation composition to delete
176 * @param version the version of the automation composition to delete
177 * @return the automation composition deleted
178 * @throws PfModelException on errors deleting the automation composition
180 public AutomationComposition deleteAutomationComposition(@NonNull final String name, @NonNull final String version)
181 throws PfModelException {
183 var automationCompositionKey = new PfConceptKey(name, version);
184 var jpaDeleteAutomationComposition = automationCompositionRepository.findById(automationCompositionKey);
186 if (jpaDeleteAutomationComposition.isEmpty()) {
187 String errorMessage = "delete of automation composition \"" + automationCompositionKey.getId()
188 + "\" failed, automation composition does not exist";
189 throw new PfModelException(Response.Status.NOT_FOUND, errorMessage);
192 automationCompositionRepository.deleteById(automationCompositionKey);
194 return jpaDeleteAutomationComposition.get().toAuthorative();