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