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.List;
25 import java.util.UUID;
26 import java.util.stream.Collectors;
27 import javax.ws.rs.core.Response.Status;
28 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionHandler;
29 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
30 import org.onap.policy.clamp.models.acm.concepts.Participant;
31 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.CommissioningResponse;
32 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
33 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
34 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
35 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
36 import org.onap.policy.common.parameters.BeanValidationResult;
37 import org.onap.policy.models.base.PfModelException;
38 import org.onap.policy.models.base.PfModelRuntimeException;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplates;
41 import org.springframework.stereotype.Service;
42 import org.springframework.transaction.annotation.Transactional;
45 * This class provides the create, read and delete actions on Commissioning of automation composition concepts in the
46 * database to the callers.
50 public class CommissioningProvider {
51 public static final String AUTOMATION_COMPOSITION_NODE_TYPE = "org.onap.policy.clamp.acm.AutomationComposition";
53 private final AcDefinitionProvider acDefinitionProvider;
54 private final AutomationCompositionProvider acProvider;
55 private final ParticipantProvider participantProvider;
56 private final SupervisionHandler supervisionHandler;
59 * Create a commissioning provider.
61 * @param acDefinitionProvider the ServiceTemplate Provider
62 * @param acProvider the AutomationComposition Provider
63 * @param supervisionHandler the Supervision Handler
64 * @param participantProvider the Participant Provider
66 public CommissioningProvider(AcDefinitionProvider acDefinitionProvider,
67 AutomationCompositionProvider acProvider, SupervisionHandler supervisionHandler,
68 ParticipantProvider participantProvider) {
69 this.acDefinitionProvider = acDefinitionProvider;
70 this.acProvider = acProvider;
71 this.supervisionHandler = supervisionHandler;
72 this.participantProvider = participantProvider;
75 private CommissioningResponse createCommissioningResponse(UUID compositionId,
76 ToscaServiceTemplate serviceTemplate) {
77 var response = new CommissioningResponse();
78 response.setCompositionId(compositionId);
80 response.setAffectedAutomationCompositionDefinitions(
81 serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
84 .map(template -> template.getKey().asIdentifier())
85 .collect(Collectors.toList()));
92 * Create automation compositions from a service template.
94 * @param serviceTemplate the service template
95 * @return the result of the commissioning operation
97 public CommissioningResponse createAutomationCompositionDefinitions(ToscaServiceTemplate serviceTemplate) {
99 if (verifyIfDefinitionExists()) {
100 throw new PfModelRuntimeException(Status.BAD_REQUEST,
101 "Delete instances, to commission automation composition definitions");
103 var acmDefinition = acDefinitionProvider.createAutomationCompositionDefinition(serviceTemplate);
104 serviceTemplate = acmDefinition.getServiceTemplate();
105 var participantList = participantProvider.getParticipants();
106 if (!participantList.isEmpty()) {
107 supervisionHandler.handleSendCommissionMessage(serviceTemplate.getName(), serviceTemplate.getVersion());
109 return createCommissioningResponse(acmDefinition.getCompositionId(), serviceTemplate);
113 * Update Composition Definition.
115 * @param compositionId The UUID of the automation composition definition to update
116 * @param serviceTemplate the service template
117 * @return the result of the commissioning operation
119 public CommissioningResponse updateCompositionDefinition(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
121 var automationCompositions = acProvider.getAutomationCompositions();
122 var result = new BeanValidationResult("AutomationCompositions", automationCompositions);
123 for (var automationComposition : automationCompositions) {
124 if (!AutomationCompositionState.UNINITIALISED.equals(automationComposition.getState())) {
125 throw new PfModelRuntimeException(Status.BAD_REQUEST,
126 "There is an Automation Composition instantioation with state in "
127 + automationComposition.getState());
129 result.addResult(AcmUtils.validateAutomationComposition(automationComposition, serviceTemplate));
131 if (!result.isValid()) {
132 throw new PfModelRuntimeException(Status.BAD_REQUEST, "Service template non valid: " + result.getMessage());
135 acDefinitionProvider.updateServiceTemplate(compositionId, serviceTemplate);
137 return createCommissioningResponse(compositionId, serviceTemplate);
141 * Delete the automation composition definition with the given name and version.
143 * @param compositionId The UUID of the automation composition definition to delete
144 * @return the result of the deletion
146 public CommissioningResponse deleteAutomationCompositionDefinition(UUID compositionId) {
148 if (verifyIfInstanceExists()) {
149 throw new PfModelRuntimeException(Status.BAD_REQUEST,
150 "Delete instances, to commission automation composition definitions");
152 List<Participant> participantList = participantProvider.getParticipants();
153 if (!participantList.isEmpty()) {
154 supervisionHandler.handleSendDeCommissionMessage();
156 var serviceTemplate = acDefinitionProvider.deleteAcDefintion(compositionId);
157 return createCommissioningResponse(compositionId, serviceTemplate);
161 * Get automation composition definition.
163 * @param acName the name of the automation composition, null for all
164 * @param acVersion the version of the automation composition, null for all
165 * @return automation composition definition
166 * @throws PfModelException on errors getting automation composition definitions
168 @Transactional(readOnly = true)
169 public ToscaServiceTemplates getAutomationCompositionDefinitions(String acName, String acVersion) {
171 var result = new ToscaServiceTemplates();
172 result.setServiceTemplates(acDefinitionProvider.getServiceTemplateList(acName, acVersion));
177 * Get automation composition definition.
179 * @param compositionId the compositionId
180 * @return automation composition definition
182 @Transactional(readOnly = true)
183 public ToscaServiceTemplate getAutomationCompositionDefinitions(UUID compositionId) {
185 return acDefinitionProvider.getAcDefinition(compositionId);
189 * Validates to see if there is any instance saved.
191 * @return true if exists instance
193 private boolean verifyIfInstanceExists() {
194 return !acProvider.getAutomationCompositions().isEmpty();
198 * Validates to see if there is any instance saved.
200 * @return true if exists instance
202 private boolean verifyIfDefinitionExists() {
203 return !acDefinitionProvider.getAllServiceTemplates().isEmpty();