dc1785e52fb6fcd03edf3e532d1a60c37537dd9f
[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 javax.ws.rs.core.Response;
27 import lombok.RequiredArgsConstructor;
28 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
29 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationCompositionDefinition;
30 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionDefinitionRepository;
31 import org.onap.policy.clamp.models.acm.persistence.repository.ToscaServiceTemplateRepository;
32 import org.onap.policy.models.base.PfModelRuntimeException;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
34 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
35 import org.springframework.stereotype.Service;
36 import org.springframework.transaction.annotation.Transactional;
37
38 @Service
39 @Transactional
40 @RequiredArgsConstructor
41 public class AcDefinitionProvider {
42
43     private final ToscaServiceTemplateRepository serviceTemplateRepository;
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 jpaServiceTemplate =
72                 ProviderUtils.getJpaAndValidate(serviceTemplate, JpaToscaServiceTemplate::new, "toscaServiceTemplate");
73         serviceTemplateRepository.save(jpaServiceTemplate);
74     }
75
76     /**
77      * Delete Automation Composition Definition.
78      *
79      * @param compositionId The UUID of the automation composition definition to delete
80      * @return the TOSCA service template that was deleted
81      */
82     public ToscaServiceTemplate deleteAcDefintion(UUID compositionId) {
83         var jpaDelete = acmDefinitionRepository.findById(compositionId.toString());
84         if (jpaDelete.isEmpty()) {
85             String errorMessage = "delete of Automation Composition Definition \"" + compositionId
86                     + "\" failed, Automation Composition Definition does not exist";
87             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
88         }
89
90         var item = jpaDelete.get().getServiceTemplate();
91         serviceTemplateRepository.deleteById(item.getKey());
92         acmDefinitionRepository.deleteById(compositionId.toString());
93         return item.toAuthorative();
94     }
95
96     /**
97      * Get the requested automation composition definitions.
98      *
99      * @param compositionId The UUID of the automation composition definition to delete
100      * @return the automation composition definition
101      */
102     @Transactional(readOnly = true)
103     public ToscaServiceTemplate getAcDefinition(UUID compositionId) {
104         var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
105         if (jpaGet.isEmpty()) {
106             String errorMessage =
107                     "Get serviceTemplate \"" + compositionId + "\" failed, serviceTemplate does not exist";
108             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
109         }
110         return jpaGet.get().getServiceTemplate().toAuthorative();
111     }
112
113     /**
114      * Get the requested automation composition definition.
115      *
116      * @param compositionId The UUID of the automation composition definition to delete
117      * @return the automation composition definition
118      */
119     @Transactional(readOnly = true)
120     public Optional<ToscaServiceTemplate> findAcDefinition(UUID compositionId) {
121         var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
122         return jpaGet.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate)
123                 .map(JpaToscaServiceTemplate::toAuthorative).findFirst();
124     }
125
126     /**
127      * Get Automation Composition Definitions.
128      *
129      * @return the Automation Composition Definitions found
130      */
131     @Transactional(readOnly = true)
132     public List<AutomationCompositionDefinition> getAllAcDefinitions() {
133         var jpaList = acmDefinitionRepository.findAll();
134         return ProviderUtils.asEntityList(jpaList);
135     }
136
137     /**
138      * Get service templates.
139      *
140      * @param name the name of the topology template to get, set to null to get all service templates
141      * @param version the version of the service template to get, set to null to get all service templates
142      * @return the topology templates found
143      */
144     @Transactional(readOnly = true)
145     public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version) {
146         var jpaList = serviceTemplateRepository.getFiltered(JpaToscaServiceTemplate.class, name, version);
147         return ProviderUtils.asEntityList(jpaList);
148     }
149 }