287ae51714c34842c56f0106087a11b4a42689a6
[policy/clamp.git] /
1 /*-
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
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.data.domain.Pageable;
49 import org.springframework.stereotype.Service;
50 import org.springframework.transaction.annotation.Isolation;
51 import org.springframework.transaction.annotation.Transactional;
52
53 /**
54  * This class provides information on automation composition concepts in the database to callers.
55  */
56 @Service
57 @Transactional
58 @AllArgsConstructor
59 public class AutomationCompositionProvider {
60
61     private final AutomationCompositionRepository automationCompositionRepository;
62     private final AutomationCompositionElementRepository acElementRepository;
63
64     /**
65      * Get automation composition.
66      *
67      * @param instanceId the ID of the automation composition to get
68      * @return the automation composition found
69      */
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");
75         }
76         return result.get().toAuthorative();
77     }
78
79     /**
80      * Find automation composition.
81      *
82      * @param instanceId the ID of the automation composition to get
83      * @return the automation composition found
84      */
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();
89     }
90
91     /**
92      * Find automation composition by automationCompositionId.
93      *
94      * @param automationCompositionId the ID of the automation composition to get
95      * @return the automation composition found
96      */
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);
103     }
104
105     /**
106      * Create automation composition.
107      *
108      * @param automationComposition the automation composition to create
109      * @return the create automation composition
110      */
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"));
116
117         // Return the saved automation composition
118         return result.toAuthorative();
119     }
120
121     /**
122      * Update automation composition.
123      *
124      * @param automationComposition the automation composition to update
125      * @return the updated automation composition
126      */
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();
134     }
135
136     /**
137      * Get all automation compositions by compositionId.
138      *
139      * @param compositionId the compositionId of the automation composition definition
140      * @return all automation compositions found
141      */
142     @Transactional(readOnly = true)
143     public List<AutomationComposition> getAcInstancesByCompositionId(UUID compositionId) {
144         return ProviderUtils
145                 .asEntityList(automationCompositionRepository.findByCompositionId(compositionId.toString()));
146     }
147
148     /**
149      * Get all automation compositions in transition..
150      *
151      * @return all automation compositions found
152      */
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());
163     }
164
165     /**
166      * Get automation compositions.
167      *
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
172      */
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());
178     }
179
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);
190
191         return Example.of(example);
192     }
193
194     /**
195      * Delete a automation composition.
196      *
197      * @param instanceId the ID of the automation composition to get
198      * @return the automation composition deleted
199      */
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);
206         }
207
208         automationCompositionRepository.deleteById(instanceId.toString());
209
210         return jpaDeleteAutomationComposition.get().toAuthorative();
211     }
212
213     /**
214      * Delete AutomationCompositionElement.
215      *
216      * @param elementId the AutomationCompositionElement Id
217      */
218     public void deleteAutomationCompositionElement(@NonNull final UUID elementId) {
219         acElementRepository.deleteById(elementId.toString());
220     }
221
222     /**
223      * Validate ElementIds.
224      *
225      * @param automationComposition the AutomationComposition
226      * @return the BeanValidationResult
227      */
228     public BeanValidationResult validateElementIds(final AutomationComposition automationComposition) {
229         var result = new BeanValidationResult(
230             "UUID elements " + automationComposition.getName(), automationComposition);
231
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) {
237                 result.addResult(
238                     element.getDescription(), element.getElementId(), ValidationStatus.INVALID, "UUID already used");
239             }
240         } else {
241             var instanceId = automationComposition.getInstanceId().toString();
242             for (var element : elements) {
243                 if (!instanceId.equals(element.getInstanceId())) {
244                     result.addResult(
245                         element.getDescription(), element.getElementId(), ValidationStatus.INVALID,
246                         "UUID already used");
247                 }
248             }
249         }
250         return result;
251     }
252 }