2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.clamp.models.acm.persistence.provider;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.Optional;
29 import javax.persistence.EntityNotFoundException;
30 import javax.ws.rs.core.Response;
31 import javax.ws.rs.core.Response.Status;
32 import lombok.AllArgsConstructor;
33 import lombok.NonNull;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
35 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationComposition;
36 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionRepository;
37 import org.onap.policy.clamp.models.acm.persistence.repository.ToscaNodeTemplateRepository;
38 import org.onap.policy.clamp.models.acm.persistence.repository.ToscaNodeTemplatesRepository;
39 import org.onap.policy.models.base.PfConceptKey;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
45 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;
46 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplates;
47 import org.springframework.stereotype.Service;
48 import org.springframework.transaction.annotation.Transactional;
51 * This class provides information on automation composition concepts in the database to callers.
56 public class AutomationCompositionProvider {
58 private final AutomationCompositionRepository automationCompositionRepository;
59 private final ToscaNodeTemplateRepository toscaNodeTemplateRepository;
60 private final ToscaNodeTemplatesRepository toscaNodeTemplatesRepository;
63 * Get automation composition.
65 * @param automationCompositionId the ID of the automation composition to get
66 * @return the automation composition found
67 * @throws PfModelException on errors getting the automation composition
69 @Transactional(readOnly = true)
70 public AutomationComposition getAutomationComposition(final ToscaConceptIdentifier automationCompositionId)
71 throws PfModelException {
73 return automationCompositionRepository.getById(automationCompositionId.asConceptKey()).toAuthorative();
74 } catch (EntityNotFoundException e) {
75 throw new PfModelException(Status.NOT_FOUND, "AutomationComposition not found", e);
80 * Find automation composition by automationCompositionId.
82 * @param name the name of the automation composition to get, null to get all automation compositions
83 * @param version the version of the automation composition to get, null to get all automation compositions
84 * @return the automation composition found
85 * @throws PfModelException on errors getting the automation composition
87 @Transactional(readOnly = true)
88 public Optional<AutomationComposition> findAutomationComposition(@NonNull final String name,
89 @NonNull final String version) throws PfModelException {
90 return findAutomationComposition(new PfConceptKey(name, version));
94 * Find automation composition by automationCompositionId.
96 * @param automationCompositionId the ID of the automation composition to get
97 * @return the automation composition found
98 * @throws PfModelException on errors getting the automation composition
100 @Transactional(readOnly = true)
101 public Optional<AutomationComposition> findAutomationComposition(
102 final ToscaConceptIdentifier automationCompositionId) throws PfModelException {
103 return findAutomationComposition(automationCompositionId.asConceptKey());
106 private Optional<AutomationComposition> findAutomationComposition(@NonNull final PfConceptKey key)
107 throws PfModelException {
109 return automationCompositionRepository.findById(key).map(JpaAutomationComposition::toAuthorative);
110 } catch (IllegalArgumentException e) {
111 throw new PfModelException(Status.BAD_REQUEST, "Not valid parameter", e);
116 * Save automation composition.
118 * @param automationComposition the automation composition to update
119 * @return the updated automation composition
120 * @throws PfModelException on errors updating the automation composition
122 public AutomationComposition saveAutomationComposition(final AutomationComposition automationComposition)
123 throws PfModelException {
125 var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
126 JpaAutomationComposition::new, "automation composition"));
128 // Return the saved participant
129 return result.toAuthorative();
130 } catch (IllegalArgumentException e) {
131 throw new PfModelException(Status.BAD_REQUEST, "Error in save automationComposition", e);
136 * Get all automation compositions.
138 * @return all automation compositions found
139 * @throws PfModelException on errors getting automation compositions
141 @Transactional(readOnly = true)
142 public List<AutomationComposition> getAutomationCompositions() throws PfModelException {
144 return ProviderUtils.asEntityList(automationCompositionRepository.findAll());
148 * Get automation compositions.
150 * @param name the name of the automation composition to get, null to get all automation compositions
151 * @param version the version of the automation composition to get, null to get all automation compositions
152 * @return the automation compositions found
153 * @throws PfModelException on errors getting automation compositions
155 @Transactional(readOnly = true)
156 public List<AutomationComposition> getAutomationCompositions(final String name, final String version)
157 throws PfModelException {
160 .asEntityList(automationCompositionRepository.getFiltered(JpaAutomationComposition.class, name, version));
164 * Saves automation compositions.
166 * @param automationCompositions a specification of the automation compositions to create
167 * @return the automation compositions created
168 * @throws PfModelException on errors creating automation compositions
170 public List<AutomationComposition> saveAutomationCompositions(
171 @NonNull final List<AutomationComposition> automationCompositions) throws PfModelException {
174 automationCompositionRepository.saveAll(ProviderUtils.getJpaAndValidateList(automationCompositions,
175 JpaAutomationComposition::new, "automation compositions"));
177 // Return the saved participant
178 return ProviderUtils.asEntityList(result);
179 } catch (IllegalArgumentException e) {
180 throw new PfModelException(Status.BAD_REQUEST, "Error in save AutomationCompositions", e);
185 * Saves Instance Properties to the database.
187 * @param serviceTemplate the service template
188 * @return a Map of tosca node templates
190 public Map<String, ToscaNodeTemplate> saveInstanceProperties(ToscaServiceTemplate serviceTemplate) {
191 Map<String, ToscaNodeTemplate> savedNodeTemplates = new HashMap<>();
193 var jpaToscaNodeTemplates = new JpaToscaNodeTemplates();
194 jpaToscaNodeTemplates.fromAuthorative(List.of(serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()));
196 toscaNodeTemplatesRepository.save(jpaToscaNodeTemplates);
197 serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().forEach(savedNodeTemplates::put);
199 return savedNodeTemplates;
203 * Delete a automation composition.
205 * @param name the name of the automation composition to delete
206 * @param version the version of the automation composition to delete
207 * @return the automation composition deleted
208 * @throws PfModelException on errors deleting the automation composition
210 public AutomationComposition deleteAutomationComposition(@NonNull final String name, @NonNull final String version)
211 throws PfModelException {
213 var automationCompositionKey = new PfConceptKey(name, version);
214 var jpaDeleteAutomationComposition = automationCompositionRepository.findById(automationCompositionKey);
216 if (jpaDeleteAutomationComposition.isEmpty()) {
217 String errorMessage = "delete of automation composition \"" + automationCompositionKey.getId()
218 + "\" failed, automation composition does not exist";
219 throw new PfModelException(Response.Status.BAD_REQUEST, errorMessage);
222 automationCompositionRepository.deleteById(automationCompositionKey);
224 return jpaDeleteAutomationComposition.get().toAuthorative();
228 * Deletes Instance Properties on the database.
230 * @param filteredToscaNodeTemplateMap filtered node templates map to delete
231 * @param filteredToscaNodeTemplateList filtered node template list to delete
233 public void deleteInstanceProperties(Map<String, ToscaNodeTemplate> filteredToscaNodeTemplateMap,
234 List<ToscaNodeTemplate> filteredToscaNodeTemplateList) {
236 var jpaToscaNodeTemplates = new JpaToscaNodeTemplates();
237 jpaToscaNodeTemplates.fromAuthorative(List.of(filteredToscaNodeTemplateMap));
239 toscaNodeTemplatesRepository.save(jpaToscaNodeTemplates);
241 filteredToscaNodeTemplateList.forEach(template -> {
242 var jpaToscaNodeTemplate = new JpaToscaNodeTemplate(template);
244 toscaNodeTemplateRepository.delete(jpaToscaNodeTemplate);
249 * Get All Node Templates.
251 * @return the list of node templates found
252 * @throws PfModelException on errors getting node templates
254 @Transactional(readOnly = true)
255 public List<ToscaNodeTemplate> getAllNodeTemplates() {
256 return ProviderUtils.asEntityList(toscaNodeTemplateRepository.findAll());
260 * Get Node Templates.
262 * @param name the name of the node template to get, null to get all node templates
263 * @param version the version of the node template to get, null to get all node templates
264 * @return the node templates found
265 * @throws PfModelException on errors getting node templates
267 @Transactional(readOnly = true)
268 public List<ToscaNodeTemplate> getNodeTemplates(final String name, final String version) {
270 .asEntityList(toscaNodeTemplateRepository.getFiltered(JpaToscaNodeTemplate.class, name, version));
274 * Get filtered node templates.
276 * @param filter the filter for the node templates to get
277 * @return the node templates found
278 * @throws PfModelException on errors getting node templates
280 @Transactional(readOnly = true)
281 public List<ToscaNodeTemplate> getFilteredNodeTemplates(
282 @NonNull final ToscaTypedEntityFilter<ToscaNodeTemplate> filter) {
284 return filter.filter(ProviderUtils.asEntityList(toscaNodeTemplateRepository
285 .getFiltered(JpaToscaNodeTemplate.class, filter.getName(), filter.getVersion())));