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.clamp.models.acm.utils.AcmUtils;
36 import org.onap.policy.models.base.PfModelRuntimeException;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
38 import org.springframework.data.domain.Example;
39 import org.springframework.stereotype.Service;
40 import org.springframework.transaction.annotation.Isolation;
41 import org.springframework.transaction.annotation.Transactional;
45 @RequiredArgsConstructor
46 public class AcDefinitionProvider {
48 private final AutomationCompositionDefinitionRepository acmDefinitionRepository;
51 * Create Automation Composition Definition.
53 * @param serviceTemplate the service template to be created
54 * @return the created ACM Definition
56 public AutomationCompositionDefinition createAutomationCompositionDefinition(
57 final ToscaServiceTemplate serviceTemplate) {
58 var acmDefinition = new AutomationCompositionDefinition();
59 var compositionId = UUID.randomUUID();
60 acmDefinition.setCompositionId(compositionId);
61 acmDefinition.setState(AcTypeState.COMMISSIONED);
62 if (serviceTemplate.getMetadata() == null) {
63 serviceTemplate.setMetadata(new HashMap<>());
65 serviceTemplate.getMetadata().put("compositionId", compositionId);
66 acmDefinition.setServiceTemplate(serviceTemplate);
67 var acElements = AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate);
68 acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
69 var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acmDefinition, JpaAutomationCompositionDefinition::new,
70 "AutomationCompositionDefinition");
71 var result = acmDefinitionRepository.save(jpaAcmDefinition);
73 return result.toAuthorative();
77 * Update a commissioned ServiceTemplate.
79 * @param compositionId The UUID of the automation composition definition to delete
80 * @param serviceTemplate the service template to be created
82 public void updateServiceTemplate(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
83 var acmDefinition = new AutomationCompositionDefinition();
84 acmDefinition.setCompositionId(compositionId);
85 acmDefinition.setState(AcTypeState.COMMISSIONED);
86 acmDefinition.setServiceTemplate(serviceTemplate);
87 var acElements = AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate);
88 acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
89 updateAcDefinition(acmDefinition);
93 * Update the AutomationCompositionDefinition.
95 * @param acDefinition the AutomationCompositionDefinition to be updated
97 public void updateAcDefinition(AutomationCompositionDefinition acDefinition) {
98 var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acDefinition, JpaAutomationCompositionDefinition::new,
99 "AutomationCompositionDefinition");
100 acmDefinitionRepository.save(jpaAcmDefinition);
101 acmDefinitionRepository.flush();
105 * Delete Automation Composition Definition.
107 * @param compositionId The UUID of the automation composition definition to delete
108 * @return the TOSCA service template that was deleted
110 public ToscaServiceTemplate deleteAcDefintion(UUID compositionId) {
111 var jpaDelete = acmDefinitionRepository.findById(compositionId.toString());
112 if (jpaDelete.isEmpty()) {
113 String errorMessage = "delete of Automation Composition Definition \"" + compositionId
114 + "\" failed, Automation Composition Definition does not exist";
115 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
118 var item = jpaDelete.get().getServiceTemplate();
119 acmDefinitionRepository.deleteById(compositionId.toString());
120 return item.toAuthorative();
124 * Get the requested automation composition definitions.
126 * @param compositionId The UUID of the automation composition definition to delete
127 * @return the automation composition definition
129 @Transactional(readOnly = true)
130 public AutomationCompositionDefinition getAcDefinition(UUID compositionId) {
131 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
132 if (jpaGet.isEmpty()) {
133 String errorMessage =
134 "Get serviceTemplate \"" + compositionId + "\" failed, serviceTemplate does not exist";
135 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
137 return jpaGet.get().toAuthorative();
141 * Get the requested automation composition definition.
143 * @param compositionId The UUID of the automation composition definition to delete
144 * @return the automation composition definition
146 @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
147 public Optional<AutomationCompositionDefinition> findAcDefinition(UUID compositionId) {
148 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
149 return jpaGet.stream().map(JpaAutomationCompositionDefinition::toAuthorative).findFirst();
153 * Get Automation Composition Definitions.
155 * @return the Automation Composition Definitions found
157 @Transactional(readOnly = true)
158 public List<AutomationCompositionDefinition> getAllAcDefinitions() {
159 var jpaList = acmDefinitionRepository.findAll();
160 return ProviderUtils.asEntityList(jpaList);
164 * Get service templates.
166 * @param name the name of the topology template to get, set to null to get all service templates
167 * @param version the version of the service template to get, set to null to get all service templates
168 * @return the topology templates found
170 @Transactional(readOnly = true)
171 public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version) {
172 List<JpaAutomationCompositionDefinition> jpaList = null;
173 if (name != null || version != null) {
174 var entity = new JpaAutomationCompositionDefinition();
175 entity.setName(name);
176 entity.setVersion(version);
177 var example = Example.of(entity);
178 jpaList = acmDefinitionRepository.findAll(example);
180 jpaList = acmDefinitionRepository.findAll();
183 return jpaList.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate)
184 .map(DocToscaServiceTemplate::toAuthorative).collect(Collectors.toList());