6b90619c9d7b6782ddfe7a3d983f03d56b230ec9
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2023 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 jakarta.ws.rs.core.Response;
26 import jakarta.ws.rs.core.Response.Status;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Optional;
30 import java.util.UUID;
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.concepts.AutomationCompositionElement;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionInfo;
36 import org.onap.policy.clamp.models.acm.concepts.DeployState;
37 import org.onap.policy.clamp.models.acm.concepts.LockState;
38 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationComposition;
39 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationCompositionElement;
40 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionElementRepository;
41 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionRepository;
42 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
43 import org.onap.policy.models.base.PfModelRuntimeException;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
45 import org.springframework.data.domain.Example;
46 import org.springframework.stereotype.Service;
47 import org.springframework.transaction.annotation.Isolation;
48 import org.springframework.transaction.annotation.Transactional;
49
50 /**
51  * This class provides information on automation composition concepts in the database to callers.
52  */
53 @Service
54 @Transactional
55 @AllArgsConstructor
56 public class AutomationCompositionProvider {
57
58     private final AutomationCompositionRepository automationCompositionRepository;
59     private final AutomationCompositionElementRepository acElementRepository;
60
61     /**
62      * Get automation composition.
63      *
64      * @param instanceId the ID of the automation composition to get
65      * @return the automation composition found
66      */
67     @Transactional(readOnly = true)
68     public AutomationComposition getAutomationComposition(final UUID instanceId) {
69         var result = automationCompositionRepository.findById(instanceId.toString());
70         if (result.isEmpty()) {
71             throw new PfModelRuntimeException(Status.NOT_FOUND, "AutomationComposition not found");
72         }
73         return result.get().toAuthorative();
74     }
75
76     /**
77      * Find automation composition.
78      *
79      * @param instanceId the ID of the automation composition to get
80      * @return the automation composition found
81      */
82     @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
83     public Optional<AutomationComposition> findAutomationComposition(final UUID instanceId) {
84         var result = automationCompositionRepository.findById(instanceId.toString());
85         return result.stream().map(JpaAutomationComposition::toAuthorative).findFirst();
86     }
87
88     /**
89      * Find automation composition by automationCompositionId.
90      *
91      * @param automationCompositionId the ID of the automation composition to get
92      * @return the automation composition found
93      */
94     @Transactional(readOnly = true)
95     public Optional<AutomationComposition> findAutomationComposition(
96             final ToscaConceptIdentifier automationCompositionId) {
97         return automationCompositionRepository
98                 .findOne(createExample(null, automationCompositionId.getName(), automationCompositionId.getVersion()))
99                 .map(JpaAutomationComposition::toAuthorative);
100     }
101
102     /**
103      * Create automation composition.
104      *
105      * @param automationComposition the automation composition to create
106      * @return the create automation composition
107      */
108     public AutomationComposition createAutomationComposition(final AutomationComposition automationComposition) {
109         automationComposition.setInstanceId(UUID.randomUUID());
110         AcmUtils.setCascadedState(automationComposition, DeployState.UNDEPLOYED, LockState.NONE);
111         var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
112                 JpaAutomationComposition::new, "automation composition"));
113
114         // Return the saved automation composition
115         return result.toAuthorative();
116     }
117
118     /**
119      * Update automation composition.
120      *
121      * @param automationComposition the automation composition to update
122      * @return the updated automation composition
123      */
124     public AutomationComposition updateAutomationComposition(
125             @NonNull final AutomationComposition automationComposition) {
126         var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
127                 JpaAutomationComposition::new, "automation composition"));
128
129         // Return the saved automation composition
130         return result.toAuthorative();
131     }
132
133     /**
134      * Get all automation compositions by compositionId.
135      *
136      * @param compositionId the compositionId of the automation composition definition
137      * @return all automation compositions found
138      */
139     @Transactional(readOnly = true)
140     public List<AutomationComposition> getAcInstancesByCompositionId(UUID compositionId) {
141         return ProviderUtils
142                 .asEntityList(automationCompositionRepository.findByCompositionId(compositionId.toString()));
143     }
144
145     /**
146      * Get automation compositions.
147      *
148      * @param name the name of the automation composition to get, null to get all automation compositions
149      * @param version the version of the automation composition to get, null to get all automation compositions
150      * @return the automation compositions found
151      */
152     @Transactional(readOnly = true)
153     public List<AutomationComposition> getAutomationCompositions(final UUID compositionId, final String name,
154             final String version) {
155
156         return ProviderUtils
157                 .asEntityList(automationCompositionRepository.findAll(createExample(compositionId, name, version)));
158     }
159
160     private Example<JpaAutomationComposition> createExample(final UUID compositionId, final String name,
161             final String version) {
162         var example = new JpaAutomationComposition();
163         example.setCompositionId(compositionId != null ? compositionId.toString() : null);
164         example.setName(name);
165         example.setVersion(version);
166         example.setInstanceId(null);
167         example.setElements(null);
168         example.setDeployState(null);
169         example.setLockState(null);
170
171         return Example.of(example);
172     }
173
174     /**
175      * Delete a automation composition.
176      *
177      * @param instanceId the ID of the automation composition to get
178      * @return the automation composition deleted
179      */
180     public AutomationComposition deleteAutomationComposition(@NonNull final UUID instanceId) {
181         var jpaDeleteAutomationComposition = automationCompositionRepository.findById(instanceId.toString());
182         if (jpaDeleteAutomationComposition.isEmpty()) {
183             var errorMessage = "delete of automation composition \"" + instanceId
184                     + "\" failed, automation composition does not exist";
185             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
186         }
187
188         automationCompositionRepository.deleteById(instanceId.toString());
189
190         return jpaDeleteAutomationComposition.get().toAuthorative();
191     }
192
193     /**
194      * Upgrade States.
195      *
196      * @param automationCompositionInfoList list of AutomationCompositionInfo
197      */
198     public void upgradeStates(@NonNull final List<AutomationCompositionInfo> automationCompositionInfoList) {
199         if (automationCompositionInfoList.isEmpty()) {
200             return;
201         }
202         List<JpaAutomationCompositionElement> jpaList = new ArrayList<>();
203         for (var acInstance : automationCompositionInfoList) {
204             for (var element : acInstance.getElements()) {
205                 var jpa = acElementRepository.getReferenceById(element.getAutomationCompositionElementId().toString());
206                 jpa.setUseState(element.getUseState());
207                 jpa.setOperationalState(element.getOperationalState());
208                 jpa.setOutProperties(element.getOutProperties());
209                 jpaList.add(jpa);
210             }
211         }
212         acElementRepository.saveAll(jpaList);
213     }
214
215     /**
216      * Update AutomationCompositionElement.
217      *
218      * @param element the AutomationCompositionElement
219      * @param instanceId the instance Id
220      */
221     public void updateAutomationCompositionElement(@NonNull final AutomationCompositionElement element,
222                 @NonNull final UUID instanceId) {
223         var jpaAcElement = new JpaAutomationCompositionElement(element.getId().toString(), instanceId.toString());
224         jpaAcElement.fromAuthorative(element);
225         ProviderUtils.validate(element, jpaAcElement, "AutomationCompositionElement");
226         acElementRepository.save(jpaAcElement);
227     }
228 }