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.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.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.models.base.PfModelRuntimeException;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
36 import org.springframework.data.domain.Example;
37 import org.springframework.stereotype.Service;
38 import org.springframework.transaction.annotation.Transactional;
42 @RequiredArgsConstructor
43 public class AcDefinitionProvider {
45 private final AutomationCompositionDefinitionRepository acmDefinitionRepository;
48 * Create Automation Composition Definition.
50 * @param serviceTemplate the service template to be created
51 * @return the created ACM Definition
53 public AutomationCompositionDefinition createAutomationCompositionDefinition(
54 final ToscaServiceTemplate serviceTemplate) {
55 var acmDefinition = new AutomationCompositionDefinition();
56 var compositionId = UUID.randomUUID();
57 acmDefinition.setCompositionId(compositionId);
58 if (serviceTemplate.getMetadata() == null) {
59 serviceTemplate.setMetadata(new HashMap<>());
61 serviceTemplate.getMetadata().put("compositionId", compositionId);
62 acmDefinition.setServiceTemplate(serviceTemplate);
63 var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acmDefinition, JpaAutomationCompositionDefinition::new,
64 "AutomationCompositionDefinition");
65 var result = acmDefinitionRepository.save(jpaAcmDefinition);
67 return result.toAuthorative();
71 * Update the ServiceTemplate.
73 * @param compositionId The UUID of the automation composition definition to delete
74 * @param serviceTemplate the service template to be created
76 public void updateServiceTemplate(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
77 var acmDefinition = new AutomationCompositionDefinition();
78 acmDefinition.setCompositionId(compositionId);
79 acmDefinition.setServiceTemplate(serviceTemplate);
80 var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acmDefinition, JpaAutomationCompositionDefinition::new,
81 "AutomationCompositionDefinition");
82 acmDefinitionRepository.save(jpaAcmDefinition);
86 * Delete Automation Composition Definition.
88 * @param compositionId The UUID of the automation composition definition to delete
89 * @return the TOSCA service template that was deleted
91 public ToscaServiceTemplate deleteAcDefintion(UUID compositionId) {
92 var jpaDelete = acmDefinitionRepository.findById(compositionId.toString());
93 if (jpaDelete.isEmpty()) {
94 String errorMessage = "delete of Automation Composition Definition \"" + compositionId
95 + "\" failed, Automation Composition Definition does not exist";
96 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
99 var item = jpaDelete.get().getServiceTemplate();
100 acmDefinitionRepository.deleteById(compositionId.toString());
101 return item.toAuthorative();
105 * Get the requested automation composition definitions.
107 * @param compositionId The UUID of the automation composition definition to delete
108 * @return the automation composition definition
110 @Transactional(readOnly = true)
111 public ToscaServiceTemplate getAcDefinition(UUID compositionId) {
112 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
113 if (jpaGet.isEmpty()) {
114 String errorMessage =
115 "Get serviceTemplate \"" + compositionId + "\" failed, serviceTemplate does not exist";
116 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
118 return jpaGet.get().getServiceTemplate().toAuthorative();
122 * Get the requested automation composition definition.
124 * @param compositionId The UUID of the automation composition definition to delete
125 * @return the automation composition definition
127 @Transactional(readOnly = true)
128 public Optional<ToscaServiceTemplate> findAcDefinition(UUID compositionId) {
129 var jpaGet = acmDefinitionRepository.findById(compositionId.toString());
130 return jpaGet.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate)
131 .map(DocToscaServiceTemplate::toAuthorative).findFirst();
135 * Get Automation Composition Definitions.
137 * @return the Automation Composition Definitions found
139 @Transactional(readOnly = true)
140 public List<AutomationCompositionDefinition> getAllAcDefinitions() {
141 var jpaList = acmDefinitionRepository.findAll();
142 return ProviderUtils.asEntityList(jpaList);
146 * Get service templates.
148 * @param name the name of the topology template to get, set to null to get all service templates
149 * @param version the version of the service template to get, set to null to get all service templates
150 * @return the topology templates found
152 @Transactional(readOnly = true)
153 public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version) {
154 List<JpaAutomationCompositionDefinition> jpaList = null;
155 if (name != null || version != null) {
156 var entity = new JpaAutomationCompositionDefinition();
157 entity.setName(name);
158 entity.setVersion(version);
159 var example = Example.of(entity);
160 jpaList = acmDefinitionRepository.findAll(example);
162 jpaList = acmDefinitionRepository.findAll();
165 return jpaList.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate)
166 .map(DocToscaServiceTemplate::toAuthorative).collect(Collectors.toList());