2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved.
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 jakarta.ws.rs.core.Response;
26 import jakarta.ws.rs.core.Response.Status;
27 import java.util.List;
28 import java.util.Optional;
30 import java.util.UUID;
31 import java.util.stream.Collectors;
32 import lombok.AllArgsConstructor;
33 import lombok.NonNull;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
36 import org.onap.policy.clamp.models.acm.concepts.DeployState;
37 import org.onap.policy.clamp.models.acm.concepts.LockState;
38 import org.onap.policy.clamp.models.acm.concepts.SubState;
39 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationComposition;
40 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionElementRepository;
41 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionRepository;
42 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
43 import org.onap.policy.common.parameters.BeanValidationResult;
44 import org.onap.policy.common.parameters.ValidationStatus;
45 import org.onap.policy.models.base.PfModelRuntimeException;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47 import org.springframework.data.domain.Example;
48 import org.springframework.data.domain.Pageable;
49 import org.springframework.stereotype.Service;
50 import org.springframework.transaction.annotation.Isolation;
51 import org.springframework.transaction.annotation.Transactional;
54 * This class provides information on automation composition concepts in the database to callers.
59 public class AutomationCompositionProvider {
61 private final AutomationCompositionRepository automationCompositionRepository;
62 private final AutomationCompositionElementRepository acElementRepository;
65 * Get automation composition.
67 * @param instanceId the ID of the automation composition to get
68 * @return the automation composition found
70 @Transactional(readOnly = true)
71 public AutomationComposition getAutomationComposition(final UUID instanceId) {
72 var result = automationCompositionRepository.findById(instanceId.toString());
73 if (result.isEmpty()) {
74 throw new PfModelRuntimeException(Status.NOT_FOUND, "AutomationComposition not found");
76 return result.get().toAuthorative();
80 * Find automation composition.
82 * @param instanceId the ID of the automation composition to get
83 * @return the automation composition found
85 @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
86 public Optional<AutomationComposition> findAutomationComposition(final UUID instanceId) {
87 var result = automationCompositionRepository.findById(instanceId.toString());
88 return result.stream().map(JpaAutomationComposition::toAuthorative).findFirst();
92 * Find automation composition by automationCompositionId.
94 * @param automationCompositionId the ID of the automation composition to get
95 * @return the automation composition found
97 @Transactional(readOnly = true)
98 public Optional<AutomationComposition> findAutomationComposition(
99 final ToscaConceptIdentifier automationCompositionId) {
100 return automationCompositionRepository
101 .findOne(createExample(null, automationCompositionId.getName(), automationCompositionId.getVersion()))
102 .map(JpaAutomationComposition::toAuthorative);
106 * Create automation composition.
108 * @param automationComposition the automation composition to create
109 * @return the create automation composition
111 public AutomationComposition createAutomationComposition(final AutomationComposition automationComposition) {
112 automationComposition.setInstanceId(UUID.randomUUID());
113 AcmUtils.setCascadedState(automationComposition, DeployState.UNDEPLOYED, LockState.NONE);
114 var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
115 JpaAutomationComposition::new, "automation composition"));
117 // Return the saved automation composition
118 return result.toAuthorative();
122 * Update automation composition.
124 * @param automationComposition the automation composition to update
125 * @return the updated automation composition
127 public AutomationComposition updateAutomationComposition(
128 @NonNull final AutomationComposition automationComposition) {
129 var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
130 JpaAutomationComposition::new, "automation composition"));
131 automationCompositionRepository.flush();
132 // Return the saved automation composition
133 return result.toAuthorative();
137 * Get all automation compositions by compositionId.
139 * @param compositionId the compositionId of the automation composition definition
140 * @return all automation compositions found
142 @Transactional(readOnly = true)
143 public List<AutomationComposition> getAcInstancesByCompositionId(UUID compositionId) {
145 .asEntityList(automationCompositionRepository.findByCompositionId(compositionId.toString()));
149 * Get all automation compositions in transition..
151 * @return all automation compositions found
153 @Transactional(readOnly = true)
154 public Set<UUID> getAcInstancesInTransition() {
155 var jpaList = automationCompositionRepository.findByDeployStateIn(List.of(DeployState.DEPLOYING,
156 DeployState.UNDEPLOYING, DeployState.DELETING, DeployState.UPDATING, DeployState.MIGRATING));
157 jpaList.addAll(automationCompositionRepository.findByLockStateIn(
158 List.of(LockState.LOCKING, LockState.UNLOCKING)));
159 jpaList.addAll(automationCompositionRepository.findBySubStateIn(
160 List.of(SubState.PREPARING, SubState.MIGRATION_PRECHECKING, SubState.REVIEWING)));
161 return jpaList.stream().map(JpaAutomationComposition::getInstanceId)
162 .map(UUID::fromString).collect(Collectors.toSet());
166 * Get automation compositions.
168 * @param name the name of the automation composition to get, null to get all automation compositions
169 * @param version the version of the automation composition to get, null to get all automation compositions
170 * @param pageable the Pageable
171 * @return the automation compositions found
173 @Transactional(readOnly = true)
174 public List<AutomationComposition> getAutomationCompositions(@NonNull final UUID compositionId, final String name,
175 final String version, @NonNull final Pageable pageable) {
176 return ProviderUtils.asEntityList(automationCompositionRepository
177 .findAll(createExample(compositionId, name, version), pageable).toList());
180 private Example<JpaAutomationComposition> createExample(final UUID compositionId, final String name,
181 final String version) {
182 var example = new JpaAutomationComposition();
183 example.setCompositionId(compositionId != null ? compositionId.toString() : null);
184 example.setName(name);
185 example.setVersion(version);
186 example.setInstanceId(null);
187 example.setElements(null);
188 example.setDeployState(null);
189 example.setLockState(null);
191 return Example.of(example);
195 * Delete a automation composition.
197 * @param instanceId the ID of the automation composition to get
198 * @return the automation composition deleted
200 public AutomationComposition deleteAutomationComposition(@NonNull final UUID instanceId) {
201 var jpaDeleteAutomationComposition = automationCompositionRepository.findById(instanceId.toString());
202 if (jpaDeleteAutomationComposition.isEmpty()) {
203 var errorMessage = "delete of automation composition \"" + instanceId
204 + "\" failed, automation composition does not exist";
205 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
208 automationCompositionRepository.deleteById(instanceId.toString());
210 return jpaDeleteAutomationComposition.get().toAuthorative();
214 * Delete AutomationCompositionElement.
216 * @param elementId the AutomationCompositionElement Id
218 public void deleteAutomationCompositionElement(@NonNull final UUID elementId) {
219 acElementRepository.deleteById(elementId.toString());
223 * Validate ElementIds.
225 * @param automationComposition the AutomationComposition
226 * @return the BeanValidationResult
228 public BeanValidationResult validateElementIds(final AutomationComposition automationComposition) {
229 var result = new BeanValidationResult(
230 "UUID elements " + automationComposition.getName(), automationComposition);
232 var ids = automationComposition
233 .getElements().values().stream().map(AutomationCompositionElement::getId).toList();
234 var elements = acElementRepository.findAllById(ids.stream().map(UUID::toString).toList());
235 if (automationComposition.getInstanceId() == null) {
236 for (var element : elements) {
238 element.getDescription(), element.getElementId(), ValidationStatus.INVALID, "UUID already used");
241 var instanceId = automationComposition.getInstanceId().toString();
242 for (var element : elements) {
243 if (!instanceId.equals(element.getInstanceId())) {
245 element.getDescription(), element.getElementId(), ValidationStatus.INVALID,
246 "UUID already used");