2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 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.HashMap;
24 import java.util.List;
25 import java.util.Optional;
26 import java.util.UUID;
27 import java.util.stream.Collectors;
28 import javax.ws.rs.core.Response;
29 import lombok.RequiredArgsConstructor;
30 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
31 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
32 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaServiceTemplate;
33 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationCompositionDefinition;
34 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionDefinitionRepository;
35 import org.onap.policy.models.base.PfModelRuntimeException;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
37 import org.springframework.data.domain.Example;
38 import org.springframework.stereotype.Service;
39 import org.springframework.transaction.annotation.Transactional;
43 @RequiredArgsConstructor
44 public class AcDefinitionProvider {
46 private final AutomationCompositionDefinitionRepository acmDefinitionRepository;
49 * Create Automation Composition Definition.
51 * @param serviceTemplate the service template to be created
52 * @return the created ACM Definition
54 public AutomationCompositionDefinition createAutomationCompositionDefinition(
55 final ToscaServiceTemplate serviceTemplate) {
56 var acmDefinition = new AutomationCompositionDefinition();
57 var compositionId = UUID.randomUUID();
58 acmDefinition.setCompositionId(compositionId);
59 acmDefinition.setState(AcTypeState.COMMISSIONED);
60 if (serviceTemplate.getMetadata() == null) {
61 serviceTemplate.setMetadata(new HashMap<>());
63 serviceTemplate.getMetadata().put("compositionId", compositionId);
64 acmDefinition.setServiceTemplate(serviceTemplate);
65 var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acmDefinition, JpaAutomationCompositionDefinition::new,
66 "AutomationCompositionDefinition");
67 var result = acmDefinitionRepository.save(jpaAcmDefinition);
69 return result.toAuthorative();
73 * Update a commissioned ServiceTemplate.
75 * @param compositionId The UUID of the automation composition definition to delete
76 * @param serviceTemplate the service template to be created
78 public void updateServiceTemplate(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
79 var acmDefinition = new AutomationCompositionDefinition();
80 acmDefinition.setCompositionId(compositionId);
81 acmDefinition.setState(AcTypeState.COMMISSIONED);
82 acmDefinition.setServiceTemplate(serviceTemplate);
83 updateAcDefinition(acmDefinition);
87 * Update the AutomationCompositionDefinition.
89 * @param acDefinition the AutomationCompositionDefinition to be updated
91 public void updateAcDefinition(AutomationCompositionDefinition acDefinition) {
92 var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acDefinition, JpaAutomationCompositionDefinition::new,
93 "AutomationCompositionDefinition");
94 acmDefinitionRepository.save(jpaAcmDefinition);
98 * Delete Automation Composition Definition.
100 * @param compositionId The UUID of the automation composition definition to delete
101 * @return the TOSCA service template that was deleted
103 public ToscaServiceTemplate deleteAcDefintion(UUID compositionId) {
104 var jpaDelete = acmDefinitionRepository.findById(compositionId.toString());
105 if (jpaDelete.isEmpty()) {
106 String errorMessage = "delete of Automation Composition Definition \"" + compositionId
107 + "\" failed, Automation Composition Definition does not exist";
108 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
111 var item = jpaDelete.get().getServiceTemplate();
112 acmDefinitionRepository.deleteById(compositionId.toString());
113 return item.toAuthorative();
117 * Get the requested automation composition definitions.
119 * @param compositionId The UUID of the automation composition definition to delete
120 * @return the automation composition definition
122 @Transactional(readOnly = true)
123 public AutomationCompositionDefinition getAcDefinition(UUID compositionId) {
124 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
125 if (jpaGet.isEmpty()) {
126 String errorMessage =
127 "Get serviceTemplate \"" + compositionId + "\" failed, serviceTemplate does not exist";
128 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
130 return jpaGet.get().toAuthorative();
134 * Get the requested automation composition definition.
136 * @param compositionId The UUID of the automation composition definition to delete
137 * @return the automation composition definition
139 @Transactional(readOnly = true)
140 public Optional<ToscaServiceTemplate> findAcDefinition(UUID compositionId) {
141 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
142 return jpaGet.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate)
143 .map(DocToscaServiceTemplate::toAuthorative).findFirst();
147 * Get Automation Composition Definitions.
149 * @return the Automation Composition Definitions found
151 @Transactional(readOnly = true)
152 public List<AutomationCompositionDefinition> getAllAcDefinitions() {
153 var jpaList = acmDefinitionRepository.findAll();
154 return ProviderUtils.asEntityList(jpaList);
158 * Get service templates.
160 * @param name the name of the topology template to get, set to null to get all service templates
161 * @param version the version of the service template to get, set to null to get all service templates
162 * @return the topology templates found
164 @Transactional(readOnly = true)
165 public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version) {
166 List<JpaAutomationCompositionDefinition> jpaList = null;
167 if (name != null || version != null) {
168 var entity = new JpaAutomationCompositionDefinition();
169 entity.setName(name);
170 entity.setVersion(version);
171 var example = Example.of(entity);
172 jpaList = acmDefinitionRepository.findAll(example);
174 jpaList = acmDefinitionRepository.findAll();
177 return jpaList.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate)
178 .map(DocToscaServiceTemplate::toAuthorative).collect(Collectors.toList());