c8ddb4374ed1be9d6937cfcc4c801f8e7725f0f4
[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.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;
50
51 /**
52  * This class provides the create, read and delete actions on Commissioning of automation composition concepts in the
53  * database to the callers.
54  */
55 @Service
56 @RequiredArgsConstructor
57 public class CommissioningProvider {
58
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;
65
66     private final ExecutorService executor = Context.taskWrapping(Executors.newFixedThreadPool(1));
67
68     private CommissioningResponse createCommissioningResponse(UUID compositionId,
69             ToscaServiceTemplate serviceTemplate) {
70         var response = new CommissioningResponse();
71         response.setCompositionId(compositionId);
72         // @formatter:off
73         response.setAffectedAutomationCompositionDefinitions(
74             serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
75                 .values()
76                 .stream()
77                 .map(template -> template.getKey().asIdentifier())
78                 .toList());
79         // @formatter:on
80
81         return response;
82     }
83
84     /**
85      * Create automation composition from a service template.
86      *
87      * @param serviceTemplate the service template
88      * @return the result of the commissioning operation
89      */
90     @Transactional
91     public CommissioningResponse createAutomationCompositionDefinition(ToscaServiceTemplate serviceTemplate) {
92
93         var acmDefinition = acDefinitionProvider.createAutomationCompositionDefinition(serviceTemplate,
94                 acRuntimeParameterGroup.getAcmParameters().getToscaElementName(),
95                 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
96         serviceTemplate = acmDefinition.getServiceTemplate();
97         return createCommissioningResponse(acmDefinition.getCompositionId(), serviceTemplate);
98     }
99
100     /**
101      * Update Composition Definition.
102      *
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
106      */
107     @Transactional
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");
112         }
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");
117         }
118         acDefinitionProvider.updateServiceTemplate(compositionId, serviceTemplate,
119                 acRuntimeParameterGroup.getAcmParameters().getToscaElementName(),
120                 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
121
122         return createCommissioningResponse(compositionId, serviceTemplate);
123     }
124
125     /**
126      * Delete the automation composition definition with the given name and version.
127      *
128      * @param compositionId The UUID of the automation composition definition to delete
129      * @return the result of the deletion
130      */
131     @Transactional
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");
136         }
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");
141         }
142         var serviceTemplate = acDefinitionProvider.deleteAcDefinition(compositionId);
143         return createCommissioningResponse(compositionId, serviceTemplate);
144     }
145
146     /**
147      * Get automation composition definition.
148      *
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
153      */
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));
159         return result;
160     }
161
162     /**
163      * Get automation composition definition.
164      *
165      * @param compositionId the compositionId
166      * @return automation composition definition
167      */
168     @Transactional(readOnly = true)
169     public AutomationCompositionDefinition getAutomationCompositionDefinition(UUID compositionId) {
170
171         return acDefinitionProvider.getAcDefinition(compositionId);
172     }
173
174     /**
175      * Validates to see if there is any instance saved.
176      *
177      * @return true if exists instance
178      */
179     private boolean verifyIfInstanceExists(UUID compositionId) {
180         return !acProvider.getAcInstancesByCompositionId(compositionId).isEmpty();
181     }
182
183     /**
184      * Composition Definition Priming.
185      *
186      * @param compositionId the compositionId
187      * @param acTypeStateUpdate the ACMTypeStateUpdate
188      */
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");
192         }
193         var acmDefinition = acDefinitionProvider.getAcDefinition(compositionId);
194         var stateOrdered = acTypeStateResolver.resolve(acTypeStateUpdate.getPrimeOrder(), acmDefinition.getState(),
195                 acmDefinition.getStateChangeResult());
196         switch (stateOrdered) {
197             case PRIME:
198                 prime(acmDefinition);
199                 break;
200
201             case DEPRIME:
202                 deprime(acmDefinition);
203
204                 break;
205
206             default:
207                 throw new PfModelRuntimeException(Status.BAD_REQUEST, "Not valid " + acTypeStateUpdate.getPrimeOrder());
208         }
209     }
210
211     private void prime(AutomationCompositionDefinition acmDefinition) {
212         var preparation = participantPrimePublisher.prepareParticipantPriming(acmDefinition);
213         acDefinitionProvider.updateAcDefinition(acmDefinition,
214                 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
215
216         executor.execute(
217                 () -> participantPrimePublisher.sendPriming(preparation, acmDefinition.getCompositionId(), null));
218     }
219
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);
228             }
229         }
230         if (!participantIds.isEmpty()) {
231             participantProvider.verifyParticipantState(participantIds);
232         }
233         acmDefinition.setState(AcTypeState.DEPRIMING);
234         acmDefinition.setLastMsg(TimestampHelper.now());
235         acDefinitionProvider.updateAcDefinition(acmDefinition,
236                 acRuntimeParameterGroup.getAcmParameters().getToscaCompositionName());
237
238         executor.execute(() -> participantPrimePublisher.sendDepriming(acmDefinition.getCompositionId()));
239     }
240
241 }