8c39f6e2b55df0984c5b57a42128a235d63e0bb3
[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.PfModelRuntimeException;
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 instanceId the ID of the automation composition to get
56      * @return the automation composition found
57      */
58     @Transactional(readOnly = true)
59     public AutomationComposition getAutomationComposition(final UUID instanceId) {
60         var result = automationCompositionRepository.findByInstanceId(instanceId.toString());
61         if (result.isEmpty()) {
62             throw new PfModelRuntimeException(Status.NOT_FOUND, "AutomationComposition not found");
63         }
64         return result.get().toAuthorative();
65     }
66
67     /**
68      * Get automation composition.
69      *
70      * @param automationCompositionId the ID of the automation composition to get
71      * @return the automation composition found
72      */
73     @Transactional(readOnly = true)
74     public AutomationComposition getAutomationComposition(final ToscaConceptIdentifier automationCompositionId) {
75         try {
76             return automationCompositionRepository.getById(automationCompositionId.asConceptKey()).toAuthorative();
77         } catch (EntityNotFoundException e) {
78             throw new PfModelRuntimeException(Status.NOT_FOUND, "AutomationComposition not found", e);
79         }
80     }
81
82     /**
83      * Find automation composition.
84      *
85      * @param instanceId the ID of the automation composition to get
86      * @return the automation composition found
87      */
88     @Transactional(readOnly = true)
89     public Optional<AutomationComposition> findAutomationComposition(final UUID instanceId) {
90         var result = automationCompositionRepository.findByInstanceId(instanceId.toString());
91         return result.stream().map(JpaAutomationComposition::toAuthorative).findFirst();
92     }
93
94     /**
95      * Find automation composition by automationCompositionId.
96      *
97      * @param automationCompositionId the ID of the automation composition to get
98      * @return the automation composition found
99      */
100     @Transactional(readOnly = true)
101     public Optional<AutomationComposition> findAutomationComposition(
102             final ToscaConceptIdentifier automationCompositionId) {
103         return findAutomationComposition(automationCompositionId.asConceptKey());
104     }
105
106     private Optional<AutomationComposition> findAutomationComposition(@NonNull final PfConceptKey key) {
107         return automationCompositionRepository.findById(key).map(JpaAutomationComposition::toAuthorative);
108     }
109
110     /**
111      * Create automation composition.
112      *
113      * @param automationComposition the automation composition to create
114      * @return the create automation composition
115      */
116     public AutomationComposition createAutomationComposition(final AutomationComposition automationComposition) {
117         automationComposition.setInstanceId(UUID.randomUUID());
118         var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
119                 JpaAutomationComposition::new, "automation composition"));
120
121         // Return the saved automation composition
122         return result.toAuthorative();
123     }
124
125     /**
126      * Update automation composition.
127      *
128      * @param automationComposition the automation composition to update
129      * @return the updated automation composition
130      */
131     public AutomationComposition updateAutomationComposition(final AutomationComposition automationComposition) {
132         var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
133                 JpaAutomationComposition::new, "automation composition"));
134
135         // Return the saved automation composition
136         return result.toAuthorative();
137     }
138
139     /**
140      * Get all automation compositions by compositionId.
141      *
142      * @param compositionId the compositionId of the automation composition definition
143      * @return all automation compositions found
144      */
145     @Transactional(readOnly = true)
146     public List<AutomationComposition> getAcInstancesByCompositionId(UUID compositionId) {
147         return ProviderUtils
148                 .asEntityList(automationCompositionRepository.findByCompositionId(compositionId.toString()));
149     }
150
151     /**
152      * Get automation compositions.
153      *
154      * @param name the name of the automation composition to get, null to get all automation compositions
155      * @param version the version of the automation composition to get, null to get all automation compositions
156      * @return the automation compositions found
157      */
158     @Transactional(readOnly = true)
159     public List<AutomationComposition> getAutomationCompositions(final String name, final String version) {
160
161         return ProviderUtils.asEntityList(
162                 automationCompositionRepository.getFiltered(JpaAutomationComposition.class, name, version));
163     }
164
165     /**
166      * Delete a automation composition.
167      *
168      * @param instanceId the ID of the automation composition to get
169      * @return the automation composition deleted
170      */
171     public AutomationComposition deleteAutomationComposition(@NonNull final UUID instanceId) {
172         var jpaDeleteAutomationComposition = automationCompositionRepository.findByInstanceId(instanceId.toString());
173         if (jpaDeleteAutomationComposition.isEmpty()) {
174             var errorMessage = "delete of automation composition \"" + instanceId
175                     + "\" failed, automation composition does not exist";
176             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
177         }
178
179         automationCompositionRepository.deleteById(jpaDeleteAutomationComposition.get().getKey());
180
181         return jpaDeleteAutomationComposition.get().toAuthorative();
182     }
183 }