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