431176ab6d95e3b4c610be6d1947ea8395994770
[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.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.data.domain.Pageable;
51 import org.springframework.stereotype.Service;
52 import org.springframework.transaction.annotation.Transactional;
53
54 /**
55  * This class provides the creation, read and delete actions on Commissioning of automation composition concepts in the
56  * database to the callers.
57  */
58 @Service
59 @RequiredArgsConstructor
60 public class CommissioningProvider {
61
62     private final AcDefinitionProvider acDefinitionProvider;
63     private final AutomationCompositionProvider acProvider;
64     private final ParticipantProvider participantProvider;
65     private final AcTypeStateResolver acTypeStateResolver;
66     private final ParticipantPrimePublisher participantPrimePublisher;
67     private final AcRuntimeParameterGroup acRuntimeParameterGroup;
68
69     private final ExecutorService executor =
70             Context.taskWrapping(Executors.newFixedThreadPool(1, new AcmThreadFactory()));
71
72     private static final Logger LOGGER =
73             LoggerFactory.getLogger(CommissioningProvider.class);
74
75     private CommissioningResponse createCommissioningResponse(UUID compositionId,
76             ToscaServiceTemplate serviceTemplate) {
77         var response = new CommissioningResponse();
78         response.setCompositionId(compositionId);
79         // @formatter:off
80         response.setAffectedAutomationCompositionDefinitions(
81             serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
82                 .values()
83                 .stream()
84                 .map(template -> template.getKey().asIdentifier())
85                 .toList());
86         // @formatter:on
87
88         return response;
89     }
90
91     /**
92      * Create automation composition from a service template.
93      *
94      * @param serviceTemplate the service template
95      * @return the result of the commissioning operation
96      */
97     @Transactional
98     public CommissioningResponse createAutomationCompositionDefinition(ToscaServiceTemplate serviceTemplate) {
99         var acmDefinition = acDefinitionProvider.createAutomationCompositionDefinition(serviceTemplate,
100                 acRuntimeParameterGroup.getAcmParameters().getToscaElementName(),
101                 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
102         serviceTemplate = acmDefinition.getServiceTemplate();
103         LOGGER.info("Create request received for ID: {}", acmDefinition.getCompositionId());
104         return createCommissioningResponse(acmDefinition.getCompositionId(), serviceTemplate);
105     }
106
107     /**
108      * Update Composition Definition.
109      *
110      * @param compositionId The UUID of the automation composition definition to update
111      * @param serviceTemplate the service template
112      * @return the result of the commissioning operation
113      */
114     @Transactional
115     public CommissioningResponse updateCompositionDefinition(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
116         LOGGER.info("Update request received for ID: {}", compositionId);
117         if (verifyIfInstanceExists(compositionId)) {
118             throw new PfModelRuntimeException(Status.BAD_REQUEST,
119                     "There are ACM instances, Update of ACM Definition not allowed");
120         }
121         var acDefinition = acDefinitionProvider.getAcDefinition(compositionId);
122         if (!AcTypeState.COMMISSIONED.equals(acDefinition.getState())) {
123             throw new PfModelRuntimeException(Status.BAD_REQUEST,
124                     "ACM not in COMMISSIONED state, Update of ACM Definition not allowed");
125         }
126         acDefinitionProvider.updateServiceTemplate(compositionId, serviceTemplate,
127                 acRuntimeParameterGroup.getAcmParameters().getToscaElementName(),
128                 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
129         return createCommissioningResponse(compositionId, serviceTemplate);
130     }
131
132     /**
133      * Delete the automation composition definition with the given name and version.
134      *
135      * @param compositionId The UUID of the automation composition definition to delete
136      * @return the result of the deletion
137      */
138     @Transactional
139     public CommissioningResponse deleteAutomationCompositionDefinition(UUID compositionId) {
140         LOGGER.info("Delete request received for ID: {}", compositionId);
141         if (verifyIfInstanceExists(compositionId)) {
142             throw new PfModelRuntimeException(Status.BAD_REQUEST,
143                     "Delete instances, to commission automation composition definitions");
144         }
145         var acDefinition = acDefinitionProvider.getAcDefinition(compositionId);
146         if (!AcTypeState.COMMISSIONED.equals(acDefinition.getState())) {
147             throw new PfModelRuntimeException(Status.BAD_REQUEST,
148                     "ACM not in COMMISSIONED state, Delete of ACM Definition not allowed");
149         }
150         var serviceTemplate = acDefinitionProvider.deleteAcDefinition(compositionId);
151         return createCommissioningResponse(compositionId, serviceTemplate);
152     }
153
154     /**
155      * Get automation composition definition.
156      *
157      * @param acName the name of the automation composition, null for all
158      * @param acVersion the version of the automation composition, null for all
159      * @param pageable the Pageable
160      * @return automation composition definition
161      */
162     @Transactional(readOnly = true)
163     public ToscaServiceTemplates getAutomationCompositionDefinitions(String acName, String acVersion,
164             @NonNull Pageable pageable) {
165         LOGGER.info("Get automation compositions request received for name: {} "
166                 + "and version: {}", acName, acVersion);
167         var result = new ToscaServiceTemplates();
168         result.setServiceTemplates(acDefinitionProvider.getServiceTemplateList(acName, acVersion, pageable));
169         return result;
170     }
171
172     /**
173      * Get automation composition definition.
174      *
175      * @param compositionId the compositionId
176      * @return automation composition definition
177      */
178     @Transactional(readOnly = true)
179     public AutomationCompositionDefinition getAutomationCompositionDefinition(UUID compositionId) {
180         LOGGER.info("Get automation composition definition request received for ID: {}", compositionId);
181         return acDefinitionProvider.getAcDefinition(compositionId);
182     }
183
184     /**
185      * Validates to see if there is any instance saved.
186      *
187      * @return true if exists instance
188      */
189     private boolean verifyIfInstanceExists(UUID compositionId) {
190         return !acProvider.getAcInstancesByCompositionId(compositionId).isEmpty();
191     }
192
193     /**
194      * Composition Definition Priming.
195      *
196      * @param compositionId the compositionId
197      * @param acTypeStateUpdate the ACMTypeStateUpdate
198      */
199     public void compositionDefinitionPriming(UUID compositionId, AcTypeStateUpdate acTypeStateUpdate) {
200         if (verifyIfInstanceExists(compositionId)) {
201             throw new PfModelRuntimeException(Status.BAD_REQUEST, "There are instances, Priming/Depriming not allowed");
202         }
203         var acmDefinition = acDefinitionProvider.getAcDefinition(compositionId);
204         var stateOrdered = acTypeStateResolver.resolve(acTypeStateUpdate.getPrimeOrder(), acmDefinition.getState(),
205                 acmDefinition.getStateChangeResult());
206         switch (stateOrdered) {
207             case PRIME:
208                 prime(acmDefinition);
209                 break;
210
211             case DEPRIME:
212                 deprime(acmDefinition);
213                 break;
214
215             default:
216                 throw new PfModelRuntimeException(Status.BAD_REQUEST, "Not valid " + acTypeStateUpdate.getPrimeOrder());
217         }
218     }
219
220     private void prime(AutomationCompositionDefinition acmDefinition) {
221         LOGGER.info("Prime request received for ID: {}", acmDefinition.getCompositionId());
222         var preparation = participantPrimePublisher.prepareParticipantPriming(acmDefinition);
223         acDefinitionProvider.updateAcDefinition(acmDefinition,
224                 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
225
226         executor.execute(
227                 () -> participantPrimePublisher.sendPriming(
228                         preparation, acmDefinition.getCompositionId(), acmDefinition.getRevisionId()));
229     }
230
231     private void deprime(AutomationCompositionDefinition acmDefinition) {
232         LOGGER.info("Deprime request received for ID: {}", acmDefinition.getCompositionId());
233         acmDefinition.setStateChangeResult(StateChangeResult.NO_ERROR);
234         var participantIds = new HashSet<UUID>();
235         for (var elementState : acmDefinition.getElementStateMap().values()) {
236             var participantId = elementState.getParticipantId();
237             if (participantId != null) {
238                 elementState.setState(AcTypeState.DEPRIMING);
239                 participantIds.add(participantId);
240             }
241         }
242         if (!participantIds.isEmpty()) {
243             participantProvider.verifyParticipantState(participantIds);
244         }
245         acmDefinition.setState(AcTypeState.DEPRIMING);
246         acmDefinition.setLastMsg(TimestampHelper.now());
247         acDefinitionProvider.updateAcDefinition(acmDefinition,
248                 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
249
250         executor.execute(() -> participantPrimePublisher.sendDepriming(
251                 acmDefinition.getCompositionId(), participantIds, acmDefinition.getRevisionId()));
252     }
253 }