2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2024 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 jakarta.ws.rs.core.Response;
26 import jakarta.ws.rs.core.Response.Status;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Optional;
30 import java.util.UUID;
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.concepts.AutomationCompositionElement;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionInfo;
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.persistence.concepts.JpaAutomationComposition;
39 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationCompositionElement;
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.stereotype.Service;
49 import org.springframework.transaction.annotation.Isolation;
50 import org.springframework.transaction.annotation.Transactional;
53 * This class provides information on automation composition concepts in the database to callers.
58 public class AutomationCompositionProvider {
60 private final AutomationCompositionRepository automationCompositionRepository;
61 private final AutomationCompositionElementRepository acElementRepository;
64 * Get automation composition.
66 * @param instanceId the ID of the automation composition to get
67 * @return the automation composition found
69 @Transactional(readOnly = true)
70 public AutomationComposition getAutomationComposition(final UUID instanceId) {
71 var result = automationCompositionRepository.findById(instanceId.toString());
72 if (result.isEmpty()) {
73 throw new PfModelRuntimeException(Status.NOT_FOUND, "AutomationComposition not found");
75 return result.get().toAuthorative();
79 * Find automation composition.
81 * @param instanceId the ID of the automation composition to get
82 * @return the automation composition found
84 @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
85 public Optional<AutomationComposition> findAutomationComposition(final UUID instanceId) {
86 var result = automationCompositionRepository.findById(instanceId.toString());
87 return result.stream().map(JpaAutomationComposition::toAuthorative).findFirst();
91 * Find automation composition by automationCompositionId.
93 * @param automationCompositionId the ID of the automation composition to get
94 * @return the automation composition found
96 @Transactional(readOnly = true)
97 public Optional<AutomationComposition> findAutomationComposition(
98 final ToscaConceptIdentifier automationCompositionId) {
99 return automationCompositionRepository
100 .findOne(createExample(null, automationCompositionId.getName(), automationCompositionId.getVersion()))
101 .map(JpaAutomationComposition::toAuthorative);
105 * Create automation composition.
107 * @param automationComposition the automation composition to create
108 * @return the create automation composition
110 public AutomationComposition createAutomationComposition(final AutomationComposition automationComposition) {
111 automationComposition.setInstanceId(UUID.randomUUID());
112 AcmUtils.setCascadedState(automationComposition, DeployState.UNDEPLOYED, LockState.NONE);
113 var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
114 JpaAutomationComposition::new, "automation composition"));
116 // Return the saved automation composition
117 return result.toAuthorative();
122 * Update automation composition state.
124 * @param acSource the automation composition to update
125 * @return the updated automation composition
127 public AutomationComposition updateAcState(final AutomationComposition acSource) {
128 var automationComposition = automationCompositionRepository
129 .getReferenceById(acSource.getInstanceId().toString());
130 automationComposition.fromAuthorativeBase(acSource);
131 var result = automationCompositionRepository.save(automationComposition);
132 automationCompositionRepository.flush();
133 // Return the saved automation composition
134 return result.toAuthorative();
138 * Update automation composition.
140 * @param automationComposition the automation composition to update
141 * @return the updated automation composition
143 public AutomationComposition updateAutomationComposition(
144 @NonNull final AutomationComposition automationComposition) {
145 var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
146 JpaAutomationComposition::new, "automation composition"));
147 automationCompositionRepository.flush();
148 // Return the saved automation composition
149 return result.toAuthorative();
153 * Get all automation compositions by compositionId.
155 * @param compositionId the compositionId of the automation composition definition
156 * @return all automation compositions found
158 @Transactional(readOnly = true)
159 public List<AutomationComposition> getAcInstancesByCompositionId(UUID compositionId) {
161 .asEntityList(automationCompositionRepository.findByCompositionId(compositionId.toString()));
165 * Get all automation compositions in transition..
167 * @return all automation compositions found
169 @Transactional(readOnly = true)
170 public List<AutomationComposition> getAcInstancesInTransition() {
171 var jpaList = automationCompositionRepository.findByDeployStateIn(List.of(DeployState.DEPLOYING,
172 DeployState.UNDEPLOYING, DeployState.DELETING, DeployState.UPDATING, DeployState.MIGRATING));
173 jpaList.addAll(automationCompositionRepository.findByLockStateIn(
174 List.of(LockState.LOCKING, LockState.UNLOCKING)));
175 return ProviderUtils.asEntityList(jpaList);
179 * Get automation compositions.
181 * @param name the name of the automation composition to get, null to get all automation compositions
182 * @param version the version of the automation composition to get, null to get all automation compositions
183 * @return the automation compositions found
185 @Transactional(readOnly = true)
186 public List<AutomationComposition> getAutomationCompositions(final UUID compositionId, final String name,
187 final String version) {
190 .asEntityList(automationCompositionRepository.findAll(createExample(compositionId, name, version)));
193 private Example<JpaAutomationComposition> createExample(final UUID compositionId, final String name,
194 final String version) {
195 var example = new JpaAutomationComposition();
196 example.setCompositionId(compositionId != null ? compositionId.toString() : null);
197 example.setName(name);
198 example.setVersion(version);
199 example.setInstanceId(null);
200 example.setElements(null);
201 example.setDeployState(null);
202 example.setLockState(null);
204 return Example.of(example);
208 * Delete a automation composition.
210 * @param instanceId the ID of the automation composition to get
211 * @return the automation composition deleted
213 public AutomationComposition deleteAutomationComposition(@NonNull final UUID instanceId) {
214 var jpaDeleteAutomationComposition = automationCompositionRepository.findById(instanceId.toString());
215 if (jpaDeleteAutomationComposition.isEmpty()) {
216 var errorMessage = "delete of automation composition \"" + instanceId
217 + "\" failed, automation composition does not exist";
218 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
221 automationCompositionRepository.deleteById(instanceId.toString());
223 return jpaDeleteAutomationComposition.get().toAuthorative();
229 * @param automationCompositionInfoList list of AutomationCompositionInfo
231 public void upgradeStates(@NonNull final List<AutomationCompositionInfo> automationCompositionInfoList) {
232 if (automationCompositionInfoList.isEmpty()) {
235 List<JpaAutomationCompositionElement> jpaList = new ArrayList<>();
236 for (var acInstance : automationCompositionInfoList) {
237 for (var element : acInstance.getElements()) {
238 var jpa = acElementRepository.getReferenceById(element.getAutomationCompositionElementId().toString());
239 jpa.setUseState(element.getUseState());
240 jpa.setOperationalState(element.getOperationalState());
241 jpa.setOutProperties(element.getOutProperties());
245 acElementRepository.saveAll(jpaList);
249 * Update AutomationCompositionElement.
251 * @param element the AutomationCompositionElement
253 public void updateAutomationCompositionElement(@NonNull final AutomationCompositionElement element) {
254 var jpaAcElement = acElementRepository.getReferenceById(element.getId().toString());
255 jpaAcElement.setMessage(element.getMessage());
256 jpaAcElement.setOutProperties(element.getOutProperties());
257 jpaAcElement.setOperationalState(element.getOperationalState());
258 jpaAcElement.setUseState(element.getUseState());
259 jpaAcElement.setDeployState(element.getDeployState());
260 jpaAcElement.setLockState(element.getLockState());
261 jpaAcElement.setRestarting(element.getRestarting());
263 ProviderUtils.validate(element, jpaAcElement, "AutomationCompositionElement");
264 acElementRepository.save(jpaAcElement);
268 * Validate ElementIds.
270 * @param automationComposition the AutomationComposition
271 * @return the BeanValidationResult
273 public BeanValidationResult validateElementIds(final AutomationComposition automationComposition) {
274 var result = new BeanValidationResult(
275 "UUID elements " + automationComposition.getName(), automationComposition);
277 var ids = automationComposition
278 .getElements().values().stream().map(AutomationCompositionElement::getId).toList();
279 var elements = acElementRepository.findAllById(ids.stream().map(UUID::toString).toList());
280 if (automationComposition.getInstanceId() == null) {
281 for (var element : elements) {
283 element.getDescription(), element.getElementId(), ValidationStatus.INVALID, "UUID already used");
286 var instanceId = automationComposition.getInstanceId().toString();
287 for (var element : elements) {
288 if (!instanceId.equals(element.getInstanceId())) {
290 element.getDescription(), element.getElementId(), ValidationStatus.INVALID,
291 "UUID already used");