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 jakarta.ws.rs.core.Response;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Optional;
27 import java.util.UUID;
28 import lombok.RequiredArgsConstructor;
29 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
30 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
31 import org.onap.policy.clamp.models.acm.document.base.ToscaServiceTemplateValidation;
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.clamp.models.acm.utils.AcmUtils;
36 import org.onap.policy.common.parameters.BeanValidationResult;
37 import org.onap.policy.models.base.PfModelRuntimeException;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
39 import org.springframework.data.domain.Example;
40 import org.springframework.stereotype.Service;
41 import org.springframework.transaction.annotation.Isolation;
42 import org.springframework.transaction.annotation.Transactional;
46 @RequiredArgsConstructor
47 public class AcDefinitionProvider {
49 private final AutomationCompositionDefinitionRepository acmDefinitionRepository;
52 * Create Automation Composition Definition.
54 * @param serviceTemplate the service template to be created
55 * @return the created ACM Definition
57 public AutomationCompositionDefinition createAutomationCompositionDefinition(
58 final ToscaServiceTemplate serviceTemplate, final String toscaElementName, String toscaCompositionName) {
59 var acmDefinition = new AutomationCompositionDefinition();
60 var compositionId = UUID.randomUUID();
61 acmDefinition.setCompositionId(compositionId);
62 acmDefinition.setState(AcTypeState.COMMISSIONED);
63 if (serviceTemplate.getMetadata() == null) {
64 serviceTemplate.setMetadata(new HashMap<>());
66 serviceTemplate.getMetadata().put("compositionId", compositionId);
67 acmDefinition.setServiceTemplate(serviceTemplate);
68 var acElements = AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate, toscaElementName);
69 acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
70 var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acmDefinition, JpaAutomationCompositionDefinition::new,
71 "AutomationCompositionDefinition");
72 var validationResult = new BeanValidationResult("AutomationCompositionDefinition", acmDefinition);
73 ToscaServiceTemplateValidation.validate(validationResult, jpaAcmDefinition.getServiceTemplate(),
74 toscaCompositionName);
75 if (! validationResult.isValid()) {
76 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
78 var result = acmDefinitionRepository.save(jpaAcmDefinition);
80 return result.toAuthorative();
84 * Update a commissioned ServiceTemplate.
86 * @param compositionId The UUID of the automation composition definition to delete
87 * @param serviceTemplate the service template to be created
89 public void updateServiceTemplate(UUID compositionId, ToscaServiceTemplate serviceTemplate, String toscaElementName,
90 String toscaCompositionName) {
91 var acmDefinition = new AutomationCompositionDefinition();
92 acmDefinition.setCompositionId(compositionId);
93 acmDefinition.setState(AcTypeState.COMMISSIONED);
94 acmDefinition.setServiceTemplate(serviceTemplate);
96 AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate, toscaElementName);
97 acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
98 updateAcDefinition(acmDefinition, toscaCompositionName);
102 * Update the AutomationCompositionDefinition.
104 * @param acDefinition the AutomationCompositionDefinition to be updated
106 public void updateAcDefinition(AutomationCompositionDefinition acDefinition, String toscaCompositionName) {
107 var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acDefinition, JpaAutomationCompositionDefinition::new,
108 "AutomationCompositionDefinition");
109 var validationResult = new BeanValidationResult("AutomationCompositionDefinition", acDefinition);
110 ToscaServiceTemplateValidation.validate(validationResult, jpaAcmDefinition.getServiceTemplate(),
111 toscaCompositionName);
112 if (! validationResult.isValid()) {
113 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
115 acmDefinitionRepository.save(jpaAcmDefinition);
116 acmDefinitionRepository.flush();
120 * Delete Automation Composition Definition.
122 * @param compositionId The UUID of the automation composition definition to delete
123 * @return the TOSCA service template that was deleted
125 public ToscaServiceTemplate deleteAcDefintion(UUID compositionId) {
126 var jpaDelete = acmDefinitionRepository.findById(compositionId.toString());
127 if (jpaDelete.isEmpty()) {
128 String errorMessage = "delete of Automation Composition Definition \"" + compositionId
129 + "\" failed, Automation Composition Definition does not exist";
130 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
133 var item = jpaDelete.get().getServiceTemplate();
134 acmDefinitionRepository.deleteById(compositionId.toString());
135 return item.toAuthorative();
139 * Get the requested automation composition definitions.
141 * @param compositionId The UUID of the automation composition definition to delete
142 * @return the automation composition definition
144 @Transactional(readOnly = true)
145 public AutomationCompositionDefinition getAcDefinition(UUID compositionId) {
146 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
147 if (jpaGet.isEmpty()) {
148 String errorMessage =
149 "Get serviceTemplate \"" + compositionId + "\" failed, serviceTemplate does not exist";
150 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
152 return jpaGet.get().toAuthorative();
156 * Get the requested automation composition definition.
158 * @param compositionId The UUID of the automation composition definition to delete
159 * @return the automation composition definition
161 @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
162 public Optional<AutomationCompositionDefinition> findAcDefinition(UUID compositionId) {
163 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
164 return jpaGet.stream().map(JpaAutomationCompositionDefinition::toAuthorative).findFirst();
168 * Get Automation Composition Definitions.
170 * @return the Automation Composition Definitions found
172 @Transactional(readOnly = true)
173 public List<AutomationCompositionDefinition> getAllAcDefinitions() {
174 var jpaList = acmDefinitionRepository.findAll();
175 return ProviderUtils.asEntityList(jpaList);
179 * Get service templates.
181 * @param name the name of the topology template to get, set to null to get all service templates
182 * @param version the version of the service template to get, set to null to get all service templates
183 * @return the topology templates found
185 @Transactional(readOnly = true)
186 public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version) {
187 List<JpaAutomationCompositionDefinition> jpaList = null;
188 if (name != null || version != null) {
189 var entity = new JpaAutomationCompositionDefinition();
190 entity.setName(name);
191 entity.setVersion(version);
192 var example = Example.of(entity);
193 jpaList = acmDefinitionRepository.findAll(example);
195 jpaList = acmDefinitionRepository.findAll();
198 return jpaList.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate)
199 .map(DocToscaServiceTemplate::toAuthorative).toList();