a518c4908bdb7cf03e1b8e0db7010e850b19c7d1
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 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.document.base.ToscaServiceTemplateValidation;
32 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaServiceTemplate;
33 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationCompositionDefinition;
34 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionDefinitionRepository;
35 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
36 import org.onap.policy.common.parameters.BeanValidationResult;
37 import org.onap.policy.models.base.PfModelRuntimeException;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
39 import org.springframework.data.domain.Example;
40 import org.springframework.stereotype.Service;
41 import org.springframework.transaction.annotation.Isolation;
42 import org.springframework.transaction.annotation.Transactional;
43
44 @Service
45 @Transactional
46 @RequiredArgsConstructor
47 public class AcDefinitionProvider {
48
49     private final AutomationCompositionDefinitionRepository acmDefinitionRepository;
50
51     /**
52      * Create Automation Composition Definition.
53      *
54      * @param serviceTemplate the service template to be created
55      * @return the created ACM Definition
56      */
57     public AutomationCompositionDefinition createAutomationCompositionDefinition(
58             final ToscaServiceTemplate serviceTemplate, final String toscaElementName, String toscaCompositionName) {
59         var acmDefinition = new AutomationCompositionDefinition();
60         var compositionId = UUID.randomUUID();
61         acmDefinition.setCompositionId(compositionId);
62         acmDefinition.setState(AcTypeState.COMMISSIONED);
63         if (serviceTemplate.getMetadata() == null) {
64             serviceTemplate.setMetadata(new HashMap<>());
65         }
66         serviceTemplate.getMetadata().put("compositionId", compositionId);
67         acmDefinition.setServiceTemplate(serviceTemplate);
68         var acElements = AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate, toscaElementName);
69         if (acElements.isEmpty()) {
70             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
71                     "NodeTemplate with element type " + toscaElementName + " must exist!");
72         }
73         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
74         var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acmDefinition, JpaAutomationCompositionDefinition::new,
75                 "AutomationCompositionDefinition");
76         var validationResult = new BeanValidationResult("AutomationCompositionDefinition", acmDefinition);
77         ToscaServiceTemplateValidation.validate(validationResult, jpaAcmDefinition.getServiceTemplate(),
78                 toscaCompositionName);
79         if (! validationResult.isValid()) {
80             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
81         }
82         var result = acmDefinitionRepository.save(jpaAcmDefinition);
83
84         return result.toAuthorative();
85     }
86
87     /**
88      * Update a commissioned ServiceTemplate.
89      *
90      * @param compositionId The UUID of the automation composition definition to delete
91      * @param serviceTemplate the service template to be created
92      */
93     public void updateServiceTemplate(UUID compositionId, ToscaServiceTemplate serviceTemplate, String toscaElementName,
94                                       String toscaCompositionName) {
95         var acmDefinition = new AutomationCompositionDefinition();
96         acmDefinition.setCompositionId(compositionId);
97         acmDefinition.setState(AcTypeState.COMMISSIONED);
98         acmDefinition.setServiceTemplate(serviceTemplate);
99         var acElements =
100                 AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate, toscaElementName);
101         if (acElements.isEmpty()) {
102             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
103                     "NodeTemplate with element type " + toscaElementName + " must exist!");
104         }
105         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
106         updateAcDefinition(acmDefinition, toscaCompositionName);
107     }
108
109     /**
110      * Update the AutomationCompositionDefinition.
111      *
112      * @param acDefinition the AutomationCompositionDefinition to be updated
113      */
114     public void updateAcDefinition(AutomationCompositionDefinition acDefinition, String toscaCompositionName) {
115         var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acDefinition, JpaAutomationCompositionDefinition::new,
116                 "AutomationCompositionDefinition");
117         var validationResult = new BeanValidationResult("AutomationCompositionDefinition", acDefinition);
118         ToscaServiceTemplateValidation.validate(validationResult, jpaAcmDefinition.getServiceTemplate(),
119                 toscaCompositionName);
120         if (! validationResult.isValid()) {
121             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
122         }
123         acmDefinitionRepository.save(jpaAcmDefinition);
124         acmDefinitionRepository.flush();
125     }
126
127     /**
128      * Delete Automation Composition Definition.
129      *
130      * @param compositionId The UUID of the automation composition definition to delete
131      * @return the TOSCA service template that was deleted
132      */
133     public ToscaServiceTemplate deleteAcDefintion(UUID compositionId) {
134         var jpaDelete = acmDefinitionRepository.findById(compositionId.toString());
135         if (jpaDelete.isEmpty()) {
136             String errorMessage = "delete of Automation Composition Definition \"" + compositionId
137                     + "\" failed, Automation Composition Definition does not exist";
138             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
139         }
140
141         var item = jpaDelete.get().getServiceTemplate();
142         acmDefinitionRepository.deleteById(compositionId.toString());
143         return item.toAuthorative();
144     }
145
146     /**
147      * Get the requested automation composition definitions.
148      *
149      * @param compositionId The UUID of the automation composition definition to delete
150      * @return the automation composition definition
151      */
152     @Transactional(readOnly = true)
153     public AutomationCompositionDefinition getAcDefinition(UUID compositionId) {
154         var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
155         if (jpaGet.isEmpty()) {
156             String errorMessage =
157                     "Get serviceTemplate \"" + compositionId + "\" failed, serviceTemplate does not exist";
158             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
159         }
160         return jpaGet.get().toAuthorative();
161     }
162
163     /**
164      * Get the requested automation composition definition.
165      *
166      * @param compositionId The UUID of the automation composition definition to delete
167      * @return the automation composition definition
168      */
169     @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
170     public Optional<AutomationCompositionDefinition> findAcDefinition(UUID compositionId) {
171         var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
172         return jpaGet.stream().map(JpaAutomationCompositionDefinition::toAuthorative).findFirst();
173     }
174
175     /**
176      * Get Automation Composition Definitions.
177      *
178      * @return the Automation Composition Definitions found
179      */
180     @Transactional(readOnly = true)
181     public List<AutomationCompositionDefinition> getAllAcDefinitions() {
182         var jpaList = acmDefinitionRepository.findAll();
183         return ProviderUtils.asEntityList(jpaList);
184     }
185
186     /**
187      * Get service templates.
188      *
189      * @param name the name of the topology template to get, set to null to get all service templates
190      * @param version the version of the service template to get, set to null to get all service templates
191      * @return the topology templates found
192      */
193     @Transactional(readOnly = true)
194     public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version) {
195         List<JpaAutomationCompositionDefinition> jpaList = null;
196         if (name != null || version != null) {
197             var entity = new JpaAutomationCompositionDefinition();
198             entity.setName(name);
199             entity.setVersion(version);
200             var example = Example.of(entity);
201             jpaList = acmDefinitionRepository.findAll(example);
202         } else {
203             jpaList = acmDefinitionRepository.findAll();
204         }
205
206         return jpaList.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate)
207                 .map(DocToscaServiceTemplate::toAuthorative).toList();
208     }
209 }