ce258094ac336f9e14ddd8d9b7c04ccf1d2f6e89
[policy/clamp.git] /
1 /*-
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
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.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;
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     /**
122      * Update automation composition state.
123      *
124      * @param acSource the automation composition to update
125      * @return the updated automation composition
126      */
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();
135     }
136
137     /**
138      * Update automation composition.
139      *
140      * @param automationComposition the automation composition to update
141      * @return the updated automation composition
142      */
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();
150     }
151
152     /**
153      * Get all automation compositions by compositionId.
154      *
155      * @param compositionId the compositionId of the automation composition definition
156      * @return all automation compositions found
157      */
158     @Transactional(readOnly = true)
159     public List<AutomationComposition> getAcInstancesByCompositionId(UUID compositionId) {
160         return ProviderUtils
161                 .asEntityList(automationCompositionRepository.findByCompositionId(compositionId.toString()));
162     }
163
164     /**
165      * Get all automation compositions in transition..
166      *
167      * @return all automation compositions found
168      */
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);
176     }
177
178     /**
179      * Get automation compositions.
180      *
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
184      */
185     @Transactional(readOnly = true)
186     public List<AutomationComposition> getAutomationCompositions(final UUID compositionId, final String name,
187             final String version) {
188
189         return ProviderUtils
190                 .asEntityList(automationCompositionRepository.findAll(createExample(compositionId, name, version)));
191     }
192
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);
203
204         return Example.of(example);
205     }
206
207     /**
208      * Delete a automation composition.
209      *
210      * @param instanceId the ID of the automation composition to get
211      * @return the automation composition deleted
212      */
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);
219         }
220
221         automationCompositionRepository.deleteById(instanceId.toString());
222
223         return jpaDeleteAutomationComposition.get().toAuthorative();
224     }
225
226     /**
227      * Upgrade States.
228      *
229      * @param automationCompositionInfoList list of AutomationCompositionInfo
230      */
231     public void upgradeStates(@NonNull final List<AutomationCompositionInfo> automationCompositionInfoList) {
232         if (automationCompositionInfoList.isEmpty()) {
233             return;
234         }
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());
242                 jpaList.add(jpa);
243             }
244         }
245         acElementRepository.saveAll(jpaList);
246     }
247
248     /**
249      * Update AutomationCompositionElement.
250      *
251      * @param element the AutomationCompositionElement
252      */
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());
262
263         ProviderUtils.validate(element, jpaAcElement, "AutomationCompositionElement");
264         acElementRepository.save(jpaAcElement);
265     }
266
267     /**
268      * Validate ElementIds.
269      *
270      * @param automationComposition the AutomationComposition
271      * @return the BeanValidationResult
272      */
273     public BeanValidationResult validateElementIds(final AutomationComposition automationComposition) {
274         var result = new BeanValidationResult(
275             "UUID elements " + automationComposition.getName(), automationComposition);
276
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) {
282                 result.addResult(
283                     element.getDescription(), element.getElementId(), ValidationStatus.INVALID, "UUID already used");
284             }
285         } else {
286             var instanceId = automationComposition.getInstanceId().toString();
287             for (var element : elements) {
288                 if (!instanceId.equals(element.getInstanceId())) {
289                     result.addResult(
290                         element.getDescription(), element.getElementId(), ValidationStatus.INVALID,
291                         "UUID already used");
292                 }
293             }
294         }
295         return result;
296     }
297 }