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