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.concepts.DocToscaServiceTemplate;
32 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationCompositionDefinition;
33 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionDefinitionRepository;
34 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
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.Isolation;
40 import org.springframework.transaction.annotation.Transactional;
44 @RequiredArgsConstructor
45 public class AcDefinitionProvider {
47 private final AutomationCompositionDefinitionRepository acmDefinitionRepository;
50 * Create Automation Composition Definition.
52 * @param serviceTemplate the service template to be created
53 * @return the created ACM Definition
55 public AutomationCompositionDefinition createAutomationCompositionDefinition(
56 final ToscaServiceTemplate serviceTemplate) {
57 var acmDefinition = new AutomationCompositionDefinition();
58 var compositionId = UUID.randomUUID();
59 acmDefinition.setCompositionId(compositionId);
60 acmDefinition.setState(AcTypeState.COMMISSIONED);
61 if (serviceTemplate.getMetadata() == null) {
62 serviceTemplate.setMetadata(new HashMap<>());
64 serviceTemplate.getMetadata().put("compositionId", compositionId);
65 acmDefinition.setServiceTemplate(serviceTemplate);
66 var acElements = AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate);
67 acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
68 var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acmDefinition, JpaAutomationCompositionDefinition::new,
69 "AutomationCompositionDefinition");
70 var result = acmDefinitionRepository.save(jpaAcmDefinition);
72 return result.toAuthorative();
76 * Update a commissioned ServiceTemplate.
78 * @param compositionId The UUID of the automation composition definition to delete
79 * @param serviceTemplate the service template to be created
81 public void updateServiceTemplate(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
82 var acmDefinition = new AutomationCompositionDefinition();
83 acmDefinition.setCompositionId(compositionId);
84 acmDefinition.setState(AcTypeState.COMMISSIONED);
85 acmDefinition.setServiceTemplate(serviceTemplate);
86 var acElements = AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate);
87 acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
88 updateAcDefinition(acmDefinition);
92 * Update the AutomationCompositionDefinition.
94 * @param acDefinition the AutomationCompositionDefinition to be updated
96 public void updateAcDefinition(AutomationCompositionDefinition acDefinition) {
97 var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acDefinition, JpaAutomationCompositionDefinition::new,
98 "AutomationCompositionDefinition");
99 acmDefinitionRepository.save(jpaAcmDefinition);
100 acmDefinitionRepository.flush();
104 * Delete Automation Composition Definition.
106 * @param compositionId The UUID of the automation composition definition to delete
107 * @return the TOSCA service template that was deleted
109 public ToscaServiceTemplate deleteAcDefintion(UUID compositionId) {
110 var jpaDelete = acmDefinitionRepository.findById(compositionId.toString());
111 if (jpaDelete.isEmpty()) {
112 String errorMessage = "delete of Automation Composition Definition \"" + compositionId
113 + "\" failed, Automation Composition Definition does not exist";
114 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
117 var item = jpaDelete.get().getServiceTemplate();
118 acmDefinitionRepository.deleteById(compositionId.toString());
119 return item.toAuthorative();
123 * Get the requested automation composition definitions.
125 * @param compositionId The UUID of the automation composition definition to delete
126 * @return the automation composition definition
128 @Transactional(readOnly = true)
129 public AutomationCompositionDefinition getAcDefinition(UUID compositionId) {
130 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
131 if (jpaGet.isEmpty()) {
132 String errorMessage =
133 "Get serviceTemplate \"" + compositionId + "\" failed, serviceTemplate does not exist";
134 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
136 return jpaGet.get().toAuthorative();
140 * Get the requested automation composition definition.
142 * @param compositionId The UUID of the automation composition definition to delete
143 * @return the automation composition definition
145 @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
146 public Optional<AutomationCompositionDefinition> findAcDefinition(UUID compositionId) {
147 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
148 return jpaGet.stream().map(JpaAutomationCompositionDefinition::toAuthorative).findFirst();
152 * Get Automation Composition Definitions.
154 * @return the Automation Composition Definitions found
156 @Transactional(readOnly = true)
157 public List<AutomationCompositionDefinition> getAllAcDefinitions() {
158 var jpaList = acmDefinitionRepository.findAll();
159 return ProviderUtils.asEntityList(jpaList);
163 * Get service templates.
165 * @param name the name of the topology template to get, set to null to get all service templates
166 * @param version the version of the service template to get, set to null to get all service templates
167 * @return the topology templates found
169 @Transactional(readOnly = true)
170 public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version) {
171 List<JpaAutomationCompositionDefinition> jpaList = null;
172 if (name != null || version != null) {
173 var entity = new JpaAutomationCompositionDefinition();
174 entity.setName(name);
175 entity.setVersion(version);
176 var example = Example.of(entity);
177 jpaList = acmDefinitionRepository.findAll(example);
179 jpaList = acmDefinitionRepository.findAll();
182 return jpaList.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate)
183 .map(DocToscaServiceTemplate::toAuthorative).toList();