9eb5e7a3209a7913aca20f056c1a5614172d7b2b
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2024 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.UUID;
28 import lombok.RequiredArgsConstructor;
29 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
30 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
31 import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState;
32 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
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.concepts.JpaNodeTemplateState;
37 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionDefinitionRepository;
38 import org.onap.policy.clamp.models.acm.persistence.repository.NodeTemplateStateRepository;
39 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
40 import org.onap.policy.common.parameters.BeanValidationResult;
41 import org.onap.policy.models.base.PfModelRuntimeException;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43 import org.springframework.data.domain.Example;
44 import org.springframework.stereotype.Service;
45 import org.springframework.transaction.annotation.Isolation;
46 import org.springframework.transaction.annotation.Transactional;
47
48 @Service
49 @Transactional
50 @RequiredArgsConstructor
51 public class AcDefinitionProvider {
52
53     private final AutomationCompositionDefinitionRepository acmDefinitionRepository;
54     private final NodeTemplateStateRepository nodeTemplateStateRepository;
55
56     /**
57      * Create Automation Composition Definition.
58      *
59      * @param serviceTemplate the service template to be created
60      * @return the created ACM Definition
61      */
62     public AutomationCompositionDefinition createAutomationCompositionDefinition(
63             final ToscaServiceTemplate serviceTemplate, final String toscaElementName, String toscaCompositionName) {
64         var acmDefinition = new AutomationCompositionDefinition();
65         var compositionId = UUID.randomUUID();
66         acmDefinition.setCompositionId(compositionId);
67         acmDefinition.setState(AcTypeState.COMMISSIONED);
68         if (serviceTemplate.getMetadata() == null) {
69             serviceTemplate.setMetadata(new HashMap<>());
70         }
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.setServiceTemplate(serviceTemplate);
104         var acElements =
105                 AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate, toscaElementName);
106         if (acElements.isEmpty()) {
107             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
108                     "NodeTemplate with element type " + toscaElementName + " must exist!");
109         }
110         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
111         updateAcDefinition(acmDefinition, toscaCompositionName);
112     }
113
114     /**
115      * Update the AutomationCompositionDefinition.
116      *
117      * @param acDefinition the AutomationCompositionDefinition to be updated
118      */
119     public void updateAcDefinition(AutomationCompositionDefinition acDefinition, String toscaCompositionName) {
120         var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acDefinition, JpaAutomationCompositionDefinition::new,
121                 "AutomationCompositionDefinition");
122         var validationResult = new BeanValidationResult("AutomationCompositionDefinition", acDefinition);
123         ToscaServiceTemplateValidation.validate(validationResult, jpaAcmDefinition.getServiceTemplate(),
124                 toscaCompositionName);
125         if (! validationResult.isValid()) {
126             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
127         }
128         acmDefinitionRepository.save(jpaAcmDefinition);
129         acmDefinitionRepository.flush();
130     }
131
132     /**
133      * Update Ac Definition AcTypeState, StateChangeResult and restarting.
134      *
135      * @param compositionId The UUID of the automation composition definition to update
136      * @param state the AcTypeState
137      * @param stateChangeResult the StateChangeResult
138      * @param restarting restarting process
139      */
140     public void updateAcDefinitionState(UUID compositionId, AcTypeState state, StateChangeResult stateChangeResult,
141                                       Boolean restarting) {
142         var jpaUpdate = acmDefinitionRepository.findById(compositionId.toString());
143         if (jpaUpdate.isEmpty()) {
144             String errorMessage = "update of Automation Composition Definition \"" + compositionId
145                 + "\" failed, Automation Composition Definition does not exist";
146             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
147         }
148         var acDefinition = jpaUpdate.get();
149         acDefinition.setState(state);
150         acDefinition.setStateChangeResult(stateChangeResult);
151         acDefinition.setRestarting(restarting);
152         acmDefinitionRepository.save(acDefinition);
153         acmDefinitionRepository.flush();
154     }
155
156     /**
157      * Update Ac DefinitionElement.
158      *
159      * @param nodeTemplateState the NodeTemplateState
160      * @param compositionId The UUID of the automation composition definition
161      */
162     public void updateAcDefinitionElement(NodeTemplateState nodeTemplateState, UUID compositionId) {
163         var jpaNodeTemplateState = new JpaNodeTemplateState(
164             nodeTemplateState.getNodeTemplateStateId().toString(), compositionId.toString());
165         jpaNodeTemplateState.fromAuthorative(nodeTemplateState);
166         nodeTemplateStateRepository.save(jpaNodeTemplateState);
167     }
168
169     /**
170      * Delete Automation Composition Definition.
171      *
172      * @param compositionId The UUID of the automation composition definition to delete
173      * @return the TOSCA service template that was deleted
174      */
175     public ToscaServiceTemplate deleteAcDefintion(UUID compositionId) {
176         var jpaDelete = acmDefinitionRepository.findById(compositionId.toString());
177         if (jpaDelete.isEmpty()) {
178             String errorMessage = "delete of Automation Composition Definition \"" + compositionId
179                     + "\" failed, Automation Composition Definition does not exist";
180             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
181         }
182
183         var item = jpaDelete.get().getServiceTemplate();
184         acmDefinitionRepository.deleteById(compositionId.toString());
185         return item.toAuthorative();
186     }
187
188     /**
189      * Get the requested automation composition definitions.
190      *
191      * @param compositionId The UUID of the automation composition definition to delete
192      * @return the automation composition definition
193      */
194     @Transactional(readOnly = true)
195     public AutomationCompositionDefinition getAcDefinition(UUID compositionId) {
196         var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
197         if (jpaGet.isEmpty()) {
198             String errorMessage =
199                     "Get serviceTemplate \"" + compositionId + "\" failed, serviceTemplate does not exist";
200             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
201         }
202         return jpaGet.get().toAuthorative();
203     }
204
205     /**
206      * Get the requested automation composition definition.
207      *
208      * @param compositionId The UUID of the automation composition definition to delete
209      * @return the automation composition definition
210      */
211     @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
212     public Optional<AutomationCompositionDefinition> findAcDefinition(UUID compositionId) {
213         var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
214         return jpaGet.stream().map(JpaAutomationCompositionDefinition::toAuthorative).findFirst();
215     }
216
217     /**
218      * Get Automation Composition Definitions in transition.
219      *
220      * @return the Automation Composition Definitions found
221      */
222     @Transactional(readOnly = true)
223     public List<AutomationCompositionDefinition> getAllAcDefinitionsInTransition() {
224         var jpaList = acmDefinitionRepository.findByStateIn(List.of(AcTypeState.PRIMING, AcTypeState.DEPRIMING));
225         return ProviderUtils.asEntityList(jpaList);
226     }
227
228     /**
229      * Get service templates.
230      *
231      * @param name the name of the topology template to get, set to null to get all service templates
232      * @param version the version of the service template to get, set to null to get all service templates
233      * @return the topology templates found
234      */
235     @Transactional(readOnly = true)
236     public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version) {
237         List<JpaAutomationCompositionDefinition> jpaList = null;
238         if (name != null || version != null) {
239             var entity = new JpaAutomationCompositionDefinition();
240             entity.setName(name);
241             entity.setVersion(version);
242             var example = Example.of(entity);
243             jpaList = acmDefinitionRepository.findAll(example);
244         } else {
245             jpaList = acmDefinitionRepository.findAll();
246         }
247
248         return jpaList.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate)
249                 .map(DocToscaServiceTemplate::toAuthorative).toList();
250     }
251 }