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