4665fec3729de047b0c91aa17506be926be85b6f
[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.instantiation;
23
24 import java.util.UUID;
25 import javax.validation.Valid;
26 import javax.ws.rs.core.Response;
27 import javax.ws.rs.core.Response.Status;
28 import lombok.AllArgsConstructor;
29 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionAcHandler;
30 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
31 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
33 import org.onap.policy.clamp.models.acm.concepts.DeployState;
34 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.AcInstanceStateUpdate;
35 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationResponse;
36 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
37 import org.onap.policy.clamp.models.acm.persistence.provider.AcInstanceStateResolver;
38 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
39 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
40 import org.onap.policy.common.parameters.BeanValidationResult;
41 import org.onap.policy.common.parameters.ObjectValidationResult;
42 import org.onap.policy.common.parameters.ValidationStatus;
43 import org.onap.policy.models.base.PfModelRuntimeException;
44 import org.springframework.stereotype.Service;
45 import org.springframework.transaction.annotation.Transactional;
46
47 /**
48  * This class is dedicated to the Instantiation of Commissioned automation composition.
49  */
50 @Service
51 @Transactional
52 @AllArgsConstructor
53 public class AutomationCompositionInstantiationProvider {
54     private static final String DO_NOT_MATCH = " do not match with ";
55
56     private final AutomationCompositionProvider automationCompositionProvider;
57     private final AcDefinitionProvider acDefinitionProvider;
58     private final AcInstanceStateResolver acInstanceStateResolver;
59     private final SupervisionAcHandler supervisionAcHandler;
60
61     /**
62      * Create automation composition.
63      *
64      * @param compositionId The UUID of the automation composition definition
65      * @param automationComposition the automation composition
66      * @return the result of the instantiation operation
67      */
68     public InstantiationResponse createAutomationComposition(UUID compositionId,
69             AutomationComposition automationComposition) {
70         if (!compositionId.equals(automationComposition.getCompositionId())) {
71             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
72                     automationComposition.getCompositionId() + DO_NOT_MATCH + compositionId);
73         }
74         var checkAutomationCompositionOpt =
75                 automationCompositionProvider.findAutomationComposition(automationComposition.getKey().asIdentifier());
76         if (checkAutomationCompositionOpt.isPresent()) {
77             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
78                     automationComposition.getKey().asIdentifier() + " already defined");
79         }
80
81         var validationResult = validateAutomationComposition(automationComposition);
82         if (!validationResult.isValid()) {
83             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
84         }
85         automationComposition = automationCompositionProvider.createAutomationComposition(automationComposition);
86
87         var response = new InstantiationResponse();
88         response.setInstanceId(automationComposition.getInstanceId());
89         response.setAffectedAutomationComposition(automationComposition.getKey().asIdentifier());
90
91         return response;
92     }
93
94     /**
95      * Update automation composition.
96      *
97      * @param compositionId The UUID of the automation composition definition
98      * @param automationComposition the automation composition
99      * @return the result of the update
100      */
101     public InstantiationResponse updateAutomationComposition(UUID compositionId,
102             AutomationComposition automationComposition) {
103         var instanceId = automationComposition.getInstanceId();
104         var acToUpdate = automationCompositionProvider.getAutomationComposition(instanceId);
105         if (!compositionId.equals(acToUpdate.getCompositionId())) {
106             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
107                     automationComposition.getCompositionId() + DO_NOT_MATCH + compositionId);
108         }
109         if (!DeployState.UNDEPLOYED.equals(acToUpdate.getDeployState())) {
110             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
111                     "Not allow to update for state in " + acToUpdate.getDeployState());
112         }
113         acToUpdate.setElements(automationComposition.getElements());
114         acToUpdate.setName(automationComposition.getName());
115         acToUpdate.setVersion(automationComposition.getVersion());
116         acToUpdate.setDescription(automationComposition.getDescription());
117         acToUpdate.setDerivedFrom(automationComposition.getDerivedFrom());
118         var validationResult = validateAutomationComposition(acToUpdate);
119         if (!validationResult.isValid()) {
120             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
121         }
122         automationComposition = automationCompositionProvider.updateAutomationComposition(acToUpdate);
123
124         var response = new InstantiationResponse();
125         response.setInstanceId(instanceId);
126         response.setAffectedAutomationComposition(automationComposition.getKey().asIdentifier());
127         return response;
128     }
129
130     /**
131      * Validate AutomationComposition.
132      *
133      * @param automationComposition AutomationComposition to validate
134      * @return the result of validation
135      */
136     private BeanValidationResult validateAutomationComposition(AutomationComposition automationComposition) {
137
138         var result = new BeanValidationResult("AutomationComposition", automationComposition);
139         var acDefinitionOpt = acDefinitionProvider.findAcDefinition(automationComposition.getCompositionId());
140         if (acDefinitionOpt.isEmpty()) {
141             result.addResult(new ObjectValidationResult("ServiceTemplate", "", ValidationStatus.INVALID,
142                     "Commissioned automation composition definition not found"));
143             return result;
144         }
145         if (!AcTypeState.PRIMED.equals(acDefinitionOpt.get().getState())) {
146             result.addResult(new ObjectValidationResult("ServiceTemplate", acDefinitionOpt.get().getState(),
147                     ValidationStatus.INVALID, "Commissioned automation composition definition not primed"));
148             return result;
149         }
150         result.addResult(AcmUtils.validateAutomationComposition(automationComposition,
151                 acDefinitionOpt.get().getServiceTemplate()));
152
153         if (result.isValid()) {
154             for (var element : automationComposition.getElements().values()) {
155                 var name = element.getDefinition().getName();
156                 var participantId = acDefinitionOpt.get().getElementStateMap().get(name).getParticipantId();
157                 element.setParticipantId(participantId);
158             }
159         }
160
161         return result;
162     }
163
164     /**
165      * Get Automation Composition.
166      *
167      * @param compositionId The UUID of the automation composition definition
168      * @param instanceId The UUID of the automation composition instance
169      * @return the Automation Composition
170      */
171     @Transactional(readOnly = true)
172     public AutomationComposition getAutomationComposition(UUID compositionId, UUID instanceId) {
173         var automationComposition = automationCompositionProvider.getAutomationComposition(instanceId);
174         if (!automationComposition.getCompositionId().equals(compositionId)) {
175             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
176                     automationComposition.getCompositionId() + DO_NOT_MATCH + compositionId);
177         }
178         return automationComposition;
179     }
180
181     /**
182      * Delete the automation composition with the given name and version.
183      *
184      * @param compositionId The UUID of the automation composition definition
185      * @param instanceId The UUID of the automation composition instance
186      * @return the result of the deletion
187      */
188     public InstantiationResponse deleteAutomationComposition(UUID compositionId, UUID instanceId) {
189         var automationComposition = automationCompositionProvider.getAutomationComposition(instanceId);
190         if (!compositionId.equals(automationComposition.getCompositionId())) {
191             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
192                     automationComposition.getCompositionId() + DO_NOT_MATCH + compositionId);
193         }
194         if (!DeployState.UNDEPLOYED.equals(automationComposition.getDeployState())) {
195             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
196                     "Automation composition state is still " + automationComposition.getDeployState());
197         }
198         var response = new InstantiationResponse();
199         var acDefinition = acDefinitionProvider.getAcDefinition(automationComposition.getCompositionId());
200         supervisionAcHandler.delete(automationComposition, acDefinition);
201         response.setInstanceId(automationComposition.getInstanceId());
202         response.setAffectedAutomationComposition(automationComposition.getKey().asIdentifier());
203         return response;
204     }
205
206     /**
207      * Get the requested automation compositions.
208      *
209      * @param name the name of the automation composition to get, null for all automation compositions
210      * @param version the version of the automation composition to get, null for all automation compositions
211      * @return the automation compositions
212      */
213     @Transactional(readOnly = true)
214     public AutomationCompositions getAutomationCompositions(UUID compositionId, String name, String version) {
215         var automationCompositions = new AutomationCompositions();
216         automationCompositions.setAutomationCompositionList(
217                 automationCompositionProvider.getAutomationCompositions(compositionId, name, version));
218
219         return automationCompositions;
220     }
221
222     /**
223      * Handle Composition Instance State.
224      *
225      * @param compositionId the compositionId
226      * @param instanceId the instanceId
227      * @param acInstanceStateUpdate the AcInstanceStateUpdate
228      */
229     public void compositionInstanceState(UUID compositionId, UUID instanceId,
230             @Valid AcInstanceStateUpdate acInstanceStateUpdate) {
231         var automationComposition = automationCompositionProvider.getAutomationComposition(instanceId);
232         if (!compositionId.equals(automationComposition.getCompositionId())) {
233             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
234                     automationComposition.getCompositionId() + DO_NOT_MATCH + compositionId);
235         }
236         var acDefinition = acDefinitionProvider.getAcDefinition(automationComposition.getCompositionId());
237         var result = acInstanceStateResolver.resolve(acInstanceStateUpdate.getDeployOrder(),
238                 acInstanceStateUpdate.getLockOrder(), automationComposition.getDeployState(),
239                 automationComposition.getLockState());
240         switch (result) {
241             case "DEPLOY":
242                 supervisionAcHandler.deploy(automationComposition, acDefinition);
243                 break;
244
245             case "UNDEPLOY":
246                 supervisionAcHandler.undeploy(automationComposition, acDefinition);
247                 break;
248
249             case "LOCK":
250                 supervisionAcHandler.lock(automationComposition, acDefinition);
251                 break;
252
253             case "UNLOCK":
254                 supervisionAcHandler.unlock(automationComposition, acDefinition);
255                 break;
256
257             default:
258                 throw new PfModelRuntimeException(Status.BAD_REQUEST, "Not valid " + acInstanceStateUpdate);
259         }
260     }
261 }