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