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.PfModelRuntimeException;
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
58 @Transactional(readOnly = true)
59 public AutomationComposition getAutomationComposition(final ToscaConceptIdentifier automationCompositionId) {
61 return automationCompositionRepository.getById(automationCompositionId.asConceptKey()).toAuthorative();
62 } catch (EntityNotFoundException e) {
63 throw new PfModelRuntimeException(Status.NOT_FOUND, "AutomationComposition not found", e);
68 * Find automation composition by automationCompositionId.
70 * @param name the name of the automation composition to get, null to get all automation compositions
71 * @param version the version of the automation composition to get, null to get all automation compositions
72 * @return the automation composition found
74 @Transactional(readOnly = true)
75 public Optional<AutomationComposition> findAutomationComposition(@NonNull final String name,
76 @NonNull final String version) {
77 return findAutomationComposition(new PfConceptKey(name, version));
81 * Find automation composition by automationCompositionId.
83 * @param automationCompositionId the ID of the automation composition to get
84 * @return the automation composition found
86 @Transactional(readOnly = true)
87 public Optional<AutomationComposition> findAutomationComposition(
88 final ToscaConceptIdentifier automationCompositionId) {
89 return findAutomationComposition(automationCompositionId.asConceptKey());
92 private Optional<AutomationComposition> findAutomationComposition(@NonNull final PfConceptKey key) {
93 return automationCompositionRepository.findById(key).map(JpaAutomationComposition::toAuthorative);
97 * Save automation composition.
99 * @param automationComposition the automation composition to update
100 * @return the updated automation composition
102 public AutomationComposition saveAutomationComposition(final AutomationComposition automationComposition) {
103 var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
104 JpaAutomationComposition::new, "automation composition"));
106 // Return the saved automation composition
107 return result.toAuthorative();
111 * Get all automation compositions by compositionId.
113 * @param compositionId the compositionId of the automation composition definition
114 * @return all automation compositions found
116 @Transactional(readOnly = true)
117 public List<AutomationComposition> getAcInstancesByCompositionId(UUID compositionId) {
119 .asEntityList(automationCompositionRepository.findByCompositionId(compositionId.toString()));
123 * Get automation compositions.
125 * @param name the name of the automation composition to get, null to get all automation compositions
126 * @param version the version of the automation composition to get, null to get all automation compositions
127 * @return the automation compositions found
129 @Transactional(readOnly = true)
130 public List<AutomationComposition> getAutomationCompositions(final String name, final String version) {
132 return ProviderUtils.asEntityList(
133 automationCompositionRepository.getFiltered(JpaAutomationComposition.class, name, version));
137 * Delete a automation composition.
139 * @param name the name of the automation composition to delete
140 * @param version the version of the automation composition to delete
141 * @return the automation composition deleted
143 public AutomationComposition deleteAutomationComposition(@NonNull final String name,
144 @NonNull final String version) {
146 var automationCompositionKey = new PfConceptKey(name, version);
147 var jpaDeleteAutomationComposition = automationCompositionRepository.findById(automationCompositionKey);
149 if (jpaDeleteAutomationComposition.isEmpty()) {
150 String errorMessage = "delete of automation composition \"" + automationCompositionKey.getId()
151 + "\" failed, automation composition does not exist";
152 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
155 automationCompositionRepository.deleteById(automationCompositionKey);
157 return jpaDeleteAutomationComposition.get().toAuthorative();