2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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.List;
24 import java.util.UUID;
25 import javax.ws.rs.core.Response;
26 import lombok.RequiredArgsConstructor;
27 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
28 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationCompositionDefinition;
29 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionDefinitionRepository;
30 import org.onap.policy.clamp.models.acm.persistence.repository.ToscaServiceTemplateRepository;
31 import org.onap.policy.models.base.PfModelRuntimeException;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
33 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
34 import org.springframework.stereotype.Service;
35 import org.springframework.transaction.annotation.Transactional;
39 @RequiredArgsConstructor
40 public class AcDefinitionProvider {
42 private final ToscaServiceTemplateRepository serviceTemplateRepository;
43 private final AutomationCompositionDefinitionRepository acmDefinitionRepository;
46 * Create Automation Composition Definition.
48 * @param serviceTemplate the service template to be created
49 * @return the created ACM Definition
51 public AutomationCompositionDefinition createAutomationCompositionDefinition(
52 final ToscaServiceTemplate serviceTemplate) {
53 var acmDefinition = new AutomationCompositionDefinition();
54 acmDefinition.setCompositionId(UUID.randomUUID());
55 acmDefinition.setServiceTemplate(serviceTemplate);
56 var result = acmDefinitionRepository.save(new JpaAutomationCompositionDefinition(acmDefinition));
57 return result.toAuthorative();
61 * Update the ServiceTemplate.
63 * @param compositionId The UUID of the automation composition definition to delete
64 * @param serviceTemplate the service template to be created
66 public void updateServiceTemplate(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
67 var jpaServiceTemplate =
68 ProviderUtils.getJpaAndValidate(serviceTemplate, JpaToscaServiceTemplate::new, "toscaServiceTemplate");
69 serviceTemplateRepository.save(jpaServiceTemplate);
73 * Delete Automation Composition Definition.
75 * @param compositionId The UUID of the automation composition definition to delete
76 * @return the TOSCA service template that was deleted
78 public ToscaServiceTemplate deleteAcDefintion(UUID compositionId) {
79 var jpaDelete = acmDefinitionRepository.findById(compositionId.toString());
80 if (jpaDelete.isEmpty()) {
81 String errorMessage = "delete of Automation Composition Definition \"" + compositionId
82 + "\" failed, Automation Composition Definition does not exist";
83 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
86 var item = jpaDelete.get().getServiceTemplate();
87 serviceTemplateRepository.deleteById(item.getKey());
88 acmDefinitionRepository.deleteById(compositionId.toString());
89 return item.toAuthorative();
93 * Get the requested automation composition definitions.
95 * @param compositionId The UUID of the automation composition definition to delete
96 * @return the automation composition definition
98 @Transactional(readOnly = true)
99 public ToscaServiceTemplate getAcDefinition(UUID compositionId) {
100 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
101 if (jpaGet.isEmpty()) {
102 String errorMessage =
103 "Get serviceTemplate \"" + compositionId + "\" failed, serviceTemplate does not exist";
104 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
106 return jpaGet.get().getServiceTemplate().toAuthorative();
110 * Get service templates.
112 * @return the topology templates found
114 @Transactional(readOnly = true)
115 public List<ToscaServiceTemplate> getAllServiceTemplates() {
116 var jpaList = serviceTemplateRepository.findAll();
117 return ProviderUtils.asEntityList(jpaList);
121 * Get service templates.
123 * @param name the name of the topology template to get, set to null to get all service templates
124 * @param version the version of the service template to get, set to null to get all service templates
125 * @return the topology templates found
127 @Transactional(readOnly = true)
128 public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version) {
129 var jpaList = serviceTemplateRepository.getFiltered(JpaToscaServiceTemplate.class, name, version);
130 return ProviderUtils.asEntityList(jpaList);