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.commissioning;
24 import java.util.UUID;
25 import java.util.stream.Collectors;
26 import javax.ws.rs.core.Response.Status;
27 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionHandler;
28 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
29 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.CommissioningResponse;
30 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
31 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
32 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
33 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
34 import org.onap.policy.common.parameters.BeanValidationResult;
35 import org.onap.policy.models.base.PfModelException;
36 import org.onap.policy.models.base.PfModelRuntimeException;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplates;
39 import org.springframework.stereotype.Service;
40 import org.springframework.transaction.annotation.Transactional;
43 * This class provides the create, read and delete actions on Commissioning of automation composition concepts in the
44 * database to the callers.
48 public class CommissioningProvider {
49 public static final String AUTOMATION_COMPOSITION_NODE_TYPE = "org.onap.policy.clamp.acm.AutomationComposition";
51 private final AcDefinitionProvider acDefinitionProvider;
52 private final AutomationCompositionProvider acProvider;
53 private final ParticipantProvider participantProvider;
54 private final SupervisionHandler supervisionHandler;
57 * Create a commissioning provider.
59 * @param acDefinitionProvider the ServiceTemplate Provider
60 * @param acProvider the AutomationComposition Provider
61 * @param supervisionHandler the Supervision Handler
62 * @param participantProvider the Participant Provider
64 public CommissioningProvider(AcDefinitionProvider acDefinitionProvider,
65 AutomationCompositionProvider acProvider, SupervisionHandler supervisionHandler,
66 ParticipantProvider participantProvider) {
67 this.acDefinitionProvider = acDefinitionProvider;
68 this.acProvider = acProvider;
69 this.supervisionHandler = supervisionHandler;
70 this.participantProvider = participantProvider;
73 private CommissioningResponse createCommissioningResponse(UUID compositionId,
74 ToscaServiceTemplate serviceTemplate) {
75 var response = new CommissioningResponse();
76 response.setCompositionId(compositionId);
78 response.setAffectedAutomationCompositionDefinitions(
79 serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
82 .map(template -> template.getKey().asIdentifier())
83 .collect(Collectors.toList()));
90 * Create automation compositions from a service template.
92 * @param serviceTemplate the service template
93 * @return the result of the commissioning operation
95 public CommissioningResponse createAutomationCompositionDefinitions(ToscaServiceTemplate serviceTemplate) {
97 if (verifyIfDefinitionExists()) {
98 throw new PfModelRuntimeException(Status.BAD_REQUEST,
99 "Delete instances, to commission automation composition definitions");
101 var acmDefinition = acDefinitionProvider.createAutomationCompositionDefinition(serviceTemplate);
102 serviceTemplate = acmDefinition.getServiceTemplate();
103 var participantList = participantProvider.getParticipants();
104 if (!participantList.isEmpty()) {
105 supervisionHandler.handleSendCommissionMessage(serviceTemplate.getName(), serviceTemplate.getVersion());
107 return createCommissioningResponse(acmDefinition.getCompositionId(), serviceTemplate);
111 * Update Composition Definition.
113 * @param compositionId The UUID of the automation composition definition to update
114 * @param serviceTemplate the service template
115 * @return the result of the commissioning operation
117 public CommissioningResponse updateCompositionDefinition(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
119 var automationCompositions = acProvider.getAcInstancesByCompositionId(compositionId);
120 var result = new BeanValidationResult("AutomationCompositions", automationCompositions);
121 for (var automationComposition : automationCompositions) {
122 if (!AutomationCompositionState.UNINITIALISED.equals(automationComposition.getState())) {
123 throw new PfModelRuntimeException(Status.BAD_REQUEST,
124 "There is an Automation Composition instantioation with state in "
125 + automationComposition.getState());
127 result.addResult(AcmUtils.validateAutomationComposition(automationComposition, serviceTemplate));
129 if (!result.isValid()) {
130 throw new PfModelRuntimeException(Status.BAD_REQUEST, "Service template non valid: " + result.getMessage());
133 acDefinitionProvider.updateServiceTemplate(compositionId, serviceTemplate);
135 return createCommissioningResponse(compositionId, serviceTemplate);
139 * Delete the automation composition definition with the given name and version.
141 * @param compositionId The UUID of the automation composition definition to delete
142 * @return the result of the deletion
144 public CommissioningResponse deleteAutomationCompositionDefinition(UUID compositionId) {
146 if (verifyIfInstanceExists(compositionId)) {
147 throw new PfModelRuntimeException(Status.BAD_REQUEST,
148 "Delete instances, to commission automation composition definitions");
150 var participantList = participantProvider.getParticipants();
151 if (!participantList.isEmpty()) {
152 supervisionHandler.handleSendDeCommissionMessage();
154 var serviceTemplate = acDefinitionProvider.deleteAcDefintion(compositionId);
155 return createCommissioningResponse(compositionId, serviceTemplate);
159 * Get automation composition definition.
161 * @param acName the name of the automation composition, null for all
162 * @param acVersion the version of the automation composition, null for all
163 * @return automation composition definition
164 * @throws PfModelException on errors getting automation composition definitions
166 @Transactional(readOnly = true)
167 public ToscaServiceTemplates getAutomationCompositionDefinitions(String acName, String acVersion) {
169 var result = new ToscaServiceTemplates();
170 result.setServiceTemplates(acDefinitionProvider.getServiceTemplateList(acName, acVersion));
175 * Get automation composition definition.
177 * @param compositionId the compositionId
178 * @return automation composition definition
180 @Transactional(readOnly = true)
181 public ToscaServiceTemplate getAutomationCompositionDefinitions(UUID compositionId) {
183 return acDefinitionProvider.getAcDefinition(compositionId);
187 * Validates to see if there is any instance saved.
189 * @return true if exists instance
191 private boolean verifyIfInstanceExists(UUID compositionId) {
192 return !acProvider.getAcInstancesByCompositionId(compositionId).isEmpty();
196 * Validates to see if there is any instance saved.
198 * @return true if exists instance
200 private boolean verifyIfDefinitionExists() {
201 return !acDefinitionProvider.getAllAcDefinitions().isEmpty();