9f267d32d91815a05a8a6ae2019800e95f47024a
[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         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
70         var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acmDefinition, JpaAutomationCompositionDefinition::new,
71                 "AutomationCompositionDefinition");
72         var validationResult = new BeanValidationResult("AutomationCompositionDefinition", acmDefinition);
73         ToscaServiceTemplateValidation.validate(validationResult, jpaAcmDefinition.getServiceTemplate(),
74                 toscaCompositionName);
75         if (! validationResult.isValid()) {
76             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
77         }
78         var result = acmDefinitionRepository.save(jpaAcmDefinition);
79
80         return result.toAuthorative();
81     }
82
83     /**
84      * Update a commissioned ServiceTemplate.
85      *
86      * @param compositionId The UUID of the automation composition definition to delete
87      * @param serviceTemplate the service template to be created
88      */
89     public void updateServiceTemplate(UUID compositionId, ToscaServiceTemplate serviceTemplate, String toscaElementName,
90                                       String toscaCompositionName) {
91         var acmDefinition = new AutomationCompositionDefinition();
92         acmDefinition.setCompositionId(compositionId);
93         acmDefinition.setState(AcTypeState.COMMISSIONED);
94         acmDefinition.setServiceTemplate(serviceTemplate);
95         var acElements =
96                 AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate, toscaElementName);
97         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
98         updateAcDefinition(acmDefinition, toscaCompositionName);
99     }
100
101     /**
102      * Update the AutomationCompositionDefinition.
103      *
104      * @param acDefinition the AutomationCompositionDefinition to be updated
105      */
106     public void updateAcDefinition(AutomationCompositionDefinition acDefinition, String toscaCompositionName) {
107         var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acDefinition, JpaAutomationCompositionDefinition::new,
108                 "AutomationCompositionDefinition");
109         var validationResult = new BeanValidationResult("AutomationCompositionDefinition", acDefinition);
110         ToscaServiceTemplateValidation.validate(validationResult, jpaAcmDefinition.getServiceTemplate(),
111                 toscaCompositionName);
112         if (! validationResult.isValid()) {
113             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
114         }
115         acmDefinitionRepository.save(jpaAcmDefinition);
116         acmDefinitionRepository.flush();
117     }
118
119     /**
120      * Delete Automation Composition Definition.
121      *
122      * @param compositionId The UUID of the automation composition definition to delete
123      * @return the TOSCA service template that was deleted
124      */
125     public ToscaServiceTemplate deleteAcDefintion(UUID compositionId) {
126         var jpaDelete = acmDefinitionRepository.findById(compositionId.toString());
127         if (jpaDelete.isEmpty()) {
128             String errorMessage = "delete of Automation Composition Definition \"" + compositionId
129                     + "\" failed, Automation Composition Definition does not exist";
130             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
131         }
132
133         var item = jpaDelete.get().getServiceTemplate();
134         acmDefinitionRepository.deleteById(compositionId.toString());
135         return item.toAuthorative();
136     }
137
138     /**
139      * Get the requested automation composition definitions.
140      *
141      * @param compositionId The UUID of the automation composition definition to delete
142      * @return the automation composition definition
143      */
144     @Transactional(readOnly = true)
145     public AutomationCompositionDefinition getAcDefinition(UUID compositionId) {
146         var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
147         if (jpaGet.isEmpty()) {
148             String errorMessage =
149                     "Get serviceTemplate \"" + compositionId + "\" failed, serviceTemplate does not exist";
150             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
151         }
152         return jpaGet.get().toAuthorative();
153     }
154
155     /**
156      * Get the requested automation composition definition.
157      *
158      * @param compositionId The UUID of the automation composition definition to delete
159      * @return the automation composition definition
160      */
161     @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
162     public Optional<AutomationCompositionDefinition> findAcDefinition(UUID compositionId) {
163         var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
164         return jpaGet.stream().map(JpaAutomationCompositionDefinition::toAuthorative).findFirst();
165     }
166
167     /**
168      * Get Automation Composition Definitions.
169      *
170      * @return the Automation Composition Definitions found
171      */
172     @Transactional(readOnly = true)
173     public List<AutomationCompositionDefinition> getAllAcDefinitions() {
174         var jpaList = acmDefinitionRepository.findAll();
175         return ProviderUtils.asEntityList(jpaList);
176     }
177
178     /**
179      * Get service templates.
180      *
181      * @param name the name of the topology template to get, set to null to get all service templates
182      * @param version the version of the service template to get, set to null to get all service templates
183      * @return the topology templates found
184      */
185     @Transactional(readOnly = true)
186     public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version) {
187         List<JpaAutomationCompositionDefinition> jpaList = null;
188         if (name != null || version != null) {
189             var entity = new JpaAutomationCompositionDefinition();
190             entity.setName(name);
191             entity.setVersion(version);
192             var example = Example.of(entity);
193             jpaList = acmDefinitionRepository.findAll(example);
194         } else {
195             jpaList = acmDefinitionRepository.findAll();
196         }
197
198         return jpaList.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate)
199                 .map(DocToscaServiceTemplate::toAuthorative).toList();
200     }
201 }