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.springframework.data.domain.Pageable;
49 import org.springframework.stereotype.Service;
50 import org.springframework.transaction.annotation.Transactional;
53 * This class provides the create, read and delete actions on Commissioning of automation composition concepts in the
54 * database to the callers.
57 @RequiredArgsConstructor
58 public class CommissioningProvider {
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;
67 private final ExecutorService executor =
68 Context.taskWrapping(Executors.newFixedThreadPool(1, new AcmThreadFactory()));
70 private CommissioningResponse createCommissioningResponse(UUID compositionId,
71 ToscaServiceTemplate serviceTemplate) {
72 var response = new CommissioningResponse();
73 response.setCompositionId(compositionId);
75 response.setAffectedAutomationCompositionDefinitions(
76 serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
79 .map(template -> template.getKey().asIdentifier())
87 * Create automation composition from a service template.
89 * @param serviceTemplate the service template
90 * @return the result of the commissioning operation
93 public CommissioningResponse createAutomationCompositionDefinition(ToscaServiceTemplate serviceTemplate) {
95 var acmDefinition = acDefinitionProvider.createAutomationCompositionDefinition(serviceTemplate,
96 acRuntimeParameterGroup.getAcmParameters().getToscaElementName(),
97 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
98 serviceTemplate = acmDefinition.getServiceTemplate();
99 return createCommissioningResponse(acmDefinition.getCompositionId(), serviceTemplate);
103 * Update Composition Definition.
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
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");
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");
120 acDefinitionProvider.updateServiceTemplate(compositionId, serviceTemplate,
121 acRuntimeParameterGroup.getAcmParameters().getToscaElementName(),
122 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
124 return createCommissioningResponse(compositionId, serviceTemplate);
128 * Delete the automation composition definition with the given name and version.
130 * @param compositionId The UUID of the automation composition definition to delete
131 * @return the result of the deletion
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");
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");
144 var serviceTemplate = acDefinitionProvider.deleteAcDefinition(compositionId);
145 return createCommissioningResponse(compositionId, serviceTemplate);
149 * Get automation composition definition.
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
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));
165 * Get automation composition definition.
167 * @param compositionId the compositionId
168 * @return automation composition definition
170 @Transactional(readOnly = true)
171 public AutomationCompositionDefinition getAutomationCompositionDefinition(UUID compositionId) {
173 return acDefinitionProvider.getAcDefinition(compositionId);
177 * Validates to see if there is any instance saved.
179 * @return true if exists instance
181 private boolean verifyIfInstanceExists(UUID compositionId) {
182 return !acProvider.getAcInstancesByCompositionId(compositionId).isEmpty();
186 * Composition Definition Priming.
188 * @param compositionId the compositionId
189 * @param acTypeStateUpdate the ACMTypeStateUpdate
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");
195 var acmDefinition = acDefinitionProvider.getAcDefinition(compositionId);
196 var stateOrdered = acTypeStateResolver.resolve(acTypeStateUpdate.getPrimeOrder(), acmDefinition.getState(),
197 acmDefinition.getStateChangeResult());
198 switch (stateOrdered) {
200 prime(acmDefinition);
204 deprime(acmDefinition);
209 throw new PfModelRuntimeException(Status.BAD_REQUEST, "Not valid " + acTypeStateUpdate.getPrimeOrder());
213 private void prime(AutomationCompositionDefinition acmDefinition) {
214 var preparation = participantPrimePublisher.prepareParticipantPriming(acmDefinition);
215 acDefinitionProvider.updateAcDefinition(acmDefinition,
216 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
219 () -> participantPrimePublisher.sendPriming(preparation, acmDefinition.getCompositionId(), null));
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);
232 if (!participantIds.isEmpty()) {
233 participantProvider.verifyParticipantState(participantIds);
235 acmDefinition.setState(AcTypeState.DEPRIMING);
236 acmDefinition.setLastMsg(TimestampHelper.now());
237 acDefinitionProvider.updateAcDefinition(acmDefinition,
238 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
240 executor.execute(() -> participantPrimePublisher.sendDepriming(acmDefinition.getCompositionId()));