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.ArrayList;
25 import java.util.List;
27 import java.util.function.Function;
28 import java.util.stream.Collectors;
29 import javax.ws.rs.core.Response;
30 import javax.ws.rs.core.Response.Status;
31 import lombok.AllArgsConstructor;
32 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionHandler;
33 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionException;
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.PfModelException;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
49 import org.springframework.stereotype.Service;
50 import org.springframework.transaction.annotation.Transactional;
53 * This class is dedicated to the Instantiation of Commissioned automation composition.
58 public class AutomationCompositionInstantiationProvider {
59 private static final String AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE = "AutomationCompositionElement";
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 compositions.
70 * @param automationCompositions the automation composition
71 * @return the result of the instantiation operation
72 * @throws PfModelException on creation errors
74 public InstantiationResponse createAutomationCompositions(AutomationCompositions automationCompositions)
75 throws PfModelException {
76 for (AutomationComposition automationComposition : automationCompositions.getAutomationCompositionList()) {
77 var checkAutomationCompositionOpt =
78 automationCompositionProvider.findAutomationComposition(automationComposition.getKey().asIdentifier());
79 if (checkAutomationCompositionOpt.isPresent()) {
80 throw new PfModelException(Response.Status.BAD_REQUEST,
81 automationComposition.getKey().asIdentifier() + " already defined");
84 BeanValidationResult validationResult = validateAutomationCompositions(automationCompositions);
85 if (!validationResult.isValid()) {
86 throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
88 automationCompositionProvider.saveAutomationCompositions(automationCompositions.getAutomationCompositionList());
90 var response = new InstantiationResponse();
91 response.setAffectedAutomationCompositions(automationCompositions.getAutomationCompositionList().stream()
92 .map(ac -> ac.getKey().asIdentifier()).collect(Collectors.toList()));
98 * Update automation compositions.
100 * @param automationCompositions the automation composition
101 * @return the result of the instantiation operation
102 * @throws PfModelException on update errors
104 public InstantiationResponse updateAutomationCompositions(AutomationCompositions automationCompositions)
105 throws PfModelException {
106 BeanValidationResult validationResult = validateAutomationCompositions(automationCompositions);
107 if (!validationResult.isValid()) {
108 throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
110 automationCompositionProvider.saveAutomationCompositions(automationCompositions.getAutomationCompositionList());
112 var response = new InstantiationResponse();
113 response.setAffectedAutomationCompositions(automationCompositions.getAutomationCompositionList().stream()
114 .map(ac -> ac.getKey().asIdentifier()).collect(Collectors.toList()));
120 * Validate AutomationCompositions.
122 * @param automationCompositions AutomationCompositions to validate
123 * @return the result of validation
124 * @throws PfModelException if automationCompositions is not valid
126 private BeanValidationResult validateAutomationCompositions(AutomationCompositions automationCompositions) {
128 var result = new BeanValidationResult("AutomationCompositions", automationCompositions);
129 for (var automationComposition : automationCompositions.getAutomationCompositionList()) {
130 var serviceTemplate = acDefinitionProvider.findAcDefinition(automationComposition.getCompositionId());
131 if (serviceTemplate.isEmpty()) {
132 result.addResult(new ObjectValidationResult("ServiceTemplate", "", ValidationStatus.INVALID,
133 "Commissioned automation composition definition not found"));
135 result.addResult(AcmUtils.validateAutomationComposition(automationComposition, serviceTemplate.get()));
142 * Delete the automation composition with the given name and version.
144 * @param name the name of the automation composition to delete
145 * @param version the version of the automation composition to delete
146 * @return the result of the deletion
147 * @throws PfModelException on deletion errors
149 public InstantiationResponse deleteAutomationComposition(String name, String version) throws PfModelException {
150 var automationCompositionOpt = automationCompositionProvider.findAutomationComposition(name, version);
151 if (automationCompositionOpt.isEmpty()) {
152 throw new PfModelException(Response.Status.NOT_FOUND, "Automation composition not found");
154 var automationComposition = automationCompositionOpt.get();
155 if (!AutomationCompositionState.UNINITIALISED.equals(automationComposition.getState())) {
156 throw new PfModelException(Response.Status.BAD_REQUEST,
157 "Automation composition state is still " + automationComposition.getState());
159 var response = new InstantiationResponse();
160 response.setAffectedAutomationCompositions(
161 List.of(automationCompositionProvider.deleteAutomationComposition(name, version).getKey().asIdentifier()));
166 * Get the requested automation compositions.
168 * @param name the name of the automation composition to get, null for all automation compositions
169 * @param version the version of the automation composition to get, null for all automation compositions
170 * @return the automation compositions
171 * @throws PfModelException on errors getting automation compositions
173 @Transactional(readOnly = true)
174 public AutomationCompositions getAutomationCompositions(String name, String version) throws PfModelException {
175 var automationCompositions = new AutomationCompositions();
176 automationCompositions
177 .setAutomationCompositionList(automationCompositionProvider.getAutomationCompositions(name, version));
179 return automationCompositions;
183 * Issue a command to automation compositions, setting their ordered state.
185 * @param command the command to issue to automation compositions
186 * @return the result of the initiation command
187 * @throws PfModelException on errors setting the ordered state on the automation compositions
188 * @throws AutomationCompositionException on ordered state invalid
190 public InstantiationResponse issueAutomationCompositionCommand(InstantiationCommand command)
191 throws AutomationCompositionException, PfModelException {
193 if (command.getOrderedState() == null) {
194 throw new AutomationCompositionException(Status.BAD_REQUEST,
195 "ordered state invalid or not specified on command");
198 var participants = participantProvider.getParticipants();
199 if (participants.isEmpty()) {
200 throw new AutomationCompositionException(Status.BAD_REQUEST, "No participants registered");
202 var validationResult = new BeanValidationResult("InstantiationCommand", command);
203 List<AutomationComposition> automationCompositions =
204 new ArrayList<>(command.getAutomationCompositionIdentifierList().size());
205 for (ToscaConceptIdentifier id : command.getAutomationCompositionIdentifierList()) {
206 var automationCompositionOpt = automationCompositionProvider.findAutomationComposition(id);
207 if (automationCompositionOpt.isEmpty()) {
208 validationResult.addResult("ToscaConceptIdentifier", id, ValidationStatus.INVALID,
209 "AutomationComposition with id " + id + " not found");
211 var automationComposition = automationCompositionOpt.get();
212 automationComposition.setCascadedOrderedState(command.getOrderedState());
213 automationCompositions.add(automationComposition);
216 if (validationResult.isValid()) {
217 validationResult = validateIssueAutomationCompositions(automationCompositions, participants);
219 if (!validationResult.isValid()) {
220 throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
222 automationCompositionProvider.saveAutomationCompositions(automationCompositions);
224 supervisionHandler.triggerAutomationCompositionSupervision(command.getAutomationCompositionIdentifierList());
225 var response = new InstantiationResponse();
226 response.setAffectedAutomationCompositions(command.getAutomationCompositionIdentifierList());
231 private BeanValidationResult validateIssueAutomationCompositions(List<AutomationComposition> automationCompositions,
232 List<Participant> participants) {
233 var result = new BeanValidationResult("AutomationCompositions", automationCompositions);
235 Map<ToscaConceptIdentifier, Participant> participantMap = participants.stream()
236 .collect(Collectors.toMap(participant -> participant.getKey().asIdentifier(), Function.identity()));
238 for (AutomationComposition automationComposition : automationCompositions) {
240 for (var element : automationComposition.getElements().values()) {
242 var subResult = new BeanValidationResult(ENTRY + element.getDefinition().getName(), element);
243 Participant p = participantMap.get(element.getParticipantId());
245 subResult.addResult(new ObjectValidationResult(AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE,
246 element.getDefinition().getName(), ValidationStatus.INVALID,
247 "Participant with ID " + element.getParticipantId() + " is not registered"));
248 } else if (!p.getParticipantType().equals(element.getParticipantType())) {
249 subResult.addResult(new ObjectValidationResult(AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE,
250 element.getDefinition().getName(), ValidationStatus.INVALID,
251 "Participant with ID " + element.getParticipantType() + " - " + element.getParticipantId()
252 + " is not registered"));
254 result.addResult(subResult);