272ea422b3bcd5e071aa636f6a5fa660f2cbf095
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2022 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 java.util.UUID;
28 import javax.persistence.EntityNotFoundException;
29 import javax.ws.rs.core.Response;
30 import javax.ws.rs.core.Response.Status;
31 import lombok.AllArgsConstructor;
32 import lombok.NonNull;
33 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
34 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationComposition;
35 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionRepository;
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.springframework.stereotype.Service;
40 import org.springframework.transaction.annotation.Transactional;
41
42 /**
43  * This class provides information on automation composition concepts in the database to callers.
44  */
45 @Service
46 @Transactional
47 @AllArgsConstructor
48 public class AutomationCompositionProvider {
49
50     private final AutomationCompositionRepository automationCompositionRepository;
51
52     /**
53      * Get automation composition.
54      *
55      * @param automationCompositionId the ID of the automation composition to get
56      * @return the automation composition found
57      * @throws PfModelException on errors getting the automation composition
58      */
59     @Transactional(readOnly = true)
60     public AutomationComposition getAutomationComposition(final ToscaConceptIdentifier automationCompositionId)
61             throws PfModelException {
62         try {
63             return automationCompositionRepository.getById(automationCompositionId.asConceptKey()).toAuthorative();
64         } catch (EntityNotFoundException e) {
65             throw new PfModelException(Status.NOT_FOUND, "AutomationComposition not found", e);
66         }
67     }
68
69     /**
70      * Find automation composition by automationCompositionId.
71      *
72      * @param name the name of the automation composition to get, null to get all automation compositions
73      * @param version the version of the automation composition to get, null to get all automation compositions
74      * @return the automation composition found
75      * @throws PfModelException on errors getting the automation composition
76      */
77     @Transactional(readOnly = true)
78     public Optional<AutomationComposition> findAutomationComposition(@NonNull final String name,
79             @NonNull final String version) throws PfModelException {
80         return findAutomationComposition(new PfConceptKey(name, version));
81     }
82
83     /**
84      * Find automation composition by automationCompositionId.
85      *
86      * @param automationCompositionId the ID of the automation composition to get
87      * @return the automation composition found
88      * @throws PfModelException on errors getting the automation composition
89      */
90     @Transactional(readOnly = true)
91     public Optional<AutomationComposition> findAutomationComposition(
92             final ToscaConceptIdentifier automationCompositionId) throws PfModelException {
93         return findAutomationComposition(automationCompositionId.asConceptKey());
94     }
95
96     private Optional<AutomationComposition> findAutomationComposition(@NonNull final PfConceptKey key)
97             throws PfModelException {
98         try {
99             return automationCompositionRepository.findById(key).map(JpaAutomationComposition::toAuthorative);
100         } catch (IllegalArgumentException e) {
101             throw new PfModelException(Status.BAD_REQUEST, "Not valid parameter", e);
102         }
103     }
104
105     /**
106      * Save automation composition.
107      *
108      * @param automationComposition the automation composition to update
109      * @return the updated automation composition
110      * @throws PfModelException on errors updating the automation composition
111      */
112     public AutomationComposition saveAutomationComposition(final AutomationComposition automationComposition)
113             throws PfModelException {
114         try {
115             var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
116                     JpaAutomationComposition::new, "automation composition"));
117
118             // Return the saved participant
119             return result.toAuthorative();
120         } catch (IllegalArgumentException e) {
121             throw new PfModelException(Status.BAD_REQUEST, "Error in save automationComposition", e);
122         }
123     }
124
125     /**
126      * Get all automation compositions by compositionId.
127      *
128      * @param compositionId the compositionId of the automation composition definition
129      * @return all automation compositions found
130      */
131     @Transactional(readOnly = true)
132     public List<AutomationComposition> getAcInstancesByCompositionId(UUID compositionId) {
133         return ProviderUtils
134                 .asEntityList(automationCompositionRepository.findByCompositionId(compositionId.toString()));
135     }
136
137     /**
138      * Get automation compositions.
139      *
140      * @param name the name of the automation composition to get, null to get all automation compositions
141      * @param version the version of the automation composition to get, null to get all automation compositions
142      * @return the automation compositions found
143      */
144     @Transactional(readOnly = true)
145     public List<AutomationComposition> getAutomationCompositions(final String name, final String version) {
146
147         return ProviderUtils.asEntityList(
148                 automationCompositionRepository.getFiltered(JpaAutomationComposition.class, name, version));
149     }
150
151     /**
152      * Saves automation compositions.
153      *
154      * @param automationCompositions a specification of the automation compositions to create
155      * @return the automation compositions created
156      * @throws PfModelException on errors creating automation compositions
157      */
158     public List<AutomationComposition> saveAutomationCompositions(
159             @NonNull final List<AutomationComposition> automationCompositions) throws PfModelException {
160         try {
161             var result =
162                     automationCompositionRepository.saveAll(ProviderUtils.getJpaAndValidateList(automationCompositions,
163                             JpaAutomationComposition::new, "automation compositions"));
164
165             // Return the saved participant
166             return ProviderUtils.asEntityList(result);
167         } catch (IllegalArgumentException e) {
168             throw new PfModelException(Status.BAD_REQUEST, "Error in save AutomationCompositions", e);
169         }
170     }
171
172     /**
173      * Delete a automation composition.
174      *
175      * @param name the name of the automation composition to delete
176      * @param version the version of the automation composition to delete
177      * @return the automation composition deleted
178      * @throws PfModelException on errors deleting the automation composition
179      */
180     public AutomationComposition deleteAutomationComposition(@NonNull final String name, @NonNull final String version)
181             throws PfModelException {
182
183         var automationCompositionKey = new PfConceptKey(name, version);
184         var jpaDeleteAutomationComposition = automationCompositionRepository.findById(automationCompositionKey);
185
186         if (jpaDeleteAutomationComposition.isEmpty()) {
187             String errorMessage = "delete of automation composition \"" + automationCompositionKey.getId()
188                     + "\" failed, automation composition does not exist";
189             throw new PfModelException(Response.Status.NOT_FOUND, errorMessage);
190         }
191
192         automationCompositionRepository.deleteById(automationCompositionKey);
193
194         return jpaDeleteAutomationComposition.get().toAuthorative();
195     }
196 }