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