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 if (acElements.isEmpty()) {
70 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
71 "NodeTemplate with element type " + toscaElementName + " must exist!");
73 acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
74 var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acmDefinition, JpaAutomationCompositionDefinition::new,
75 "AutomationCompositionDefinition");
76 var validationResult = new BeanValidationResult("AutomationCompositionDefinition", acmDefinition);
77 ToscaServiceTemplateValidation.validate(validationResult, jpaAcmDefinition.getServiceTemplate(),
78 toscaCompositionName);
79 if (! validationResult.isValid()) {
80 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
82 var result = acmDefinitionRepository.save(jpaAcmDefinition);
84 return result.toAuthorative();
88 * Update a commissioned ServiceTemplate.
90 * @param compositionId The UUID of the automation composition definition to delete
91 * @param serviceTemplate the service template to be created
93 public void updateServiceTemplate(UUID compositionId, ToscaServiceTemplate serviceTemplate, String toscaElementName,
94 String toscaCompositionName) {
95 var acmDefinition = new AutomationCompositionDefinition();
96 acmDefinition.setCompositionId(compositionId);
97 acmDefinition.setState(AcTypeState.COMMISSIONED);
98 acmDefinition.setServiceTemplate(serviceTemplate);
100 AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate, toscaElementName);
101 if (acElements.isEmpty()) {
102 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
103 "NodeTemplate with element type " + toscaElementName + " must exist!");
105 acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
106 updateAcDefinition(acmDefinition, toscaCompositionName);
110 * Update the AutomationCompositionDefinition.
112 * @param acDefinition the AutomationCompositionDefinition to be updated
114 public void updateAcDefinition(AutomationCompositionDefinition acDefinition, String toscaCompositionName) {
115 var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acDefinition, JpaAutomationCompositionDefinition::new,
116 "AutomationCompositionDefinition");
117 var validationResult = new BeanValidationResult("AutomationCompositionDefinition", acDefinition);
118 ToscaServiceTemplateValidation.validate(validationResult, jpaAcmDefinition.getServiceTemplate(),
119 toscaCompositionName);
120 if (! validationResult.isValid()) {
121 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
123 acmDefinitionRepository.save(jpaAcmDefinition);
124 acmDefinitionRepository.flush();
128 * Delete Automation Composition Definition.
130 * @param compositionId The UUID of the automation composition definition to delete
131 * @return the TOSCA service template that was deleted
133 public ToscaServiceTemplate deleteAcDefintion(UUID compositionId) {
134 var jpaDelete = acmDefinitionRepository.findById(compositionId.toString());
135 if (jpaDelete.isEmpty()) {
136 String errorMessage = "delete of Automation Composition Definition \"" + compositionId
137 + "\" failed, Automation Composition Definition does not exist";
138 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
141 var item = jpaDelete.get().getServiceTemplate();
142 acmDefinitionRepository.deleteById(compositionId.toString());
143 return item.toAuthorative();
147 * Get the requested automation composition definitions.
149 * @param compositionId The UUID of the automation composition definition to delete
150 * @return the automation composition definition
152 @Transactional(readOnly = true)
153 public AutomationCompositionDefinition getAcDefinition(UUID compositionId) {
154 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
155 if (jpaGet.isEmpty()) {
156 String errorMessage =
157 "Get serviceTemplate \"" + compositionId + "\" failed, serviceTemplate does not exist";
158 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
160 return jpaGet.get().toAuthorative();
164 * Get the requested automation composition definition.
166 * @param compositionId The UUID of the automation composition definition to delete
167 * @return the automation composition definition
169 @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
170 public Optional<AutomationCompositionDefinition> findAcDefinition(UUID compositionId) {
171 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
172 return jpaGet.stream().map(JpaAutomationCompositionDefinition::toAuthorative).findFirst();
176 * Get Automation Composition Definitions.
178 * @return the Automation Composition Definitions found
180 @Transactional(readOnly = true)
181 public List<AutomationCompositionDefinition> getAllAcDefinitions() {
182 var jpaList = acmDefinitionRepository.findAll();
183 return ProviderUtils.asEntityList(jpaList);
187 * Get service templates.
189 * @param name the name of the topology template to get, set to null to get all service templates
190 * @param version the version of the service template to get, set to null to get all service templates
191 * @return the topology templates found
193 @Transactional(readOnly = true)
194 public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version) {
195 List<JpaAutomationCompositionDefinition> jpaList = null;
196 if (name != null || version != null) {
197 var entity = new JpaAutomationCompositionDefinition();
198 entity.setName(name);
199 entity.setVersion(version);
200 var example = Example.of(entity);
201 jpaList = acmDefinitionRepository.findAll(example);
203 jpaList = acmDefinitionRepository.findAll();
206 return jpaList.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate)
207 .map(DocToscaServiceTemplate::toAuthorative).toList();