2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2024 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.concepts.NodeTemplateState;
32 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
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.concepts.JpaNodeTemplateState;
37 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionDefinitionRepository;
38 import org.onap.policy.clamp.models.acm.persistence.repository.NodeTemplateStateRepository;
39 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
40 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
41 import org.onap.policy.common.parameters.BeanValidationResult;
42 import org.onap.policy.models.base.PfModelRuntimeException;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 import org.springframework.data.domain.Example;
45 import org.springframework.stereotype.Service;
46 import org.springframework.transaction.annotation.Isolation;
47 import org.springframework.transaction.annotation.Transactional;
51 @RequiredArgsConstructor
52 public class AcDefinitionProvider {
54 private final AutomationCompositionDefinitionRepository acmDefinitionRepository;
55 private final NodeTemplateStateRepository nodeTemplateStateRepository;
58 * Create Automation Composition Definition.
60 * @param serviceTemplate the service template to be created
61 * @return the created ACM Definition
63 public AutomationCompositionDefinition createAutomationCompositionDefinition(
64 final ToscaServiceTemplate serviceTemplate, final String toscaElementName, String toscaCompositionName) {
65 var acmDefinition = new AutomationCompositionDefinition();
66 var compositionId = UUID.randomUUID();
67 acmDefinition.setCompositionId(compositionId);
68 acmDefinition.setState(AcTypeState.COMMISSIONED);
69 if (serviceTemplate.getMetadata() == null) {
70 serviceTemplate.setMetadata(new HashMap<>());
72 acmDefinition.setLastMsg(TimestampHelper.now());
73 serviceTemplate.getMetadata().put("compositionId", compositionId);
74 acmDefinition.setServiceTemplate(serviceTemplate);
75 var acElements = AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate, toscaElementName);
76 if (acElements.isEmpty()) {
77 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
78 "NodeTemplate with element type " + toscaElementName + " must exist!");
80 acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
81 var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acmDefinition, JpaAutomationCompositionDefinition::new,
82 acmDefinition.getClass().getSimpleName());
83 var validationResult = new BeanValidationResult(acmDefinition.getClass().getSimpleName(), acmDefinition);
84 ToscaServiceTemplateValidation.validate(validationResult, jpaAcmDefinition.getServiceTemplate(),
85 toscaCompositionName);
86 if (! validationResult.isValid()) {
87 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
89 var result = acmDefinitionRepository.save(jpaAcmDefinition);
91 return result.toAuthorative();
95 * Update a commissioned ServiceTemplate.
97 * @param compositionId The UUID of the automation composition definition to delete
98 * @param serviceTemplate the service template to be created
100 public void updateServiceTemplate(UUID compositionId, ToscaServiceTemplate serviceTemplate, String toscaElementName,
101 String toscaCompositionName) {
102 var acmDefinition = new AutomationCompositionDefinition();
103 acmDefinition.setCompositionId(compositionId);
104 acmDefinition.setState(AcTypeState.COMMISSIONED);
105 acmDefinition.setLastMsg(TimestampHelper.now());
106 acmDefinition.setServiceTemplate(serviceTemplate);
108 AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate, toscaElementName);
109 if (acElements.isEmpty()) {
110 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
111 "NodeTemplate with element type " + toscaElementName + " must exist!");
113 acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
114 updateAcDefinition(acmDefinition, toscaCompositionName);
118 * Update the AutomationCompositionDefinition.
120 * @param acDefinition the AutomationCompositionDefinition to be updated
122 public void updateAcDefinition(AutomationCompositionDefinition acDefinition, String toscaCompositionName) {
123 var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acDefinition, JpaAutomationCompositionDefinition::new,
124 "AutomationCompositionDefinition");
125 var validationResult = new BeanValidationResult("AutomationCompositionDefinition", acDefinition);
126 ToscaServiceTemplateValidation.validate(validationResult, jpaAcmDefinition.getServiceTemplate(),
127 toscaCompositionName);
128 if (! validationResult.isValid()) {
129 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
131 acmDefinitionRepository.save(jpaAcmDefinition);
132 acmDefinitionRepository.flush();
136 * Update Ac Definition AcTypeState, StateChangeResult and restarting.
138 * @param compositionId The UUID of the automation composition definition to update
139 * @param state the AcTypeState
140 * @param stateChangeResult the StateChangeResult
141 * @param restarting restarting process
143 public void updateAcDefinitionState(UUID compositionId, AcTypeState state, StateChangeResult stateChangeResult,
144 Boolean restarting) {
145 var jpaUpdate = acmDefinitionRepository.findById(compositionId.toString());
146 if (jpaUpdate.isEmpty()) {
147 String errorMessage = "update of Automation Composition Definition \"" + compositionId
148 + "\" failed, Automation Composition Definition does not exist";
149 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
151 var acDefinition = jpaUpdate.get();
152 acDefinition.setState(state);
153 acDefinition.setStateChangeResult(stateChangeResult);
154 acDefinition.setRestarting(restarting);
155 acmDefinitionRepository.save(acDefinition);
156 acmDefinitionRepository.flush();
160 * Update Ac DefinitionElement.
162 * @param nodeTemplateState the NodeTemplateState
163 * @param compositionId The UUID of the automation composition definition
165 public void updateAcDefinitionElement(NodeTemplateState nodeTemplateState, UUID compositionId) {
166 var jpaNodeTemplateState = new JpaNodeTemplateState(
167 nodeTemplateState.getNodeTemplateStateId().toString(), compositionId.toString());
168 jpaNodeTemplateState.fromAuthorative(nodeTemplateState);
169 nodeTemplateStateRepository.save(jpaNodeTemplateState);
173 * Delete Automation Composition Definition.
175 * @param compositionId The UUID of the automation composition definition to delete
176 * @return the TOSCA service template that was deleted
178 public ToscaServiceTemplate deleteAcDefintion(UUID compositionId) {
179 var jpaDelete = acmDefinitionRepository.findById(compositionId.toString());
180 if (jpaDelete.isEmpty()) {
181 String errorMessage = "delete of Automation Composition Definition \"" + compositionId
182 + "\" failed, Automation Composition Definition does not exist";
183 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
186 var item = jpaDelete.get().getServiceTemplate();
187 acmDefinitionRepository.deleteById(compositionId.toString());
188 return item.toAuthorative();
192 * Get the requested automation composition definitions.
194 * @param compositionId The UUID of the automation composition definition to delete
195 * @return the automation composition definition
197 @Transactional(readOnly = true)
198 public AutomationCompositionDefinition getAcDefinition(UUID compositionId) {
199 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
200 if (jpaGet.isEmpty()) {
201 String errorMessage =
202 "Get serviceTemplate \"" + compositionId + "\" failed, serviceTemplate does not exist";
203 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
205 return jpaGet.get().toAuthorative();
209 * Get the requested automation composition definition.
211 * @param compositionId The UUID of the automation composition definition to delete
212 * @return the automation composition definition
214 @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
215 public Optional<AutomationCompositionDefinition> findAcDefinition(UUID compositionId) {
216 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
217 return jpaGet.stream().map(JpaAutomationCompositionDefinition::toAuthorative).findFirst();
221 * Get Automation Composition Definitions in transition.
223 * @return the Automation Composition Definitions found
225 @Transactional(readOnly = true)
226 public List<AutomationCompositionDefinition> getAllAcDefinitionsInTransition() {
227 var jpaList = acmDefinitionRepository.findByStateIn(List.of(AcTypeState.PRIMING, AcTypeState.DEPRIMING));
228 return ProviderUtils.asEntityList(jpaList);
232 * Get service templates.
234 * @param name the name of the topology template to get, set to null to get all service templates
235 * @param version the version of the service template to get, set to null to get all service templates
236 * @return the topology templates found
238 @Transactional(readOnly = true)
239 public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version) {
240 List<JpaAutomationCompositionDefinition> jpaList = null;
241 if (name != null || version != null) {
242 var entity = new JpaAutomationCompositionDefinition();
243 entity.setName(name);
244 entity.setVersion(version);
245 var example = Example.of(entity);
246 jpaList = acmDefinitionRepository.findAll(example);
248 jpaList = acmDefinitionRepository.findAll();
251 return jpaList.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate)
252 .map(DocToscaServiceTemplate::toAuthorative).toList();