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.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);
103 * Delete Automation Composition Definition.
105 * @param compositionId The UUID of the automation composition definition to delete
106 * @return the TOSCA service template that was deleted
108 public ToscaServiceTemplate deleteAcDefintion(UUID compositionId) {
109 var jpaDelete = acmDefinitionRepository.findById(compositionId.toString());
110 if (jpaDelete.isEmpty()) {
111 String errorMessage = "delete of Automation Composition Definition \"" + compositionId
112 + "\" failed, Automation Composition Definition does not exist";
113 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
116 var item = jpaDelete.get().getServiceTemplate();
117 acmDefinitionRepository.deleteById(compositionId.toString());
118 return item.toAuthorative();
122 * Get the requested automation composition definitions.
124 * @param compositionId The UUID of the automation composition definition to delete
125 * @return the automation composition definition
127 @Transactional(readOnly = true)
128 public AutomationCompositionDefinition getAcDefinition(UUID compositionId) {
129 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
130 if (jpaGet.isEmpty()) {
131 String errorMessage =
132 "Get serviceTemplate \"" + compositionId + "\" failed, serviceTemplate does not exist";
133 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
135 return jpaGet.get().toAuthorative();
139 * Get the requested automation composition definition.
141 * @param compositionId The UUID of the automation composition definition to delete
142 * @return the automation composition definition
144 @Transactional(readOnly = true)
145 public Optional<AutomationCompositionDefinition> findAcDefinition(UUID compositionId) {
146 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
147 return jpaGet.stream().map(JpaAutomationCompositionDefinition::toAuthorative).findFirst();
151 * Get Automation Composition Definitions.
153 * @return the Automation Composition Definitions found
155 @Transactional(readOnly = true)
156 public List<AutomationCompositionDefinition> getAllAcDefinitions() {
157 var jpaList = acmDefinitionRepository.findAll();
158 return ProviderUtils.asEntityList(jpaList);
162 * Get service templates.
164 * @param name the name of the topology template to get, set to null to get all service templates
165 * @param version the version of the service template to get, set to null to get all service templates
166 * @return the topology templates found
168 @Transactional(readOnly = true)
169 public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version) {
170 List<JpaAutomationCompositionDefinition> jpaList = null;
171 if (name != null || version != null) {
172 var entity = new JpaAutomationCompositionDefinition();
173 entity.setName(name);
174 entity.setVersion(version);
175 var example = Example.of(entity);
176 jpaList = acmDefinitionRepository.findAll(example);
178 jpaList = acmDefinitionRepository.findAll();
181 return jpaList.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate)
182 .map(DocToscaServiceTemplate::toAuthorative).collect(Collectors.toList());