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