760613026dafff7221ddd88dc8411a4a324c6887
[policy/clamp.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.clamp.models.acm.persistence.provider;
24
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
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;
49
50 /**
51  * This class provides information on automation composition concepts in the database to callers.
52  */
53 @Service
54 @Transactional
55 @AllArgsConstructor
56 public class AutomationCompositionProvider {
57
58     private final AutomationCompositionRepository automationCompositionRepository;
59     private final ToscaNodeTemplateRepository toscaNodeTemplateRepository;
60     private final ToscaNodeTemplatesRepository toscaNodeTemplatesRepository;
61
62     /**
63      * Get automation composition.
64      *
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
68      */
69     @Transactional(readOnly = true)
70     public AutomationComposition getAutomationComposition(final ToscaConceptIdentifier automationCompositionId)
71         throws PfModelException {
72         try {
73             return automationCompositionRepository.getById(automationCompositionId.asConceptKey()).toAuthorative();
74         } catch (EntityNotFoundException e) {
75             throw new PfModelException(Status.NOT_FOUND, "AutomationComposition not found", e);
76         }
77     }
78
79     /**
80      * Find automation composition by automationCompositionId.
81      *
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
86      */
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));
91     }
92
93     /**
94      * Find automation composition by automationCompositionId.
95      *
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
99      */
100     @Transactional(readOnly = true)
101     public Optional<AutomationComposition> findAutomationComposition(
102         final ToscaConceptIdentifier automationCompositionId) throws PfModelException {
103         return findAutomationComposition(automationCompositionId.asConceptKey());
104     }
105
106     private Optional<AutomationComposition> findAutomationComposition(@NonNull final PfConceptKey key)
107         throws PfModelException {
108         try {
109             return automationCompositionRepository.findById(key).map(JpaAutomationComposition::toAuthorative);
110         } catch (IllegalArgumentException e) {
111             throw new PfModelException(Status.BAD_REQUEST, "Not valid parameter", e);
112         }
113     }
114
115     /**
116      * Save automation composition.
117      *
118      * @param automationComposition the automation composition to update
119      * @return the updated automation composition
120      * @throws PfModelException on errors updating the automation composition
121      */
122     public AutomationComposition saveAutomationComposition(final AutomationComposition automationComposition)
123         throws PfModelException {
124         try {
125             var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
126                 JpaAutomationComposition::new, "automation composition"));
127
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);
132         }
133     }
134
135     /**
136      * Get all automation compositions.
137      *
138      * @return all automation compositions found
139      * @throws PfModelException on errors getting automation compositions
140      */
141     @Transactional(readOnly = true)
142     public List<AutomationComposition> getAutomationCompositions() throws PfModelException {
143
144         return ProviderUtils.asEntityList(automationCompositionRepository.findAll());
145     }
146
147     /**
148      * Get automation compositions.
149      *
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
154      */
155     @Transactional(readOnly = true)
156     public List<AutomationComposition> getAutomationCompositions(final String name, final String version)
157         throws PfModelException {
158
159         return ProviderUtils
160             .asEntityList(automationCompositionRepository.getFiltered(JpaAutomationComposition.class, name, version));
161     }
162
163     /**
164      * Saves automation compositions.
165      *
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
169      */
170     public List<AutomationComposition> saveAutomationCompositions(
171         @NonNull final List<AutomationComposition> automationCompositions) throws PfModelException {
172         try {
173             var result =
174                 automationCompositionRepository.saveAll(ProviderUtils.getJpaAndValidateList(automationCompositions,
175                     JpaAutomationComposition::new, "automation compositions"));
176
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);
181         }
182     }
183
184     /**
185      * Saves Instance Properties to the database.
186      *
187      * @param serviceTemplate the service template
188      * @return a Map of tosca node templates
189      */
190     public Map<String, ToscaNodeTemplate> saveInstanceProperties(ToscaServiceTemplate serviceTemplate) {
191         Map<String, ToscaNodeTemplate> savedNodeTemplates = new HashMap<>();
192
193         var jpaToscaNodeTemplates = new JpaToscaNodeTemplates();
194         jpaToscaNodeTemplates.fromAuthorative(List.of(serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()));
195
196         toscaNodeTemplatesRepository.save(jpaToscaNodeTemplates);
197         serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().forEach(savedNodeTemplates::put);
198
199         return savedNodeTemplates;
200     }
201
202     /**
203      * Delete a automation composition.
204      *
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
209      */
210     public AutomationComposition deleteAutomationComposition(@NonNull final String name, @NonNull final String version)
211         throws PfModelException {
212
213         var automationCompositionKey = new PfConceptKey(name, version);
214         var jpaDeleteAutomationComposition = automationCompositionRepository.findById(automationCompositionKey);
215
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);
220         }
221
222         automationCompositionRepository.deleteById(automationCompositionKey);
223
224         return jpaDeleteAutomationComposition.get().toAuthorative();
225     }
226
227     /**
228      * Deletes Instance Properties on the database.
229      *
230      * @param filteredToscaNodeTemplateMap filtered node templates map to delete
231      * @param filteredToscaNodeTemplateList filtered node template list to delete
232      */
233     public void deleteInstanceProperties(Map<String, ToscaNodeTemplate> filteredToscaNodeTemplateMap,
234         List<ToscaNodeTemplate> filteredToscaNodeTemplateList) {
235
236         var jpaToscaNodeTemplates = new JpaToscaNodeTemplates();
237         jpaToscaNodeTemplates.fromAuthorative(List.of(filteredToscaNodeTemplateMap));
238
239         toscaNodeTemplatesRepository.save(jpaToscaNodeTemplates);
240
241         filteredToscaNodeTemplateList.forEach(template -> {
242             var jpaToscaNodeTemplate = new JpaToscaNodeTemplate(template);
243
244             toscaNodeTemplateRepository.delete(jpaToscaNodeTemplate);
245         });
246     }
247
248     /**
249      * Get All Node Templates.
250      *
251      * @return the list of node templates found
252      * @throws PfModelException on errors getting node templates
253      */
254     @Transactional(readOnly = true)
255     public List<ToscaNodeTemplate> getAllNodeTemplates() {
256         return ProviderUtils.asEntityList(toscaNodeTemplateRepository.findAll());
257     }
258
259     /**
260      * Get Node Templates.
261      *
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
266      */
267     @Transactional(readOnly = true)
268     public List<ToscaNodeTemplate> getNodeTemplates(final String name, final String version) {
269         return ProviderUtils
270             .asEntityList(toscaNodeTemplateRepository.getFiltered(JpaToscaNodeTemplate.class, name, version));
271     }
272
273     /**
274      * Get filtered node templates.
275      *
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
279      */
280     @Transactional(readOnly = true)
281     public List<ToscaNodeTemplate> getFilteredNodeTemplates(
282         @NonNull final ToscaTypedEntityFilter<ToscaNodeTemplate> filter) {
283
284         return filter.filter(ProviderUtils.asEntityList(toscaNodeTemplateRepository
285             .getFiltered(JpaToscaNodeTemplate.class, filter.getName(), filter.getVersion())));
286     }
287 }