bd99a756bfc6277b54e08d32fea6915f0f0dd69e
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2025 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.models.acm.persistence.provider;
22
23 import jakarta.ws.rs.core.Response;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Optional;
27 import java.util.Set;
28 import java.util.UUID;
29 import java.util.stream.Collectors;
30 import lombok.RequiredArgsConstructor;
31 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
33 import org.onap.policy.clamp.models.acm.document.base.ToscaServiceTemplateValidation;
34 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaServiceTemplate;
35 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationCompositionDefinition;
36 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionDefinitionRepository;
37 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
38 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
39 import org.onap.policy.common.parameters.BeanValidationResult;
40 import org.onap.policy.models.base.PfModelRuntimeException;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
42 import org.springframework.data.domain.Example;
43 import org.springframework.stereotype.Service;
44 import org.springframework.transaction.annotation.Transactional;
45
46 @Service
47 @Transactional
48 @RequiredArgsConstructor
49 public class AcDefinitionProvider {
50
51     private static final String NAME = "AutomationCompositionDefinition";
52
53     private final AutomationCompositionDefinitionRepository acmDefinitionRepository;
54
55     /**
56      * Create Automation Composition Definition.
57      *
58      * @param serviceTemplate the service template to be created
59      * @return the created ACM Definition
60      */
61     public AutomationCompositionDefinition createAutomationCompositionDefinition(
62             final ToscaServiceTemplate serviceTemplate, final String toscaElementName, String toscaCompositionName) {
63         var acmDefinition = new AutomationCompositionDefinition();
64         var compositionId = UUID.randomUUID();
65         acmDefinition.setCompositionId(compositionId);
66         acmDefinition.setState(AcTypeState.COMMISSIONED);
67         if (serviceTemplate.getMetadata() == null) {
68             serviceTemplate.setMetadata(new HashMap<>());
69         }
70         acmDefinition.setLastMsg(TimestampHelper.now());
71         serviceTemplate.getMetadata().put("compositionId", compositionId);
72         acmDefinition.setServiceTemplate(serviceTemplate);
73         var acElements = AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate, toscaElementName);
74         if (acElements.isEmpty()) {
75             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
76                     "NodeTemplate with element type " + toscaElementName + " must exist!");
77         }
78         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
79         var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acmDefinition, JpaAutomationCompositionDefinition::new,
80             acmDefinition.getClass().getSimpleName());
81         var validationResult = new BeanValidationResult(acmDefinition.getClass().getSimpleName(), acmDefinition);
82         ToscaServiceTemplateValidation.validate(validationResult, jpaAcmDefinition.getServiceTemplate(),
83                 toscaCompositionName);
84         if (! validationResult.isValid()) {
85             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
86         }
87         var result = acmDefinitionRepository.save(jpaAcmDefinition);
88
89         return result.toAuthorative();
90     }
91
92     /**
93      * Update a commissioned ServiceTemplate.
94      *
95      * @param compositionId The UUID of the automation composition definition to delete
96      * @param serviceTemplate the service template to be created
97      */
98     public void updateServiceTemplate(UUID compositionId, ToscaServiceTemplate serviceTemplate, String toscaElementName,
99                                       String toscaCompositionName) {
100         var acmDefinition = new AutomationCompositionDefinition();
101         acmDefinition.setCompositionId(compositionId);
102         acmDefinition.setState(AcTypeState.COMMISSIONED);
103         acmDefinition.setLastMsg(TimestampHelper.now());
104         acmDefinition.setServiceTemplate(serviceTemplate);
105         var acElements =
106                 AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate, toscaElementName);
107         if (acElements.isEmpty()) {
108             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
109                     "NodeTemplate with element type " + toscaElementName + " must exist!");
110         }
111         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
112         updateAcDefinition(acmDefinition, toscaCompositionName);
113     }
114
115     /**
116      * Update the AutomationCompositionDefinition.
117      *
118      * @param acDefinition the AutomationCompositionDefinition to be updated
119      */
120     public void updateAcDefinition(AutomationCompositionDefinition acDefinition, String toscaCompositionName) {
121         var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acDefinition, JpaAutomationCompositionDefinition::new,
122                 NAME);
123         var validationResult = new BeanValidationResult(NAME, acDefinition);
124         ToscaServiceTemplateValidation.validate(validationResult, jpaAcmDefinition.getServiceTemplate(),
125                 toscaCompositionName);
126         if (! validationResult.isValid()) {
127             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
128         }
129         acmDefinitionRepository.save(jpaAcmDefinition);
130         acmDefinitionRepository.flush();
131     }
132
133     /**
134      * Update Ac Definition with unchanged service template.
135      *
136      * @param acDefinition the AutomationCompositionDefinition to be updated
137      */
138     public void updateAcDefinitionState(AutomationCompositionDefinition acDefinition) {
139         var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acDefinition, JpaAutomationCompositionDefinition::new,
140                 NAME);
141         acmDefinitionRepository.save(jpaAcmDefinition);
142         acmDefinitionRepository.flush();
143     }
144
145     /**
146      * Delete Automation Composition Definition.
147      *
148      * @param compositionId The UUID of the automation composition definition to delete
149      * @return the TOSCA service template that was deleted
150      */
151     public ToscaServiceTemplate deleteAcDefintion(UUID compositionId) {
152         var jpaDelete = acmDefinitionRepository.findById(compositionId.toString());
153         if (jpaDelete.isEmpty()) {
154             String errorMessage = "delete of Automation Composition Definition \"" + compositionId
155                     + "\" failed, Automation Composition Definition does not exist";
156             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
157         }
158
159         var item = jpaDelete.get().getServiceTemplate();
160         acmDefinitionRepository.deleteById(compositionId.toString());
161         return item.toAuthorative();
162     }
163
164     /**
165      * Get the requested automation composition definitions.
166      *
167      * @param compositionId The UUID of the automation composition definition to delete
168      * @return the automation composition definition
169      */
170     @Transactional(readOnly = true)
171     public AutomationCompositionDefinition getAcDefinition(UUID compositionId) {
172         var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
173         if (jpaGet.isEmpty()) {
174             String errorMessage =
175                     "Get serviceTemplate \"" + compositionId + "\" failed, serviceTemplate does not exist";
176             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
177         }
178         return jpaGet.get().toAuthorative();
179     }
180
181     /**
182      * Get the requested automation composition definition.
183      *
184      * @param compositionId The UUID of the automation composition definition to delete
185      * @return the automation composition definition
186      */
187     @Transactional(readOnly = true)
188     public Optional<AutomationCompositionDefinition> findAcDefinition(UUID compositionId) {
189         var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
190         return jpaGet.stream().map(JpaAutomationCompositionDefinition::toAuthorative).findFirst();
191     }
192
193     /**
194      * Get Automation Composition Definitions in transition.
195      *
196      * @return the Automation Composition Definitions found
197      */
198     @Transactional(readOnly = true)
199     public Set<UUID> getAllAcDefinitionsInTransition() {
200         var jpaList = acmDefinitionRepository.findByStateIn(List.of(AcTypeState.PRIMING, AcTypeState.DEPRIMING));
201         return jpaList.stream().map(JpaAutomationCompositionDefinition::getCompositionId)
202                 .map(UUID::fromString).collect(Collectors.toSet());
203     }
204
205     /**
206      * Get service templates.
207      *
208      * @param name the name of the topology template to get, set to null to get all service templates
209      * @param version the version of the service template to get, set to null to get all service templates
210      * @return the topology templates found
211      */
212     @Transactional(readOnly = true)
213     public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version) {
214         List<JpaAutomationCompositionDefinition> jpaList = null;
215         if (name != null || version != null) {
216             var entity = new JpaAutomationCompositionDefinition();
217             entity.setName(name);
218             entity.setVersion(version);
219             var example = Example.of(entity);
220             jpaList = acmDefinitionRepository.findAll(example);
221         } else {
222             jpaList = acmDefinitionRepository.findAll();
223         }
224
225         return jpaList.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate)
226                 .map(DocToscaServiceTemplate::toAuthorative).toList();
227     }
228 }