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.function.Function;
26 import java.util.stream.Collectors;
27 import javax.ws.rs.core.Response;
28 import javax.ws.rs.core.Response.Status;
29 import lombok.AllArgsConstructor;
30 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionHandler;
31 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionException;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
33 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
35 import org.onap.policy.clamp.models.acm.concepts.Participant;
36 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationCommand;
37 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationResponse;
38 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
39 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
40 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
41 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
42 import org.onap.policy.common.parameters.BeanValidationResult;
43 import org.onap.policy.common.parameters.ObjectValidationResult;
44 import org.onap.policy.common.parameters.ValidationStatus;
45 import org.onap.policy.models.base.PfModelRuntimeException;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47 import org.springframework.stereotype.Service;
48 import org.springframework.transaction.annotation.Transactional;
51 * This class is dedicated to the Instantiation of Commissioned automation composition.
56 public class AutomationCompositionInstantiationProvider {
57 private static final String AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE = "AutomationCompositionElement";
59 private final AutomationCompositionProvider automationCompositionProvider;
60 private final SupervisionHandler supervisionHandler;
61 private final ParticipantProvider participantProvider;
62 private final AcDefinitionProvider acDefinitionProvider;
63 private static final String ENTRY = "entry ";
66 * Create automation composition.
68 * @param automationComposition the automation composition
69 * @return the result of the instantiation operation
71 public InstantiationResponse createAutomationComposition(AutomationComposition automationComposition) {
73 var checkAutomationCompositionOpt =
74 automationCompositionProvider.findAutomationComposition(automationComposition.getKey().asIdentifier());
75 if (checkAutomationCompositionOpt.isPresent()) {
76 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
77 automationComposition.getKey().asIdentifier() + " already defined");
80 var validationResult = validateAutomationComposition(automationComposition);
81 if (!validationResult.isValid()) {
82 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
84 automationComposition = automationCompositionProvider.createAutomationComposition(automationComposition);
86 var response = new InstantiationResponse();
87 response.setInstanceId(automationComposition.getInstanceId());
88 response.setAffectedAutomationComposition(automationComposition.getKey().asIdentifier());
94 * Update automation composition.
96 * @param automationComposition the automation composition
97 * @return the result of the instantiation operation
99 public InstantiationResponse updateAutomationComposition(AutomationComposition automationComposition) {
100 var validationResult = validateAutomationComposition(automationComposition);
101 if (!validationResult.isValid()) {
102 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
104 automationCompositionProvider.updateAutomationComposition(automationComposition);
106 var response = new InstantiationResponse();
107 response.setInstanceId(automationComposition.getInstanceId());
108 response.setAffectedAutomationComposition(automationComposition.getKey().asIdentifier());
114 * Validate AutomationComposition.
116 * @param automationComposition AutomationComposition to validate
117 * @return the result of validation
119 private BeanValidationResult validateAutomationComposition(AutomationComposition automationComposition) {
121 var result = new BeanValidationResult("AutomationComposition", automationComposition);
122 var serviceTemplate = acDefinitionProvider.findAcDefinition(automationComposition.getCompositionId());
123 if (serviceTemplate.isEmpty()) {
124 result.addResult(new ObjectValidationResult("ServiceTemplate", "", ValidationStatus.INVALID,
125 "Commissioned automation composition definition not found"));
127 result.addResult(AcmUtils.validateAutomationComposition(automationComposition, serviceTemplate.get()));
133 * Delete the automation composition with the given name and version.
135 * @param name the name of the automation composition to delete
136 * @param version the version of the automation composition to delete
137 * @return the result of the deletion
139 public InstantiationResponse deleteAutomationComposition(String name, String version) {
140 var automationCompositionOpt =
141 automationCompositionProvider.findAutomationComposition(new ToscaConceptIdentifier(name, version));
142 if (automationCompositionOpt.isEmpty()) {
143 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "Automation composition not found");
145 var automationComposition = automationCompositionOpt.get();
146 if (!AutomationCompositionState.UNINITIALISED.equals(automationComposition.getState())) {
147 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
148 "Automation composition state is still " + automationComposition.getState());
150 var response = new InstantiationResponse();
151 automationComposition =
152 automationCompositionProvider.deleteAutomationComposition(automationComposition.getInstanceId());
153 response.setInstanceId(automationComposition.getInstanceId());
154 response.setAffectedAutomationComposition(automationComposition.getKey().asIdentifier());
159 * Get the requested automation compositions.
161 * @param name the name of the automation composition to get, null for all automation compositions
162 * @param version the version of the automation composition to get, null for all automation compositions
163 * @return the automation compositions
165 @Transactional(readOnly = true)
166 public AutomationCompositions getAutomationCompositions(String name, String version) {
167 var automationCompositions = new AutomationCompositions();
168 automationCompositions
169 .setAutomationCompositionList(automationCompositionProvider.getAutomationCompositions(name, version));
171 return automationCompositions;
175 * Issue a command to automation compositions, setting their ordered state.
177 * @param command the command to issue to automation compositions
178 * @return the result of the initiation command
179 * @throws AutomationCompositionException on ordered state invalid
181 public InstantiationResponse issueAutomationCompositionCommand(InstantiationCommand command)
182 throws AutomationCompositionException {
184 if (command.getOrderedState() == null) {
185 throw new AutomationCompositionException(Status.BAD_REQUEST,
186 "ordered state invalid or not specified on command");
189 var participants = participantProvider.getParticipants();
190 if (participants.isEmpty()) {
191 throw new AutomationCompositionException(Status.BAD_REQUEST, "No participants registered");
193 var automationCompositionOpt =
194 automationCompositionProvider.findAutomationComposition(command.getAutomationCompositionIdentifier());
195 if (automationCompositionOpt.isEmpty()) {
196 throw new AutomationCompositionException(Response.Status.BAD_REQUEST,
197 "AutomationComposition with id " + command.getAutomationCompositionIdentifier() + " not found");
200 var automationComposition = automationCompositionOpt.get();
201 var validationResult = validateIssueAutomationComposition(automationComposition, participants);
202 if (!validationResult.isValid()) {
203 throw new AutomationCompositionException(Response.Status.BAD_REQUEST, validationResult.getResult());
206 automationComposition.setCascadedOrderedState(command.getOrderedState());
207 supervisionHandler.triggerAutomationCompositionSupervision(automationComposition);
208 automationCompositionProvider.updateAutomationComposition(automationComposition);
209 var response = new InstantiationResponse();
210 response.setAffectedAutomationComposition(command.getAutomationCompositionIdentifier());
215 private BeanValidationResult validateIssueAutomationComposition(AutomationComposition automationComposition,
216 List<Participant> participants) {
217 var result = new BeanValidationResult("AutomationComposition", automationComposition);
219 var participantMap = participants.stream()
220 .collect(Collectors.toMap(participant -> participant.getKey().asIdentifier(), Function.identity()));
222 for (var element : automationComposition.getElements().values()) {
224 var subResult = new BeanValidationResult(ENTRY + element.getDefinition().getName(), element);
225 var p = participantMap.get(element.getParticipantId());
227 subResult.addResult(new ObjectValidationResult(AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE,
228 element.getDefinition().getName(), ValidationStatus.INVALID,
229 "Participant with ID " + element.getParticipantId() + " is not registered"));
230 } else if (!p.getParticipantType().equals(element.getParticipantType())) {
231 subResult.addResult(new ObjectValidationResult(AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE,
232 element.getDefinition().getName(), ValidationStatus.INVALID,
233 "Participant with ID " + element.getParticipantType() + " - " + element.getParticipantId()
234 + " is not registered"));
236 result.addResult(subResult);