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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.models.acm.persistence.provider;
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;
40 @RequiredArgsConstructor
41 public class AcDefinitionProvider {
43 private final ToscaServiceTemplateRepository serviceTemplateRepository;
44 private final AutomationCompositionDefinitionRepository acmDefinitionRepository;
47 * Create Automation Composition Definition.
49 * @param serviceTemplate the service template to be created
50 * @return the created ACM Definition
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);
61 return result.toAuthorative();
65 * Update the ServiceTemplate.
67 * @param compositionId The UUID of the automation composition definition to delete
68 * @param serviceTemplate the service template to be created
70 public void updateServiceTemplate(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
71 var jpaServiceTemplate =
72 ProviderUtils.getJpaAndValidate(serviceTemplate, JpaToscaServiceTemplate::new, "toscaServiceTemplate");
73 serviceTemplateRepository.save(jpaServiceTemplate);
77 * Delete Automation Composition Definition.
79 * @param compositionId The UUID of the automation composition definition to delete
80 * @return the TOSCA service template that was deleted
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);
90 var item = jpaDelete.get().getServiceTemplate();
91 serviceTemplateRepository.deleteById(item.getKey());
92 acmDefinitionRepository.deleteById(compositionId.toString());
93 return item.toAuthorative();
97 * Get the requested automation composition definitions.
99 * @param compositionId The UUID of the automation composition definition to delete
100 * @return the automation composition definition
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);
110 return jpaGet.get().getServiceTemplate().toAuthorative();
114 * Get the requested automation composition definition.
116 * @param compositionId The UUID of the automation composition definition to delete
117 * @return the automation composition definition
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();
127 * Get Automation Composition Definitions.
129 * @return the Automation Composition Definitions found
131 @Transactional(readOnly = true)
132 public List<AutomationCompositionDefinition> getAllAcDefinitions() {
133 var jpaList = acmDefinitionRepository.findAll();
134 return ProviderUtils.asEntityList(jpaList);
138 * Get service templates.
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
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);