2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2025 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;
28 import java.util.UUID;
29 import java.util.stream.Collectors;
30 import lombok.RequiredArgsConstructor;
31 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
33 import org.onap.policy.clamp.models.acm.document.base.ToscaServiceTemplateValidation;
34 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaServiceTemplate;
35 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationCompositionDefinition;
36 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionDefinitionRepository;
37 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
38 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
39 import org.onap.policy.common.parameters.BeanValidationResult;
40 import org.onap.policy.models.base.PfModelRuntimeException;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
42 import org.springframework.data.domain.Example;
43 import org.springframework.stereotype.Service;
44 import org.springframework.transaction.annotation.Transactional;
48 @RequiredArgsConstructor
49 public class AcDefinitionProvider {
51 private static final String NAME = "AutomationCompositionDefinition";
53 private final AutomationCompositionDefinitionRepository acmDefinitionRepository;
56 * Create Automation Composition Definition.
58 * @param serviceTemplate the service template to be created
59 * @return the created ACM Definition
61 public AutomationCompositionDefinition createAutomationCompositionDefinition(
62 final ToscaServiceTemplate serviceTemplate, final String toscaElementName, String toscaCompositionName) {
63 var acmDefinition = new AutomationCompositionDefinition();
64 var compositionId = UUID.randomUUID();
65 acmDefinition.setCompositionId(compositionId);
66 acmDefinition.setState(AcTypeState.COMMISSIONED);
67 if (serviceTemplate.getMetadata() == null) {
68 serviceTemplate.setMetadata(new HashMap<>());
70 acmDefinition.setLastMsg(TimestampHelper.now());
71 serviceTemplate.getMetadata().put("compositionId", compositionId);
72 acmDefinition.setServiceTemplate(serviceTemplate);
73 var acElements = AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate, toscaElementName);
74 if (acElements.isEmpty()) {
75 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
76 "NodeTemplate with element type " + toscaElementName + " must exist!");
78 acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
79 var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acmDefinition, JpaAutomationCompositionDefinition::new,
80 acmDefinition.getClass().getSimpleName());
81 var validationResult = new BeanValidationResult(acmDefinition.getClass().getSimpleName(), acmDefinition);
82 ToscaServiceTemplateValidation.validate(validationResult, jpaAcmDefinition.getServiceTemplate(),
83 toscaCompositionName);
84 if (! validationResult.isValid()) {
85 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
87 var result = acmDefinitionRepository.save(jpaAcmDefinition);
89 return result.toAuthorative();
93 * Update a commissioned ServiceTemplate.
95 * @param compositionId The UUID of the automation composition definition to delete
96 * @param serviceTemplate the service template to be created
98 public void updateServiceTemplate(UUID compositionId, ToscaServiceTemplate serviceTemplate, String toscaElementName,
99 String toscaCompositionName) {
100 var acmDefinition = new AutomationCompositionDefinition();
101 acmDefinition.setCompositionId(compositionId);
102 acmDefinition.setState(AcTypeState.COMMISSIONED);
103 acmDefinition.setLastMsg(TimestampHelper.now());
104 acmDefinition.setServiceTemplate(serviceTemplate);
106 AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate, toscaElementName);
107 if (acElements.isEmpty()) {
108 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
109 "NodeTemplate with element type " + toscaElementName + " must exist!");
111 acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
112 updateAcDefinition(acmDefinition, toscaCompositionName);
116 * Update the AutomationCompositionDefinition.
118 * @param acDefinition the AutomationCompositionDefinition to be updated
120 public void updateAcDefinition(AutomationCompositionDefinition acDefinition, String toscaCompositionName) {
121 var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acDefinition, JpaAutomationCompositionDefinition::new,
123 var validationResult = new BeanValidationResult(NAME, acDefinition);
124 ToscaServiceTemplateValidation.validate(validationResult, jpaAcmDefinition.getServiceTemplate(),
125 toscaCompositionName);
126 if (! validationResult.isValid()) {
127 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
129 acmDefinitionRepository.save(jpaAcmDefinition);
130 acmDefinitionRepository.flush();
134 * Update Ac Definition with unchanged service template.
136 * @param acDefinition the AutomationCompositionDefinition to be updated
138 public void updateAcDefinitionState(AutomationCompositionDefinition acDefinition) {
139 var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acDefinition, JpaAutomationCompositionDefinition::new,
141 acmDefinitionRepository.save(jpaAcmDefinition);
142 acmDefinitionRepository.flush();
146 * Delete Automation Composition Definition.
148 * @param compositionId The UUID of the automation composition definition to delete
149 * @return the TOSCA service template that was deleted
151 public ToscaServiceTemplate deleteAcDefinition(UUID compositionId) {
152 var jpaDelete = acmDefinitionRepository.findById(compositionId.toString());
153 if (jpaDelete.isEmpty()) {
154 String errorMessage = "delete of Automation Composition Definition \"" + compositionId
155 + "\" failed, Automation Composition Definition does not exist";
156 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
159 var item = jpaDelete.get().getServiceTemplate();
160 acmDefinitionRepository.deleteById(compositionId.toString());
161 return item.toAuthorative();
165 * Get the requested automation composition definitions.
167 * @param compositionId The UUID of the automation composition definition to delete
168 * @return the automation composition definition
170 @Transactional(readOnly = true)
171 public AutomationCompositionDefinition getAcDefinition(UUID compositionId) {
172 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
173 if (jpaGet.isEmpty()) {
174 String errorMessage =
175 "Get serviceTemplate \"" + compositionId + "\" failed, serviceTemplate does not exist";
176 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
178 return jpaGet.get().toAuthorative();
182 * Get the requested automation composition definition.
184 * @param compositionId The UUID of the automation composition definition to delete
185 * @return the automation composition definition
187 @Transactional(readOnly = true)
188 public Optional<AutomationCompositionDefinition> findAcDefinition(UUID compositionId) {
189 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
190 return jpaGet.stream().map(JpaAutomationCompositionDefinition::toAuthorative).findFirst();
194 * Get Automation Composition Definitions in transition.
196 * @return the Automation Composition Definitions found
198 @Transactional(readOnly = true)
199 public Set<UUID> getAllAcDefinitionsInTransition() {
200 var jpaList = acmDefinitionRepository.findByStateIn(List.of(AcTypeState.PRIMING, AcTypeState.DEPRIMING));
201 return jpaList.stream().map(JpaAutomationCompositionDefinition::getCompositionId)
202 .map(UUID::fromString).collect(Collectors.toSet());
206 * Get service templates.
208 * @param name the name of the topology template to get, set to null to get all service templates
209 * @param version the version of the service template to get, set to null to get all service templates
210 * @return the topology templates found
212 @Transactional(readOnly = true)
213 public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version) {
214 List<JpaAutomationCompositionDefinition> jpaList = null;
215 if (name != null || version != null) {
216 var entity = new JpaAutomationCompositionDefinition();
217 entity.setName(name);
218 entity.setVersion(version);
219 var example = Example.of(entity);
220 jpaList = acmDefinitionRepository.findAll(example);
222 jpaList = acmDefinitionRepository.findAll();
225 return jpaList.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate)
226 .map(DocToscaServiceTemplate::toAuthorative).toList();