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