3bfcf72edf719af4b2a26400a0e9f321454a4402
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2025 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.clamp.models.acm.persistence.provider;
24
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;
29 import java.util.Set;
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.stereotype.Service;
49 import org.springframework.transaction.annotation.Isolation;
50 import org.springframework.transaction.annotation.Transactional;
51
52 /**
53  * This class provides information on automation composition concepts in the database to callers.
54  */
55 @Service
56 @Transactional
57 @AllArgsConstructor
58 public class AutomationCompositionProvider {
59
60     private final AutomationCompositionRepository automationCompositionRepository;
61     private final AutomationCompositionElementRepository acElementRepository;
62
63     /**
64      * Get automation composition.
65      *
66      * @param instanceId the ID of the automation composition to get
67      * @return the automation composition found
68      */
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");
74         }
75         return result.get().toAuthorative();
76     }
77
78     /**
79      * Find automation composition.
80      *
81      * @param instanceId the ID of the automation composition to get
82      * @return the automation composition found
83      */
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();
88     }
89
90     /**
91      * Find automation composition by automationCompositionId.
92      *
93      * @param automationCompositionId the ID of the automation composition to get
94      * @return the automation composition found
95      */
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);
102     }
103
104     /**
105      * Create automation composition.
106      *
107      * @param automationComposition the automation composition to create
108      * @return the create automation composition
109      */
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"));
115
116         // Return the saved automation composition
117         return result.toAuthorative();
118     }
119
120     /**
121      * Update automation composition.
122      *
123      * @param automationComposition the automation composition to update
124      * @return the updated automation composition
125      */
126     public AutomationComposition updateAutomationComposition(
127             @NonNull final AutomationComposition automationComposition) {
128         var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
129                 JpaAutomationComposition::new, "automation composition"));
130         automationCompositionRepository.flush();
131         // Return the saved automation composition
132         return result.toAuthorative();
133     }
134
135     /**
136      * Get all automation compositions by compositionId.
137      *
138      * @param compositionId the compositionId of the automation composition definition
139      * @return all automation compositions found
140      */
141     @Transactional(readOnly = true)
142     public List<AutomationComposition> getAcInstancesByCompositionId(UUID compositionId) {
143         return ProviderUtils
144                 .asEntityList(automationCompositionRepository.findByCompositionId(compositionId.toString()));
145     }
146
147     /**
148      * Get all automation compositions in transition..
149      *
150      * @return all automation compositions found
151      */
152     @Transactional(readOnly = true)
153     public Set<UUID> getAcInstancesInTransition() {
154         var jpaList = automationCompositionRepository.findByDeployStateIn(List.of(DeployState.DEPLOYING,
155             DeployState.UNDEPLOYING, DeployState.DELETING, DeployState.UPDATING, DeployState.MIGRATING));
156         jpaList.addAll(automationCompositionRepository.findByLockStateIn(
157             List.of(LockState.LOCKING, LockState.UNLOCKING)));
158         jpaList.addAll(automationCompositionRepository.findBySubStateIn(
159                 List.of(SubState.PREPARING, SubState.MIGRATION_PRECHECKING, SubState.REVIEWING)));
160         return jpaList.stream().map(JpaAutomationComposition::getInstanceId)
161                 .map(UUID::fromString).collect(Collectors.toSet());
162     }
163
164     /**
165      * Get automation compositions.
166      *
167      * @param name the name of the automation composition to get, null to get all automation compositions
168      * @param version the version of the automation composition to get, null to get all automation compositions
169      * @return the automation compositions found
170      */
171     @Transactional(readOnly = true)
172     public List<AutomationComposition> getAutomationCompositions(final UUID compositionId, final String name,
173             final String version) {
174
175         return ProviderUtils
176                 .asEntityList(automationCompositionRepository.findAll(createExample(compositionId, name, version)));
177     }
178
179     private Example<JpaAutomationComposition> createExample(final UUID compositionId, final String name,
180             final String version) {
181         var example = new JpaAutomationComposition();
182         example.setCompositionId(compositionId != null ? compositionId.toString() : null);
183         example.setName(name);
184         example.setVersion(version);
185         example.setInstanceId(null);
186         example.setElements(null);
187         example.setDeployState(null);
188         example.setLockState(null);
189
190         return Example.of(example);
191     }
192
193     /**
194      * Delete a automation composition.
195      *
196      * @param instanceId the ID of the automation composition to get
197      * @return the automation composition deleted
198      */
199     public AutomationComposition deleteAutomationComposition(@NonNull final UUID instanceId) {
200         var jpaDeleteAutomationComposition = automationCompositionRepository.findById(instanceId.toString());
201         if (jpaDeleteAutomationComposition.isEmpty()) {
202             var errorMessage = "delete of automation composition \"" + instanceId
203                     + "\" failed, automation composition does not exist";
204             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
205         }
206
207         automationCompositionRepository.deleteById(instanceId.toString());
208
209         return jpaDeleteAutomationComposition.get().toAuthorative();
210     }
211
212     /**
213      * Delete AutomationCompositionElement.
214      *
215      * @param elementId the AutomationCompositionElement Id
216      */
217     public void deleteAutomationCompositionElement(@NonNull final UUID elementId) {
218         acElementRepository.deleteById(elementId.toString());
219     }
220
221     /**
222      * Validate ElementIds.
223      *
224      * @param automationComposition the AutomationComposition
225      * @return the BeanValidationResult
226      */
227     public BeanValidationResult validateElementIds(final AutomationComposition automationComposition) {
228         var result = new BeanValidationResult(
229             "UUID elements " + automationComposition.getName(), automationComposition);
230
231         var ids = automationComposition
232             .getElements().values().stream().map(AutomationCompositionElement::getId).toList();
233         var elements = acElementRepository.findAllById(ids.stream().map(UUID::toString).toList());
234         if (automationComposition.getInstanceId() == null) {
235             for (var element : elements) {
236                 result.addResult(
237                     element.getDescription(), element.getElementId(), ValidationStatus.INVALID, "UUID already used");
238             }
239         } else {
240             var instanceId = automationComposition.getInstanceId().toString();
241             for (var element : elements) {
242                 if (!instanceId.equals(element.getInstanceId())) {
243                     result.addResult(
244                         element.getDescription(), element.getElementId(), ValidationStatus.INVALID,
245                         "UUID already used");
246                 }
247             }
248         }
249         return result;
250     }
251 }