3d2813eb29f6dc17d0e6c1a7b669ca730c3172a9
[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 by automationCompositionId.
84      *
85      * @param automationCompositionId the ID of the automation composition to get
86      * @return the automation composition found
87      */
88     @Transactional(readOnly = true)
89     public Optional<AutomationComposition> findAutomationComposition(
90             final ToscaConceptIdentifier automationCompositionId) {
91         return findAutomationComposition(automationCompositionId.asConceptKey());
92     }
93
94     private Optional<AutomationComposition> findAutomationComposition(@NonNull final PfConceptKey key) {
95         return automationCompositionRepository.findById(key).map(JpaAutomationComposition::toAuthorative);
96     }
97
98     /**
99      * Create automation composition.
100      *
101      * @param automationComposition the automation composition to create
102      * @return the create automation composition
103      */
104     public AutomationComposition createAutomationComposition(final AutomationComposition automationComposition) {
105         automationComposition.setInstanceId(UUID.randomUUID());
106         var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
107                 JpaAutomationComposition::new, "automation composition"));
108
109         // Return the saved automation composition
110         return result.toAuthorative();
111     }
112
113     /**
114      * Update automation composition.
115      *
116      * @param automationComposition the automation composition to update
117      * @return the updated automation composition
118      */
119     public AutomationComposition updateAutomationComposition(final AutomationComposition automationComposition) {
120         var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
121                 JpaAutomationComposition::new, "automation composition"));
122
123         // Return the saved automation composition
124         return result.toAuthorative();
125     }
126
127     /**
128      * Get all automation compositions by compositionId.
129      *
130      * @param compositionId the compositionId of the automation composition definition
131      * @return all automation compositions found
132      */
133     @Transactional(readOnly = true)
134     public List<AutomationComposition> getAcInstancesByCompositionId(UUID compositionId) {
135         return ProviderUtils
136                 .asEntityList(automationCompositionRepository.findByCompositionId(compositionId.toString()));
137     }
138
139     /**
140      * Get automation compositions.
141      *
142      * @param name the name of the automation composition to get, null to get all automation compositions
143      * @param version the version of the automation composition to get, null to get all automation compositions
144      * @return the automation compositions found
145      */
146     @Transactional(readOnly = true)
147     public List<AutomationComposition> getAutomationCompositions(final String name, final String version) {
148
149         return ProviderUtils.asEntityList(
150                 automationCompositionRepository.getFiltered(JpaAutomationComposition.class, name, version));
151     }
152
153     /**
154      * Delete a automation composition.
155      *
156      * @param instanceId the ID of the automation composition to get
157      * @return the automation composition deleted
158      */
159     public AutomationComposition deleteAutomationComposition(@NonNull final UUID instanceId) {
160         var jpaDeleteAutomationComposition = automationCompositionRepository.findByInstanceId(instanceId.toString());
161         if (jpaDeleteAutomationComposition.isEmpty()) {
162             var errorMessage = "delete of automation composition \"" + instanceId
163                     + "\" failed, automation composition does not exist";
164             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
165         }
166
167         automationCompositionRepository.deleteById(jpaDeleteAutomationComposition.get().getKey());
168
169         return jpaDeleteAutomationComposition.get().toAuthorative();
170     }
171 }