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.springframework.stereotype.Service;
47 import org.springframework.transaction.annotation.Transactional;
50 * This class is dedicated to the Instantiation of Commissioned automation composition.
55 public class AutomationCompositionInstantiationProvider {
56 private static final String AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE = "AutomationCompositionElement";
58 private final AutomationCompositionProvider automationCompositionProvider;
59 private final SupervisionHandler supervisionHandler;
60 private final ParticipantProvider participantProvider;
61 private final AcDefinitionProvider acDefinitionProvider;
62 private static final String ENTRY = "entry ";
65 * Create automation composition.
67 * @param automationComposition the automation composition
68 * @return the result of the instantiation operation
70 public InstantiationResponse createAutomationComposition(AutomationComposition automationComposition) {
72 var checkAutomationCompositionOpt =
73 automationCompositionProvider.findAutomationComposition(automationComposition.getKey().asIdentifier());
74 if (checkAutomationCompositionOpt.isPresent()) {
75 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
76 automationComposition.getKey().asIdentifier() + " already defined");
79 var validationResult = validateAutomationComposition(automationComposition);
80 if (!validationResult.isValid()) {
81 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
83 automationComposition = automationCompositionProvider.saveAutomationComposition(automationComposition);
85 var response = new InstantiationResponse();
86 response.setAffectedAutomationComposition(automationComposition.getKey().asIdentifier());
92 * Update automation composition.
94 * @param automationComposition the automation composition
95 * @return the result of the instantiation operation
97 public InstantiationResponse updateAutomationComposition(AutomationComposition automationComposition) {
98 var validationResult = validateAutomationComposition(automationComposition);
99 if (!validationResult.isValid()) {
100 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
102 automationCompositionProvider.saveAutomationComposition(automationComposition);
104 var response = new InstantiationResponse();
105 response.setAffectedAutomationComposition(automationComposition.getKey().asIdentifier());
111 * Validate AutomationComposition.
113 * @param automationComposition AutomationComposition to validate
114 * @return the result of validation
116 private BeanValidationResult validateAutomationComposition(AutomationComposition automationComposition) {
118 var result = new BeanValidationResult("AutomationComposition", automationComposition);
119 var serviceTemplate = acDefinitionProvider.findAcDefinition(automationComposition.getCompositionId());
120 if (serviceTemplate.isEmpty()) {
121 result.addResult(new ObjectValidationResult("ServiceTemplate", "", ValidationStatus.INVALID,
122 "Commissioned automation composition definition not found"));
124 result.addResult(AcmUtils.validateAutomationComposition(automationComposition, serviceTemplate.get()));
130 * Delete the automation composition with the given name and version.
132 * @param name the name of the automation composition to delete
133 * @param version the version of the automation composition to delete
134 * @return the result of the deletion
136 public InstantiationResponse deleteAutomationComposition(String name, String version) {
137 var automationCompositionOpt = automationCompositionProvider.findAutomationComposition(name, version);
138 if (automationCompositionOpt.isEmpty()) {
139 throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "Automation composition not found");
141 var automationComposition = automationCompositionOpt.get();
142 if (!AutomationCompositionState.UNINITIALISED.equals(automationComposition.getState())) {
143 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
144 "Automation composition state is still " + automationComposition.getState());
146 var response = new InstantiationResponse();
147 response.setAffectedAutomationComposition(
148 automationCompositionProvider.deleteAutomationComposition(name, version).getKey().asIdentifier());
153 * Get the requested automation compositions.
155 * @param name the name of the automation composition to get, null for all automation compositions
156 * @param version the version of the automation composition to get, null for all automation compositions
157 * @return the automation compositions
159 @Transactional(readOnly = true)
160 public AutomationCompositions getAutomationCompositions(String name, String version) {
161 var automationCompositions = new AutomationCompositions();
162 automationCompositions
163 .setAutomationCompositionList(automationCompositionProvider.getAutomationCompositions(name, version));
165 return automationCompositions;
169 * Issue a command to automation compositions, setting their ordered state.
171 * @param command the command to issue to automation compositions
172 * @return the result of the initiation command
173 * @throws AutomationCompositionException on ordered state invalid
175 public InstantiationResponse issueAutomationCompositionCommand(InstantiationCommand command)
176 throws AutomationCompositionException {
178 if (command.getOrderedState() == null) {
179 throw new AutomationCompositionException(Status.BAD_REQUEST,
180 "ordered state invalid or not specified on command");
183 var participants = participantProvider.getParticipants();
184 if (participants.isEmpty()) {
185 throw new AutomationCompositionException(Status.BAD_REQUEST, "No participants registered");
187 var automationCompositionOpt =
188 automationCompositionProvider.findAutomationComposition(command.getAutomationCompositionIdentifier());
189 if (automationCompositionOpt.isEmpty()) {
190 throw new AutomationCompositionException(Response.Status.BAD_REQUEST,
191 "AutomationComposition with id " + command.getAutomationCompositionIdentifier() + " not found");
194 var automationComposition = automationCompositionOpt.get();
195 var validationResult = validateIssueAutomationComposition(automationComposition, participants);
196 if (!validationResult.isValid()) {
197 throw new AutomationCompositionException(Response.Status.BAD_REQUEST, validationResult.getResult());
200 automationComposition.setCascadedOrderedState(command.getOrderedState());
201 supervisionHandler.triggerAutomationCompositionSupervision(automationComposition);
202 automationCompositionProvider.saveAutomationComposition(automationComposition);
203 var response = new InstantiationResponse();
204 response.setAffectedAutomationComposition(command.getAutomationCompositionIdentifier());
209 private BeanValidationResult validateIssueAutomationComposition(AutomationComposition automationComposition,
210 List<Participant> participants) {
211 var result = new BeanValidationResult("AutomationComposition", automationComposition);
213 var participantMap = participants.stream()
214 .collect(Collectors.toMap(participant -> participant.getKey().asIdentifier(), Function.identity()));
216 for (var element : automationComposition.getElements().values()) {
218 var subResult = new BeanValidationResult(ENTRY + element.getDefinition().getName(), element);
219 var p = participantMap.get(element.getParticipantId());
221 subResult.addResult(new ObjectValidationResult(AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE,
222 element.getDefinition().getName(), ValidationStatus.INVALID,
223 "Participant with ID " + element.getParticipantId() + " is not registered"));
224 } else if (!p.getParticipantType().equals(element.getParticipantType())) {
225 subResult.addResult(new ObjectValidationResult(AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE,
226 element.getDefinition().getName(), ValidationStatus.INVALID,
227 "Participant with ID " + element.getParticipantType() + " - " + element.getParticipantId()
228 + " is not registered"));
230 result.addResult(subResult);