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.messages.rest.commissioning.CommissioningResponse;
29 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
30 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
31 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
32 import org.onap.policy.models.base.PfModelException;
33 import org.onap.policy.models.base.PfModelRuntimeException;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplates;
36 import org.springframework.stereotype.Service;
37 import org.springframework.transaction.annotation.Transactional;
40 * This class provides the create, read and delete actions on Commissioning of automation composition concepts in the
41 * database to the callers.
45 public class CommissioningProvider {
46 public static final String AUTOMATION_COMPOSITION_NODE_TYPE = "org.onap.policy.clamp.acm.AutomationComposition";
48 private final AcDefinitionProvider acDefinitionProvider;
49 private final AutomationCompositionProvider acProvider;
50 private final ParticipantProvider participantProvider;
51 private final SupervisionHandler supervisionHandler;
54 * Create a commissioning provider.
56 * @param acDefinitionProvider the ServiceTemplate Provider
57 * @param acProvider the AutomationComposition Provider
58 * @param supervisionHandler the Supervision Handler
59 * @param participantProvider the Participant Provider
61 public CommissioningProvider(AcDefinitionProvider acDefinitionProvider, AutomationCompositionProvider acProvider,
62 SupervisionHandler supervisionHandler, ParticipantProvider participantProvider) {
63 this.acDefinitionProvider = acDefinitionProvider;
64 this.acProvider = acProvider;
65 this.supervisionHandler = supervisionHandler;
66 this.participantProvider = participantProvider;
69 private CommissioningResponse createCommissioningResponse(UUID compositionId,
70 ToscaServiceTemplate serviceTemplate) {
71 var response = new CommissioningResponse();
72 response.setCompositionId(compositionId);
74 response.setAffectedAutomationCompositionDefinitions(
75 serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
78 .map(template -> template.getKey().asIdentifier())
79 .collect(Collectors.toList()));
86 * Create automation compositions from a service template.
88 * @param serviceTemplate the service template
89 * @return the result of the commissioning operation
91 public CommissioningResponse createAutomationCompositionDefinitions(ToscaServiceTemplate serviceTemplate) {
93 var acmDefinition = acDefinitionProvider.createAutomationCompositionDefinition(serviceTemplate);
94 serviceTemplate = acmDefinition.getServiceTemplate();
95 var participantList = participantProvider.getParticipants();
96 if (!participantList.isEmpty()) {
97 supervisionHandler.handleSendCommissionMessage(acmDefinition);
99 return createCommissioningResponse(acmDefinition.getCompositionId(), serviceTemplate);
103 * Update Composition Definition.
105 * @param compositionId The UUID of the automation composition definition to update
106 * @param serviceTemplate the service template
107 * @return the result of the commissioning operation
109 public CommissioningResponse updateCompositionDefinition(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
111 var automationCompositions = acProvider.getAcInstancesByCompositionId(compositionId);
112 if (!automationCompositions.isEmpty()) {
113 throw new PfModelRuntimeException(Status.BAD_REQUEST,
114 "There are ACM instances, Update of ACM Definition not allowed");
116 acDefinitionProvider.updateServiceTemplate(compositionId, serviceTemplate);
118 return createCommissioningResponse(compositionId, serviceTemplate);
122 * Delete the automation composition definition with the given name and version.
124 * @param compositionId The UUID of the automation composition definition to delete
125 * @return the result of the deletion
127 public CommissioningResponse deleteAutomationCompositionDefinition(UUID compositionId) {
129 if (verifyIfInstanceExists(compositionId)) {
130 throw new PfModelRuntimeException(Status.BAD_REQUEST,
131 "Delete instances, to commission automation composition definitions");
133 var participantList = participantProvider.getParticipants();
134 if (!participantList.isEmpty()) {
135 supervisionHandler.handleSendDeCommissionMessage(compositionId);
137 var serviceTemplate = acDefinitionProvider.deleteAcDefintion(compositionId);
138 return createCommissioningResponse(compositionId, serviceTemplate);
142 * Get automation composition definition.
144 * @param acName the name of the automation composition, null for all
145 * @param acVersion the version of the automation composition, null for all
146 * @return automation composition definition
147 * @throws PfModelException on errors getting automation composition definitions
149 @Transactional(readOnly = true)
150 public ToscaServiceTemplates getAutomationCompositionDefinitions(String acName, String acVersion) {
152 var result = new ToscaServiceTemplates();
153 result.setServiceTemplates(acDefinitionProvider.getServiceTemplateList(acName, acVersion));
158 * Get automation composition definition.
160 * @param compositionId the compositionId
161 * @return automation composition definition
163 @Transactional(readOnly = true)
164 public ToscaServiceTemplate getAutomationCompositionDefinitions(UUID compositionId) {
166 return acDefinitionProvider.getAcDefinition(compositionId);
170 * Validates to see if there is any instance saved.
172 * @return true if exists instance
174 private boolean verifyIfInstanceExists(UUID compositionId) {
175 return !acProvider.getAcInstancesByCompositionId(compositionId).isEmpty();