edb3386e2f1545c4c202c4b3ad393ab92468c39e
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved.
4  * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.acm.runtime.commissioning;
23
24 import io.opentelemetry.context.Context;
25 import jakarta.ws.rs.core.Response.Status;
26 import java.util.HashSet;
27 import java.util.UUID;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Executors;
30 import lombok.NonNull;
31 import lombok.RequiredArgsConstructor;
32 import org.onap.policy.clamp.acm.runtime.main.parameters.AcRuntimeParameterGroup;
33 import org.onap.policy.clamp.acm.runtime.supervision.AcmThreadFactory;
34 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantPrimePublisher;
35 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
37 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
38 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.AcTypeStateUpdate;
39 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.CommissioningResponse;
40 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
41 import org.onap.policy.clamp.models.acm.persistence.provider.AcTypeStateResolver;
42 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
43 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
44 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
45 import org.onap.policy.models.base.PfModelRuntimeException;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplates;
48 import org.springframework.data.domain.Pageable;
49 import org.springframework.stereotype.Service;
50 import org.springframework.transaction.annotation.Transactional;
51
52 /**
53  * This class provides the create, read and delete actions on Commissioning of automation composition concepts in the
54  * database to the callers.
55  */
56 @Service
57 @RequiredArgsConstructor
58 public class CommissioningProvider {
59
60     private final AcDefinitionProvider acDefinitionProvider;
61     private final AutomationCompositionProvider acProvider;
62     private final ParticipantProvider participantProvider;
63     private final AcTypeStateResolver acTypeStateResolver;
64     private final ParticipantPrimePublisher participantPrimePublisher;
65     private final AcRuntimeParameterGroup acRuntimeParameterGroup;
66
67     private final ExecutorService executor =
68             Context.taskWrapping(Executors.newFixedThreadPool(1, new AcmThreadFactory()));
69
70     private CommissioningResponse createCommissioningResponse(UUID compositionId,
71             ToscaServiceTemplate serviceTemplate) {
72         var response = new CommissioningResponse();
73         response.setCompositionId(compositionId);
74         // @formatter:off
75         response.setAffectedAutomationCompositionDefinitions(
76             serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
77                 .values()
78                 .stream()
79                 .map(template -> template.getKey().asIdentifier())
80                 .toList());
81         // @formatter:on
82
83         return response;
84     }
85
86     /**
87      * Create automation composition from a service template.
88      *
89      * @param serviceTemplate the service template
90      * @return the result of the commissioning operation
91      */
92     @Transactional
93     public CommissioningResponse createAutomationCompositionDefinition(ToscaServiceTemplate serviceTemplate) {
94
95         var acmDefinition = acDefinitionProvider.createAutomationCompositionDefinition(serviceTemplate,
96                 acRuntimeParameterGroup.getAcmParameters().getToscaElementName(),
97                 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
98         serviceTemplate = acmDefinition.getServiceTemplate();
99         return createCommissioningResponse(acmDefinition.getCompositionId(), serviceTemplate);
100     }
101
102     /**
103      * Update Composition Definition.
104      *
105      * @param compositionId The UUID of the automation composition definition to update
106      * @param serviceTemplate the service template
107      * @return the result of the commissioning operation
108      */
109     @Transactional
110     public CommissioningResponse updateCompositionDefinition(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
111         if (verifyIfInstanceExists(compositionId)) {
112             throw new PfModelRuntimeException(Status.BAD_REQUEST,
113                     "There are ACM instances, Update of ACM Definition not allowed");
114         }
115         var acDefinition = acDefinitionProvider.getAcDefinition(compositionId);
116         if (!AcTypeState.COMMISSIONED.equals(acDefinition.getState())) {
117             throw new PfModelRuntimeException(Status.BAD_REQUEST,
118                     "ACM not in COMMISSIONED state, Update of ACM Definition not allowed");
119         }
120         acDefinitionProvider.updateServiceTemplate(compositionId, serviceTemplate,
121                 acRuntimeParameterGroup.getAcmParameters().getToscaElementName(),
122                 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
123
124         return createCommissioningResponse(compositionId, serviceTemplate);
125     }
126
127     /**
128      * Delete the automation composition definition with the given name and version.
129      *
130      * @param compositionId The UUID of the automation composition definition to delete
131      * @return the result of the deletion
132      */
133     @Transactional
134     public CommissioningResponse deleteAutomationCompositionDefinition(UUID compositionId) {
135         if (verifyIfInstanceExists(compositionId)) {
136             throw new PfModelRuntimeException(Status.BAD_REQUEST,
137                     "Delete instances, to commission automation composition definitions");
138         }
139         var acDefinition = acDefinitionProvider.getAcDefinition(compositionId);
140         if (!AcTypeState.COMMISSIONED.equals(acDefinition.getState())) {
141             throw new PfModelRuntimeException(Status.BAD_REQUEST,
142                     "ACM not in COMMISSIONED state, Delete of ACM Definition not allowed");
143         }
144         var serviceTemplate = acDefinitionProvider.deleteAcDefinition(compositionId);
145         return createCommissioningResponse(compositionId, serviceTemplate);
146     }
147
148     /**
149      * Get automation composition definition.
150      *
151      * @param acName the name of the automation composition, null for all
152      * @param acVersion the version of the automation composition, null for all
153      * @param pageable the Pageable
154      * @return automation composition definition
155      */
156     @Transactional(readOnly = true)
157     public ToscaServiceTemplates getAutomationCompositionDefinitions(String acName, String acVersion,
158             @NonNull Pageable pageable) {
159         var result = new ToscaServiceTemplates();
160         result.setServiceTemplates(acDefinitionProvider.getServiceTemplateList(acName, acVersion, pageable));
161         return result;
162     }
163
164     /**
165      * Get automation composition definition.
166      *
167      * @param compositionId the compositionId
168      * @return automation composition definition
169      */
170     @Transactional(readOnly = true)
171     public AutomationCompositionDefinition getAutomationCompositionDefinition(UUID compositionId) {
172
173         return acDefinitionProvider.getAcDefinition(compositionId);
174     }
175
176     /**
177      * Validates to see if there is any instance saved.
178      *
179      * @return true if exists instance
180      */
181     private boolean verifyIfInstanceExists(UUID compositionId) {
182         return !acProvider.getAcInstancesByCompositionId(compositionId).isEmpty();
183     }
184
185     /**
186      * Composition Definition Priming.
187      *
188      * @param compositionId the compositionId
189      * @param acTypeStateUpdate the ACMTypeStateUpdate
190      */
191     public void compositionDefinitionPriming(UUID compositionId, AcTypeStateUpdate acTypeStateUpdate) {
192         if (verifyIfInstanceExists(compositionId)) {
193             throw new PfModelRuntimeException(Status.BAD_REQUEST, "There are instances, Priming/Depriming not allowed");
194         }
195         var acmDefinition = acDefinitionProvider.getAcDefinition(compositionId);
196         var stateOrdered = acTypeStateResolver.resolve(acTypeStateUpdate.getPrimeOrder(), acmDefinition.getState(),
197                 acmDefinition.getStateChangeResult());
198         switch (stateOrdered) {
199             case PRIME:
200                 prime(acmDefinition);
201                 break;
202
203             case DEPRIME:
204                 deprime(acmDefinition);
205
206                 break;
207
208             default:
209                 throw new PfModelRuntimeException(Status.BAD_REQUEST, "Not valid " + acTypeStateUpdate.getPrimeOrder());
210         }
211     }
212
213     private void prime(AutomationCompositionDefinition acmDefinition) {
214         var preparation = participantPrimePublisher.prepareParticipantPriming(acmDefinition);
215         acDefinitionProvider.updateAcDefinition(acmDefinition,
216                 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
217
218         executor.execute(
219                 () -> participantPrimePublisher.sendPriming(preparation, acmDefinition.getCompositionId(), null));
220     }
221
222     private void deprime(AutomationCompositionDefinition acmDefinition) {
223         acmDefinition.setStateChangeResult(StateChangeResult.NO_ERROR);
224         var participantIds = new HashSet<UUID>();
225         for (var elementState : acmDefinition.getElementStateMap().values()) {
226             var participantId = elementState.getParticipantId();
227             if (participantId != null) {
228                 elementState.setState(AcTypeState.DEPRIMING);
229                 participantIds.add(participantId);
230             }
231         }
232         if (!participantIds.isEmpty()) {
233             participantProvider.verifyParticipantState(participantIds);
234         }
235         acmDefinition.setState(AcTypeState.DEPRIMING);
236         acmDefinition.setLastMsg(TimestampHelper.now());
237         acDefinitionProvider.updateAcDefinition(acmDefinition,
238                 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
239
240         executor.execute(() -> participantPrimePublisher.sendDepriming(acmDefinition.getCompositionId()));
241     }
242
243 }