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