961f12a5c6f82beef2eac32b639cbb8ed54c3904
[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 java.util.HashSet;
25 import java.util.UUID;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28 import java.util.stream.Collectors;
29 import javax.ws.rs.core.Response.Status;
30 import lombok.RequiredArgsConstructor;
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     public static final String AUTOMATION_COMPOSITION_NODE_TYPE = "org.onap.policy.clamp.acm.AutomationComposition";
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
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                 .collect(Collectors.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         serviceTemplate = acmDefinition.getServiceTemplate();
92         return createCommissioningResponse(acmDefinition.getCompositionId(), serviceTemplate);
93     }
94
95     /**
96      * Update Composition Definition.
97      *
98      * @param compositionId The UUID of the automation composition definition to update
99      * @param serviceTemplate the service template
100      * @return the result of the commissioning operation
101      */
102     @Transactional
103     public CommissioningResponse updateCompositionDefinition(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
104         if (verifyIfInstanceExists(compositionId)) {
105             throw new PfModelRuntimeException(Status.BAD_REQUEST,
106                     "There are ACM instances, Update of ACM Definition not allowed");
107         }
108         var acDefinition = acDefinitionProvider.getAcDefinition(compositionId);
109         if (!AcTypeState.COMMISSIONED.equals(acDefinition.getState())) {
110             throw new PfModelRuntimeException(Status.BAD_REQUEST,
111                     "ACM not in COMMISSIONED state, Update of ACM Definition not allowed");
112         }
113         acDefinitionProvider.updateServiceTemplate(compositionId, serviceTemplate);
114
115         return createCommissioningResponse(compositionId, serviceTemplate);
116     }
117
118     /**
119      * Delete the automation composition definition with the given name and version.
120      *
121      * @param compositionId The UUID of the automation composition definition to delete
122      * @return the result of the deletion
123      */
124     @Transactional
125     public CommissioningResponse deleteAutomationCompositionDefinition(UUID compositionId) {
126         if (verifyIfInstanceExists(compositionId)) {
127             throw new PfModelRuntimeException(Status.BAD_REQUEST,
128                     "Delete instances, to commission automation composition definitions");
129         }
130         var acDefinition = acDefinitionProvider.getAcDefinition(compositionId);
131         if (!AcTypeState.COMMISSIONED.equals(acDefinition.getState())) {
132             throw new PfModelRuntimeException(Status.BAD_REQUEST,
133                     "ACM not in COMMISSIONED state, Delete of ACM Definition not allowed");
134         }
135         var serviceTemplate = acDefinitionProvider.deleteAcDefintion(compositionId);
136         return createCommissioningResponse(compositionId, serviceTemplate);
137     }
138
139     /**
140      * Get automation composition definition.
141      *
142      * @param acName the name of the automation composition, null for all
143      * @param acVersion the version of the automation composition, null for all
144      * @return automation composition definition
145      * @throws PfModelException on errors getting automation composition definitions
146      */
147     @Transactional(readOnly = true)
148     public ToscaServiceTemplates getAutomationCompositionDefinitions(String acName, String acVersion) {
149
150         var result = new ToscaServiceTemplates();
151         result.setServiceTemplates(acDefinitionProvider.getServiceTemplateList(acName, acVersion));
152         return result;
153     }
154
155     /**
156      * Get automation composition definition.
157      *
158      * @param compositionId the compositionId
159      * @return automation composition definition
160      */
161     @Transactional(readOnly = true)
162     public AutomationCompositionDefinition getAutomationCompositionDefinition(UUID compositionId) {
163
164         return acDefinitionProvider.getAcDefinition(compositionId);
165     }
166
167     /**
168      * Validates to see if there is any instance saved.
169      *
170      * @return true if exists instance
171      */
172     private boolean verifyIfInstanceExists(UUID compositionId) {
173         return !acProvider.getAcInstancesByCompositionId(compositionId).isEmpty();
174     }
175
176     /**
177      * Composition Definition Priming.
178      *
179      * @param compositionId the compositionId
180      * @param acTypeStateUpdate the ACMTypeStateUpdate
181      */
182     public void compositionDefinitionPriming(UUID compositionId, AcTypeStateUpdate acTypeStateUpdate) {
183         if (verifyIfInstanceExists(compositionId)) {
184             throw new PfModelRuntimeException(Status.BAD_REQUEST, "There are instances, Priming/Depriming not allowed");
185         }
186         var acmDefinition = acDefinitionProvider.getAcDefinition(compositionId);
187         if (acmDefinition.getRestarting() != null) {
188             throw new PfModelRuntimeException(Status.BAD_REQUEST,
189                     "There is a restarting process, Priming/Depriming not allowed");
190         }
191         var stateOrdered = acTypeStateResolver.resolve(acTypeStateUpdate.getPrimeOrder(), acmDefinition.getState(),
192                 acmDefinition.getStateChangeResult());
193         switch (stateOrdered) {
194             case PRIME:
195                 prime(acmDefinition);
196                 break;
197
198             case DEPRIME:
199                 deprime(acmDefinition);
200
201                 break;
202
203             default:
204                 throw new PfModelRuntimeException(Status.BAD_REQUEST, "Not valid " + acTypeStateUpdate.getPrimeOrder());
205         }
206     }
207
208     private void prime(AutomationCompositionDefinition acmDefinition) {
209         acmDefinition.setStateChangeResult(StateChangeResult.NO_ERROR);
210         var preparation = participantPrimePublisher.prepareParticipantPriming(acmDefinition);
211         acDefinitionProvider.updateAcDefinition(acmDefinition);
212
213         executor.execute(
214                 () -> participantPrimePublisher.sendPriming(preparation, acmDefinition.getCompositionId(), null));
215     }
216
217     private void deprime(AutomationCompositionDefinition acmDefinition) {
218         acmDefinition.setStateChangeResult(StateChangeResult.NO_ERROR);
219         var participantIds = new HashSet<UUID>();
220         for (var elementState : acmDefinition.getElementStateMap().values()) {
221             var participantId = elementState.getParticipantId();
222             if (participantId != null) {
223                 elementState.setState(AcTypeState.DEPRIMING);
224                 participantIds.add(participantId);
225             }
226         }
227         if (!participantIds.isEmpty()) {
228             acmParticipantProvider.verifyParticipantState(participantIds);
229         }
230         acmDefinition.setState(AcTypeState.DEPRIMING);
231         acDefinitionProvider.updateAcDefinition(acmDefinition);
232
233         executor.execute(() -> participantPrimePublisher.sendDepriming(acmDefinition.getCompositionId()));
234     }
235
236 }