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 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;
41 @RequiredArgsConstructor
42 public class AcDefinitionProvider {
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 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);
80 * Delete Automation Composition Definition.
82 * @param compositionId The UUID of the automation composition definition to delete
83 * @return the TOSCA service template that was deleted
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);
93 var item = jpaDelete.get().getServiceTemplate();
94 acmDefinitionRepository.deleteById(compositionId.toString());
95 return item.toAuthorative();
99 * Get the requested automation composition definitions.
101 * @param compositionId The UUID of the automation composition definition to delete
102 * @return the automation composition definition
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);
112 return jpaGet.get().getServiceTemplate().toAuthorative();
116 * Get the requested automation composition definition.
118 * @param compositionId The UUID of the automation composition definition to delete
119 * @return the automation composition definition
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();
129 * Get Automation Composition Definitions.
131 * @return the Automation Composition Definitions found
133 @Transactional(readOnly = true)
134 public List<AutomationCompositionDefinition> getAllAcDefinitions() {
135 var jpaList = acmDefinitionRepository.findAll();
136 return ProviderUtils.asEntityList(jpaList);
140 * Get service templates.
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
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);
156 jpaList = acmDefinitionRepository.findAll();
159 return jpaList.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate)
160 .map(DocToscaServiceTemplate::toAuthorative).collect(Collectors.toList());