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.comm.ParticipantPrimePublisher;
34 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
36 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
37 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.AcTypeStateUpdate;
38 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.CommissioningResponse;
39 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
40 import org.onap.policy.clamp.models.acm.persistence.provider.AcTypeStateResolver;
41 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
42 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
43 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
44 import org.onap.policy.models.base.PfModelRuntimeException;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplates;
47 import org.springframework.data.domain.Pageable;
48 import org.springframework.stereotype.Service;
49 import org.springframework.transaction.annotation.Transactional;
52 * This class provides the create, read and delete actions on Commissioning of automation composition concepts in the
53 * database to the callers.
56 @RequiredArgsConstructor
57 public class CommissioningProvider {
59 private final AcDefinitionProvider acDefinitionProvider;
60 private final AutomationCompositionProvider acProvider;
61 private final ParticipantProvider participantProvider;
62 private final AcTypeStateResolver acTypeStateResolver;
63 private final ParticipantPrimePublisher participantPrimePublisher;
64 private final AcRuntimeParameterGroup acRuntimeParameterGroup;
66 private final ExecutorService executor = Context.taskWrapping(Executors.newFixedThreadPool(1));
68 private CommissioningResponse createCommissioningResponse(UUID compositionId,
69 ToscaServiceTemplate serviceTemplate) {
70 var response = new CommissioningResponse();
71 response.setCompositionId(compositionId);
73 response.setAffectedAutomationCompositionDefinitions(
74 serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
77 .map(template -> template.getKey().asIdentifier())
85 * Create automation composition from a service template.
87 * @param serviceTemplate the service template
88 * @return the result of the commissioning operation
91 public CommissioningResponse createAutomationCompositionDefinition(ToscaServiceTemplate serviceTemplate) {
93 var acmDefinition = acDefinitionProvider.createAutomationCompositionDefinition(serviceTemplate,
94 acRuntimeParameterGroup.getAcmParameters().getToscaElementName(),
95 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
96 serviceTemplate = acmDefinition.getServiceTemplate();
97 return createCommissioningResponse(acmDefinition.getCompositionId(), serviceTemplate);
101 * Update Composition Definition.
103 * @param compositionId The UUID of the automation composition definition to update
104 * @param serviceTemplate the service template
105 * @return the result of the commissioning operation
108 public CommissioningResponse updateCompositionDefinition(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
109 if (verifyIfInstanceExists(compositionId)) {
110 throw new PfModelRuntimeException(Status.BAD_REQUEST,
111 "There are ACM instances, Update of ACM Definition not allowed");
113 var acDefinition = acDefinitionProvider.getAcDefinition(compositionId);
114 if (!AcTypeState.COMMISSIONED.equals(acDefinition.getState())) {
115 throw new PfModelRuntimeException(Status.BAD_REQUEST,
116 "ACM not in COMMISSIONED state, Update of ACM Definition not allowed");
118 acDefinitionProvider.updateServiceTemplate(compositionId, serviceTemplate,
119 acRuntimeParameterGroup.getAcmParameters().getToscaElementName(),
120 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
122 return createCommissioningResponse(compositionId, serviceTemplate);
126 * Delete the automation composition definition with the given name and version.
128 * @param compositionId The UUID of the automation composition definition to delete
129 * @return the result of the deletion
132 public CommissioningResponse deleteAutomationCompositionDefinition(UUID compositionId) {
133 if (verifyIfInstanceExists(compositionId)) {
134 throw new PfModelRuntimeException(Status.BAD_REQUEST,
135 "Delete instances, to commission automation composition definitions");
137 var acDefinition = acDefinitionProvider.getAcDefinition(compositionId);
138 if (!AcTypeState.COMMISSIONED.equals(acDefinition.getState())) {
139 throw new PfModelRuntimeException(Status.BAD_REQUEST,
140 "ACM not in COMMISSIONED state, Delete of ACM Definition not allowed");
142 var serviceTemplate = acDefinitionProvider.deleteAcDefinition(compositionId);
143 return createCommissioningResponse(compositionId, serviceTemplate);
147 * Get automation composition definition.
149 * @param acName the name of the automation composition, null for all
150 * @param acVersion the version of the automation composition, null for all
151 * @param pageable the Pageable
152 * @return automation composition definition
154 @Transactional(readOnly = true)
155 public ToscaServiceTemplates getAutomationCompositionDefinitions(String acName, String acVersion,
156 @NonNull Pageable pageable) {
157 var result = new ToscaServiceTemplates();
158 result.setServiceTemplates(acDefinitionProvider.getServiceTemplateList(acName, acVersion, pageable));
163 * Get automation composition definition.
165 * @param compositionId the compositionId
166 * @return automation composition definition
168 @Transactional(readOnly = true)
169 public AutomationCompositionDefinition getAutomationCompositionDefinition(UUID compositionId) {
171 return acDefinitionProvider.getAcDefinition(compositionId);
175 * Validates to see if there is any instance saved.
177 * @return true if exists instance
179 private boolean verifyIfInstanceExists(UUID compositionId) {
180 return !acProvider.getAcInstancesByCompositionId(compositionId).isEmpty();
184 * Composition Definition Priming.
186 * @param compositionId the compositionId
187 * @param acTypeStateUpdate the ACMTypeStateUpdate
189 public void compositionDefinitionPriming(UUID compositionId, AcTypeStateUpdate acTypeStateUpdate) {
190 if (verifyIfInstanceExists(compositionId)) {
191 throw new PfModelRuntimeException(Status.BAD_REQUEST, "There are instances, Priming/Depriming not allowed");
193 var acmDefinition = acDefinitionProvider.getAcDefinition(compositionId);
194 var stateOrdered = acTypeStateResolver.resolve(acTypeStateUpdate.getPrimeOrder(), acmDefinition.getState(),
195 acmDefinition.getStateChangeResult());
196 switch (stateOrdered) {
198 prime(acmDefinition);
202 deprime(acmDefinition);
207 throw new PfModelRuntimeException(Status.BAD_REQUEST, "Not valid " + acTypeStateUpdate.getPrimeOrder());
211 private void prime(AutomationCompositionDefinition acmDefinition) {
212 var preparation = participantPrimePublisher.prepareParticipantPriming(acmDefinition);
213 acDefinitionProvider.updateAcDefinition(acmDefinition,
214 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
217 () -> participantPrimePublisher.sendPriming(preparation, acmDefinition.getCompositionId(), null));
220 private void deprime(AutomationCompositionDefinition acmDefinition) {
221 acmDefinition.setStateChangeResult(StateChangeResult.NO_ERROR);
222 var participantIds = new HashSet<UUID>();
223 for (var elementState : acmDefinition.getElementStateMap().values()) {
224 var participantId = elementState.getParticipantId();
225 if (participantId != null) {
226 elementState.setState(AcTypeState.DEPRIMING);
227 participantIds.add(participantId);
230 if (!participantIds.isEmpty()) {
231 participantProvider.verifyParticipantState(participantIds);
233 acmDefinition.setState(AcTypeState.DEPRIMING);
234 acmDefinition.setLastMsg(TimestampHelper.now());
235 acDefinitionProvider.updateAcDefinition(acmDefinition,
236 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
238 executor.execute(() -> participantPrimePublisher.sendDepriming(acmDefinition.getCompositionId()));