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 var acmDefinition = acDefinitionProvider.createAutomationCompositionDefinition(serviceTemplate);
98 serviceTemplate = acmDefinition.getServiceTemplate();
99 var participantList = participantProvider.getParticipants();
100 if (!participantList.isEmpty()) {
101 supervisionHandler.handleSendCommissionMessage(acmDefinition);
103 return createCommissioningResponse(acmDefinition.getCompositionId(), serviceTemplate);
107 * Update Composition Definition.
109 * @param compositionId The UUID of the automation composition definition to update
110 * @param serviceTemplate the service template
111 * @return the result of the commissioning operation
113 public CommissioningResponse updateCompositionDefinition(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
115 var automationCompositions = acProvider.getAcInstancesByCompositionId(compositionId);
116 var result = new BeanValidationResult("AutomationCompositions", automationCompositions);
117 for (var automationComposition : automationCompositions) {
118 if (!AutomationCompositionState.UNINITIALISED.equals(automationComposition.getState())) {
119 throw new PfModelRuntimeException(Status.BAD_REQUEST,
120 "There is an Automation Composition instantioation with state in "
121 + automationComposition.getState());
123 result.addResult(AcmUtils.validateAutomationComposition(automationComposition, serviceTemplate));
125 if (!result.isValid()) {
126 throw new PfModelRuntimeException(Status.BAD_REQUEST, "Service template non valid: " + result.getMessage());
129 acDefinitionProvider.updateServiceTemplate(compositionId, serviceTemplate);
131 return createCommissioningResponse(compositionId, serviceTemplate);
135 * Delete the automation composition definition with the given name and version.
137 * @param compositionId The UUID of the automation composition definition to delete
138 * @return the result of the deletion
140 public CommissioningResponse deleteAutomationCompositionDefinition(UUID compositionId) {
142 if (verifyIfInstanceExists(compositionId)) {
143 throw new PfModelRuntimeException(Status.BAD_REQUEST,
144 "Delete instances, to commission automation composition definitions");
146 var participantList = participantProvider.getParticipants();
147 if (!participantList.isEmpty()) {
148 supervisionHandler.handleSendDeCommissionMessage(compositionId);
150 var serviceTemplate = acDefinitionProvider.deleteAcDefintion(compositionId);
151 return createCommissioningResponse(compositionId, serviceTemplate);
155 * Get automation composition definition.
157 * @param acName the name of the automation composition, null for all
158 * @param acVersion the version of the automation composition, null for all
159 * @return automation composition definition
160 * @throws PfModelException on errors getting automation composition definitions
162 @Transactional(readOnly = true)
163 public ToscaServiceTemplates getAutomationCompositionDefinitions(String acName, String acVersion) {
165 var result = new ToscaServiceTemplates();
166 result.setServiceTemplates(acDefinitionProvider.getServiceTemplateList(acName, acVersion));
171 * Get automation composition definition.
173 * @param compositionId the compositionId
174 * @return automation composition definition
176 @Transactional(readOnly = true)
177 public ToscaServiceTemplate getAutomationCompositionDefinitions(UUID compositionId) {
179 return acDefinitionProvider.getAcDefinition(compositionId);
183 * Validates to see if there is any instance saved.
185 * @return true if exists instance
187 private boolean verifyIfInstanceExists(UUID compositionId) {
188 return !acProvider.getAcInstancesByCompositionId(compositionId).isEmpty();