5740207c7c954a4f293039d3a244cdd735c547e6
[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.UUID;
25 import javax.ws.rs.core.Response;
26 import lombok.RequiredArgsConstructor;
27 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
28 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationCompositionDefinition;
29 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionDefinitionRepository;
30 import org.onap.policy.clamp.models.acm.persistence.repository.ToscaServiceTemplateRepository;
31 import org.onap.policy.models.base.PfModelRuntimeException;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
33 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
34 import org.springframework.stereotype.Service;
35 import org.springframework.transaction.annotation.Transactional;
36
37 @Service
38 @Transactional
39 @RequiredArgsConstructor
40 public class AcDefinitionProvider {
41
42     private final ToscaServiceTemplateRepository serviceTemplateRepository;
43     private final AutomationCompositionDefinitionRepository acmDefinitionRepository;
44
45     /**
46      * Create Automation Composition Definition.
47      *
48      * @param serviceTemplate the service template to be created
49      * @return the created ACM Definition
50      */
51     public AutomationCompositionDefinition createAutomationCompositionDefinition(
52             final ToscaServiceTemplate serviceTemplate) {
53         var acmDefinition = new AutomationCompositionDefinition();
54         acmDefinition.setCompositionId(UUID.randomUUID());
55         acmDefinition.setServiceTemplate(serviceTemplate);
56         var result = acmDefinitionRepository.save(new JpaAutomationCompositionDefinition(acmDefinition));
57         return result.toAuthorative();
58     }
59
60     /**
61      * Update the ServiceTemplate.
62      *
63      * @param compositionId The UUID of the automation composition definition to delete
64      * @param serviceTemplate the service template to be created
65      */
66     public void updateServiceTemplate(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
67         var jpaServiceTemplate =
68                 ProviderUtils.getJpaAndValidate(serviceTemplate, JpaToscaServiceTemplate::new, "toscaServiceTemplate");
69         serviceTemplateRepository.save(jpaServiceTemplate);
70     }
71
72     /**
73      * Delete Automation Composition Definition.
74      *
75      * @param compositionId The UUID of the automation composition definition to delete
76      * @return the TOSCA service template that was deleted
77      */
78     public ToscaServiceTemplate deleteAcDefintion(UUID compositionId) {
79         var jpaDelete = acmDefinitionRepository.findById(compositionId.toString());
80         if (jpaDelete.isEmpty()) {
81             String errorMessage = "delete of Automation Composition Definition \"" + compositionId
82                     + "\" failed, Automation Composition Definition does not exist";
83             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
84         }
85
86         var item = jpaDelete.get().getServiceTemplate();
87         serviceTemplateRepository.deleteById(item.getKey());
88         acmDefinitionRepository.deleteById(compositionId.toString());
89         return item.toAuthorative();
90     }
91
92     /**
93      * Get the requested automation composition definitions.
94      *
95      * @param compositionId The UUID of the automation composition definition to delete
96      * @return the automation composition definition
97      */
98     @Transactional(readOnly = true)
99     public ToscaServiceTemplate getAcDefinition(UUID compositionId) {
100         var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
101         if (jpaGet.isEmpty()) {
102             String errorMessage =
103                     "Get serviceTemplate \"" + compositionId + "\" failed, serviceTemplate does not exist";
104             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
105         }
106         return jpaGet.get().getServiceTemplate().toAuthorative();
107     }
108
109     /**
110      * Get service templates.
111      *
112      * @return the topology templates found
113      */
114     @Transactional(readOnly = true)
115     public List<ToscaServiceTemplate> getAllServiceTemplates() {
116         var jpaList = serviceTemplateRepository.findAll();
117         return ProviderUtils.asEntityList(jpaList);
118     }
119
120     /**
121      * Get service templates.
122      *
123      * @param name the name of the topology template to get, set to null to get all service templates
124      * @param version the version of the service template to get, set to null to get all service templates
125      * @return the topology templates found
126      */
127     @Transactional(readOnly = true)
128     public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version) {
129         var jpaList = serviceTemplateRepository.getFiltered(JpaToscaServiceTemplate.class, name, version);
130         return ProviderUtils.asEntityList(jpaList);
131     }
132 }