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