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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.acm.runtime.commissioning;
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;
55 * This class provides the creation, read and delete actions on Commissioning of automation composition concepts in the
56 * database to the callers.
59 @RequiredArgsConstructor
60 public class CommissioningProvider {
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;
69 private final ExecutorService executor =
70 Context.taskWrapping(Executors.newFixedThreadPool(1, new AcmThreadFactory()));
72 private static final Logger LOGGER =
73 LoggerFactory.getLogger(CommissioningProvider.class);
75 private CommissioningResponse createCommissioningResponse(UUID compositionId,
76 ToscaServiceTemplate serviceTemplate) {
77 var response = new CommissioningResponse();
78 response.setCompositionId(compositionId);
80 response.setAffectedAutomationCompositionDefinitions(
81 serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
84 .map(template -> template.getKey().asIdentifier())
92 * Create automation composition from a service template.
94 * @param serviceTemplate the service template
95 * @return the result of the commissioning operation
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);
108 * Update Composition Definition.
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
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");
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");
126 acDefinitionProvider.updateServiceTemplate(compositionId, serviceTemplate,
127 acRuntimeParameterGroup.getAcmParameters().getToscaElementName(),
128 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
129 return createCommissioningResponse(compositionId, serviceTemplate);
133 * Delete the automation composition definition with the given name and version.
135 * @param compositionId The UUID of the automation composition definition to delete
136 * @return the result of the deletion
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");
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");
150 var serviceTemplate = acDefinitionProvider.deleteAcDefinition(compositionId);
151 return createCommissioningResponse(compositionId, serviceTemplate);
155 * Get automation composition definition.
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
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));
173 * Get automation composition definition.
175 * @param compositionId the compositionId
176 * @return automation composition definition
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);
185 * Validates to see if there is any instance saved.
187 * @return true if exists instance
189 private boolean verifyIfInstanceExists(UUID compositionId) {
190 return !acProvider.getAcInstancesByCompositionId(compositionId).isEmpty();
194 * Composition Definition Priming.
196 * @param compositionId the compositionId
197 * @param acTypeStateUpdate the ACMTypeStateUpdate
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");
203 var acmDefinition = acDefinitionProvider.getAcDefinition(compositionId);
204 var stateOrdered = acTypeStateResolver.resolve(acTypeStateUpdate.getPrimeOrder(), acmDefinition.getState(),
205 acmDefinition.getStateChangeResult());
206 switch (stateOrdered) {
208 prime(acmDefinition);
212 deprime(acmDefinition);
216 throw new PfModelRuntimeException(Status.BAD_REQUEST, "Not valid " + acTypeStateUpdate.getPrimeOrder());
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());
227 () -> participantPrimePublisher.sendPriming(
228 preparation, acmDefinition.getCompositionId(), acmDefinition.getRevisionId()));
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);
242 if (!participantIds.isEmpty()) {
243 participantProvider.verifyParticipantState(participantIds);
245 acmDefinition.setState(AcTypeState.DEPRIMING);
246 acmDefinition.setLastMsg(TimestampHelper.now());
247 acDefinitionProvider.updateAcDefinition(acmDefinition,
248 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
250 executor.execute(() -> participantPrimePublisher.sendDepriming(
251 acmDefinition.getCompositionId(), participantIds, acmDefinition.getRevisionId()));