5ee7d02f635e07072f980ea2d15e2bc0216dc4a5
[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.List;
26 import java.util.Optional;
27 import javax.persistence.EntityNotFoundException;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.Response.Status;
30 import lombok.AllArgsConstructor;
31 import lombok.NonNull;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
33 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationComposition;
34 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionRepository;
35 import org.onap.policy.clamp.models.acm.persistence.repository.ToscaNodeTemplateRepository;
36 import org.onap.policy.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfModelException;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;
42 import org.springframework.stereotype.Service;
43 import org.springframework.transaction.annotation.Transactional;
44
45 /**
46  * This class provides information on automation composition concepts in the database to callers.
47  */
48 @Service
49 @Transactional
50 @AllArgsConstructor
51 public class AutomationCompositionProvider {
52
53     private final AutomationCompositionRepository automationCompositionRepository;
54     private final ToscaNodeTemplateRepository toscaNodeTemplateRepository;
55
56     /**
57      * Get automation composition.
58      *
59      * @param automationCompositionId the ID of the automation composition to get
60      * @return the automation composition found
61      * @throws PfModelException on errors getting the automation composition
62      */
63     @Transactional(readOnly = true)
64     public AutomationComposition getAutomationComposition(final ToscaConceptIdentifier automationCompositionId)
65         throws PfModelException {
66         try {
67             return automationCompositionRepository.getById(automationCompositionId.asConceptKey()).toAuthorative();
68         } catch (EntityNotFoundException e) {
69             throw new PfModelException(Status.NOT_FOUND, "AutomationComposition not found", e);
70         }
71     }
72
73     /**
74      * Find automation composition by automationCompositionId.
75      *
76      * @param name the name of the automation composition to get, null to get all automation compositions
77      * @param version the version of the automation composition to get, null to get all automation compositions
78      * @return the automation composition found
79      * @throws PfModelException on errors getting the automation composition
80      */
81     @Transactional(readOnly = true)
82     public Optional<AutomationComposition> findAutomationComposition(@NonNull final String name,
83         @NonNull final String version) throws PfModelException {
84         return findAutomationComposition(new PfConceptKey(name, version));
85     }
86
87     /**
88      * Find automation composition by automationCompositionId.
89      *
90      * @param automationCompositionId the ID of the automation composition to get
91      * @return the automation composition found
92      * @throws PfModelException on errors getting the automation composition
93      */
94     @Transactional(readOnly = true)
95     public Optional<AutomationComposition> findAutomationComposition(
96         final ToscaConceptIdentifier automationCompositionId) throws PfModelException {
97         return findAutomationComposition(automationCompositionId.asConceptKey());
98     }
99
100     private Optional<AutomationComposition> findAutomationComposition(@NonNull final PfConceptKey key)
101         throws PfModelException {
102         try {
103             return automationCompositionRepository.findById(key).map(JpaAutomationComposition::toAuthorative);
104         } catch (IllegalArgumentException e) {
105             throw new PfModelException(Status.BAD_REQUEST, "Not valid parameter", e);
106         }
107     }
108
109     /**
110      * Save automation composition.
111      *
112      * @param automationComposition the automation composition to update
113      * @return the updated automation composition
114      * @throws PfModelException on errors updating the automation composition
115      */
116     public AutomationComposition saveAutomationComposition(final AutomationComposition automationComposition)
117         throws PfModelException {
118         try {
119             var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
120                 JpaAutomationComposition::new, "automation composition"));
121
122             // Return the saved participant
123             return result.toAuthorative();
124         } catch (IllegalArgumentException e) {
125             throw new PfModelException(Status.BAD_REQUEST, "Error in save automationComposition", e);
126         }
127     }
128
129     /**
130      * Get all automation compositions.
131      *
132      * @return all automation compositions found
133      */
134     @Transactional(readOnly = true)
135     public List<AutomationComposition> getAutomationCompositions() {
136
137         return ProviderUtils.asEntityList(automationCompositionRepository.findAll());
138     }
139
140     /**
141      * Get automation compositions.
142      *
143      * @param name the name of the automation composition to get, null to get all automation compositions
144      * @param version the version of the automation composition to get, null to get all automation compositions
145      * @return the automation compositions found
146      */
147     @Transactional(readOnly = true)
148     public List<AutomationComposition> getAutomationCompositions(final String name, final String version) {
149
150         return ProviderUtils
151             .asEntityList(automationCompositionRepository.getFiltered(JpaAutomationComposition.class, name, version));
152     }
153
154     /**
155      * Saves automation compositions.
156      *
157      * @param automationCompositions a specification of the automation compositions to create
158      * @return the automation compositions created
159      * @throws PfModelException on errors creating automation compositions
160      */
161     public List<AutomationComposition> saveAutomationCompositions(
162         @NonNull final List<AutomationComposition> automationCompositions) throws PfModelException {
163         try {
164             var result =
165                 automationCompositionRepository.saveAll(ProviderUtils.getJpaAndValidateList(automationCompositions,
166                     JpaAutomationComposition::new, "automation compositions"));
167
168             // Return the saved participant
169             return ProviderUtils.asEntityList(result);
170         } catch (IllegalArgumentException e) {
171             throw new PfModelException(Status.BAD_REQUEST, "Error in save AutomationCompositions", e);
172         }
173     }
174
175     /**
176      * Delete a automation composition.
177      *
178      * @param name the name of the automation composition to delete
179      * @param version the version of the automation composition to delete
180      * @return the automation composition deleted
181      * @throws PfModelException on errors deleting the automation composition
182      */
183     public AutomationComposition deleteAutomationComposition(@NonNull final String name, @NonNull final String version)
184         throws PfModelException {
185
186         var automationCompositionKey = new PfConceptKey(name, version);
187         var jpaDeleteAutomationComposition = automationCompositionRepository.findById(automationCompositionKey);
188
189         if (jpaDeleteAutomationComposition.isEmpty()) {
190             String errorMessage = "delete of automation composition \"" + automationCompositionKey.getId()
191                 + "\" failed, automation composition does not exist";
192             throw new PfModelException(Response.Status.BAD_REQUEST, errorMessage);
193         }
194
195         automationCompositionRepository.deleteById(automationCompositionKey);
196
197         return jpaDeleteAutomationComposition.get().toAuthorative();
198     }
199
200     /**
201      * Get All Node Templates.
202      *
203      * @return the list of node templates found
204      */
205     @Transactional(readOnly = true)
206     public List<ToscaNodeTemplate> getAllNodeTemplates() {
207         return ProviderUtils.asEntityList(toscaNodeTemplateRepository.findAll());
208     }
209
210     /**
211      * Get Node Templates.
212      *
213      * @param name the name of the node template to get, null to get all node templates
214      * @param version the version of the node template to get, null to get all node templates
215      * @return the node templates found
216      * @throws PfModelException on errors getting node templates
217      */
218     @Transactional(readOnly = true)
219     public List<ToscaNodeTemplate> getNodeTemplates(final String name, final String version) {
220         return ProviderUtils
221             .asEntityList(toscaNodeTemplateRepository.getFiltered(JpaToscaNodeTemplate.class, name, version));
222     }
223
224     /**
225      * Get filtered node templates.
226      *
227      * @param filter the filter for the node templates to get
228      * @return the node templates found
229      * @throws PfModelException on errors getting node templates
230      */
231     @Transactional(readOnly = true)
232     public List<ToscaNodeTemplate> getFilteredNodeTemplates(
233         @NonNull final ToscaTypedEntityFilter<ToscaNodeTemplate> filter) {
234
235         return filter.filter(ProviderUtils.asEntityList(toscaNodeTemplateRepository
236             .getFiltered(JpaToscaNodeTemplate.class, filter.getName(), filter.getVersion())));
237     }
238 }