2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 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 lombok.RequiredArgsConstructor;
28 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionHandler;
29 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
30 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
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.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 @RequiredArgsConstructor
49 public class CommissioningProvider {
50 public static final String AUTOMATION_COMPOSITION_NODE_TYPE = "org.onap.policy.clamp.acm.AutomationComposition";
52 private final AcDefinitionProvider acDefinitionProvider;
53 private final AutomationCompositionProvider acProvider;
54 private final SupervisionHandler supervisionHandler;
55 private final ParticipantProvider participantProvider;
57 private CommissioningResponse createCommissioningResponse(UUID compositionId,
58 ToscaServiceTemplate serviceTemplate) {
59 var response = new CommissioningResponse();
60 response.setCompositionId(compositionId);
62 response.setAffectedAutomationCompositionDefinitions(
63 serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
66 .map(template -> template.getKey().asIdentifier())
67 .collect(Collectors.toList()));
74 * Create automation composition from a service template.
76 * @param serviceTemplate the service template
77 * @return the result of the commissioning operation
79 public CommissioningResponse createAutomationCompositionDefinition(ToscaServiceTemplate serviceTemplate) {
81 var acmDefinition = acDefinitionProvider.createAutomationCompositionDefinition(serviceTemplate);
82 serviceTemplate = acmDefinition.getServiceTemplate();
83 var participantList = participantProvider.getParticipants();
84 if (!participantList.isEmpty()) {
85 supervisionHandler.handleSendCommissionMessage(acmDefinition);
87 return createCommissioningResponse(acmDefinition.getCompositionId(), serviceTemplate);
91 * Update Composition Definition.
93 * @param compositionId The UUID of the automation composition definition to update
94 * @param serviceTemplate the service template
95 * @return the result of the commissioning operation
97 public CommissioningResponse updateCompositionDefinition(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
98 if (verifyIfInstanceExists(compositionId)) {
99 throw new PfModelRuntimeException(Status.BAD_REQUEST,
100 "There are ACM instances, Update of ACM Definition not allowed");
102 var acDefinition = acDefinitionProvider.getAcDefinition(compositionId);
103 if (AcTypeState.COMMISSIONED.equals(acDefinition.getState())) {
104 throw new PfModelRuntimeException(Status.BAD_REQUEST,
105 "ACM not in COMMISSIONED state, Update of ACM Definition not allowed");
107 acDefinitionProvider.updateServiceTemplate(compositionId, serviceTemplate);
109 return createCommissioningResponse(compositionId, serviceTemplate);
113 * Delete the automation composition definition with the given name and version.
115 * @param compositionId The UUID of the automation composition definition to delete
116 * @return the result of the deletion
118 public CommissioningResponse deleteAutomationCompositionDefinition(UUID compositionId) {
120 if (verifyIfInstanceExists(compositionId)) {
121 throw new PfModelRuntimeException(Status.BAD_REQUEST,
122 "Delete instances, to commission automation composition definitions");
124 var participantList = participantProvider.getParticipants();
125 if (!participantList.isEmpty()) {
126 supervisionHandler.handleSendDeCommissionMessage(compositionId);
128 var serviceTemplate = acDefinitionProvider.deleteAcDefintion(compositionId);
129 return createCommissioningResponse(compositionId, serviceTemplate);
133 * Get automation composition definition.
135 * @param acName the name of the automation composition, null for all
136 * @param acVersion the version of the automation composition, null for all
137 * @return automation composition definition
138 * @throws PfModelException on errors getting automation composition definitions
140 @Transactional(readOnly = true)
141 public ToscaServiceTemplates getAutomationCompositionDefinitions(String acName, String acVersion) {
143 var result = new ToscaServiceTemplates();
144 result.setServiceTemplates(acDefinitionProvider.getServiceTemplateList(acName, acVersion));
149 * Get automation composition definition.
151 * @param compositionId the compositionId
152 * @return automation composition definition
154 @Transactional(readOnly = true)
155 public AutomationCompositionDefinition getAutomationCompositionDefinition(UUID compositionId) {
157 return acDefinitionProvider.getAcDefinition(compositionId);
161 * Validates to see if there is any instance saved.
163 * @return true if exists instance
165 private boolean verifyIfInstanceExists(UUID compositionId) {
166 return !acProvider.getAcInstancesByCompositionId(compositionId).isEmpty();