53288d15ea7a6732f5fa0e4a0e7ffdaf7cadf504
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2022 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.List;
25 import java.util.UUID;
26 import java.util.function.Function;
27 import java.util.stream.Collectors;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.Response.Status;
30 import lombok.AllArgsConstructor;
31 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionHandler;
32 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionException;
33 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
37 import org.onap.policy.clamp.models.acm.concepts.Participant;
38 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationCommand;
39 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationResponse;
40 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
41 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
42 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
43 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
44 import org.onap.policy.common.parameters.BeanValidationResult;
45 import org.onap.policy.common.parameters.ObjectValidationResult;
46 import org.onap.policy.common.parameters.ValidationStatus;
47 import org.onap.policy.models.base.PfModelRuntimeException;
48 import org.springframework.stereotype.Service;
49 import org.springframework.transaction.annotation.Transactional;
50
51 /**
52  * This class is dedicated to the Instantiation of Commissioned automation composition.
53  */
54 @Service
55 @Transactional
56 @AllArgsConstructor
57 public class AutomationCompositionInstantiationProvider {
58     private static final String AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE = "AutomationCompositionElement";
59     private static final String DO_NOT_MATCH = " do not match with ";
60
61     private final AutomationCompositionProvider automationCompositionProvider;
62     private final SupervisionHandler supervisionHandler;
63     private final ParticipantProvider participantProvider;
64     private final AcDefinitionProvider acDefinitionProvider;
65     private static final String ENTRY = "entry ";
66
67     /**
68      * Create automation composition.
69      *
70      * @param compositionId The UUID of the automation composition definition
71      * @param automationComposition the automation composition
72      * @return the result of the instantiation operation
73      */
74     public InstantiationResponse createAutomationComposition(UUID compositionId,
75             AutomationComposition automationComposition) {
76         if (!compositionId.equals(automationComposition.getCompositionId())) {
77             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
78                     automationComposition.getCompositionId() + DO_NOT_MATCH + compositionId);
79         }
80         var checkAutomationCompositionOpt =
81                 automationCompositionProvider.findAutomationComposition(automationComposition.getKey().asIdentifier());
82         if (checkAutomationCompositionOpt.isPresent()) {
83             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
84                     automationComposition.getKey().asIdentifier() + " already defined");
85         }
86
87         var validationResult = validateAutomationComposition(automationComposition);
88         if (!validationResult.isValid()) {
89             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
90         }
91         automationComposition = automationCompositionProvider.createAutomationComposition(automationComposition);
92
93         var response = new InstantiationResponse();
94         response.setInstanceId(automationComposition.getInstanceId());
95         response.setAffectedAutomationComposition(automationComposition.getKey().asIdentifier());
96
97         return response;
98     }
99
100     /**
101      * Update automation composition.
102      *
103      * @param compositionId The UUID of the automation composition definition
104      * @param automationComposition the automation composition
105      * @return the result of the update
106      */
107     public InstantiationResponse updateAutomationComposition(UUID compositionId,
108             AutomationComposition automationComposition) {
109         var instanceId = automationComposition.getInstanceId();
110         var acToUpdate = automationCompositionProvider.getAutomationComposition(instanceId);
111         if (!compositionId.equals(acToUpdate.getCompositionId())) {
112             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
113                     automationComposition.getCompositionId() + DO_NOT_MATCH + compositionId);
114         }
115         acToUpdate.setElements(automationComposition.getElements());
116         acToUpdate.setName(automationComposition.getName());
117         acToUpdate.setVersion(automationComposition.getVersion());
118         acToUpdate.setDescription(automationComposition.getDescription());
119         acToUpdate.setDerivedFrom(automationComposition.getDerivedFrom());
120         var validationResult = validateAutomationComposition(acToUpdate);
121         if (!validationResult.isValid()) {
122             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
123         }
124         automationComposition = automationCompositionProvider.updateAutomationComposition(acToUpdate);
125
126         var response = new InstantiationResponse();
127         response.setInstanceId(instanceId);
128         response.setAffectedAutomationComposition(automationComposition.getKey().asIdentifier());
129         return response;
130     }
131
132     /**
133      * Validate AutomationComposition.
134      *
135      * @param automationComposition AutomationComposition to validate
136      * @return the result of validation
137      */
138     private BeanValidationResult validateAutomationComposition(AutomationComposition automationComposition) {
139
140         var result = new BeanValidationResult("AutomationComposition", automationComposition);
141         var serviceTemplate = acDefinitionProvider.findAcDefinition(automationComposition.getCompositionId());
142         if (serviceTemplate.isEmpty()) {
143             result.addResult(new ObjectValidationResult("ServiceTemplate", "", ValidationStatus.INVALID,
144                     "Commissioned automation composition definition not found"));
145         } else {
146             result.addResult(AcmUtils.validateAutomationComposition(automationComposition, serviceTemplate.get()));
147         }
148         return result;
149     }
150
151     /**
152      * Get Automation Composition.
153      *
154      * @param compositionId The UUID of the automation composition definition
155      * @param instanceId The UUID of the automation composition instance
156      * @return the Automation Composition
157      */
158     @Transactional(readOnly = true)
159     public AutomationComposition getAutomationComposition(UUID compositionId, UUID instanceId) {
160         var automationComposition = automationCompositionProvider.getAutomationComposition(instanceId);
161         if (!automationComposition.getCompositionId().equals(compositionId)) {
162             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
163                     "Composition Id " + compositionId + DO_NOT_MATCH + automationComposition.getCompositionId());
164         }
165         return automationComposition;
166     }
167
168     /**
169      * Delete the automation composition with the given name and version.
170      *
171      * @param compositionId The UUID of the automation composition definition
172      * @param instanceId The UUID of the automation composition instance
173      * @return the result of the deletion
174      */
175     public InstantiationResponse deleteAutomationComposition(UUID compositionId, UUID instanceId) {
176         var automationComposition = automationCompositionProvider.getAutomationComposition(instanceId);
177         if (!compositionId.equals(automationComposition.getCompositionId())) {
178             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
179                     automationComposition.getCompositionId() + DO_NOT_MATCH + compositionId);
180         }
181         if (!AutomationCompositionState.UNINITIALISED.equals(automationComposition.getState())) {
182             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
183                     "Automation composition state is still " + automationComposition.getState());
184         }
185         var response = new InstantiationResponse();
186         automationComposition =
187                 automationCompositionProvider.deleteAutomationComposition(automationComposition.getInstanceId());
188         response.setInstanceId(automationComposition.getInstanceId());
189         response.setAffectedAutomationComposition(automationComposition.getKey().asIdentifier());
190         return response;
191     }
192
193     /**
194      * Get the requested automation compositions.
195      *
196      * @param name the name of the automation composition to get, null for all automation compositions
197      * @param version the version of the automation composition to get, null for all automation compositions
198      * @return the automation compositions
199      */
200     @Transactional(readOnly = true)
201     public AutomationCompositions getAutomationCompositions(UUID compositionId, String name, String version) {
202         var automationCompositions = new AutomationCompositions();
203         automationCompositions.setAutomationCompositionList(
204                 automationCompositionProvider.getAutomationCompositions(compositionId, name, version));
205
206         return automationCompositions;
207     }
208
209     /**
210      * Issue a command to automation compositions, setting their ordered state.
211      *
212      * @param automationComposition the AutomationComposition
213      * @param command the command to issue to automation compositions
214      */
215     public void issueAutomationCompositionCommand(AutomationComposition automationComposition,
216             InstantiationCommand command) {
217
218         if (command.getOrderedState() == null) {
219             throw new AutomationCompositionRuntimeException(Status.BAD_REQUEST,
220                     "ordered state invalid or not specified on command");
221         }
222
223         var participants = participantProvider.getParticipants();
224         if (participants.isEmpty()) {
225             throw new AutomationCompositionRuntimeException(Status.BAD_REQUEST, "No participants registered");
226         }
227         var validationResult = validateIssueAutomationComposition(automationComposition, participants);
228         if (!validationResult.isValid()) {
229             throw new AutomationCompositionRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
230         }
231
232         automationComposition.setCascadedOrderedState(command.getOrderedState());
233         try {
234             supervisionHandler.triggerAutomationCompositionSupervision(automationComposition);
235         } catch (AutomationCompositionException e) {
236             throw new AutomationCompositionRuntimeException(Response.Status.BAD_REQUEST, e.getMessage());
237         }
238         automationCompositionProvider.updateAutomationComposition(automationComposition);
239     }
240
241     private BeanValidationResult validateIssueAutomationComposition(AutomationComposition automationComposition,
242             List<Participant> participants) {
243         var result = new BeanValidationResult("AutomationComposition", automationComposition);
244
245         var participantMap = participants.stream()
246                 .collect(Collectors.toMap(participant -> participant.getParticipantId(), Function.identity()));
247
248         for (var element : automationComposition.getElements().values()) {
249
250             var subResult = new BeanValidationResult(ENTRY + element.getDefinition().getName(), element);
251             var p = participantMap.get(element.getParticipantId());
252             if (p == null) {
253                 subResult.addResult(new ObjectValidationResult(AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE,
254                         element.getDefinition().getName(), ValidationStatus.INVALID,
255                         "Participant with ID " + element.getParticipantId() + " is not registered"));
256             } else if (!p.getParticipantType().equals(element.getParticipantType())) {
257                 subResult.addResult(new ObjectValidationResult(AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE,
258                         element.getDefinition().getName(), ValidationStatus.INVALID,
259                         "Participant with ID " + element.getParticipantType() + " - " + element.getParticipantId()
260                                 + " is not registered"));
261             }
262             result.addResult(subResult);
263         }
264
265         return result;
266     }
267 }