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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.acm.runtime.instantiation;
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;
52 * This class is dedicated to the Instantiation of Commissioned automation composition.
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 ";
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 ";
68 * Create automation composition.
70 * @param compositionId The UUID of the automation composition definition
71 * @param automationComposition the automation composition
72 * @return the result of the instantiation operation
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);
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");
87 var validationResult = validateAutomationComposition(automationComposition);
88 if (!validationResult.isValid()) {
89 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
91 automationComposition = automationCompositionProvider.createAutomationComposition(automationComposition);
93 var response = new InstantiationResponse();
94 response.setInstanceId(automationComposition.getInstanceId());
95 response.setAffectedAutomationComposition(automationComposition.getKey().asIdentifier());
101 * Update automation composition.
103 * @param compositionId The UUID of the automation composition definition
104 * @param automationComposition the automation composition
105 * @return the result of the update
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);
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());
124 automationComposition = automationCompositionProvider.updateAutomationComposition(acToUpdate);
126 var response = new InstantiationResponse();
127 response.setInstanceId(instanceId);
128 response.setAffectedAutomationComposition(automationComposition.getKey().asIdentifier());
133 * Validate AutomationComposition.
135 * @param automationComposition AutomationComposition to validate
136 * @return the result of validation
138 private BeanValidationResult validateAutomationComposition(AutomationComposition automationComposition) {
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"));
146 result.addResult(AcmUtils.validateAutomationComposition(automationComposition, serviceTemplate.get()));
152 * Get Automation Composition.
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
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());
165 return automationComposition;
169 * Delete the automation composition with the given name and version.
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
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);
181 if (!AutomationCompositionState.UNINITIALISED.equals(automationComposition.getState())) {
182 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
183 "Automation composition state is still " + automationComposition.getState());
185 var response = new InstantiationResponse();
186 automationComposition =
187 automationCompositionProvider.deleteAutomationComposition(automationComposition.getInstanceId());
188 response.setInstanceId(automationComposition.getInstanceId());
189 response.setAffectedAutomationComposition(automationComposition.getKey().asIdentifier());
194 * Get the requested automation compositions.
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
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));
206 return automationCompositions;
210 * Issue a command to automation compositions, setting their ordered state.
212 * @param automationComposition the AutomationComposition
213 * @param command the command to issue to automation compositions
215 public void issueAutomationCompositionCommand(AutomationComposition automationComposition,
216 InstantiationCommand command) {
218 if (command.getOrderedState() == null) {
219 throw new AutomationCompositionRuntimeException(Status.BAD_REQUEST,
220 "ordered state invalid or not specified on command");
223 var participants = participantProvider.getParticipants();
224 if (participants.isEmpty()) {
225 throw new AutomationCompositionRuntimeException(Status.BAD_REQUEST, "No participants registered");
227 var validationResult = validateIssueAutomationComposition(automationComposition, participants);
228 if (!validationResult.isValid()) {
229 throw new AutomationCompositionRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
232 automationComposition.setCascadedOrderedState(command.getOrderedState());
234 supervisionHandler.triggerAutomationCompositionSupervision(automationComposition);
235 } catch (AutomationCompositionException e) {
236 throw new AutomationCompositionRuntimeException(Response.Status.BAD_REQUEST, e.getMessage());
238 automationCompositionProvider.updateAutomationComposition(automationComposition);
241 private BeanValidationResult validateIssueAutomationComposition(AutomationComposition automationComposition,
242 List<Participant> participants) {
243 var result = new BeanValidationResult("AutomationComposition", automationComposition);
245 var participantMap = participants.stream()
246 .collect(Collectors.toMap(participant -> participant.getParticipantId(), Function.identity()));
248 for (var element : automationComposition.getElements().values()) {
250 var subResult = new BeanValidationResult(ENTRY + element.getDefinition().getName(), element);
251 var p = participantMap.get(element.getParticipantId());
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"));
262 result.addResult(subResult);